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-9858 Add method to obtain websocket URL to get console access into components #754

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading