Skip to content

Commit

Permalink
Fix permadiff when Access Context Manager returns a different order f…
Browse files Browse the repository at this point in the history
…or ingress / egress rule identities (#12572)

Co-authored-by: Charlesleonius <[email protected]>
  • Loading branch information
Charlesleonius and Charlesleonius authored Dec 27, 2024
1 parent 6cde904 commit d630fcf
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ properties:
https://cloud.google.com/iam/docs/principal-identifiers#v1 are supported.
item_type:
type: String
diff_suppress_func: AccessContextManagerServicePerimeterDryRunEgressPolicyEgressFromIdentitiesDiffSuppressFunc
custom_flatten: templates/terraform/custom_flatten/accesscontextmanager_egress_policy_from_identities_custom_flatten.go.tmpl
- name: 'sources'
type: Array
description: 'Sources that this EgressPolicy authorizes access from.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ properties:
https://cloud.google.com/iam/docs/principal-identifiers#v1 are supported.
item_type:
type: String
diff_suppress_func: AccessContextManagerServicePerimeterDryRunIngressPolicyIngressFromIdentitiesDiffSuppressFunc
custom_flatten: templates/terraform/custom_flatten/accesscontextmanager_ingress_policy_from_identities_custom_flatten.go.tmpl
- name: 'sources'
type: Array
description: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ properties:
https://cloud.google.com/iam/docs/principal-identifiers#v1 are supported.
item_type:
type: String
diff_suppress_func: AccessContextManagerServicePerimeterEgressPolicyEgressFromIdentitiesDiffSuppressFunc
custom_flatten: templates/terraform/custom_flatten/accesscontextmanager_egress_policy_from_identities_custom_flatten.go.tmpl
- name: 'sources'
type: Array
description: 'Sources that this EgressPolicy authorizes access from.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ properties:
https://cloud.google.com/iam/docs/principal-identifiers#v1 are supported.
item_type:
type: String
diff_suppress_func: AccessContextManagerServicePerimeterIngressPolicyIngressFromIdentitiesDiffSuppressFunc
custom_flatten: templates/terraform/custom_flatten/accesscontextmanager_ingress_policy_from_identities_custom_flatten.go.tmpl
- name: 'sources'
type: Array
description: |
Expand Down
42 changes: 42 additions & 0 deletions mmv1/templates/terraform/constants/access_context_manager.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,48 @@ func {{$.ResourceName}}IngressToResourcesDiffSuppressFunc(_, _, _ string, d *sch
return slices.Equal(oldResources, newResources)
}

func {{$.ResourceName}}EgressFromIdentitiesDiffSuppressFunc(_, _, _ string, d *schema.ResourceData) bool {
old, new := d.GetChange("egress_from.0.identities")

oldResources, err := tpgresource.InterfaceSliceToStringSlice(old)
if err != nil {
log.Printf("[ERROR] Failed to convert egress from identities config value: %s", err)
return false
}

newResources, err := tpgresource.InterfaceSliceToStringSlice(new)
if err != nil {
log.Printf("[ERROR] Failed to convert egress from identities api value: %s", err)
return false
}

sort.Strings(oldResources)
sort.Strings(newResources)

return slices.Equal(oldResources, newResources)
}

func {{$.ResourceName}}IngressFromIdentitiesDiffSuppressFunc(_, _, _ string, d *schema.ResourceData) bool {
old, new := d.GetChange("ingress_from.0.identities")

oldResources, err := tpgresource.InterfaceSliceToStringSlice(old)
if err != nil {
log.Printf("[ERROR] Failed to convert ingress from identities config value: %s", err)
return false
}

newResources, err := tpgresource.InterfaceSliceToStringSlice(new)
if err != nil {
log.Printf("[ERROR] Failed to convert ingress from identities api value: %s", err)
return false
}

sort.Strings(oldResources)
sort.Strings(newResources)

return slices.Equal(oldResources, newResources)
}

func {{$.ResourceName}}IdentityTypeDiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
if old == "" && new == "IDENTITY_TYPE_UNSPECIFIED" {
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
rawConfigValue := d.Get("egress_from.0.identities")
// Convert config value to []string
configValue, err := tpgresource.InterfaceSliceToStringSlice(rawConfigValue)
if err != nil {
log.Printf("[ERROR] Failed to convert egress from identities config value: %s", err)
return v
}
sortedConfigValue := append([]string{}, configValue...)
sort.Strings(sortedConfigValue)

// Convert v to []string
apiValue, err := tpgresource.InterfaceSliceToStringSlice(v)
if err != nil {
log.Printf("[ERROR] Failed to convert egress from identities API value: %s", err)
return v
}
sortedApiValue := append([]string{}, apiValue...)
sort.Strings(sortedApiValue)

if (slices.Equal(sortedApiValue, sortedConfigValue)) {
return configValue
}

return apiValue
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
rawConfigValue := d.Get("ingress_from.0.identities")
// Convert config value to []string
configValue, err := tpgresource.InterfaceSliceToStringSlice(rawConfigValue)
if err != nil {
log.Printf("[ERROR] Failed to convert ingress from identities config value: %s", err)
return v
}
sortedConfigValue := append([]string{}, configValue...)
sort.Strings(sortedConfigValue)

// Convert v to []string
apiValue, err := tpgresource.InterfaceSliceToStringSlice(v)
if err != nil {
log.Printf("[ERROR] Failed to convert ingress from identities API value: %s", err)
return v
}
sortedApiValue := append([]string{}, apiValue...)
sort.Strings(sortedApiValue)

if (slices.Equal(sortedApiValue, sortedConfigValue)) {
return configValue
}

return apiValue
}

0 comments on commit d630fcf

Please sign in to comment.