Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APPS-8263: methods for managing App Platform dev DBs #665

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
APPS-8263: methods for managing App Platform dev DBs
  • Loading branch information
jcodybaker committed Jan 26, 2024
commit 6152391969417f5ef19ab3b9813498732c8380ee
83 changes: 83 additions & 0 deletions apps.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ type AppsService interface {

ListBuildpacks(ctx context.Context) ([]*Buildpack, *Response, error)
UpgradeBuildpack(ctx context.Context, appID string, opts UpgradeBuildpackOptions) (*UpgradeBuildpackResponse, *Response, error)

GetAppDatabaseConnectionDetails(ctx context.Context, appID string) ([]*GetDatabaseConnectionDetailsResponse, *Response, error)
ResetDatabasePassword(ctx context.Context, appID string, component string) (*Deployment, *Response, error)
ToggleDatabaseTrustedSource(
ctx context.Context,
appID string,
component string,
opts ToggleDatabaseTrustedSourceOptions,
) (
*ToggleDatabaseTrustedSourceResponse,
*Response,
error,
)
}

// AppLogs represent app logs.
Expand Down Expand Up @@ -90,6 +103,12 @@ type UpgradeBuildpackOptions struct {
TriggerDeployment bool `json:"trigger_deployment,omitempty"`
}

// ToggleDatabaseTrustedSourceOptions provides optional parameters for ToggleDatabaseTrustedSource.
type ToggleDatabaseTrustedSourceOptions struct {
// Enable, if true, indicates the database should enable the trusted sources firewall.
Enable bool
}

type appRoot struct {
App *App `json:"app"`
}
Expand Down Expand Up @@ -498,6 +517,60 @@ func (s *AppsServiceOp) UpgradeBuildpack(ctx context.Context, appID string, opts
return root, resp, nil
}

// GetAppDatabaseConnectionDetails retrieves credentials for databases associated with the app.
func (s *AppsServiceOp) GetAppDatabaseConnectionDetails(ctx context.Context, appID string) ([]*GetDatabaseConnectionDetailsResponse, *Response, error) {
path := fmt.Sprintf("%s/%s/database_connection_details", appsBasePath, appID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(GetAppDatabaseConnectionDetailsResponse)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.ConnectionDetails, resp, nil
}

// ResetDatabasePassword resets credentials for a database component associated with the app.
func (s *AppsServiceOp) ResetDatabasePassword(ctx context.Context, appID string, component string) (*Deployment, *Response, error) {
path := fmt.Sprintf("%s/%s/components/%s/reset_password", appsBasePath, appID, component)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return nil, nil, err
}
root := new(deploymentRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Deployment, resp, nil
}

// ToggleDatabaseTrustedSource enables/disables trusted sources on the specified dev database component.
func (s *AppsServiceOp) ToggleDatabaseTrustedSource(
ctx context.Context,
appID string,
component string,
opts ToggleDatabaseTrustedSourceOptions,
) (
*ToggleDatabaseTrustedSourceResponse,
*Response,
error,
) {
path := fmt.Sprintf("%s/%s/components/%s/trusted_sources", appsBasePath, appID, component)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, opts)
if err != nil {
return nil, nil, err
}
root := new(ToggleDatabaseTrustedSourceResponse)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, nil
}

// AppComponentType is an app component type.
type AppComponentType string

Expand Down
65 changes: 65 additions & 0 deletions apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ var (
MajorVersion: 0,
},
}

testConnectionDetails = []*GetDatabaseConnectionDetailsResponse{
{
Host: "db1.b.db.ondigitalocean.com",
Port: 3306,
Username: "example",
Password: "PASSWORD",
DatabaseName: "db",
ComponentName: "example-service",
},
}
)

func TestApps_CreateApp(t *testing.T) {
Expand Down Expand Up @@ -741,6 +752,60 @@ func TestApps_UpgradeBuildpack(t *testing.T) {
assert.Equal(t, response, gotResponse)
}

func TestApps_GetAppDatabaseConnectionDetails(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/database_connection_details", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

json.NewEncoder(w).Encode(&GetAppDatabaseConnectionDetailsResponse{ConnectionDetails: testConnectionDetails})
})

appConnectionDetails, _, err := client.Apps.GetAppDatabaseConnectionDetails(ctx, testApp.ID)
require.NoError(t, err)
assert.Equal(t, testConnectionDetails, appConnectionDetails)
}

func TestApps_ResetDatabasePassword(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/components/%s/reset_password",
testApp.ID, testApp.Spec.GetServices()[0].GetName(),
), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)

json.NewEncoder(w).Encode(&ResetDatabasePasswordResponse{Deployment: &testDeployment})
})

deployment, _, err := client.Apps.ResetDatabasePassword(ctx, testApp.ID, testApp.Spec.GetServices()[0].GetName())
require.NoError(t, err)
assert.Equal(t, &testDeployment, deployment)
}

func TestApps_ToggleDatabaseTrustedSource(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/components/%s/trusted_sources",
testApp.ID, testApp.Spec.GetServices()[0].GetName(),
), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)

json.NewEncoder(w).Encode(&ToggleDatabaseTrustedSourceResponse{IsEnabled: true})
})

resp, _, err := client.Apps.ToggleDatabaseTrustedSource(
ctx,
testApp.ID,
testApp.Spec.GetServices()[0].GetName(),
ToggleDatabaseTrustedSourceOptions{Enable: true},
)
require.NoError(t, err)
assert.Equal(t, &ToggleDatabaseTrustedSourceResponse{IsEnabled: true}, resp)
}

func TestApps_ToURN(t *testing.T) {
app := &App{
ID: "deadbeef-dead-4aa5-beef-deadbeef347d",
Expand Down
Loading