-
Notifications
You must be signed in to change notification settings - Fork 1
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 deployment environment API endpoint #2412
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
Comment on lines
+38
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many of these error responses can be sent as specific agent errors, like in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is exactly right. I kept it generic for now, and figured we would add agent errors when they were needed by the agent |
||
} | ||
|
||
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) | ||
} | ||
} |
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure about the name here. Maybe we should be more direct about this being the Connect Content environment?
This is on the
deployment
resource which implies this is the content, and not the configuration's environment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense as you have it 👍