Skip to content

Commit

Permalink
fix: modify plan wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wai-wong-edb committed Jan 31, 2025
1 parent a4662c8 commit 2b5fd08
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pkg/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// build tag assign terraform resource as, using api response as input
func buildTFRsrcAssignTagsAs(tfRsrcTagsOut *[]commonTerraform.Tag, apiRespTags []commonApi.Tag) {
func buildTfRsrcTagsAs(tfRsrcTagsOut *[]commonTerraform.Tag, apiRespTags []commonApi.Tag) {
*tfRsrcTagsOut = []commonTerraform.Tag{}
for _, v := range apiRespTags {
*tfRsrcTagsOut = append(*tfRsrcTagsOut, commonTerraform.Tag{
Expand All @@ -23,7 +23,7 @@ func buildTFRsrcAssignTagsAs(tfRsrcTagsOut *[]commonTerraform.Tag, apiRespTags [
}

// build tag assign request using terraform resource as input
func buildAPIReqAssignTags(tfRsrcTags []commonTerraform.Tag) []commonApi.Tag {
func buildApiReqTags(tfRsrcTags []commonTerraform.Tag) []commonApi.Tag {
tags := []commonApi.Tag{}
for _, tag := range tfRsrcTags {
tags = append(tags, commonApi.Tag{
Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/resource_analytics_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func generateAnalyticsClusterModelCreate(ctx context.Context, client *api.Cluste
}
}

cluster.Tags = buildAPIReqAssignTags(clusterResource.Tags)
cluster.Tags = buildApiReqTags(clusterResource.Tags)

return cluster, nil
}
Expand Down Expand Up @@ -558,7 +558,7 @@ func readAnalyticsCluster(ctx context.Context, client *api.ClusterClient, tfClus
tfClusterResource.ServiceAccountIds = StringSliceToSet(utils.ToValue(&responseCluster.ServiceAccountIds))
}

buildTFRsrcAssignTagsAs(&tfClusterResource.Tags, responseCluster.Tags)
buildTfRsrcTagsAs(&tfClusterResource.Tags, responseCluster.Tags)

return nil
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/provider/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,25 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest
}
}

// func (c *clusterResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
// config := resp.Plan

// // Validate existing tag. Existing tag colors cannot be changed in a cluster create request and must be removed.
// // To change tag color, use tag request
// existingTags, err := c.client.TagClient().List(ctx)
// if err != nil {
// resp.Diagnostics.AddError("Error fetching existing tags", err.Error())
// }
// for _, configTag := range config.Elements() {
// tagName := configTag.(basetypes.ObjectValue).Attributes()["tag_name"].(basetypes.StringValue).ValueString()
// for _, existingTag := range existingTags {
// if existingTag.TagName == tagName {
// resp.Diagnostics.AddError("Existing tag color cannot be changed", fmt.Sprintf("Please remove existing tag color for tag %s", tagName))
// }
// }
// }
// }

func (c *clusterResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var config ClusterResourceModel
Expand Down Expand Up @@ -991,7 +1010,7 @@ func readCluster(ctx context.Context, client *api.ClusterClient, tfClusterResour
}
}

buildTFRsrcAssignTagsAs(&tfClusterResource.Tags, responseCluster.Tags)
buildTfRsrcTagsAs(&tfClusterResource.Tags, responseCluster.Tags)

if responseCluster.EncryptionKeyResp != nil {
tfClusterResource.TransparentDataEncryption = &TransparentDataEncryptionModel{}
Expand Down Expand Up @@ -1213,7 +1232,7 @@ func (c *clusterResource) generateGenericClusterModel(ctx context.Context, clust
cluster.ServiceAccountIds = svAccIds
cluster.PeAllowedPrincipalIds = principalIds

cluster.Tags = buildAPIReqAssignTags(clusterResource.Tags)
cluster.Tags = buildApiReqTags(clusterResource.Tags)

if clusterResource.TransparentDataEncryption != nil {
if !clusterResource.TransparentDataEncryption.KeyId.IsNull() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/provider/resource_pgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func (p pgdResource) Create(ctx context.Context, req resource.CreateRequest, res
Password: config.Password,
}

clusterReqBody.Tags = buildAPIReqAssignTags(config.Tags)
clusterReqBody.Tags = buildApiReqTags(config.Tags)

clusterReqBody.Groups = &[]any{}

Expand Down Expand Up @@ -867,7 +867,7 @@ func (p pgdResource) Read(ctx context.Context, req resource.ReadRequest, resp *r
state.ClusterId = clusterResp.ClusterId
state.ClusterName = clusterResp.ClusterName

buildTFRsrcAssignTagsAs(&state.Tags, clusterResp.Tags)
buildTfRsrcTagsAs(&state.Tags, clusterResp.Tags)

buildTFGroupsAs(ctx, &resp.Diagnostics, resp.State, *clusterResp, &state)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -955,7 +955,7 @@ func (p pgdResource) Update(ctx context.Context, req resource.UpdateRequest, res
Password: plan.Password,
}

clusterReqBody.Tags = buildAPIReqAssignTags(plan.Tags)
clusterReqBody.Tags = buildApiReqTags(plan.Tags)

clusterReqBody.Groups = &[]any{}

Expand Down Expand Up @@ -1207,7 +1207,7 @@ func (p *pgdResource) retryFuncAs(ctx context.Context, diags *diag.Diagnostics,
return retry.RetryableError(errors.New("instance not yet ready"))
}

buildTFRsrcAssignTagsAs(&outPgdTfResource.Tags, pgdResp.Tags)
buildTfRsrcTagsAs(&outPgdTfResource.Tags, pgdResp.Tags)

return nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (p projectResource) Create(ctx context.Context, req resource.CreateRequest,

projectReqModel := models.Project{
ProjectName: *config.ProjectName,
Tags: buildAPIReqAssignTags(config.Tags),
Tags: buildApiReqTags(config.Tags),
}

projectId, err := p.client.Create(ctx, projectReqModel)
Expand All @@ -194,7 +194,7 @@ func (p projectResource) Create(ctx context.Context, req resource.CreateRequest,
})
}

buildTFRsrcAssignTagsAs(&config.Tags, project.Tags)
buildTfRsrcTagsAs(&config.Tags, project.Tags)

diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -230,7 +230,7 @@ func (p projectResource) Read(ctx context.Context, req resource.ReadRequest, res
})
}

buildTFRsrcAssignTagsAs(&state.Tags, project.Tags)
buildTfRsrcTagsAs(&state.Tags, project.Tags)

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand All @@ -250,7 +250,7 @@ func (p projectResource) Update(ctx context.Context, req resource.UpdateRequest,

projectReqModel := models.Project{
ProjectName: *plan.ProjectName,
Tags: buildAPIReqAssignTags(plan.Tags),
Tags: buildApiReqTags(plan.Tags),
}

_, err := p.client.Update(ctx, *plan.ProjectID, projectReqModel)
Expand All @@ -259,7 +259,7 @@ func (p projectResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}

buildTFRsrcAssignTagsAs(&plan.Tags, projectReqModel.Tags)
buildTfRsrcTagsAs(&plan.Tags, projectReqModel.Tags)

diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/resource_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (r *regionResource) ensureStatueUpdated(ctx context.Context, region Region)
}

diags := frameworkdiag.Diagnostics{}
if err := r.client.Update(ctx, *region.Status, *region.ProjectID, *region.CloudProvider, *region.RegionID, buildAPIReqAssignTags(region.Tags)); err != nil {
if err := r.client.Update(ctx, *region.Status, *region.ProjectID, *region.CloudProvider, *region.RegionID, buildApiReqTags(region.Tags)); err != nil {
if appendDiagFromBAErr(err, &diags) {
return diags
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func (r *regionResource) writeState(ctx context.Context, region Region, state *t
region.Status = &read.Status
region.Continent = &read.Continent

buildTFRsrcAssignTagsAs(&region.Tags, read.Tags)
buildTfRsrcTagsAs(&region.Tags, read.Tags)

return state.Set(ctx, &region)
}
Expand Down

0 comments on commit 2b5fd08

Please sign in to comment.