Skip to content

Commit b91c51f

Browse files
vandyliuichung08
andauthored
Add astro team roles resource for workspace/deployment-team memberships (#69)
Co-authored-by: Isaac Chung <69920967+ichung08@users.noreply.github.com>
1 parent 978016f commit b91c51f

File tree

12 files changed

+864
-0
lines changed

12 files changed

+864
-0
lines changed

.github/workflows/testacc.yml

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ jobs:
8989
HYBRID_NODE_POOL_ID: clnp86ly5000301ndzfxz895w
9090
ASTRO_API_HOST: https://api.astronomer-dev.io
9191
SKIP_CLUSTER_RESOURCE_TESTS: ${{ env.SKIP_CLUSTER_RESOURCE_TESTS }}
92+
HOSTED_TEAM_ID: clwbclrc100bl01ozjj5s4jmq
9293
TESTARGS: "-failfast"
9394
run: make testacc
9495

@@ -129,6 +130,7 @@ jobs:
129130
HYBRID_CLUSTER_ID: clqqongl40fmv01m96dvxh2nu
130131
HYBRID_NODE_POOL_ID: clqqongl40fmu01m94pwp4kct
131132
ASTRO_API_HOST: https://api.astronomer-stage.io
133+
HOSTED_TEAM_ID: clwv0r0x7091n01l0t1fm4vxy
132134
TESTARGS: "-failfast"
133135
run: make testacc
134136

@@ -169,5 +171,6 @@ jobs:
169171
HYBRID_CLUSTER_ID: clnp86ly5000401ndagu20g81
170172
HYBRID_NODE_POOL_ID: clnp86ly5000301ndzfxz895w
171173
ASTRO_API_HOST: https://api.astronomer-dev.io
174+
HOSTED_TEAM_ID: clwbclrc100bl01ozjj5s4jmq
172175
TESTARGS: "-failfast"
173176
run: make testacc

docs/resources/team_roles.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "astro_team_roles Resource - astro"
4+
subcategory: ""
5+
description: |-
6+
Team Roles resource
7+
---
8+
9+
# astro_team_roles (Resource)
10+
11+
Team Roles resource
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "astro_team_roles" "organization_role_only" {
17+
team_id = "clnp86ly5000401ndaga21g81"
18+
organization_role = "ORGANIZATION_MEMBER"
19+
}
20+
21+
resource "astro_team_roles" "workspace_roles" {
22+
team_id = "clnp86ly5000401ndaga21g81"
23+
organization_role = "ORGANIZATION_MEMBER"
24+
workspace_roles = [
25+
{
26+
workspace_id = "clwp86ly5000401ndaga21g85"
27+
role = "WORKSPACE_ADMIN"
28+
},
29+
{
30+
workspace_id = "clwp86ly5000401ndaga21g82"
31+
role = "WORKSPACE_MEMBER"
32+
}
33+
]
34+
}
35+
36+
resource "astro_team_roles" "deployment_roles" {
37+
team_id = "clnp86ly5000401ndaga21g81"
38+
organization_role = "ORGANIZATION_MEMBER"
39+
deployment_roles = [
40+
{
41+
deployment_id = "cldp86ly5000401ndaga21g86"
42+
role = "DEPLOYMENT_ADMIN"
43+
}
44+
]
45+
}
46+
47+
resource "astro_team_roles" "all_roles" {
48+
team_id = "clnp86ly5000401ndaga21g81"
49+
organization_role = "ORGANIZATION_MEMBER"
50+
workspace_roles = [
51+
{
52+
workspace_id = "clwp86ly5000401ndaga21g85"
53+
role = "WORKSPACE_OWNER"
54+
},
55+
{
56+
workspace_id = "clwp86ly5000401ndaga21g82"
57+
role = "WORKSPACE_MEMBER"
58+
}
59+
]
60+
deployment_roles = [
61+
{
62+
deployment_id = "cldp86ly5000401ndaga21g86"
63+
role = "my custom role"
64+
}
65+
]
66+
}
67+
```
68+
69+
<!-- schema generated by tfplugindocs -->
70+
## Schema
71+
72+
### Required
73+
74+
- `organization_role` (String) The role to assign to the organization
75+
- `team_id` (String) The ID of the team to assign the roles to
76+
77+
### Optional
78+
79+
- `deployment_roles` (Attributes Set) The roles to assign to the deployments (see [below for nested schema](#nestedatt--deployment_roles))
80+
- `workspace_roles` (Attributes Set) The roles to assign to the workspaces (see [below for nested schema](#nestedatt--workspace_roles))
81+
82+
<a id="nestedatt--deployment_roles"></a>
83+
### Nested Schema for `deployment_roles`
84+
85+
Required:
86+
87+
- `deployment_id` (String) The ID of the deployment to assign the role to
88+
- `role` (String) The role to assign to the deployment
89+
90+
91+
<a id="nestedatt--workspace_roles"></a>
92+
### Nested Schema for `workspace_roles`
93+
94+
Required:
95+
96+
- `role` (String) The role to assign to the workspace
97+
- `workspace_id` (String) The ID of the workspace to assign the role to
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
resource "astro_team_roles" "organization_role_only" {
2+
team_id = "clnp86ly5000401ndaga21g81"
3+
organization_role = "ORGANIZATION_MEMBER"
4+
}
5+
6+
resource "astro_team_roles" "workspace_roles" {
7+
team_id = "clnp86ly5000401ndaga21g81"
8+
organization_role = "ORGANIZATION_MEMBER"
9+
workspace_roles = [
10+
{
11+
workspace_id = "clwp86ly5000401ndaga21g85"
12+
role = "WORKSPACE_ADMIN"
13+
},
14+
{
15+
workspace_id = "clwp86ly5000401ndaga21g82"
16+
role = "WORKSPACE_MEMBER"
17+
}
18+
]
19+
}
20+
21+
resource "astro_team_roles" "deployment_roles" {
22+
team_id = "clnp86ly5000401ndaga21g81"
23+
organization_role = "ORGANIZATION_MEMBER"
24+
deployment_roles = [
25+
{
26+
deployment_id = "cldp86ly5000401ndaga21g86"
27+
role = "DEPLOYMENT_ADMIN"
28+
}
29+
]
30+
}
31+
32+
resource "astro_team_roles" "all_roles" {
33+
team_id = "clnp86ly5000401ndaga21g81"
34+
organization_role = "ORGANIZATION_MEMBER"
35+
workspace_roles = [
36+
{
37+
workspace_id = "clwp86ly5000401ndaga21g85"
38+
role = "WORKSPACE_OWNER"
39+
},
40+
{
41+
workspace_id = "clwp86ly5000401ndaga21g82"
42+
role = "WORKSPACE_MEMBER"
43+
}
44+
]
45+
deployment_roles = [
46+
{
47+
deployment_id = "cldp86ly5000401ndaga21g86"
48+
role = "my custom role"
49+
}
50+
]
51+
}

internal/provider/models/role.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package models
2+
3+
import (
4+
"context"
5+
6+
"github.com/astronomer/terraform-provider-astro/internal/clients/iam"
7+
"github.com/astronomer/terraform-provider-astro/internal/provider/schemas"
8+
"github.com/hashicorp/terraform-plugin-framework/diag"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
)
11+
12+
type WorkspaceRole struct {
13+
WorkspaceId types.String `tfsdk:"workspace_id"`
14+
Role types.String `tfsdk:"role"`
15+
}
16+
17+
func WorkspaceRoleTypesObject(
18+
ctx context.Context,
19+
role iam.WorkspaceRole,
20+
) (types.Object, diag.Diagnostics) {
21+
obj := WorkspaceRole{
22+
WorkspaceId: types.StringValue(role.WorkspaceId),
23+
Role: types.StringValue(string(role.Role)),
24+
}
25+
return types.ObjectValueFrom(ctx, schemas.WorkspaceRoleAttributeTypes(), obj)
26+
}
27+
28+
type DeploymentRole struct {
29+
DeploymentId types.String `tfsdk:"deployment_id"`
30+
Role types.String `tfsdk:"role"`
31+
}
32+
33+
func DeploymentRoleTypesObject(
34+
ctx context.Context,
35+
role iam.DeploymentRole,
36+
) (types.Object, diag.Diagnostics) {
37+
obj := DeploymentRole{
38+
DeploymentId: types.StringValue(role.DeploymentId),
39+
Role: types.StringValue(role.Role),
40+
}
41+
return types.ObjectValueFrom(ctx, schemas.DeploymentRoleAttributeTypes(), obj)
42+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package models
2+
3+
import (
4+
"context"
5+
6+
"github.com/astronomer/terraform-provider-astro/internal/clients/iam"
7+
"github.com/astronomer/terraform-provider-astro/internal/provider/schemas"
8+
"github.com/astronomer/terraform-provider-astro/internal/utils"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
)
12+
13+
// TeamRoles describes the team_roles resource
14+
type TeamRoles struct {
15+
TeamId types.String `tfsdk:"team_id"`
16+
OrganizationRole types.String `tfsdk:"organization_role"`
17+
WorkspaceRoles types.Set `tfsdk:"workspace_roles"`
18+
DeploymentRoles types.Set `tfsdk:"deployment_roles"`
19+
}
20+
21+
func (data *TeamRoles) ReadFromResponse(
22+
ctx context.Context,
23+
teamId string,
24+
teamRoles *iam.SubjectRoles,
25+
) diag.Diagnostics {
26+
var diags diag.Diagnostics
27+
data.TeamId = types.StringValue(teamId)
28+
data.OrganizationRole = types.StringPointerValue((*string)(teamRoles.OrganizationRole))
29+
data.WorkspaceRoles, diags = utils.ObjectSet(ctx, teamRoles.WorkspaceRoles, schemas.WorkspaceRoleAttributeTypes(), WorkspaceRoleTypesObject)
30+
if diags.HasError() {
31+
return diags
32+
}
33+
data.DeploymentRoles, diags = utils.ObjectSet(ctx, teamRoles.DeploymentRoles, schemas.DeploymentRoleAttributeTypes(), DeploymentRoleTypesObject)
34+
if diags.HasError() {
35+
return diags
36+
}
37+
return nil
38+
}

internal/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func (p *AstroProvider) Resources(ctx context.Context) []func() resource.Resourc
123123
resources.NewWorkspaceResource,
124124
resources.NewDeploymentResource,
125125
resources.NewClusterResource,
126+
resources.NewTeamRolesResource,
126127
}
127128
}
128129

internal/provider/provider_test_utils.go

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func TestAccPreCheck(t *testing.T) {
4444
if hybridNodePoolId := os.Getenv("HYBRID_NODE_POOL_ID"); len(hybridNodePoolId) == 0 {
4545
missingEnvVars = append(missingEnvVars, "HYBRID_NODE_POOL_ID")
4646
}
47+
if hostedTeamId := os.Getenv("HOSTED_TEAM_ID"); len(hostedTeamId) == 0 {
48+
missingEnvVars = append(missingEnvVars, "HOSTED_TEAM_ID")
49+
}
4750
if len(missingEnvVars) > 0 {
4851
t.Fatalf("Pre-check failed: %+v must be set for acceptance tests", strings.Join(missingEnvVars, ", "))
4952
}

0 commit comments

Comments
 (0)