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

Add support for Update in Port Assignment #168

Open
wants to merge 6 commits 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.1.19 (unreleased)

- Add Update support for `catalystcenter_fabric_port_assignment` resource
- Add support for auto_generated_vlan_name in `catalystcenter_anycast_gateway` resource
- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165)

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/fabric_port_assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Read-Only:
- `connected_device_type` (String) Connected device type of the port assignment
- `data_vlan_name` (String) Data VLAN name of the port assignment
- `fabric_id` (String) ID of the fabric the device is assigned to
- `id` (String) ID of the port assignment
- `interface_description` (String) Interface description of the port assignment
- `interface_name` (String) Interface name of the port assignment
- `network_device_id` (String) Network device ID of the port assignment
Expand Down
1 change: 1 addition & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description: |-

## 0.1.19 (unreleased)

- Add Update support for `catalystcenter_fabric_port_assignment` resource
- Add support for auto_generated_vlan_name in `catalystcenter_anycast_gateway` resource
- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165)

Expand Down
1 change: 1 addition & 0 deletions docs/resources/fabric_port_assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Optional:
- `authenticate_template_name` (String) Authenticate template name of the port assignment
- Choices: `No Authentication`, `Open Authentication`, `Closed Authentication`, `Low Impact`
- `data_vlan_name` (String) Data VLAN name of the port assignment
- `id` (String) ID of the port assignment
- `interface_description` (String) Interface description of the port assignment
- `security_group_name` (String) Security group name of the port assignment
- `voice_vlan_name` (String) Voice VLAN name of the port assignment
13 changes: 8 additions & 5 deletions gen/definitions/fabric_port_assignment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
name: Fabric Port Assignment
rest_endpoint: /dna/intent/api/v1/sda/portAssignments
res_description: Manages port assignments in SD-Access fabric.
no_update: true
no_import: true
root_list: true
update_computed: true
data_source_no_id: true
id_from_attribute: true
root_list: true
skip_minimum_test: true
device_unreachability_warning: true
doc_category: SDA
Expand Down Expand Up @@ -37,22 +37,25 @@ attributes:
mandatory: true
description: List of port assignments in SD-Access fabric
attributes:
- model_name: id
type: String
computed: true
description: ID of the port assignment
exclude_test: true
- model_name: fabricId
query_param: true
type: String
mandatory: true
description: ID of the fabric the device is assigned to
example: c4b85bb2-ce3f-4db9-a32b-e439a388ac2f
test_value: catalystcenter_fabric_site.test.id
- model_name: networkDeviceId
match_id: true
query_param: true
mandatory: true
description: Network device ID of the port assignment
type: String
example: 5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1
- model_name: interfaceName
query_param: true
id: true
mandatory: true
type: String
description: Interface name of the port assignment
Expand Down
1 change: 1 addition & 0 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type YamlConfig struct {
ImportNoId bool `yaml:"import_no_id"`
PostUpdate bool `yaml:"post_update"`
PutCreate bool `yaml:"put_create"`
UpdateComputed bool `yaml:"update_computed"`
RootList bool `yaml:"root_list"`
NoReadPrefix bool `yaml:"no_read_prefix"`
IdPath string `yaml:"id_path"`
Expand Down
1 change: 1 addition & 0 deletions gen/schema/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ no_import: bool(required=False) # Set to true if the resource does not support i
import_no_id: bool(required=False) # Set to true if import does not require an ID
post_update: bool(required=False) # Set to true if the POST request is used for update
put_create: bool(required=False) # Set to true if the PUT request is used for create
update_computed: bool(required=False) # Set to true if the computed attributes should be updated, only relevant if computed is set on one of the attributes
root_list: bool(required=False) # Set to true if the root element of the data structure is a list
no_read_prefix: bool(required=False) # Set to true if it is an Open API endpoint put the response is not embeeded into a "response" element
id_path: str(required=False) # Path to the ID in the response (use "." to access nested elements)
Expand Down
194 changes: 194 additions & 0 deletions gen/templates/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,200 @@ func (data *{{camelCase .Name}}) updateFromBody(ctx context.Context, res gjson.R
}
// End of section. //template:end updateFromBody

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

// fromBodyUnknowns updates the Unknown Computed tfstate values from a JSON.
// Known values are not changed (usual for Computed attributes with UseStateForUnknown or with Default).
func (data *{{camelCase .Name}}) fromBodyUnknowns(ctx context.Context, res gjson.Result) {
{{- if .RootList}}
{{- range .Attributes}}
{{if .ResponseDataPath}}
res = res.Get("{{.ResponseDataPath}}")
{{- end}}
{{- end}}
{{- end}}
{{- range .Attributes}}
{{- if and (not .Value) (not .WriteOnly) (not .Reference) (not .CreateQueryPath) (not .QueryParamNoBody)}}
{{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64") (eq .Type "Bool")}}
if data.{{toGoName .TfName}}.IsUnknown() {
if value := res.Get("{{if .ResponseDataPath}}{{.ResponseDataPath}}{{else}}{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}{{end}}"); value.Exists() && !data.{{toGoName .TfName}}.IsNull() {
data.{{toGoName .TfName}} = types.{{.Type}}Value(value.{{if eq .Type "Int64"}}Int{{else if eq .Type "Float64"}}Float{{else}}{{.Type}}{{end}}())
} else {{if .DefaultValue}}if data.{{toGoName .TfName}}.Value{{.Type}}() != {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}} {{end}}{
data.{{toGoName .TfName}} = types.{{.Type}}Null()
}
}
{{- else if isListSet .}}
if value := res.Get("{{if .ResponseDataPath}}{{.ResponseDataPath}}{{else}}{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}{{end}}"); value.Exists() && !data.{{toGoName .TfName}}.IsNull() {
data.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(value.Array())
} else {
data.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type)
}
{{- else if eq .Type "Map"}}
if value := res.Get("{{if .ResponseDataPath}}{{.ResponseDataPath}}{{else}}{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}{{end}}"); value.Exists() && !data.{{toGoName .TfName}}.IsNull() {
data.{{toGoName .TfName}} = helpers.GetStringMap(value.Map())
} else {
data.{{toGoName .TfName}} = types.MapNull(types.StringType)
}
{{- else if isNestedListSet .}}
{{- $list := (toGoName .TfName)}}
for i := range data.{{toGoName .TfName}} {
keys := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if not .Computed}}{{if or .Id $noId}}{{if or (eq .Type "Int64") (eq .Type "Bool") (eq .Type "String")}}"{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}", {{end}}{{end}}{{end}}{{end}} }
keyValues := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if not .Computed}}{{if or .Id $noId}}{{if eq .Type "Int64"}}strconv.FormatInt(data.{{$list}}[i].{{toGoName .TfName}}.ValueInt64(), 10), {{else if eq .Type "Bool"}}strconv.FormatBool(data.{{$list}}[i].{{toGoName .TfName}}.ValueBool()), {{else if eq .Type "String"}}data.{{$list}}[i].{{toGoName .TfName}}.Value{{.Type}}(), {{end}}{{end}}{{end}}{{end}} }

