Skip to content

Commit

Permalink
APPS-9858 Add method to obtain websocket URL to get console access in…
Browse files Browse the repository at this point in the history
…to components (digitalocean#754)

* APPS-9858 Add method to obtain websocket URL to exec into components

* Add test

---------

Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
blesswinsamuel and andrewsomething authored Nov 13, 2024
1 parent 96a7737 commit 8f5ac1b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
27 changes: 27 additions & 0 deletions apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type AppsService interface {
CreateDeployment(ctx context.Context, appID string, create ...*DeploymentCreateRequest) (*Deployment, *Response, error)

GetLogs(ctx context.Context, appID, deploymentID, component string, logType AppLogType, follow bool, tailLines int) (*AppLogs, *Response, error)
GetExec(ctx context.Context, appID, deploymentID, component string) (*AppExec, *Response, error)

ListRegions(ctx context.Context) ([]*AppRegion, *Response, error)

Expand Down Expand Up @@ -77,6 +78,11 @@ type AppLogs struct {
HistoricURLs []string `json:"historic_urls"`
}

// AppExec represents the websocket URL used for sending/receiving console input and output.
type AppExec struct {
URL string `json:"url"`
}

// AppUpdateRequest represents a request to update an app.
type AppUpdateRequest struct {
Spec *AppSpec `json:"spec"`
Expand Down Expand Up @@ -368,6 +374,27 @@ func (s *AppsServiceOp) GetLogs(ctx context.Context, appID, deploymentID, compon
return logs, resp, nil
}

// GetExec retrieves the websocket URL used for sending/receiving console input and output.
func (s *AppsServiceOp) GetExec(ctx context.Context, appID, deploymentID, component string) (*AppExec, *Response, error) {
var url string
if deploymentID == "" {
url = fmt.Sprintf("%s/%s/components/%s/exec", appsBasePath, appID, component)
} else {
url = fmt.Sprintf("%s/%s/deployments/%s/components/%s/exec", appsBasePath, appID, deploymentID, component)
}

req, err := s.client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
logs := new(AppExec)
resp, err := s.client.Do(ctx, req, logs)
if err != nil {
return nil, resp, err
}
return logs, resp, nil
}

// ListRegions lists all regions supported by App Platform.
func (s *AppsServiceOp) ListRegions(ctx context.Context) ([]*AppRegion, *Response, error) {
path := fmt.Sprintf("%s/regions", appsBasePath)
Expand Down
32 changes: 32 additions & 0 deletions apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,38 @@ func TestApps_GetLogs(t *testing.T) {
assert.NotEmpty(t, logs.LiveURL)
}

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

ctx := context.Background()

mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments/%s/components/%s/exec", testApp.ID, testDeployment.ID, "service-name"), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

_, hasComponent := r.URL.Query()["component_name"]
assert.False(t, hasComponent)

json.NewEncoder(w).Encode(&AppExec{URL: "https://exec.url1"})
})
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/components/%s/exec", testApp.ID, "service-name"), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

_, hasComponent := r.URL.Query()["component_name"]
assert.False(t, hasComponent)

json.NewEncoder(w).Encode(&AppExec{URL: "https://exec.url2"})
})

exec, _, err := client.Apps.GetExec(ctx, testApp.ID, testDeployment.ID, "service-name")
require.NoError(t, err)
assert.Equal(t, "https://exec.url1", exec.URL)

exec, _, err = client.Apps.GetExec(ctx, testApp.ID, "", "service-name")
require.NoError(t, err)
assert.Equal(t, "https://exec.url2", exec.URL)
}

func TestApps_GetLogs_ActiveDeployment(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 8f5ac1b

Please sign in to comment.