-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deployment environment API endpoint
- Loading branch information
Showing
8 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package api | ||
|
||
// Copyright (C) 2024 by Posit Software, PBC. | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/posit-dev/publisher/internal/accounts" | ||
"github.com/posit-dev/publisher/internal/clients/http_client" | ||
"github.com/posit-dev/publisher/internal/deployment" | ||
"github.com/posit-dev/publisher/internal/events" | ||
"github.com/posit-dev/publisher/internal/logging" | ||
"github.com/posit-dev/publisher/internal/util" | ||
) | ||
|
||
func GetDeploymentEnvironmentHandlerFunc(base util.AbsolutePath, log logging.Logger, accountList accounts.AccountList) http.HandlerFunc { | ||
return func(w http.ResponseWriter, req *http.Request) { | ||
name := mux.Vars(req)["name"] | ||
projectDir, _, err := ProjectDirFromRequest(base, w, req, log) | ||
if err != nil { | ||
// Response already returned by ProjectDirFromRequest | ||
return | ||
} | ||
|
||
path := deployment.GetDeploymentPath(projectDir, name) | ||
d, err := deployment.FromFile(path) | ||
if err != nil { | ||
// If the deployment file doesn't exist, return a 404 | ||
if errors.Is(err, fs.ErrNotExist) { | ||
http.NotFound(w, req) | ||
return | ||
} | ||
// If the deployment file is in error return a 400 | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(fmt.Sprintf("deployment %s is invalid: %s", name, err))) | ||
return | ||
} | ||
|
||
if !d.IsDeployed() { | ||
// If the deployment file is not deployed, it cannot have | ||
// environment variables on the server so return a 400 | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(fmt.Sprintf("deployment %s is not deployed", name))) | ||
return | ||
} | ||
|
||
account, err := accountList.GetAccountByServerURL(d.ServerURL) | ||
if err != nil { | ||
// If the deployment server URL doesn't have an associated | ||
// credential return a 400 | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(fmt.Sprintf("no credential found to use with deployment %s", name))) | ||
return | ||
} | ||
|
||
client, err := clientFactory(account, 30*time.Second, events.NewNullEmitter(), log) | ||
if err != nil { | ||
// If the client cannot be created, we did something wrong, | ||
// return a 500 | ||
InternalError(w, req, log, err) | ||
return | ||
} | ||
env, err := client.GetEnvVars(d.ID, log) | ||
// TODO content on the server could be deleted | ||
if err != nil { | ||
httpErr, ok := err.(*http_client.HTTPError) | ||
if ok { | ||
// Pass through HTTP Error from Connect | ||
w.WriteHeader(httpErr.Status) | ||
w.Write([]byte(httpErr.Error())) | ||
return | ||
} | ||
// If we get anything other than a HTTP Error from Connect client, | ||
// return a 500 | ||
InternalError(w, req, log, err) | ||
return | ||
} | ||
|
||
JsonResult(w, http.StatusOK, env) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/posit-dev/publisher/internal/accounts" | ||
"github.com/posit-dev/publisher/internal/clients/connect" | ||
"github.com/posit-dev/publisher/internal/clients/http_client" | ||
"github.com/posit-dev/publisher/internal/deployment" | ||
"github.com/posit-dev/publisher/internal/events" | ||
"github.com/posit-dev/publisher/internal/logging" | ||
"github.com/posit-dev/publisher/internal/types" | ||
"github.com/posit-dev/publisher/internal/util" | ||
"github.com/posit-dev/publisher/internal/util/utiltest" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
// Copyright (C) 2024 by Posit Software, PBC. | ||
|
||
type GetDeploymentEnvSuite struct { | ||
utiltest.Suite | ||
log logging.Logger | ||
cwd util.AbsolutePath | ||
} | ||
|
||
func TestGetDeploymentEnvSuite(t *testing.T) { | ||
suite.Run(t, new(GetDeploymentEnvSuite)) | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) SetupSuite() { | ||
s.log = logging.New() | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) SetupTest() { | ||
fs := afero.NewMemMapFs() | ||
cwd, err := util.Getwd(fs) | ||
s.Nil(err) | ||
s.cwd = cwd | ||
s.cwd.MkdirAll(0700) | ||
|
||
clientFactory = connect.NewConnectClient | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) TestGetDeploymentEnv() { | ||
path := deployment.GetDeploymentPath(s.cwd, "dep") | ||
d := deployment.New() | ||
d.ID = "123" | ||
d.ServerURL = "https://connect.example.com" | ||
d.WriteFile(path) | ||
|
||
lister := &accounts.MockAccountList{} | ||
acct := &accounts.Account{ | ||
Name: "myAccount", | ||
URL: "https://connect.example.com", | ||
ServerType: accounts.ServerTypeConnect, | ||
} | ||
lister.On("GetAccountByServerURL", "https://connect.example.com").Return(acct, nil) | ||
|
||
client := connect.NewMockClient() | ||
var env types.Environment = []string{"foo", "bar"} | ||
client.On("GetEnvVars", types.ContentID("123"), s.log).Return(&env, nil) | ||
clientFactory = func(account *accounts.Account, timeout time.Duration, emitter events.Emitter, log logging.Logger) (connect.APIClient, error) { | ||
return client, nil | ||
} | ||
|
||
h := GetDeploymentEnvironmentHandlerFunc(s.cwd, s.log, lister) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/deployments/dep/environment", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "dep"}) | ||
h(rec, req) | ||
|
||
s.Equal(http.StatusOK, rec.Result().StatusCode) | ||
res := types.Environment{} | ||
dec := json.NewDecoder(rec.Body) | ||
dec.DisallowUnknownFields() | ||
s.NoError(dec.Decode(&res)) | ||
s.Equal(env, res) | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) TestGetDeploymentEnvDeploymentNotFound() { | ||
h := GetDeploymentEnvironmentHandlerFunc(s.cwd, s.log, &accounts.MockAccountList{}) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/deployments/nonexistant/environment", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"id": "nonexistant"}) | ||
h(rec, req) | ||
|
||
s.Equal(http.StatusNotFound, rec.Result().StatusCode) | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) TestGetDeploymentEnvFileError() { | ||
path := deployment.GetDeploymentPath(s.cwd, "dep") | ||
err := path.WriteFile([]byte(`foo = 1`), 0666) | ||
s.NoError(err) | ||
|
||
h := GetDeploymentEnvironmentHandlerFunc(s.cwd, s.log, &accounts.MockAccountList{}) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/deployments/dep/environment", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "dep"}) | ||
h(rec, req) | ||
|
||
s.Equal(http.StatusBadRequest, rec.Result().StatusCode) | ||
body, _ := io.ReadAll(rec.Body) | ||
s.Contains(string(body), "deployment dep is invalid") | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) TestGetDeploymentEnvDeploymentNotDeployed() { | ||
path := deployment.GetDeploymentPath(s.cwd, "dep") | ||
d := deployment.New() | ||
d.WriteFile(path) | ||
|
||
h := GetDeploymentEnvironmentHandlerFunc(s.cwd, s.log, &accounts.MockAccountList{}) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/deployments/dep/environment", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "dep"}) | ||
h(rec, req) | ||
|
||
s.Equal(http.StatusBadRequest, rec.Result().StatusCode) | ||
body, _ := io.ReadAll(rec.Body) | ||
s.Contains(string(body), "deployment dep is not deployed") | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) TestGetDeploymentEnvNoCredential() { | ||
path := deployment.GetDeploymentPath(s.cwd, "dep") | ||
d := deployment.New() | ||
d.ID = "123" | ||
d.ServerURL = "https://connect.example.com" | ||
d.WriteFile(path) | ||
|
||
lister := &accounts.MockAccountList{} | ||
lister.On("GetAccountByServerURL", "https://connect.example.com").Return(nil, errors.New("no such account")) | ||
|
||
h := GetDeploymentEnvironmentHandlerFunc(s.cwd, s.log, lister) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/deployments/dep/environment", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "dep"}) | ||
h(rec, req) | ||
|
||
s.Equal(http.StatusBadRequest, rec.Result().StatusCode) | ||
body, _ := io.ReadAll(rec.Body) | ||
s.Contains(string(body), "no credential found to use with deployment dep") | ||
} | ||
|
||
func (s *GetDeploymentEnvSuite) TestGetDeploymentEnvPassesStatusFromServer() { | ||
path := deployment.GetDeploymentPath(s.cwd, "dep") | ||
d := deployment.New() | ||
d.ID = "123" | ||
d.ServerURL = "https://connect.example.com" | ||
d.WriteFile(path) | ||
|
||
lister := &accounts.MockAccountList{} | ||
acct := &accounts.Account{ | ||
Name: "myAccount", | ||
URL: "https://connect.example.com", | ||
ServerType: accounts.ServerTypeConnect, | ||
} | ||
lister.On("GetAccountByServerURL", "https://connect.example.com").Return(acct, nil) | ||
|
||
client := connect.NewMockClient() | ||
httpErr := http_client.NewHTTPError("https://connect.example.com", "GET", http.StatusNotFound) | ||
client.On("GetEnvVars", types.ContentID("123"), s.log).Return(nil, httpErr) | ||
clientFactory = func(account *accounts.Account, timeout time.Duration, emitter events.Emitter, log logging.Logger) (connect.APIClient, error) { | ||
return client, nil | ||
} | ||
|
||
h := GetDeploymentEnvironmentHandlerFunc(s.cwd, s.log, lister) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/deployments/dep/environment", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "dep"}) | ||
h(rec, req) | ||
|
||
s.Equal(http.StatusNotFound, rec.Result().StatusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters