Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix requires_replace for fmc_security_zones #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-beta2 (Unreleased)

- (Fix) Change value of `interface_type` within `fmc_security_zones` item should replace just this object, not entire bulk resource

## 2.0.0-beta1

- Initial release
4 changes: 4 additions & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ description: |-

# Changelog

## 2.0.0-beta2 (Unreleased)

- (Fix) Change value of `interface_type` within `fmc_security_zones` item should replace just this object, not entire bulk resource

## 2.0.0-beta1

- Initial release
Expand Down
1 change: 1 addition & 0 deletions gen/definitions/security_zones.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ attributes:
to mode NONE of associated interfaces.
enum_values: [PASSIVE, INLINE, SWITCHED, ROUTED, ASA]
example: ROUTED
requires_replace: true
- model_name: type
type: String
description: Type of the object; this value is always 'SecurityZone'.
Expand Down
16 changes: 16 additions & 0 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ func HasResourceId(attributes []YamlConfigAttribute) bool {
return false
}

// Templating helper function to return true if any of the attributes has `requires_replace: true`
func HasRequiresReplace(attributes []YamlConfigAttribute) bool {
for _, attr := range attributes {
if attr.RequiresReplace {
return true
}
if len(attr.Attributes) > 0 {
if HasRequiresReplace(attr.Attributes) {
return true
}
}
}
return false
}

