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

Add GET /v3/apps/:guid/environment_variables endpoint #3583

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions api/handlers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,24 @@ func getDomainsForRoutes(ctx context.Context, domainRepo CFDomainRepository, aut
return routeRecords, nil
}

func (h *App) getEnvVars(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.app.get-env-vars")
appGUID := routing.URLParam(r, "guid")

appEnvRecord, err := h.appRepo.GetAppEnv(r.Context(), authInfo, appGUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Failed to fetch app environment variables", "AppGUID", appGUID)
}

appEnvVarsRecord := repositories.AppEnvVarsRecord{
AppGUID: appEnvRecord.AppGUID,
EnvironmentVariables: appEnvRecord.EnvironmentVariables,
}

return routing.NewResponse(http.StatusOK).WithBody(presenter.ForAppEnvVars(appEnvVarsRecord, h.serverURL)), nil
}

func (h *App) updateEnvVars(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.app.update-env-vars")
Expand Down Expand Up @@ -717,6 +735,7 @@ func (h *App) AuthenticatedRoutes() []routing.Route {
{Method: "GET", Pattern: AppProcessStatsByTypePath, Handler: h.getProcessStats},
{Method: "GET", Pattern: AppRoutesPath, Handler: h.getRoutes},
{Method: "DELETE", Pattern: AppPath, Handler: h.delete},
{Method: "GET", Pattern: AppEnvVarsPath, Handler: h.getEnvVars},
{Method: "PATCH", Pattern: AppEnvVarsPath, Handler: h.updateEnvVars},
{Method: "GET", Pattern: AppEnvPath, Handler: h.getEnvironment},
{Method: "GET", Pattern: AppPackagesPath, Handler: h.getPackages},
Expand Down
31 changes: 31 additions & 0 deletions api/handlers/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,37 @@ var _ = Describe("App", func() {
})
})

Describe("GET /v3/apps/:guid/environment_variables", func() {
BeforeEach(func() {
appRepo.GetAppEnvReturns(repositories.AppEnvRecord{
AppGUID: appGUID,
EnvironmentVariables: map[string]string{"VAR": "VAL"},
}, nil)

req = createHttpRequest("GET", "/v3/apps/"+appGUID+"/environment_variables", nil)
})

It("returns the app environment variables", func() {
Expect(appRepo.GetAppEnvCallCount()).To(Equal(1))
_, actualAuthInfo, _ := appRepo.GetAppEnvArgsForCall(0)
Expect(actualAuthInfo).To(Equal(authInfo))

Expect(rr).To(HaveHTTPStatus(http.StatusOK))
Expect(rr).To(HaveHTTPHeaderWithValue("Content-Type", "application/json"))
Expect(rr).To(HaveHTTPBody(MatchJSONPath("$.var.VAR", "VAL")))
})

When("there is an error fetching the app env", func() {
BeforeEach(func() {
appRepo.GetAppEnvReturns(repositories.AppEnvRecord{}, errors.New("unknown!"))
})

It("returns an error", func() {
expectUnknownError()
})
})
})

Describe("PATCH /v3/apps/:guid/environment_variables", func() {
var payload *payloads.AppPatchEnvVars

Expand Down
22 changes: 22 additions & 0 deletions tests/e2e/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,28 @@ var _ = Describe("Apps", func() {
})

Describe("Fetching app environment variables", func() {
var result map[string]interface{}

BeforeEach(func() {
appGUID, _ = pushTestApp(space1GUID, defaultAppBitsFile)
setEnv(appGUID, map[string]interface{}{
"foo": "var",
})
})

It("succeeds", func() {
var err error
resp, err = adminClient.R().
SetResult(&result).
Get("/v3/apps/" + appGUID + "/environment_variables")

Expect(err).NotTo(HaveOccurred())
Expect(resp).To(HaveRestyStatusCode(http.StatusOK))
Expect(result).To(HaveKeyWithValue("var", HaveKeyWithValue("foo", "var")))
})
})

Describe("Fetching app environment", func() {
var (
result map[string]interface{}
instanceGUID, instanceGUID2 string
Expand Down
Loading