diff --git a/go.mod b/go.mod index 2fac367..d2ded35 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/stretchr/testify v1.11.1 github.com/temporalio/cli/cliext v0.0.0-20260602200703-8bb57b77ad55 go.temporal.io/api v1.63.6-0.20260811224032-89570b10e9e2 - go.temporal.io/cloud-sdk v0.23.0 + go.temporal.io/cloud-sdk v0.24.0 go.temporal.io/sdk v1.44.1 go.temporal.io/sdk/contrib/envconfig v1.0.0 golang.org/x/oauth2 v0.36.0 diff --git a/go.sum b/go.sum index ec83f5e..83f30ff 100644 --- a/go.sum +++ b/go.sum @@ -154,8 +154,8 @@ go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09 go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0= go.temporal.io/api v1.63.6-0.20260811224032-89570b10e9e2 h1:VeNTxlOHETym01zX6kSXHIVw4nc2RrX/KVgepQ/dr/8= go.temporal.io/api v1.63.6-0.20260811224032-89570b10e9e2/go.mod h1:SrlW2JMwVlDP4nRWSNznUFqnSHd+YeMDS1BkYo63HCQ= -go.temporal.io/cloud-sdk v0.23.0 h1:15uh8IEi+L8YrGux93usWqwJ/vv1N4QsuS71drikqwc= -go.temporal.io/cloud-sdk v0.23.0/go.mod h1:W2O9t9tvo3Q/LhGgYdj8JijWbN5C84os+cz/BadIHYI= +go.temporal.io/cloud-sdk v0.24.0 h1:Ehzr35vQClRo9A8GIzVbrV82SKyP7fT4PY8WJBx4DL0= +go.temporal.io/cloud-sdk v0.24.0/go.mod h1:W2O9t9tvo3Q/LhGgYdj8JijWbN5C84os+cz/BadIHYI= go.temporal.io/sdk v1.44.1 h1:Mt2OZLZpqkzDIdg9YyQzO0Rb/HqCDnnqHlIAGAJ5gqM= go.temporal.io/sdk v1.44.1/go.mod h1:vkApR12F9/Y8OR+hkxe7WyXQFuCX6clhzqnAk6rzDAM= go.temporal.io/sdk/contrib/envconfig v1.0.0 h1:1Q/swVgB4EW/p3k7rI9/4hpU4/DC57FSRbU90+UisXw= diff --git a/temporalcloudcli/commands.custom_role.go b/temporalcloudcli/commands.custom_role.go index d5f6cd5..ccfebdb 100644 --- a/temporalcloudcli/commands.custom_role.go +++ b/temporalcloudcli/commands.custom_role.go @@ -30,6 +30,7 @@ func (c *CloudCustomRoleListCommand) run(cctx *CommandContext, _ []string) error res, err := client.GetCustomRoles(cctx, &cloudservice.GetCustomRolesRequest{ PageSize: int32(c.PageSize), PageToken: c.PageToken, + ProjectId: c.ProjectId, }) if err != nil { return err @@ -41,7 +42,7 @@ func (c *CloudCustomRoleListCommand) run(cctx *CommandContext, _ []string) error }{CustomRoles: res.CustomRoles, NextPageToken: res.NextPageToken}, printer.PrintResourceOptions{ Fields: []string{"Id", "State", "CreatedTime"}, - SpecFields: []string{"Name", "Description"}, + SpecFields: []string{"Name", "Description", "ProjectId"}, }, printer.TableOptions{}, ) @@ -52,6 +53,12 @@ func (c *CloudCustomRoleCreateCommand) run(cctx *CommandContext, _ []string) err if err != nil { return err } + if c.ProjectId != "" { + if spec.ProjectId != "" && spec.ProjectId != c.ProjectId { + return fmt.Errorf("--project-id %q conflicts with projectId %q in --spec", c.ProjectId, spec.ProjectId) + } + spec.ProjectId = c.ProjectId + } yes, err := cctx.GetPrompter().PromptYes("Create") if err != nil { return err @@ -84,6 +91,11 @@ func (c *CloudCustomRoleUpdateCommand) run(cctx *CommandContext, _ []string) err if err != nil { return err } + // AIDEV-NOTE: project_id is immutable and --spec replaces the whole spec. Keep the existing + // project when the provided spec omits it; sending it empty would be rejected by the server. + if spec.ProjectId == "" { + spec.ProjectId = res.GetCustomRole().GetSpec().GetProjectId() + } yes, err := cctx.GetPrompter().PromptApply(res.CustomRole.Spec, spec, false) if err != nil { return err @@ -141,7 +153,7 @@ func (c *CloudCustomRoleApplyCommand) run(cctx *CommandContext, _ []string) erro if err != nil { return err } - existing, err := findCustomRoleByName(cctx, client, spec.Name) + existing, err := findCustomRoleByName(cctx, client, spec.Name, spec.ProjectId) if err != nil { return err } @@ -230,15 +242,22 @@ func parseCustomRoleSpec(cctx *CommandContext, raw string) (*identityv1.CustomRo // Roles per account are typically small (tens), so paging is fine. // Names are not unique server-side, so we scan every page and error on // duplicates rather than silently picking one. +// AIDEV-NOTE: GetCustomRoles without a project_id returns only account-scoped roles, so the +// lookup must filter by the spec's project; otherwise applying a project role spec never finds +// the existing role and creates a duplicate. func findCustomRoleByName( cctx *CommandContext, client cloudservice.CloudServiceClient, name string, + projectID string, ) (*identityv1.CustomRole, error) { var match *identityv1.CustomRole var pageToken string for { - res, err := client.GetCustomRoles(cctx, &cloudservice.GetCustomRolesRequest{PageToken: pageToken}) + res, err := client.GetCustomRoles(cctx, &cloudservice.GetCustomRolesRequest{ + PageToken: pageToken, + ProjectId: projectID, + }) if err != nil { return nil, err } diff --git a/temporalcloudcli/commands.custom_role_test.go b/temporalcloudcli/commands.custom_role_test.go index 58160d4..88225a3 100644 --- a/temporalcloudcli/commands.custom_role_test.go +++ b/temporalcloudcli/commands.custom_role_test.go @@ -40,6 +40,35 @@ var testRole = &identityv1.CustomRole{ Spec: testRoleSpec, } +var testProjectRoleSpec = &identityv1.CustomRoleSpec{ + Name: "reader", + Description: "read-only", + Permissions: []*identityv1.CustomRoleSpec_Permission{{ + Resources: &identityv1.CustomRoleSpec_Resources{ + ResourceType: "namespace", + AllowAll: true, + }, + Actions: []string{"read"}, + }}, + ProjectId: "proj-1", +} + +const testProjectRoleSpecJSON = `{ + "name": "reader", + "description": "read-only", + "permissions": [{ + "resources": {"resource_type": "namespace", "allow_all": true}, + "actions": ["read"] + }], + "projectId": "proj-1" +}` + +var testProjectRole = &identityv1.CustomRole{ + Id: "role-p1", + ResourceVersion: "rv-p1", + Spec: testProjectRoleSpec, +} + // ---- GetCustomRole ---- func TestGetCustomRole(t *testing.T) { @@ -115,6 +144,17 @@ func TestListCustomRoles(t *testing.T) { Return(&cloudservice.GetCustomRolesResponse{}, nil) }, }, + { + name: "WithProjectId", + cmd: temporalcloudcli.CloudCustomRoleListCommand{ProjectId: "proj-1"}, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetCustomRoles(mock.Anything, &cloudservice.GetCustomRolesRequest{ProjectId: "proj-1"}, mock.Anything). + Return(&cloudservice.GetCustomRolesResponse{ + CustomRoles: []*identityv1.CustomRole{testProjectRole}, + }, nil) + }, + }, { name: "Error", cmd: temporalcloudcli.CloudCustomRoleListCommand{}, @@ -170,6 +210,43 @@ func TestCreateCustomRole(t *testing.T) { promptOptions: temporalcloudcli.TestPromptOptions{ExpectPromptYes: true, PromptResult: true}, asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-create"}, }, + { + name: "WithProjectIdFlag", + cmd: temporalcloudcli.CloudCustomRoleCreateCommand{Spec: testRoleSpecJSON, ProjectId: "proj-1"}, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + CreateCustomRole(mock.Anything, &cloudservice.CreateCustomRoleRequest{ + Spec: testProjectRoleSpec, + }, mock.Anything). + Return(&cloudservice.CreateCustomRoleResponse{ + RoleId: "role-p1", + AsyncOperation: &operation.AsyncOperation{Id: "op-create"}, + }, nil) + }, + promptOptions: temporalcloudcli.TestPromptOptions{ExpectPromptYes: true, PromptResult: true}, + asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-create"}, + }, + { + name: "WithProjectIdInSpec", + cmd: temporalcloudcli.CloudCustomRoleCreateCommand{Spec: testProjectRoleSpecJSON}, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + CreateCustomRole(mock.Anything, &cloudservice.CreateCustomRoleRequest{ + Spec: testProjectRoleSpec, + }, mock.Anything). + Return(&cloudservice.CreateCustomRoleResponse{ + RoleId: "role-p1", + AsyncOperation: &operation.AsyncOperation{Id: "op-create"}, + }, nil) + }, + promptOptions: temporalcloudcli.TestPromptOptions{ExpectPromptYes: true, PromptResult: true}, + asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-create"}, + }, + { + name: "ProjectIdConflict", + cmd: temporalcloudcli.CloudCustomRoleCreateCommand{Spec: testProjectRoleSpecJSON, ProjectId: "proj-2"}, + expectedErr: `--project-id "proj-2" conflicts with projectId "proj-1" in --spec`, + }, { name: "PromptDeclined", cmd: temporalcloudcli.CloudCustomRoleCreateCommand{Spec: testRoleSpecJSON}, @@ -240,6 +317,29 @@ func TestUpdateCustomRole(t *testing.T) { promptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: true}, asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-update"}, }, + { + name: "KeepsExistingProjectId", + cmd: temporalcloudcli.CloudCustomRoleUpdateCommand{ + RoleIdOptions: temporalcloudcli.RoleIdOptions{RoleId: "role-p1"}, + Spec: testRoleSpecJSON, + }, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetCustomRole(mock.Anything, &cloudservice.GetCustomRoleRequest{RoleId: "role-p1"}, mock.Anything). + Return(&cloudservice.GetCustomRoleResponse{CustomRole: testProjectRole}, nil) + c.EXPECT(). + UpdateCustomRole(mock.Anything, &cloudservice.UpdateCustomRoleRequest{ + RoleId: "role-p1", + Spec: testProjectRoleSpec, + ResourceVersion: "rv-p1", + }, mock.Anything). + Return(&cloudservice.UpdateCustomRoleResponse{ + AsyncOperation: &operation.AsyncOperation{Id: "op-update"}, + }, nil) + }, + promptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: true}, + asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-update"}, + }, { name: "ResourceVersionOverride", cmd: temporalcloudcli.CloudCustomRoleUpdateCommand{ @@ -425,6 +525,33 @@ func TestApplyCustomRole_Update(t *testing.T) { }) } +// AIDEV-NOTE: A project role spec must be looked up within its project. An unfiltered lookup +// returns only account-scoped roles, so apply would create a duplicate instead of updating. +func TestApplyCustomRole_ProjectLookup(t *testing.T) { + temporalcloudcli.TestCommand(t, &temporalcloudcli.CloudCustomRoleApplyCommand{Spec: testProjectRoleSpecJSON}, + temporalcloudcli.TestCommandOptions{ + CloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetCustomRoles(mock.Anything, &cloudservice.GetCustomRolesRequest{ProjectId: "proj-1"}, mock.Anything). + Return(&cloudservice.GetCustomRolesResponse{ + CustomRoles: []*identityv1.CustomRole{testProjectRole}, + }, nil) + c.EXPECT(). + UpdateCustomRole(mock.Anything, &cloudservice.UpdateCustomRoleRequest{ + RoleId: "role-p1", + Spec: testProjectRoleSpec, + ResourceVersion: "rv-p1", + }, mock.Anything). + Return(&cloudservice.UpdateCustomRoleResponse{ + AsyncOperation: &operation.AsyncOperation{Id: "op-apply-project"}, + }, nil) + }, + PromptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: true}, + AsyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-apply-project"}, + JSONOutput: true, + }) +} + // AIDEV-NOTE: Apply lookup pages until it finds a match by Name. // This test exercises the multi-page path through findCustomRoleByName. func TestApplyCustomRole_PagedLookup(t *testing.T) { diff --git a/temporalcloudcli/commands.gen.go b/temporalcloudcli/commands.gen.go index 0c4edd8..ecdaa1f 100644 --- a/temporalcloudcli/commands.gen.go +++ b/temporalcloudcli/commands.gen.go @@ -1609,9 +1609,9 @@ func NewCloudCustomRoleApplyCommand(cctx *CommandContext, parent *CloudCustomRol s.Command.Use = "apply [flags]" s.Command.Short = "[Experimental] Create or update a custom role from a specification" if hasHighlighting { - s.Command.Long = "Apply a custom role configuration to Temporal Cloud. Creates a new role\nif no role with the given name exists, or updates the existing one to\nmatch the specification.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample:\n\n\x1b[1mtemporal cloud custom-role apply --spec @custom-role.json\x1b[0m" + s.Command.Long = "Apply a custom role configuration to Temporal Cloud. Creates a new role\nif no role with the given name exists, or updates the existing one to\nmatch the specification. If the spec sets \"projectId\", the name is\nmatched against roles in that project; otherwise against\naccount-scoped roles.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample:\n\n\x1b[1mtemporal cloud custom-role apply --spec @custom-role.json\x1b[0m" } else { - s.Command.Long = "Apply a custom role configuration to Temporal Cloud. Creates a new role\nif no role with the given name exists, or updates the existing one to\nmatch the specification.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample:\n\n```\ntemporal cloud custom-role apply --spec @custom-role.json\n```" + s.Command.Long = "Apply a custom role configuration to Temporal Cloud. Creates a new role\nif no role with the given name exists, or updates the existing one to\nmatch the specification. If the spec sets \"projectId\", the name is\nmatched against roles in that project; otherwise against\naccount-scoped roles.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample:\n\n```\ntemporal cloud custom-role apply --spec @custom-role.json\n```" } s.Command.Args = cobra.NoArgs s.Command.Flags().StringVar(&s.Spec, "spec", "", "Custom role specification in JSON format. Provide inline JSON directly, or use '@path/to/file.json' to load from a file. Required.") @@ -1633,7 +1633,8 @@ type CloudCustomRoleCreateCommand struct { Command cobra.Command ClientOptions AsyncOperationOptions - Spec string + Spec string + ProjectId string } func NewCloudCustomRoleCreateCommand(cctx *CommandContext, parent *CloudCustomRoleCommand) *CloudCustomRoleCreateCommand { @@ -1643,13 +1644,14 @@ func NewCloudCustomRoleCreateCommand(cctx *CommandContext, parent *CloudCustomRo s.Command.Use = "create [flags]" s.Command.Short = "[Experimental] Create a Temporal Cloud custom role" if hasHighlighting { - s.Command.Long = "Create a new Temporal Cloud custom role from a JSON specification.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample with inline JSON:\n\n\x1b[1mtemporal cloud custom-role create --spec '{\"name\":\"reader\",\"description\":\"...\",\"permissions\":[...]}'\x1b[0m\n\nExample with file path:\n\n\x1b[1mtemporal cloud custom-role create --spec @custom-role.json\x1b[0m" + s.Command.Long = "Create a new Temporal Cloud custom role from a JSON specification.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample with inline JSON:\n\n\x1b[1mtemporal cloud custom-role create --spec '{\"name\":\"reader\",\"description\":\"...\",\"permissions\":[...]}'\x1b[0m\n\nExample with file path:\n\n\x1b[1mtemporal cloud custom-role create --spec @custom-role.json\x1b[0m\n\nExample scoped to a project:\n\n\x1b[1mtemporal cloud custom-role create --spec @custom-role.json --project-id my-project-id\x1b[0m" } else { - s.Command.Long = "Create a new Temporal Cloud custom role from a JSON specification.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample with inline JSON:\n\n```\ntemporal cloud custom-role create --spec '{\"name\":\"reader\",\"description\":\"...\",\"permissions\":[...]}'\n```\n\nExample with file path:\n\n```\ntemporal cloud custom-role create --spec @custom-role.json\n```" + s.Command.Long = "Create a new Temporal Cloud custom role from a JSON specification.\n\nThe specification can be provided as inline JSON or loaded from a file\nby prefixing the path with '@'.\n\nExample with inline JSON:\n\n```\ntemporal cloud custom-role create --spec '{\"name\":\"reader\",\"description\":\"...\",\"permissions\":[...]}'\n```\n\nExample with file path:\n\n```\ntemporal cloud custom-role create --spec @custom-role.json\n```\n\nExample scoped to a project:\n\n```\ntemporal cloud custom-role create --spec @custom-role.json --project-id my-project-id\n```" } s.Command.Args = cobra.NoArgs s.Command.Flags().StringVar(&s.Spec, "spec", "", "Custom role specification in JSON format. Provide inline JSON directly, or use '@path/to/file.json' to load from a file. Required.") _ = cobra.MarkFlagRequired(s.Command.Flags(), "spec") + s.Command.Flags().StringVar(&s.ProjectId, "project-id", "", "Scope the custom role to this project. Equivalent to setting \"projectId\" in the spec. The project cannot be changed after the role is created.") s.ClientOptions.BuildFlags(s.Command.Flags()) s.AsyncOperationOptions.BuildFlags(s.Command.Flags()) s.Command.Run = func(c *cobra.Command, args []string) { @@ -1763,6 +1765,7 @@ type CloudCustomRoleListCommand struct { ClientOptions PageSize int PageToken string + ProjectId string } func NewCloudCustomRoleListCommand(cctx *CommandContext, parent *CloudCustomRoleCommand) *CloudCustomRoleListCommand { @@ -1779,6 +1782,7 @@ func NewCloudCustomRoleListCommand(cctx *CommandContext, parent *CloudCustomRole s.Command.Args = cobra.NoArgs s.Command.Flags().IntVar(&s.PageSize, "page-size", 0, "Number of custom roles to return per page. Use for paginated results.") s.Command.Flags().StringVar(&s.PageToken, "page-token", "", "Token for retrieving the next page of results in a paginated list.") + s.Command.Flags().StringVar(&s.ProjectId, "project-id", "", "List only the custom roles scoped to this project. If omitted, only account-scoped custom roles are listed.") s.ClientOptions.BuildFlags(s.Command.Flags()) s.Command.Run = func(c *cobra.Command, args []string) { if err := s.run(cctx, args); err != nil { @@ -1805,9 +1809,9 @@ func NewCloudCustomRoleUpdateCommand(cctx *CommandContext, parent *CloudCustomRo s.Command.Use = "update [flags]" s.Command.Short = "[Experimental] Update an existing Temporal Cloud custom role" if hasHighlighting { - s.Command.Long = "Update an existing Temporal Cloud custom role from a JSON specification.\nReplaces the role's spec with the provided one.\n\nExample:\n\n\x1b[1mtemporal cloud custom-role update --role-id my-role-id --spec @custom-role.json\x1b[0m" + s.Command.Long = "Update an existing Temporal Cloud custom role from a JSON specification.\nReplaces the role's spec with the provided one. If the spec omits\n\"projectId\", the role's existing project is kept.\n\nExample:\n\n\x1b[1mtemporal cloud custom-role update --role-id my-role-id --spec @custom-role.json\x1b[0m" } else { - s.Command.Long = "Update an existing Temporal Cloud custom role from a JSON specification.\nReplaces the role's spec with the provided one.\n\nExample:\n\n```\ntemporal cloud custom-role update --role-id my-role-id --spec @custom-role.json\n```" + s.Command.Long = "Update an existing Temporal Cloud custom role from a JSON specification.\nReplaces the role's spec with the provided one. If the spec omits\n\"projectId\", the role's existing project is kept.\n\nExample:\n\n```\ntemporal cloud custom-role update --role-id my-role-id --spec @custom-role.json\n```" } s.Command.Args = cobra.NoArgs s.Command.Flags().StringVar(&s.Spec, "spec", "", "Custom role specification in JSON format. Provide inline JSON directly, or use '@path/to/file.json' to load from a file. Required.") diff --git a/temporalcloudcli/commands.yml b/temporalcloudcli/commands.yml index 63e821b..afc5cec 100644 --- a/temporalcloudcli/commands.yml +++ b/temporalcloudcli/commands.yml @@ -3538,6 +3538,11 @@ commands: type: string description: | Token for retrieving the next page of results in a paginated list. + - name: project-id + type: string + description: | + List only the custom roles scoped to this project. If omitted, only + account-scoped custom roles are listed. - name: cloud custom-role create summary: '[Experimental] Create a Temporal Cloud custom role' description: | @@ -3557,6 +3562,12 @@ commands: ``` temporal cloud custom-role create --spec @custom-role.json ``` + + Example scoped to a project: + + ``` + temporal cloud custom-role create --spec @custom-role.json --project-id my-project-id + ``` has-init: false option-sets: - client @@ -3568,11 +3579,18 @@ commands: description: | Custom role specification in JSON format. Provide inline JSON directly, or use '@path/to/file.json' to load from a file. + - name: project-id + type: string + description: | + Scope the custom role to this project. Equivalent to setting + "projectId" in the spec. The project cannot be changed after + the role is created. - name: cloud custom-role update summary: '[Experimental] Update an existing Temporal Cloud custom role' description: | Update an existing Temporal Cloud custom role from a JSON specification. - Replaces the role's spec with the provided one. + Replaces the role's spec with the provided one. If the spec omits + "projectId", the role's existing project is kept. Example: @@ -3613,7 +3631,9 @@ commands: description: | Apply a custom role configuration to Temporal Cloud. Creates a new role if no role with the given name exists, or updates the existing one to - match the specification. + match the specification. If the spec sets "projectId", the name is + matched against roles in that project; otherwise against + account-scoped roles. The specification can be provided as inline JSON or loaded from a file by prefixing the path with '@'.