Skip to content

Commit

Permalink
fix: validation for principal ids pgd fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wai-wong-edb committed Nov 26, 2024
1 parent c51de46 commit 27bde9c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/plan_modifier/cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func CustomCloudProvider() planmodifier.String {
func CustomClusterCloudProvider() planmodifier.String {
return customCloudProviderModifier{}
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/plan_modifier/data_group_custom_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"strings"

"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/pgd/terraform"
"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -153,6 +154,27 @@ func (m CustomDataGroupDiffModifier) PlanModifyList(ctx context.Context, req pla
pDg.Connection = types.StringUnknown()
}

// validation to remove principal ids and service account ids if cloud provider is not bah
if !strings.Contains(*pDg.Provider.CloudProviderId, "bah") {
if !pDg.PeAllowedPrincipalIds.IsNull() && len(pDg.PeAllowedPrincipalIds.Elements()) > 0 {
resp.Diagnostics.AddError("your cloud account 'pe_allowed_principal_ids' field not allowed error",
fmt.Sprintf("field 'pe_allowed_principal_ids' for region %v should only be set if you are using 'bah' cloud provider, please remove 'pe_allowed_principal_ids'\n", pDg.Region.RegionId))
return
}

if !pDg.ServiceAccountIds.IsNull() && len(pDg.ServiceAccountIds.Elements()) > 0 {
resp.Diagnostics.AddError("your cloud account 'service_account_ids' field not allowed error",
fmt.Sprintf("field 'service_account_ids' for region %v should only be set if you are using cloud provider 'bah:gcp', please remove 'service_account_ids'\n", pDg.Region.RegionId))
return
}
} else if strings.Contains(*pDg.Provider.CloudProviderId, "bah") && !strings.Contains(*pDg.Provider.CloudProviderId, "bah:gcp") {
if !pDg.ServiceAccountIds.IsNull() && len(pDg.ServiceAccountIds.Elements()) > 0 {
resp.Diagnostics.AddError("your cloud account 'service_account_ids' field not allowed error",
fmt.Sprintf("you are not using cloud provider 'bah:gcp' for region %v, field 'service_account_ids' should only be set if you are using cloud provider 'bah:gcp', please remove 'service_account_ids'", pDg.Region.RegionId))
return
}
}

newDgPlan = append(newDgPlan, pDg)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/resource_analytics_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (r *analyticsClusterResource) Schema(ctx context.Context, req resource.Sche
"cloud_provider": schema.StringAttribute{
Description: "Cloud provider. For example, \"aws\" or \"bah:aws\".",
Required: true,
PlanModifiers: []planmodifier.String{plan_modifier.CustomCloudProvider()},
PlanModifiers: []planmodifier.String{plan_modifier.CustomClusterCloudProvider()},
},
"pg_type": schema.StringAttribute{
MarkdownDescription: "Postgres type. For example, \"epas\" or \"pgextended\".",
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest
"cloud_provider": schema.StringAttribute{
Description: "Cloud provider. For example, \"aws\", \"azure\", \"gcp\" or \"bah:aws\", \"bah:gcp\".",
Required: true,
PlanModifiers: []planmodifier.String{plan_modifier.CustomCloudProvider()},
PlanModifiers: []planmodifier.String{plan_modifier.CustomClusterCloudProvider()},
},
"pg_type": schema.StringAttribute{
MarkdownDescription: "Postgres type. For example, \"epas\", \"pgextended\", or \"postgres\".",
Expand Down
5 changes: 2 additions & 3 deletions pkg/provider/resource_pgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,8 @@ func PgdSchema(ctx context.Context) schema.Schema {
Required: true,
Attributes: map[string]schema.Attribute{
"cloud_provider_id": schema.StringAttribute{
Description: "Data group cloud provider id.",
Required: true,
PlanModifiers: []planmodifier.String{plan_modifier.CustomCloudProvider()},
Description: "Data group cloud provider id.",
Required: true,
},
},
},
Expand Down

0 comments on commit 27bde9c

Please sign in to comment.