// Templating helper function to return true if type is a list or set without nested elements
func IsListSet(attribute YamlConfigAttribute) bool {
if (attribute.Type == "List" || attribute.Type == "Set") && attribute.ElementType != "" {
Expand Down Expand Up @@ -403,6 +418,7 @@ var functions = template.FuncMap{
"hasId": HasId,
"hasReference": HasReference,
"hasResourceId": HasResourceId,
"hasRequiresReplace": HasRequiresReplace,
"isListSet": IsListSet,
"isList": IsList,
"isSet": IsSet,
Expand Down
132 changes: 98 additions & 34 deletions gen/templates/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,97 @@ func (data *{{camelCase .Name}}) fromBodyUnknowns(ctx context.Context, res gjson
}

// End of section. //template:end fromBodyUnknowns

// Section below is generated&owned by "gen/generator.go". //template:begin Clone

{{if .IsBulk}}
func (data *{{camelCase .Name}}) Clone() {{camelCase .Name}} {
ret := *data
ret.Items = maps.Clone(data.Items)

return ret
}
{{- end}}

// End of section. //template:end Clone

// Section below is generated&owned by "gen/generator.go". //template:begin toBodyNonBulk

{{if .IsBulk}}
// Updates done one-by-one require different API body
func (data {{camelCase .Name}}) toBodyNonBulk(ctx context.Context, state {{camelCase .Name}}) string {
// This is one-by-one update, so only one element to update is expected
if len(data.Items) > 1 {
tflog.Error(ctx, "Found more than one element to chage. Only one will be changed.")
}

// Utilize existing toBody function
body := data.toBody(ctx, state)

// Get first element only
return gjson.Get(body, "0").String()
}
{{- end}}

// End of section. //template:end toBodyNonBulk

// Section below is generated&owned by "gen/generator.go". //template:begin findObjectsToBeReplaced

{{if and .IsBulk (hasRequiresReplace .Attributes) }}
// Check if single object within bulk requires replace due to `requires_replace`
// Since here we assume object has changed, it must be present in both state and plan (data)
func (data {{camelCase .Name}}) findObjectsToBeReplaced(ctx context.Context, state {{camelCase .Name}}) {{camelCase .Name}} {
// Prepare empty object to be filled in with objects that require replace
var toBeReplaced {{camelCase .Name}}
toBeReplaced.Items = make(map[string]{{camelCase .Name}}Items)

// Iterate over all objects in plan
for key, item := range data.Items {
// Check if object is present in state
if _, ok := state.Items[key]; !ok {
// Object is not present in state, hence it's not a candidate for replace
continue
}

// Check if any field marked as `requires_replace` has changed
{{- range .Attributes}}
{{- if eq .TfName "items"}}
{{- range .Attributes}}
{{- if .RequiresReplace }}
{{- if (eq .Type "String")}}
if item.{{toGoName .TfName}} != state.Items[key].{{toGoName .TfName}} {
toBeReplaced.Items[key] = item
continue
}
{{- else}}
{{- errorf "requires_replace is not supported for %v" .Type }}
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}
}

return toBeReplaced
}
{{- end}}

// End of section. //template:end findObjectsToBeReplaced

// Section below is generated&owned by "gen/generator.go". //template:begin clearItemIds

{{if and .IsBulk (hasRequiresReplace .Attributes) }}
func (data *{{camelCase .Name}}) clearItemsIds(ctx context.Context) {
for key, value := range data.Items {
tmp := value
tmp.Id = types.StringNull()
data.Items[key] = tmp
}
}
{{- end}}

// End of section. //template:end clearItemIds

{{- range .Attributes}}
{{- if isNestedMap .}}
{{- $found := false }}
Expand Down Expand Up @@ -567,6 +658,9 @@ func (data *{{camelCase .Name}}) fromBodyUnknowns(ctx context.Context, res gjson
{{- if hasResourceId .Attributes}}
{{- errorf "resource_id not yet implemented at this depth"}}
{{- end}}
{{- if and $.IsBulk .RequiresReplace}}
{{- errorf "requires_replace is not supported for nested objects in bulk operations" }}
{{- end}}

{{- range .Attributes}}
{{- if isNestedMap .}}
Expand All @@ -581,40 +675,10 @@ func (data *{{camelCase .Name}}) fromBodyUnknowns(ctx context.Context, res gjson
{{- range .Attributes}}
{{- errorf "attributes not yet implemented at this depth"}}
{{- end}}
{{- if and $.IsBulk .RequiresReplace}}
{{- errorf "requires_replace is not supported for nested objects in bulk operations" }}
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}

// Section below is generated&owned by "gen/generator.go". //template:begin Clone

{{if .IsBulk}}
func (data *{{camelCase .Name}}) Clone() {{camelCase .Name}} {
ret := *data
ret.Items = maps.Clone(data.Items)

return ret
}
{{- end}}

// End of section. //template:end Clone

// Section below is generated&owned by "gen/generator.go". //template:begin toBodyNonBulk

{{if .IsBulk}}
// Updates done one-by-one require different API body
func (data {{camelCase .Name}}) toBodyNonBulk(ctx context.Context, state {{camelCase .Name}}) string {
// This is one-by-one update, so only one element to update is expected
if len(data.Items) > 1 {
tflog.Error(ctx, "Found more than one element to chage. Only one will be changed.")
}

// Utilize existing toBody function
body := data.toBody(ctx, state)

// Get first element only
return gjson.Get(body, "0").String()
}
{{- end}}

// End of section. //template:end toBodyNonBulk
{{- end}}
44 changes: 40 additions & 4 deletions gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S
{{- end}}
{{- if or .Id .Reference .RequiresReplace (and .Computed (not .ComputedRefreshValue))}}
PlanModifiers: []planmodifier.{{.Type}}{
{{- if or .Id .Reference .RequiresReplace}}
{{- if or .Id .Reference (and .RequiresReplace (not $.IsBulk))}}
{{snakeCase .Type}}planmodifier.RequiresReplace(),
{{end}}
{{- if and .Computed (not .ComputedRefreshValue)}}
Expand All @@ -210,6 +210,7 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S
{{- end}}
{{- if isNestedListMapSet .}}
{{- $useStateForUnknown := isNestedMap .}}
{{- $itemsList := .}}
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
{{- range .Attributes}}
Expand Down Expand Up @@ -281,9 +282,19 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S
{{- end}}
{{- if or (and .ResourceId $useStateForUnknown) (and .Computed (not .ComputedRefreshValue))}}
PlanModifiers: []planmodifier.{{.Type}}{
{{- if and $.IsBulk (eq .TfName "id") (hasRequiresReplace $.Attributes) }}
{{- if eq .Type "String"}}
{{- range $itemsList.Attributes }}
{{- if .RequiresReplace}}
planmodifiers.ConditionalUseStateForUnknownString("{{.TfName}}"),
{{- end}}
{{- end }}
{{- end}}
{{- else}}
{{snakeCase .Type}}planmodifier.UseStateForUnknown(),
{{- end}}
},
{{- else if .RequiresReplace}}
{{- else if and .RequiresReplace (not $.IsBulk)}}
PlanModifiers: []planmodifier.{{.Type}}{
{{snakeCase .Type}}planmodifier.RequiresReplace(),
},
Expand Down Expand Up @@ -360,7 +371,7 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S
{{- end}}
{{- if or .RequiresReplace .Computed}}
PlanModifiers: []planmodifier.{{.Type}}{
{{- if .RequiresReplace}}
{{- if and .RequiresReplace (not $.IsBulk)}}
{{snakeCase .Type}}planmodifier.RequiresReplace(),
{{end}}
{{- if and .Computed (not .ComputedRefreshValue)}}
Expand Down Expand Up @@ -440,7 +451,7 @@ func (r *{{camelCase .Name}}Resource) Schema(ctx context.Context, req resource.S
{{- end}}
{{- if or .RequiresReplace .Computed}}
PlanModifiers: []planmodifier.{{.Type}}{
{{- if .RequiresReplace}}
{{- if and .RequiresReplace (not $.IsBulk)}}
{{snakeCase .Type}}planmodifier.RequiresReplace(),
{{end}}
{{- if and .Computed (not .ComputedRefreshValue)}}
Expand Down Expand Up @@ -758,10 +769,19 @@ func (r *{{camelCase .Name}}Resource) Update(ctx context.Context, req resource.U

{{- if .IsBulk}}

{{- if hasRequiresReplace $.Attributes}}
// Get objects that need to be replaced due to `requires_replace` flag
toBeReplaced := plan.findObjectsToBeReplaced(ctx, state)
{{- end}}

// DELETE
// Delete objects (that are present in state, but missing in plan)
{{- if hasRequiresReplace $.Attributes}}
toDelete := toBeReplaced.Clone()
{{- else}}
var toDelete {{camelCase .Name}}
toDelete.Items = make(map[string]{{camelCase .Name}}Items)
{{- end}}
planOwnedIDs := make(map[string]string, len(plan.Items))

// Prepare list of ID that are in plan
Expand Down Expand Up @@ -790,8 +810,13 @@ func (r *{{camelCase .Name}}Resource) Update(ctx context.Context, req resource.U

// CREATE
// Create new objects (objects that have missing IDs in plan)
{{- if hasRequiresReplace $.Attributes}}
toCreate := toBeReplaced.Clone()
toCreate.clearItemsIds(ctx)
{{- else}}
var toCreate {{camelCase .Name}}
toCreate.Items = make(map[string]{{camelCase .Name}}Items)
{{- end}}
// Scan plan for items with no ID
for k, v := range plan.Items {
if v.Id.IsUnknown() || v.Id.IsNull() {
Expand All @@ -817,7 +842,18 @@ func (r *{{camelCase .Name}}Resource) Update(ctx context.Context, req resource.U
var toUpdate {{camelCase .Name}}
toUpdate.Items = make(map[string]{{camelCase .Name}}Items)

{{- if hasRequiresReplace $.Attributes}}

for tmp, valueState := range state.Items {
// Check if the ID from state is on toBeReplaced list
if _, ok := toBeReplaced.Items[tmp]; ok {
// If it is, skip it as it was handled by delete/create processes
continue
}
{{- else }}

for _, valueState := range state.Items {
{{- end}}

// Check if the ID from plan exists on list of ID owned by state
if keyState, ok := planOwnedIDs[valueState.Id.ValueString()]; ok {
Expand Down
41 changes: 41 additions & 0 deletions internal/provider/model_fmc_security_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,44 @@ func (data SecurityZones) toBodyNonBulk(ctx context.Context, state SecurityZones
}

// End of section. //template:end toBodyNonBulk

// Section below is generated&owned by "gen/generator.go". //template:begin findObjectsToBeReplaced

// Check if single object within bulk requires replace due to `requires_replace`
// Since here we assume object has changed, it must be present in both state and plan (data)
func (data SecurityZones) findObjectsToBeReplaced(ctx context.Context, state SecurityZones) SecurityZones {
// Prepare empty object to be filled in with objects that require replace
var toBeReplaced SecurityZones
toBeReplaced.Items = make(map[string]SecurityZonesItems)

// Iterate over all objects in plan
for key, item := range data.Items {
// Check if object is present in state
if _, ok := state.Items[key]; !ok {
// Object is not present in state, hence it's not a candidate for replace
continue
}

// Check if any field marked as `requires_replace` has changed
if item.InterfaceType != state.Items[key].InterfaceType {
toBeReplaced.Items[key] = item
continue
}
}

return toBeReplaced
}

// End of section. //template:end findObjectsToBeReplaced

// Section below is generated&owned by "gen/generator.go". //template:begin clearItemIds

func (data *SecurityZones) clearItemsIds(ctx context.Context) {
for key, value := range data.Items {
tmp := value
tmp.Id = types.StringNull()
data.Items[key] = tmp
}
}

// End of section. //template:end clearItemIds
Loading
Loading