Skip to content

Commit

Permalink
refactor out role and fix type schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ichung08 committed Jul 17, 2024
1 parent e56da5d commit 3e1fac9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 49 deletions.
7 changes: 1 addition & 6 deletions docs/resources/api_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ resource "astro_api_token" "example" {
name = "api token"
description = "api token description"
type = "ORGANIZATION"
role = "ORGANIZATION_OWNER"
roles = [{
"role" : "ORGANIZATION_OWNER",
"entity_id" : "clx42kkcm01fo01o06agtmshg",
Expand All @@ -30,7 +29,6 @@ resource "astro_api_token" "example" {
name = "api token"
description = "api token description"
type = "ORGANIZATION"
role = "ORGANIZATION_OWNER"
roles = [{
"role" : "ORGANIZATION_OWNER",
"entity_id" : "clx42kkcm01fo01o06agtmshg",
Expand All @@ -52,7 +50,6 @@ resource "astro_api_token" "example_workspace_token" {
name = "workspace api token"
description = "workspace api token description"
type = "WORKSPACE"
role = "WORKSPACE_OWNER"
roles = [{
"role" : "WORKSPACE_OWNER",
"entity_id" : "clx42sxw501gl01o0gjenthnh",
Expand All @@ -64,7 +61,6 @@ resource "astro_api_token" "example_workspace_token_with_deployment_role" {
name = "workspace api token"
description = "workspace api token description"
type = "WORKSPACE"
role = "WORKSPACE_OWNER"
roles = [{
"role" : "WORKSPACE_OWNER",
"entity_id" : "clx42sxw501gl01o0gjenthnh",
Expand All @@ -84,9 +80,8 @@ resource "astro_api_token" "example_workspace_token_with_deployment_role" {
### Required

- `name` (String) API Token name
- `role` (String) The role assigned to the API Token
- `roles` (Attributes Set) The roles assigned to the API Token (see [below for nested schema](#nestedatt--roles))
- `type` (String) API Token type
- `type` (String) API Token type - if changing this value, the API Token will be recreated with the new type

### Optional

Expand Down
4 changes: 0 additions & 4 deletions examples/resources/astro_api_token/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ resource "astro_api_token" "example" {
name = "api token"
description = "api token description"
type = "ORGANIZATION"
role = "ORGANIZATION_OWNER"
roles = [{
"role" : "ORGANIZATION_OWNER",
"entity_id" : "clx42kkcm01fo01o06agtmshg",
Expand All @@ -15,7 +14,6 @@ resource "astro_api_token" "example" {
name = "api token"
description = "api token description"
type = "ORGANIZATION"
role = "ORGANIZATION_OWNER"
roles = [{
"role" : "ORGANIZATION_OWNER",
"entity_id" : "clx42kkcm01fo01o06agtmshg",
Expand All @@ -37,7 +35,6 @@ resource "astro_api_token" "example_workspace_token" {
name = "workspace api token"
description = "workspace api token description"
type = "WORKSPACE"
role = "WORKSPACE_OWNER"
roles = [{
"role" : "WORKSPACE_OWNER",
"entity_id" : "clx42sxw501gl01o0gjenthnh",
Expand All @@ -49,7 +46,6 @@ resource "astro_api_token" "example_workspace_token_with_deployment_role" {
name = "workspace api token"
description = "workspace api token description"
type = "WORKSPACE"
role = "WORKSPACE_OWNER"
roles = [{
"role" : "WORKSPACE_OWNER",
"entity_id" : "clx42sxw501gl01o0gjenthnh",
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/models/api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type ApiTokenResource struct {
UpdatedBy types.Object `tfsdk:"updated_by"`
ExpiryPeriodInDays types.Int64 `tfsdk:"expiry_period_in_days"`
LastUsedAt types.String `tfsdk:"last_used_at"`
Role types.String `tfsdk:"role"`
Roles types.Set `tfsdk:"roles"`
Token types.String `tfsdk:"token"`
}
Expand Down Expand Up @@ -88,7 +87,7 @@ func (data *ApiTokenDataSource) ReadFromResponse(ctx context.Context, apiToken *
return diags
}

func (data *ApiTokenResource) ReadFromResponse(ctx context.Context, apiToken *iam.ApiToken, role *string) diag.Diagnostics {
func (data *ApiTokenResource) ReadFromResponse(ctx context.Context, apiToken *iam.ApiToken) diag.Diagnostics {
var diags diag.Diagnostics
data.Id = types.StringValue(apiToken.Id)
data.Name = types.StringValue(apiToken.Name)
Expand Down Expand Up @@ -123,7 +122,6 @@ func (data *ApiTokenResource) ReadFromResponse(ctx context.Context, apiToken *ia
} else {
data.LastUsedAt = types.StringValue("")
}
data.Role = types.StringPointerValue(role)
data.Roles, diags = utils.ObjectSet(ctx, apiToken.Roles, schemas.ApiTokenRoleAttributeTypes(), ApiTokenRoleTypesObject)
if diags.HasError() {
return diags
Expand Down
32 changes: 13 additions & 19 deletions internal/provider/resources/resource_api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,10 @@ func (r *ApiTokenResource) Create(
return
}

role := data.Role.ValueString()
if len(role) == 0 {
role, _, diags = RequestApiTokenRole(ctx, roles, data.Type.ValueString())
if diags != nil {
resp.Diagnostics.Append(diags...)
return
}
role, _, diags := RequestApiTokenRole(roles, data.Type.ValueString())
if diags != nil {
resp.Diagnostics.Append(diags...)
return
}

// Create the API token request
Expand All @@ -116,7 +113,7 @@ func (r *ApiTokenResource) Create(
// If the entity type is WORKSPACE or DEPLOYMENT, set the entity id
if createApiTokenRequest.Type == iam.WORKSPACE || createApiTokenRequest.Type == iam.DEPLOYMENT {
var entityId string
_, entityId, diags = RequestApiTokenRole(ctx, roles, data.Type.ValueString())
_, entityId, diags = RequestApiTokenRole(roles, data.Type.ValueString())
if diags != nil {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -196,7 +193,7 @@ func (r *ApiTokenResource) Create(
return
}

diags = data.ReadFromResponse(ctx, apiTokenResp.JSON200, data.Role.ValueStringPointer())
diags = data.ReadFromResponse(ctx, apiTokenResp.JSON200)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -249,7 +246,7 @@ func (r *ApiTokenResource) Read(
return
}

diags := data.ReadFromResponse(ctx, apiToken.JSON200, data.Role.ValueStringPointer())
diags := data.ReadFromResponse(ctx, apiToken.JSON200)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -353,7 +350,7 @@ func (r *ApiTokenResource) Update(
return
}

diags = data.ReadFromResponse(ctx, apiTokenResp.JSON200, data.Role.ValueStringPointer())
diags = data.ReadFromResponse(ctx, apiTokenResp.JSON200)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -430,13 +427,10 @@ func (r *ApiTokenResource) ValidateConfig(
return
}

tokenRole := data.Role.ValueString()
if len(tokenRole) == 0 {
tokenRole, _, diags = RequestApiTokenRole(ctx, roles, data.Type.ValueString())
if diags != nil {
resp.Diagnostics.Append(diags...)
return
}
tokenRole, _, diags := RequestApiTokenRole(roles, data.Type.ValueString())
if diags != nil {
resp.Diagnostics.Append(diags...)
return
}

entityType := data.Type.ValueString()
Expand Down Expand Up @@ -553,7 +547,7 @@ func RequestApiTokenRoles(ctx context.Context, apiTokenRolesObjSet types.Set) ([
return apiTokenRoles, nil
}

func RequestApiTokenRole(ctx context.Context, roles []iam.ApiTokenRole, entityType string) (string, string, diag.Diagnostics) {
func RequestApiTokenRole(roles []iam.ApiTokenRole, entityType string) (string, string, diag.Diagnostics) {
for _, role := range roles {
if role.EntityType == iam.ApiTokenRoleEntityType(entityType) {
return role.Role, role.EntityId, nil
Expand Down
21 changes: 4 additions & 17 deletions internal/provider/schemas/api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ func ApiTokenResourceSchemaAttributes() map[string]resourceSchema.Attribute {
Computed: true,
},
"type": resourceSchema.StringAttribute{
MarkdownDescription: "API Token type",
MarkdownDescription: "API Token type - if changing this value, the API Token will be recreated with the new type",
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(string(iam.ApiTokenTypeORGANIZATION),
string(iam.ApiTokenTypeWORKSPACE),
string(iam.ApiTokenRoleEntityTypeDEPLOYMENT),
),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplaceIfConfigured(),
},
},
"start_at": resourceSchema.StringAttribute{
MarkdownDescription: "time when the API token will become valid in UTC",
Expand Down Expand Up @@ -144,22 +147,6 @@ func ApiTokenResourceSchemaAttributes() map[string]resourceSchema.Attribute {
MarkdownDescription: "API Token last used timestamp",
Computed: true,
},
"role": resourceSchema.StringAttribute{
MarkdownDescription: "The role assigned to the API Token",
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(string(iam.ORGANIZATIONBILLINGADMIN),
string(iam.ORGANIZATIONMEMBER),
string(iam.ORGANIZATIONOWNER),
string(iam.WORKSPACEACCESSOR),
string(iam.WORKSPACEAUTHOR),
string(iam.WORKSPACEMEMBER),
string(iam.WORKSPACEOWNER),
string(iam.WORKSPACEOPERATOR),
"DEPLOYMENT_ADMIN",
),
},
},
"roles": resourceSchema.SetNestedAttribute{
NestedObject: resourceSchema.NestedAttributeObject{
Attributes: ResourceApiTokenRoleSchemaAttributes(),
Expand Down

0 comments on commit 3e1fac9

Please sign in to comment.