Skip to content
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

Feat: refactor team role assignment #823

Merged
merged 2 commits into from
Apr 16, 2024

Conversation

TomerHeber
Copy link
Collaborator

@TomerHeber TomerHeber commented Apr 15, 2024

Solution

Part of #817

All team role assignment resources are now using the same API code.

  1. Created a common API code.
  2. Added unit tests.
  3. Updated resources (3 in total) to use the new API code.
  4. Updated acceptance tests.

@@ -0,0 +1,12 @@
package client
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this to a common location.

@@ -0,0 +1,82 @@
package client
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all relevant resources are now using this API code.
Deleted the rest of the files (above).

@@ -0,0 +1,133 @@
package client_test
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit test for the new code.

@@ -86,31 +88,15 @@ func resourceTeamEnvironmentAssignmentRead(ctx context.Context, d *schema.Resour
return nil
}

func resourceTeamEnvironmentAssignmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create and update are the same.

Copy link
Contributor

@razbensimon razbensimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing job

client/api_client.go Show resolved Hide resolved
Comment on lines +12 to +14
EnvironmentId string `json:"environmentId,omitempty"`
OrganizationId string `json:"organizationId,omitempty"`
ProjectId string `json:"projectId,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can you add validation that 1 of them will be sent/exist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The terraform resource schema's forces correctness for the end-user.
If I add a validation (which I could) it would be used to detect developer bugs, and not end-user errors

I don't mind adding validations. However, it will not add much value (just a little bit).
Let me know - happy to add if you think it's required.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The terraform resource schema's forces correctness for the end-user.

Good enough for me

Comment on lines 26 to 27
Describe("CreateOrUpdate", func() {
Describe("Success", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add separate tests for each scope level (org, project, env).
see if needed in the whole file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added seperate test cases for the Get and Delete.
However, CreateOrUpdate behaves the same irrespective of the type. Therefore, it's a little redundant.
For completeness sake, I'll add it.

Comment on lines +42 to +43
func resourceTeamEnvironmentAssignmentCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general comment, I was triggered by this line.
User Environment Assignment has the same structure and API behind the scenes.
using userId instead of teamId and maybe the path is different (in 3 of the APIs).

We probably should commonize / reuse / sync the structure of your changes to that scope too.
is there anything to sync there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - we could make an extra step.
Probably need to repeat this whole exercise for users.

right now user assignments are using different APIs (project, environment, organization). Therefore, if they're merged to one API (?), we can do that extra step afterwards.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repeat this whole exercise for users.

currently, only the env level has the new api.
so not urgent, anyways Org and Project will have the old apis for now.

Comment on lines +3 to +12
const (
Admin string = "Admin"
Deployer string = "Deployer"
Planner string = "Planner"
Viewer string = "Viewer"
)

func IsBuiltinProjectRole(role string) bool {
return role == Admin || role == Deployer || role == Planner || role == Viewer
}
Copy link
Contributor

@razbensimon razbensimon Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those are suitable for both Environment and Project default roles.
for OrganizationRoles we have User, Admin.

why there is no IsBuiltintRole for the other scopes?
are we missing tests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because of the schema used by resourceTeamProjectAssignment:


		Schema: map[string]*schema.Schema{
			"team_id": {
				Type:        schema.TypeString,
				Description: "id of the team",
				Required:    true,
				ForceNew:    true,
			},
			"project_id": {
				Type:        schema.TypeString,
				Description: "id of the project",
				Required:    true,
				ForceNew:    true,
			},
			"role": {
				Type:             schema.TypeString,
				Description:      "the assigned built-in role (Admin, Planner, Viewer, Deployer)",
				Optional:         true,
				ValidateDiagFunc: ValidateRole,
				ExactlyOneOf:     []string{"custom_role_id", "role"},
			},
			"custom_role_id": {
				Type:             schema.TypeString,
				Description:      "id of the assigned custom role",
				Optional:         true,
				ExactlyOneOf:     []string{"custom_role_id", "role"},
				ValidateDiagFunc: ValidateNotEmptyString,
			},
		},
	}

It's the only one using role and custom_role_id - instead of just role_id.
I thought of deprecating these two fields and adding a role_id instead... but haven't done so yet.

What we can do, is add a proper validation for the other resources (organization and environment). (They're documented, but there's no schema level enforcement).

Copy link
Contributor

@razbensimon razbensimon Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yeah we need a schema validation for Org and Environment too.
  2. role (or role_id) should be an uuid or one of the builtins (list depends on scope) [then we can deprecate]

each can be in different PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

choosing role for both - probably will make less breaking change

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add validation.
I'll deprecate the existing two fields.

This will be done in a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I'm a bit confused, in the platform we don't use role_id at all, is it ok the behaviour here will be different?

Copy link
Contributor

@razbensimon razbensimon Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYM by not using the id at all?
we do for being able to assign a custom role (uuid) or default role (constant string)

Copy link
Contributor

@razbensimon razbensimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for the test

@github-actions github-actions bot added the ready to merge PR approved - can be merged once the PR owner is ready label Apr 15, 2024
@TomerHeber
Copy link
Collaborator Author

Waiting for the test

Thank you. Will add the additional tests and merge.

Comment on lines +3 to +7
const (
Admin string = "Admin"
Deployer string = "Deployer"
Planner string = "Planner"
Viewer string = "Viewer"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are common for both organization/project/environment ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not at this stage.
Not all are allowed for each of the types.

There will be a follow-up PR that will leverage these.

@TomerHeber
Copy link
Collaborator Author

Waiting for the test

@razbensimon

Tests added.
I will merge for now and add additional improvements (as discussed) after I complete work on other more urgent isssues.

@TomerHeber TomerHeber merged commit 282d73c into main Apr 16, 2024
5 checks passed
@TomerHeber TomerHeber deleted the feat-refactor-team-role-assignment-#817 branch April 16, 2024 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-client feature provider ready to merge PR approved - can be merged once the PR owner is ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants