From 9bdb66f5755c2a588840bfec1d21d73b43bf6ff8 Mon Sep 17 00:00:00 2001 From: Sean Yeh <109418+seanyeh@users.noreply.github.com> Date: Thu, 5 Sep 2024 14:41:34 -0500 Subject: [PATCH] Add anonymous open env routes --- cmd/esc/cli/cli_test.go | 8 ++++ cmd/esc/cli/client/client.go | 34 ++++++++++++++++ cmd/esc/cli/client/client_test.go | 67 +++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) diff --git a/cmd/esc/cli/cli_test.go b/cmd/esc/cli/cli_test.go index 9d24b813..0f75086e 100644 --- a/cmd/esc/cli/cli_test.go +++ b/cmd/esc/cli/cli_test.go @@ -670,10 +670,18 @@ func (c *testPulumiClient) GetOpenEnvironmentWithProject(ctx context.Context, or return env, nil } +func (c *testPulumiClient) GetAnonymousOpenEnvironment(ctx context.Context, orgName, openEnvID string) (*esc.Environment, error) { + return c.GetOpenEnvironmentWithProject(ctx, orgName, "project", "yaml", openEnvID) +} + func (c *testPulumiClient) GetOpenProperty(ctx context.Context, orgName, projectName, envName, openEnvID, property string) (*esc.Value, error) { return nil, errors.New("NYI") } +func (c *testPulumiClient) GetAnonymousOpenProperty(ctx context.Context, orgName, openEnvID, property string) (*esc.Value, error) { + return nil, errors.New("NYI") +} + func (c *testPulumiClient) GetEnvironmentTag( ctx context.Context, orgName, projectName, envName, key string, diff --git a/cmd/esc/cli/client/client.go b/cmd/esc/cli/client/client.go index 8f125c90..7abb063c 100644 --- a/cmd/esc/cli/client/client.go +++ b/cmd/esc/cli/client/client.go @@ -180,6 +180,10 @@ type Client interface { // environment envName and org orgName. GetOpenEnvironmentWithProject(ctx context.Context, orgName, projectName, envName, openEnvID string) (*esc.Environment, error) + // GetAnonymousOpenEnvironment returns the AST, values, and schema for the open environment with ID openEnvID in + // an anonymous environment. + GetAnonymousOpenEnvironment(ctx context.Context, orgName, openEnvID string) (*esc.Environment, error) + // GetOpenProperty returns the value of a single property in the open environment with ID openEnvID in // environment envName and org orgName. // @@ -192,6 +196,10 @@ type Client interface { // GetOpenProperty(ctx context.Context, orgName, projectName, envName, openEnvID, property string) (*esc.Value, error) + // GetOpenProperty returns the value of a single property in the open environment with ID openEnvID in + // an anonymous environment. + GetAnonymousOpenProperty(ctx context.Context, orgName, openEnvID, property string) (*esc.Value, error) + // ListEnvironmentTags lists the tags for the given environment. ListEnvironmentTags( ctx context.Context, @@ -677,6 +685,16 @@ func (pc *client) GetOpenEnvironmentWithProject(ctx context.Context, orgName, pr return &resp, nil } +func (pc *client) GetAnonymousOpenEnvironment(ctx context.Context, orgName, openSessionID string) (*esc.Environment, error) { + var resp esc.Environment + path := fmt.Sprintf("/api/esc/environments/%s/yaml/open/%s", orgName, openSessionID) + err := pc.restCall(ctx, http.MethodGet, path, nil, nil, &resp) + if err != nil { + return nil, err + } + return &resp, nil +} + func (pc *client) GetOpenProperty(ctx context.Context, orgName, projectName, envName, openSessionID, property string) (*esc.Value, error) { queryObj := struct { Property string `url:"property"` @@ -693,6 +711,22 @@ func (pc *client) GetOpenProperty(ctx context.Context, orgName, projectName, env return &resp, nil } +func (pc *client) GetAnonymousOpenProperty(ctx context.Context, orgName, openSessionID, property string) (*esc.Value, error) { + queryObj := struct { + Property string `url:"property"` + }{ + Property: property, + } + + var resp esc.Value + path := fmt.Sprintf("/api/esc/environments/%s/yaml/open/%s", orgName, openSessionID) + err := pc.restCall(ctx, http.MethodGet, path, queryObj, nil, &resp) + if err != nil { + return nil, err + } + return &resp, nil +} + type ListEnvironmentTagsOptions struct { After string `url:"after"` Count *int `url:"count"` diff --git a/cmd/esc/cli/client/client_test.go b/cmd/esc/cli/client/client_test.go index 8dcef8ef..25595059 100644 --- a/cmd/esc/cli/client/client_test.go +++ b/cmd/esc/cli/client/client_test.go @@ -633,6 +633,40 @@ func TestGetOpenEnvironment(t *testing.T) { }) } +func TestGetAnonymousOpenEnvironment(t *testing.T) { + t.Run("OK", func(t *testing.T) { + expected := &esc.Environment{ + Exprs: map[string]esc.Expr{"foo": {Literal: "bar"}}, + Properties: map[string]esc.Value{"foo": esc.NewValue("bar")}, + Schema: schema.Record(map[string]schema.Builder{"foo": schema.String().Const("bar")}).Schema(), + } + + client := newTestClient(t, http.MethodGet, "/api/esc/environments/test-org/yaml/open/session", func(w http.ResponseWriter, r *http.Request) { + err := json.NewEncoder(w).Encode(expected) + require.NoError(t, err) + }) + + env, err := client.GetAnonymousOpenEnvironment(context.Background(), "test-org", "session") + require.NoError(t, err) + assert.Equal(t, expected, env) + }) + + t.Run("Not found", func(t *testing.T) { + client := newTestClient(t, http.MethodGet, "/api/esc/environments/test-org/yaml/open/session", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + + err := json.NewEncoder(w).Encode(apitype.ErrorResponse{ + Code: 404, + Message: "not found", + }) + require.NoError(t, err) + }) + + _, err := client.GetAnonymousOpenEnvironment(context.Background(), "test-org", "session") + assert.ErrorContains(t, err, "not found") + }) +} + func TestGetOpenProperty(t *testing.T) { t.Run("OK", func(t *testing.T) { property := `foo[0].baz["qux"]` @@ -666,6 +700,39 @@ func TestGetOpenProperty(t *testing.T) { }) } +func TestGetAnonymousOpenProperty(t *testing.T) { + t.Run("OK", func(t *testing.T) { + property := `foo[0].baz["qux"]` + expected := esc.NewValue("bar") + + client := newTestClient(t, http.MethodGet, "/api/esc/environments/test-org/yaml/open/session", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, property, r.URL.Query().Get("property")) + + err := json.NewEncoder(w).Encode(expected) + require.NoError(t, err) + }) + + val, err := client.GetAnonymousOpenProperty(context.Background(), "test-org", "session", property) + require.NoError(t, err) + assert.Equal(t, &expected, val) + }) + + t.Run("Not found", func(t *testing.T) { + client := newTestClient(t, http.MethodGet, "/api/esc/environments/test-org/yaml/open/session", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + + err := json.NewEncoder(w).Encode(apitype.ErrorResponse{ + Code: 404, + Message: "not found", + }) + require.NoError(t, err) + }) + + _, err := client.GetAnonymousOpenProperty(context.Background(), "test-org", "session", "foo") + assert.ErrorContains(t, err, "not found") + }) +} + func TestGetEnvironmentTag(t *testing.T) { t.Run("OK", func(t *testing.T) { ts := time.Now()