var r gjson.Result
res.{{if .ModelName}}Get("{{if .ResponseDataPath}}{{.ResponseDataPath}}{{else}}{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}{{end}}").{{end}}ForEach(
func(_, v gjson.Result) bool {
found := false
for ik := range keys {
if v.Get(keys[ik]).String() == keyValues[ik] {
found = true
continue
}
found = false
break
}
if found {
r = v
return false
}
return true
},
)

{{- range .Attributes}}
{{- if and (not .Value) (not .WriteOnly) (not .Reference) .Computed}}
{{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64") (eq .Type "Bool")}}
if data.{{$list}}[i].{{toGoName .TfName}}.IsUnknown() {
if value := r.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{toGoName .TfName}} = types.{{.Type}}Value(value.{{if eq .Type "Int64"}}Int{{else if eq .Type "Float64"}}Float{{else}}{{.Type}}{{end}}())
} else {{if .DefaultValue}}if data.{{$list}}[i].{{toGoName .TfName}}.Value{{.Type}}() != {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}} {{end}}{
data.{{$list}}[i].{{toGoName .TfName}} = types.{{.Type}}Null()
}
}
{{- else if isListSet .}}
if value := r.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(value.Array())
} else {
data.{{$list}}[i].{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type)
}
{{- else if eq .Type "Map"}}
if value := r.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{toGoName .TfName}} = helpers.GetStringMap(value.Map())
} else {
data.{{$list}}[i].{{toGoName .TfName}} = types.MapNull(types.StringType)
}
{{- else if isNestedListSet .}}
{{- $clist := (toGoName .TfName)}}
for ci := range data.{{$list}}[i].{{toGoName .TfName}} {
keys := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if or .Id $noId}}{{if or (eq .Type "Int64") (eq .Type "Bool") (eq .Type "String")}}"{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}", {{end}}{{end}}{{end}} }
keyValues := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if or .Id $noId}}{{if eq .Type "Int64"}}strconv.FormatInt(data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}}.ValueInt64(), 10), {{else if eq .Type "Bool"}}strconv.FormatBool(data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}}.ValueBool()), {{else if eq .Type "String"}}data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}}.Value{{.Type}}(), {{end}}{{end}}{{end}} }

var cr gjson.Result
r.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}").ForEach(
func(_, v gjson.Result) bool {
found := false
for ik := range keys {
if v.Get(keys[ik]).String() == keyValues[ik] {
found = true
continue
}
found = false
break
}
if found {
cr = v
return false
}
return true
},
)

