Skip to content

Commit 9c69bd5

Browse files
add transitPeerNetworkId as id to transit_peer_network resource
1 parent 5545db0 commit 9c69bd5

14 files changed

+44
-81
lines changed

docs/data-sources/transit_peer_network.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ This data source can read the Transit Peer Network.
1414

1515
```terraform
1616
data "catalystcenter_transit_peer_network" "example" {
17-
id = "TRANSIT_1"
17+
transit_peer_network_name = "TRANSIT_1"
1818
}
1919
```
2020

2121
<!-- schema generated by tfplugindocs -->
2222
## Schema
2323

24-
### Optional
24+
### Required
2525

26-
- `id` (String) The id of the object
2726
- `transit_peer_network_name` (String) Transit Peer Network Name
2827

2928
### Read-Only
3029

3130
- `autonomous_system_number` (String) Autonomous System Number
31+
- `id` (String) The id of the object
3232
- `routing_protocol_name` (String) Routing Protocol Name
3333
- `transit_control_plane_settings` (Attributes List) Transit Control Plane Settings info (see [below for nested schema](#nestedatt--transit_control_plane_settings))
3434
- `transit_peer_network_type` (String) Transit Peer Network Type

docs/resources/transit_peer_network.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ resource "catalystcenter_transit_peer_network" "example" {
1818
transit_peer_network_type = "ip_transit"
1919
routing_protocol_name = "BGP"
2020
autonomous_system_number = "65010"
21-
transit_control_plane_settings = [
22-
{
23-
site_name_hierarchy = "Global/Area1"
24-
device_management_ip_address = "10.0.0.1"
25-
}
26-
]
2721
}
2822
```
2923

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
data "catalystcenter_transit_peer_network" "example" {
2-
id = "TRANSIT_1"
2+
transit_peer_network_name = "TRANSIT_1"
33
}

examples/resources/catalystcenter_transit_peer_network/resource.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,4 @@ resource "catalystcenter_transit_peer_network" "example" {
33
transit_peer_network_type = "ip_transit"
44
routing_protocol_name = "BGP"
55
autonomous_system_number = "65010"
6-
transit_control_plane_settings = [
7-
{
8-
site_name_hierarchy = "Global/Area1"
9-
device_management_ip_address = "10.0.0.1"
10-
}
11-
]
126
}

gen/definitions/transit_peer_network.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
---
22
name: Transit Peer Network
33
rest_endpoint: /dna/intent/api/v1/business/sda/transit-peer-network
4-
id_from_attribute: true
5-
id_query_param: transitPeerNetworkName
6-
delete_id_query_param: transitPeerNetworkName
4+
id_from_query_path: .
5+
id_from_query_path_attribute: transitPeerNetworkId
6+
import_no_id: true
7+
data_source_no_id: true
78
no_update: true
89
skip_minimum_test: true
910
doc_category: SDA
1011
attributes:
1112
- model_name: transitPeerNetworkName
1213
type: String
13-
id: true
14-
data_source_query: true
14+
query_param: true
15+
delete_query_param: true
16+
mandatory: true
17+
requires_replace: true
1518
description: Transit Peer Network Name
1619
example: TRANSIT_1
1720
- model_name: transitPeerNetworkType
@@ -37,6 +40,7 @@ attributes:
3740
- model_name: transitControlPlaneSettings
3841
data_path: sdaTransitSettings
3942
write_only: true
43+
exclude_test: true
4044
type: List
4145
description: Transit Control Plane Settings info
4246
attributes:

gen/definitions/wireless_profile.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ doc_category: Wireless
1313
attributes:
1414
- model_name: wirelessProfileName
1515
tf_name: name
16+
delete_query_param_name: name
1617
delete_query_param: true
1718
type: String
1819
match_id: true

gen/templates/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context, state {{camelCase .N
307307
func (data *{{camelCase .Name}}) fromBody(ctx context.Context, res gjson.Result) {
308308
{{- if .DataSourceNoId}}
309309
// Retrieve the 'id' attribute, if Data Source doesn't require id
310-
if value := res.Get("{{if .IdFromQueryPath}}{{.IdFromQueryPath}}.{{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}{{end}}"); value.Exists() {
310+
if value := res.Get("{{if .IdFromQueryPath}}{{if eq .IdFromQueryPath "." }}{{else}}{{.IdFromQueryPath}}.{{end}}{{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}{{end}}"); value.Exists() {
311311
data.Id = types.StringValue(value.String())
312312
} else {
313313
data.Id = types.StringNull()

gen/templates/resource.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C
457457
return
458458
}
459459
{{- if and .IdFromQueryPathAttribute .IdFromQueryPath (not .GetExtraQueryParams) (not .GetFromAll) }}
460-
plan.Id = types.StringValue(res.Get("{{if .IdFromQueryPath}}{{.IdFromQueryPath}}.{{end}}{{.IdFromQueryPathAttribute}}").String())
460+
plan.Id = types.StringValue(res.Get("{{if eq .IdFromQueryPath "." }}{{else}}{{.IdFromQueryPath}}.{{end}}{{.IdFromQueryPathAttribute}}").String())
461461
{{- else}}
462462
plan.Id = types.StringValue(res.Get("{{.IdFromQueryPath}}.#({{if $id.ResponseModelName}}{{$id.ResponseModelName}}{{else}}{{$id.ModelName}}{{end}}==\""+ plan.{{toGoName $id.TfName}}.Value{{$id.Type}}() +"\").{{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}").String())
463463
{{- end}}
@@ -611,9 +611,12 @@ func (r *{{camelCase .Name}}Resource) Delete(ctx context.Context, req resource.D
611611
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}})
612612
{{- else if .DeleteIdQueryParam}}
613613
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "?{{.DeleteIdQueryParam}}=" + url.QueryEscape(state.Id.ValueString()))
614-
{{- else if hasDeleteQueryParam .Attributes}}
615-
{{- $deleteQueryParam := getDeleteQueryParam .Attributes}}
616-
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "?{{$deleteQueryParam.TfName}}=" + url.QueryEscape(state.{{toGoName $deleteQueryParam.TfName}}.Value{{$deleteQueryParam.Type}}()))
614+
{{- else if hasDeleteQueryParam .Attributes }}
615+
{{- $queryParams := generateQueryParamString "DELETE" "state" .Attributes }}
616+
{{- if $queryParams }}
617+
params := {{$queryParams}}
618+
{{- end}}
619+
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + params)
617620
{{- else}}
618621
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "/" + url.QueryEscape(state.Id.ValueString()))
619622
{{- end}}

internal/provider/data_source_catalystcenter_transit_peer_network.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ import (
2323
"fmt"
2424
"net/url"
2525

26-
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
2726
"github.com/hashicorp/terraform-plugin-framework/datasource"
2827
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
29-
"github.com/hashicorp/terraform-plugin-framework/path"
30-
"github.com/hashicorp/terraform-plugin-framework/types"
3128
"github.com/hashicorp/terraform-plugin-log/tflog"
3229
cc "github.com/netascode/go-catalystcenter"
33-
"github.com/tidwall/gjson"
3430
)
3531

3632
// End of section. //template:end imports
@@ -63,13 +59,11 @@ func (d *TransitPeerNetworkDataSource) Schema(ctx context.Context, req datasourc
6359
Attributes: map[string]schema.Attribute{
6460
"id": schema.StringAttribute{
6561
MarkdownDescription: "The id of the object",
66-
Optional: true,
6762
Computed: true,
6863
},
6964
"transit_peer_network_name": schema.StringAttribute{
7065
MarkdownDescription: "Transit Peer Network Name",
71-
Optional: true,
72-
Computed: true,
66+
Required: true,
7367
},
7468
"transit_peer_network_type": schema.StringAttribute{
7569
MarkdownDescription: "Transit Peer Network Type",
@@ -102,14 +96,6 @@ func (d *TransitPeerNetworkDataSource) Schema(ctx context.Context, req datasourc
10296
},
10397
}
10498
}
105-
func (d *TransitPeerNetworkDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
106-
return []datasource.ConfigValidator{
107-
datasourcevalidator.ExactlyOneOf(
108-
path.MatchRoot("id"),
109-
path.MatchRoot("transit_peer_network_name"),
110-
),
111-
}
112-
}
11399

114100
func (d *TransitPeerNetworkDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
115101
if req.ProviderData == nil {
@@ -133,31 +119,9 @@ func (d *TransitPeerNetworkDataSource) Read(ctx context.Context, req datasource.
133119
}
134120

135121
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.Id.String()))
136-
if config.Id.IsNull() && !config.TransitPeerNetworkName.IsNull() {
137-
res, err := d.client.Get(config.getPath())
138-
if err != nil {
139-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve objects, got error: %s", err))
140-
return
141-
}
142-
if value := res; len(value.Array()) > 0 {
143-
value.ForEach(func(k, v gjson.Result) bool {
144-
if config.TransitPeerNetworkName.ValueString() == v.Get("transitPeerNetworkName").String() {
145-
config.Id = types.StringValue(v.Get("id").String())
146-
tflog.Debug(ctx, fmt.Sprintf("%s: Found object with transitPeerNetworkName '%v', id: %v", config.Id.String(), config.TransitPeerNetworkName.ValueString(), config.Id.String()))
147-
return false
148-
}
149-
return true
150-
})
151-
}
152-
153-
if config.Id.IsNull() {
154-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to find object with transitPeerNetworkName: %s", config.TransitPeerNetworkName.ValueString()))
155-
return
156-
}
157-
}
158122

159123
params := ""
160-
params += "?transitPeerNetworkName=" + url.QueryEscape(config.Id.ValueString())
124+
params += "?transitPeerNetworkName=" + url.QueryEscape(config.TransitPeerNetworkName.ValueString())
161125
res, err := d.client.Get(config.getPath() + params)
162126
if err != nil {
163127
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err))

internal/provider/data_source_catalystcenter_transit_peer_network_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,12 @@ func testAccDataSourceCcTransitPeerNetworkConfig() string {
5757
config += ` transit_peer_network_type = "ip_transit"` + "\n"
5858
config += ` routing_protocol_name = "BGP"` + "\n"
5959
config += ` autonomous_system_number = "65010"` + "\n"
60-
config += ` transit_control_plane_settings = [{` + "\n"
61-
config += ` site_name_hierarchy = "Global/Area1"` + "\n"
62-
config += ` device_management_ip_address = "10.0.0.1"` + "\n"
63-
config += ` }]` + "\n"
6460
config += `}` + "\n"
6561

6662
config += `
6763
data "catalystcenter_transit_peer_network" "test" {
6864
id = catalystcenter_transit_peer_network.test.id
65+
transit_peer_network_name = "TRANSIT_1"
6966
}
7067
`
7168
return config

0 commit comments

Comments
 (0)