Skip to content

modify transit_peer_network resource to add id #100

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

Merged
merged 9 commits into from
Aug 2, 2024
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.1.10 (unreleased)

- Add `transitPeerNetworkId` as `id` to `transit_peer_network` resource
- Add `anycast_gateway` resource and data source, this resource now only works with Catalyst Center version 2.3.7.5+ `/sda/anycastGateways`
- BREAKING CHANGE: Modified `fabric_site` resource to use `/dna/intent/api/v1/sda/fabricSites` API endpoint, this resource now only works with Catalyst Center version 2.3.7.5+
- Fix issue with mandatory attributes in `transit_peer_network` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/92)
Expand Down
6 changes: 3 additions & 3 deletions docs/data-sources/transit_peer_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ This data source can read the Transit Peer Network.

```terraform
data "catalystcenter_transit_peer_network" "example" {
id = "TRANSIT_1"
transit_peer_network_name = "TRANSIT_1"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional
### Required

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

### Read-Only

- `autonomous_system_number` (String) Autonomous System Number
- `id` (String) The id of the object
- `routing_protocol_name` (String) Routing Protocol Name
- `transit_control_plane_settings` (Attributes List) Transit Control Plane Settings info (see [below for nested schema](#nestedatt--transit_control_plane_settings))
- `transit_peer_network_type` (String) Transit Peer Network Type
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.10 (unreleased)

- Add `transitPeerNetworkId` as `id` to `transit_peer_network` resource
- Add `anycast_gateway` resource and data source, this resource now only works with Catalyst Center version 2.3.7.5+ `/sda/anycastGateways`
- BREAKING CHANGE: Modified `fabric_site` resource to use `/dna/intent/api/v1/sda/fabricSites` API endpoint, this resource now only works with Catalyst Center version 2.3.7.5+
- Fix issue with mandatory attributes in `transit_peer_network` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/92)
Expand Down
6 changes: 0 additions & 6 deletions docs/resources/transit_peer_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ resource "catalystcenter_transit_peer_network" "example" {
transit_peer_network_type = "ip_transit"
routing_protocol_name = "BGP"
autonomous_system_number = "65010"
transit_control_plane_settings = [
{
site_name_hierarchy = "Global/Area1"
device_management_ip_address = "10.0.0.1"
}
]
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
data "catalystcenter_transit_peer_network" "example" {
id = "TRANSIT_1"
transit_peer_network_name = "TRANSIT_1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@ resource "catalystcenter_transit_peer_network" "example" {
transit_peer_network_type = "ip_transit"
routing_protocol_name = "BGP"
autonomous_system_number = "65010"
transit_control_plane_settings = [
{
site_name_hierarchy = "Global/Area1"
device_management_ip_address = "10.0.0.1"
}
]
}
14 changes: 9 additions & 5 deletions gen/definitions/transit_peer_network.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
---
name: Transit Peer Network
rest_endpoint: /dna/intent/api/v1/business/sda/transit-peer-network
id_from_attribute: true
id_query_param: transitPeerNetworkName
delete_id_query_param: transitPeerNetworkName
id_from_query_path: .
id_from_query_path_attribute: transitPeerNetworkId
import_no_id: true
data_source_no_id: true
no_update: true
skip_minimum_test: true
doc_category: SDA
attributes:
- model_name: transitPeerNetworkName
type: String
id: true
data_source_query: true
query_param: true
delete_query_param: true
mandatory: true
requires_replace: true
description: Transit Peer Network Name
example: TRANSIT_1
- model_name: transitPeerNetworkType
Expand All @@ -37,6 +40,7 @@ attributes:
- model_name: transitControlPlaneSettings
data_path: sdaTransitSettings
write_only: true
exclude_test: true
type: List
description: Transit Control Plane Settings info
attributes:
Expand Down
1 change: 1 addition & 0 deletions gen/definitions/wireless_profile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ doc_category: Wireless
attributes:
- model_name: wirelessProfileName
tf_name: name
delete_query_param_name: name
delete_query_param: true
type: String
match_id: true
Expand Down
2 changes: 1 addition & 1 deletion gen/templates/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context, state {{camelCase .N
func (data *{{camelCase .Name}}) fromBody(ctx context.Context, res gjson.Result) {
{{- if .DataSourceNoId}}
// Retrieve the 'id' attribute, if Data Source doesn't require id
if value := res.Get("{{if .IdFromQueryPath}}{{.IdFromQueryPath}}.{{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}{{end}}"); value.Exists() {
if value := res.Get("{{if .IdFromQueryPath}}{{if eq .IdFromQueryPath "." }}{{else}}{{.IdFromQueryPath}}.{{end}}{{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}{{end}}"); value.Exists() {
data.Id = types.StringValue(value.String())
} else {
data.Id = types.StringNull()
Expand Down
11 changes: 7 additions & 4 deletions gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C
return
}
{{- if and .IdFromQueryPathAttribute .IdFromQueryPath (not .GetExtraQueryParams) (not .GetFromAll) }}
plan.Id = types.StringValue(res.Get("{{if .IdFromQueryPath}}{{.IdFromQueryPath}}.{{end}}{{.IdFromQueryPathAttribute}}").String())
plan.Id = types.StringValue(res.Get("{{if eq .IdFromQueryPath "." }}{{else}}{{.IdFromQueryPath}}.{{end}}{{.IdFromQueryPathAttribute}}").String())
{{- else}}
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())
{{- end}}
Expand Down Expand Up @@ -611,9 +611,12 @@ func (r *{{camelCase .Name}}Resource) Delete(ctx context.Context, req resource.D
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}})
{{- else if .DeleteIdQueryParam}}
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "?{{.DeleteIdQueryParam}}=" + url.QueryEscape(state.Id.ValueString()))
{{- else if hasDeleteQueryParam .Attributes}}
{{- $deleteQueryParam := getDeleteQueryParam .Attributes}}
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "?{{$deleteQueryParam.TfName}}=" + url.QueryEscape(state.{{toGoName $deleteQueryParam.TfName}}.Value{{$deleteQueryParam.Type}}()))
{{- else if hasDeleteQueryParam .Attributes }}
{{- $queryParams := generateQueryParamString "DELETE" "state" .Attributes }}
{{- if $queryParams }}
params := {{$queryParams}}
{{- end}}
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + params)
{{- else}}
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "/" + url.QueryEscape(state.Id.ValueString()))
{{- end}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ import (
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
cc "github.com/netascode/go-catalystcenter"
"github.com/tidwall/gjson"
)

// End of section. //template:end imports
Expand Down Expand Up @@ -63,13 +59,11 @@ func (d *TransitPeerNetworkDataSource) Schema(ctx context.Context, req datasourc
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The id of the object",
Optional: true,
Computed: true,
},
"transit_peer_network_name": schema.StringAttribute{
MarkdownDescription: "Transit Peer Network Name",
Optional: true,
Computed: true,
Required: true,
},
"transit_peer_network_type": schema.StringAttribute{
MarkdownDescription: "Transit Peer Network Type",
Expand Down Expand Up @@ -102,14 +96,6 @@ func (d *TransitPeerNetworkDataSource) Schema(ctx context.Context, req datasourc
},
}
}
func (d *TransitPeerNetworkDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{
datasourcevalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("transit_peer_network_name"),
),
}
}

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

tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.Id.String()))
if config.Id.IsNull() && !config.TransitPeerNetworkName.IsNull() {
res, err := d.client.Get(config.getPath())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve objects, got error: %s", err))
return
}
if value := res; len(value.Array()) > 0 {
value.ForEach(func(k, v gjson.Result) bool {
if config.TransitPeerNetworkName.ValueString() == v.Get("transitPeerNetworkName").String() {
config.Id = types.StringValue(v.Get("id").String())
tflog.Debug(ctx, fmt.Sprintf("%s: Found object with transitPeerNetworkName '%v', id: %v", config.Id.String(), config.TransitPeerNetworkName.ValueString(), config.Id.String()))
return false
}
return true
})
}

if config.Id.IsNull() {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to find object with transitPeerNetworkName: %s", config.TransitPeerNetworkName.ValueString()))
return
}
}

params := ""
params += "?transitPeerNetworkName=" + url.QueryEscape(config.Id.ValueString())
params += "?transitPeerNetworkName=" + url.QueryEscape(config.TransitPeerNetworkName.ValueString())
res, err := d.client.Get(config.getPath() + params)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,12 @@ func testAccDataSourceCcTransitPeerNetworkConfig() string {
config += ` transit_peer_network_type = "ip_transit"` + "\n"
config += ` routing_protocol_name = "BGP"` + "\n"
config += ` autonomous_system_number = "65010"` + "\n"
config += ` transit_control_plane_settings = [{` + "\n"
config += ` site_name_hierarchy = "Global/Area1"` + "\n"
config += ` device_management_ip_address = "10.0.0.1"` + "\n"
config += ` }]` + "\n"
config += `}` + "\n"

config += `
data "catalystcenter_transit_peer_network" "test" {
id = catalystcenter_transit_peer_network.test.id
transit_peer_network_name = "TRANSIT_1"
}
`
return config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func (data TransitPeerNetwork) toBody(ctx context.Context, state TransitPeerNetw

// Section below is generated&owned by "gen/generator.go". //template:begin fromBody
func (data *TransitPeerNetwork) fromBody(ctx context.Context, res gjson.Result) {
// Retrieve the 'id' attribute, if Data Source doesn't require id
if value := res.Get("transitPeerNetworkId"); value.Exists() {
data.Id = types.StringValue(value.String())
} else {
data.Id = types.StringNull()
}
if value := res.Get("transitPeerNetworkName"); value.Exists() {
data.TransitPeerNetworkName = types.StringValue(value.String())
} else {
Expand Down
15 changes: 11 additions & 4 deletions internal/provider/resource_catalystcenter_transit_peer_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ func (r *TransitPeerNetworkResource) Create(ctx context.Context, req resource.Cr
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (POST), got error: %s, %s", err, res.String()))
return
}
plan.Id = types.StringValue(fmt.Sprint(plan.TransitPeerNetworkName.ValueString()))
params = ""
params += "?transitPeerNetworkName=" + url.QueryEscape(plan.TransitPeerNetworkName.ValueString())
res, err = r.client.Get(plan.getPath() + params)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (GET), got error: %s, %s", err, res.String()))
return
}
plan.Id = types.StringValue(res.Get("transitPeerNetworkId").String())

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

Expand All @@ -186,7 +193,7 @@ func (r *TransitPeerNetworkResource) Read(ctx context.Context, req resource.Read
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", state.Id.String()))

params := ""
params += "?transitPeerNetworkName=" + url.QueryEscape(state.Id.ValueString())
params += "?transitPeerNetworkName=" + url.QueryEscape(state.TransitPeerNetworkName.ValueString())
res, err := r.client.Get(state.getPath() + params)
if err != nil && strings.Contains(err.Error(), "StatusCode 404") {
resp.State.RemoveResource(ctx)
Expand Down Expand Up @@ -250,7 +257,8 @@ func (r *TransitPeerNetworkResource) Delete(ctx context.Context, req resource.De
}

tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Delete(state.getPath() + "?transitPeerNetworkName=" + url.QueryEscape(state.Id.ValueString()))
params := "?transitPeerNetworkName=" + url.QueryEscape(state.TransitPeerNetworkName.ValueString())
res, err := r.client.Delete(state.getPath() + params)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
Expand All @@ -275,7 +283,6 @@ func (r *TransitPeerNetworkResource) ImportState(ctx context.Context, req resour
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("transit_peer_network_name"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[0])...)
}

// End of section. //template:end import
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ func TestAccCcTransitPeerNetwork(t *testing.T) {
Config: testAccCcTransitPeerNetworkConfig_all(),
Check: resource.ComposeTestCheckFunc(checks...),
})
steps = append(steps, resource.TestStep{
ResourceName: "catalystcenter_transit_peer_network.test",
ImportState: true,
})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -74,10 +70,6 @@ func testAccCcTransitPeerNetworkConfig_all() string {
config += ` transit_peer_network_type = "ip_transit"` + "\n"
config += ` routing_protocol_name = "BGP"` + "\n"
config += ` autonomous_system_number = "65010"` + "\n"
config += ` transit_control_plane_settings = [{` + "\n"
config += ` site_name_hierarchy = "Global/Area1"` + "\n"
config += ` device_management_ip_address = "10.0.0.1"` + "\n"
config += ` }]` + "\n"
config += `}` + "\n"
return config
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ func (r *WirelessProfileResource) Delete(ctx context.Context, req resource.Delet
}

tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Delete(state.getPath() + "?name=" + url.QueryEscape(state.Name.ValueString()))
params := "?name=" + url.QueryEscape(state.Name.ValueString())
res, err := r.client.Delete(state.getPath() + params)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
Expand Down
1 change: 1 addition & 0 deletions templates/guides/changelog.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description: |-

## 0.1.10 (unreleased)

- Add `transitPeerNetworkId` as `id` to `transit_peer_network` resource
- Add `anycast_gateway` resource and data source, this resource now only works with Catalyst Center version 2.3.7.5+ `/sda/anycastGateways`
- BREAKING CHANGE: Modified `fabric_site` resource to use `/dna/intent/api/v1/sda/fabricSites` API endpoint, this resource now only works with Catalyst Center version 2.3.7.5+
- Fix issue with mandatory attributes in `transit_peer_network` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/92)
Expand Down