From bee6fd1539b5792c3e25399eb363390a2672c61d Mon Sep 17 00:00:00 2001 From: Blesswin Samuel Date: Fri, 8 Nov 2024 19:46:05 +0530 Subject: [PATCH 1/2] APPS-9858 Add method to obtain websocket URL to exec into components --- apps.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/apps.go b/apps.go index ac79265..24fe738 100644 --- a/apps.go +++ b/apps.go @@ -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) @@ -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"` @@ -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) From afff5fa0a27f9c05fdde8e209a311dff141a2f91 Mon Sep 17 00:00:00 2001 From: Blesswin Samuel Date: Fri, 8 Nov 2024 22:35:38 +0530 Subject: [PATCH 2/2] Add test --- apps_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/apps_test.go b/apps_test.go index eb743d3..5ffa2d9 100644 --- a/apps_test.go +++ b/apps_test.go @@ -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()