Skip to content

Commit

Permalink
Add anonymous open env routes
Browse files Browse the repository at this point in the history
  • Loading branch information
seanyeh committed Sep 6, 2024
1 parent d3ce354 commit 9bdb66f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/esc/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions cmd/esc/cli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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,
Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
67 changes: 67 additions & 0 deletions cmd/esc/cli/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 9bdb66f

Please sign in to comment.