{{- range .Attributes}}
{{- if and (not .Value) (not .WriteOnly) (not .Reference)}}
{{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64") (eq .Type "Bool")}}
if value := cr.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}} = types.{{.Type}}Value(value.{{if eq .Type "Int64"}}Int{{else if eq .Type "Float64"}}Float{{else}}{{.Type}}{{end}}())
} else {{if .DefaultValue}}if data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}}.Value{{.Type}}() != {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}} {{end}}{
data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}} = types.{{.Type}}Null()
}
{{- else if isListSet .}}
if value := cr.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(value.Array())
} else {
data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type)
}
{{- else if eq .Type "Map"}}
if value := cr.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}} = helpers.GetStringMap(value.Map())
} else {
data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}} = types.MapNull(types.StringType)
}
{{- else if isNestedListSet .}}
{{- $cclist := (toGoName .TfName)}}
for cci := range data.{{$list}}[i].{{$clist}}[ci].{{toGoName .TfName}} {
keys := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if or .Id $noId}}{{if or (eq .Type "Int64") (eq .Type "Bool") (eq .Type "String")}}"{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}", {{end}}{{end}}{{end}} }
keyValues := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if or .Id $noId}}{{if eq .Type "Int64"}}strconv.FormatInt(data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}}.ValueInt64(), 10), {{else if eq .Type "Bool"}}strconv.FormatBool(data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}}.ValueBool()), {{else if eq .Type "String"}}data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}}.Value{{.Type}}(), {{end}}{{end}}{{end}} }

var ccr gjson.Result
cr.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}").ForEach(
func(_, v gjson.Result) bool {
found := false
for ik := range keys {
if v.Get(keys[ik]).String() == keyValues[ik] {
found = true
continue
}
found = false
break
}
if found {
ccr = v
return false
}
return true
},
)

{{- range .Attributes}}
{{- if and (not .Value) (not .WriteOnly) (not .Reference)}}
{{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64") (eq .Type "Bool")}}
if value := ccr.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}} = types.{{.Type}}Value(value.{{if eq .Type "Int64"}}Int{{else if eq .Type "Float64"}}Float{{else}}{{.Type}}{{end}}())
} else {{if .DefaultValue}}if data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}}.Value{{.Type}}() != {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}} {{end}}{
data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}} = types.{{.Type}}Null()
}
{{- else if isListSet .}}
if value := ccr.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(value.Array())
} else {
data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type)
}
{{- else if eq .Type "Map"}}
if value := ccr.Get("{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}"); value.Exists() && !data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}}.IsNull() {
data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}} = helpers.GetStringMap(value.Map())
} else {
data.{{$list}}[i].{{$clist}}[ci].{{$cclist}}[cci].{{toGoName .TfName}} = types.MapNull(types.StringType)
}
{{- end}}
{{- end}}
{{- end}}
}

{{- end}}
{{- end}}
{{- end}}
}
{{- end}}
{{- end}}
{{- end}}
}
{{- end}}
{{- end}}
{{- end}}
}
// End of section. //template:end fromBodyUnknowns


// Section below is generated&owned by "gen/generator.go". //template:begin isNull
func (data *{{camelCase .Name}}) isNull(ctx context.Context, res gjson.Result) bool {
{{- range .Attributes}}
Expand Down
33 changes: 33 additions & 0 deletions gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,39 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C
plan.Id = types.StringValue(fmt.Sprint(plan.{{toGoName $id.TfName}}.Value{{$id.Type}}()))
{{- end}}

{{- if .UpdateComputed }}
params = ""
{{- $queryParams := generateQueryParamString "GET" "plan" .Attributes }}

{{- if .IdQueryParam}}
params += "?{{.IdQueryParam}}=" + url.QueryEscape(plan.Id.ValueString())
{{- else if and (hasQueryParam .Attributes) (not .GetRequiresId)}}
{{- if $queryParams }}
params += {{$queryParams}}
{{- end}}
{{- else if and (not .GetNoId) (not .GetFromAll)}}
params += "/" + url.QueryEscape(plan.Id.ValueString())
{{- end}}
{{- if .GetExtraQueryParams}}
params += "{{.GetExtraQueryParams}}"
{{- end}}
res, err = r.client.Get({{if .GetRestEndpoint}}"{{.GetRestEndpoint}}"{{else}}plan.getPath(){{end}} + params)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (GET), got error: %s, %s", err, res.String()))
return
}

{{- if .GetFromAll}}
{{- if .IdFromAttribute}}
{{- $id := getId .Attributes}}
res = res.Get("{{.IdFromQueryPath}}.#({{if $id.ResponseModelName}}{{$id.ResponseModelName}}{{else}}{{$id.ModelName}}{{end}}==\"" + state.{{toGoName $id.TfName}}.Value{{$id.Type}}() + "\")")
{{- else}}
res = res.Get("{{.IdFromQueryPath}}.#({{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}==\"" + state.Id.ValueString() + "\")")
{{- end}}
{{- end}}
plan.fromBodyUnknowns(ctx, res)
{{- end}}

tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString()))

diags = resp.State.Set(ctx, &plan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (d *FabricPortAssignmentDataSource) Schema(ctx context.Context, req datasou
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "ID of the port assignment",
Computed: true,
},
"fabric_id": schema.StringAttribute{
MarkdownDescription: "ID of the fabric the device is assigned to",
Computed: true,
Expand Down
Loading
Loading