diff --git a/.gitignore b/.gitignore index fdc3a695..35e4bafb 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ website/vendor *.winfile eol=crlf gen/full_definitions +test-epic-*.tf diff --git a/docs/data-sources/bgp_address_family_vpnv4.md b/docs/data-sources/bgp_address_family_vpnv4.md new file mode 100644 index 00000000..b7b0b98e --- /dev/null +++ b/docs/data-sources/bgp_address_family_vpnv4.md @@ -0,0 +1,36 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "iosxe_bgp_address_family_vpnv4 Data Source - terraform-provider-iosxe" +subcategory: "BGP" +description: |- + This data source can read the BGP Address Family VPNv4 configuration. +--- + +# iosxe_bgp_address_family_vpnv4 (Data Source) + +This data source can read the BGP Address Family VPNv4 configuration. + +## Example Usage + +```terraform +data "iosxe_bgp_address_family_vpnv4" "example" { + asn = "65000" + af_name = "unicast" +} +``` + + +## Schema + +### Required + +- `af_name` (String) +- `asn` (String) + +### Optional + +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The path of the retrieved object. diff --git a/docs/data-sources/bgp_address_family_vpnv6.md b/docs/data-sources/bgp_address_family_vpnv6.md new file mode 100644 index 00000000..ac11c94a --- /dev/null +++ b/docs/data-sources/bgp_address_family_vpnv6.md @@ -0,0 +1,36 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "iosxe_bgp_address_family_vpnv6 Data Source - terraform-provider-iosxe" +subcategory: "BGP" +description: |- + This data source can read the BGP Address Family VPNv6 configuration. +--- + +# iosxe_bgp_address_family_vpnv6 (Data Source) + +This data source can read the BGP Address Family VPNv6 configuration. + +## Example Usage + +```terraform +data "iosxe_bgp_address_family_vpnv6" "example" { + asn = "65000" + af_name = "unicast" +} +``` + + +## Schema + +### Required + +- `af_name` (String) +- `asn` (String) + +### Optional + +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The path of the retrieved object. diff --git a/docs/resources/bgp_address_family_vpnv4.md b/docs/resources/bgp_address_family_vpnv4.md new file mode 100644 index 00000000..62ade34e --- /dev/null +++ b/docs/resources/bgp_address_family_vpnv4.md @@ -0,0 +1,48 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "iosxe_bgp_address_family_vpnv4 Resource - terraform-provider-iosxe" +subcategory: "BGP" +description: |- + This resource can manage the BGP Address Family VPNv4 configuration. +--- + +# iosxe_bgp_address_family_vpnv4 (Resource) + +This resource can manage the BGP Address Family VPNv4 configuration. + +## Example Usage + +```terraform +resource "iosxe_bgp_address_family_vpnv4" "example" { + asn = "65000" + af_name = "unicast" +} +``` + + +## Schema + +### Required + +- `af_name` (String) - Choices: `flowspec`, `multicast`, `unicast` +- `asn` (String) + +### Optional + +- `delete_mode` (String) Configure behavior when deleting/destroying the resource. Either delete the entire object (YANG container) being managed, or only delete the individual resource attributes configured explicitly and leave everything else as-is. Default value is `all`. + - Choices: `all`, `attributes` +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The path of the object. + +## Import + +Import is supported using the following syntax: + +The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example: + +```shell +terraform import iosxe_bgp_address_family_vpnv4.example "," +``` diff --git a/docs/resources/bgp_address_family_vpnv6.md b/docs/resources/bgp_address_family_vpnv6.md new file mode 100644 index 00000000..c6267346 --- /dev/null +++ b/docs/resources/bgp_address_family_vpnv6.md @@ -0,0 +1,48 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "iosxe_bgp_address_family_vpnv6 Resource - terraform-provider-iosxe" +subcategory: "BGP" +description: |- + This resource can manage the BGP Address Family VPNv6 configuration. +--- + +# iosxe_bgp_address_family_vpnv6 (Resource) + +This resource can manage the BGP Address Family VPNv6 configuration. + +## Example Usage + +```terraform +resource "iosxe_bgp_address_family_vpnv6" "example" { + asn = "65000" + af_name = "unicast" +} +``` + + +## Schema + +### Required + +- `af_name` (String) - Choices: `flowspec`, `multicast`, `unicast` +- `asn` (String) + +### Optional + +- `delete_mode` (String) Configure behavior when deleting/destroying the resource. Either delete the entire object (YANG container) being managed, or only delete the individual resource attributes configured explicitly and leave everything else as-is. Default value is `all`. + - Choices: `all`, `attributes` +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The path of the object. + +## Import + +Import is supported using the following syntax: + +The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example: + +```shell +terraform import iosxe_bgp_address_family_vpnv6.example "," +``` diff --git a/examples/data-sources/iosxe_bgp_address_family_vpnv4/data-source.tf b/examples/data-sources/iosxe_bgp_address_family_vpnv4/data-source.tf new file mode 100644 index 00000000..06d991ad --- /dev/null +++ b/examples/data-sources/iosxe_bgp_address_family_vpnv4/data-source.tf @@ -0,0 +1,4 @@ +data "iosxe_bgp_address_family_vpnv4" "example" { + asn = "65000" + af_name = "unicast" +} diff --git a/examples/data-sources/iosxe_bgp_address_family_vpnv6/data-source.tf b/examples/data-sources/iosxe_bgp_address_family_vpnv6/data-source.tf new file mode 100644 index 00000000..cd3aac3b --- /dev/null +++ b/examples/data-sources/iosxe_bgp_address_family_vpnv6/data-source.tf @@ -0,0 +1,4 @@ +data "iosxe_bgp_address_family_vpnv6" "example" { + asn = "65000" + af_name = "unicast" +} diff --git a/examples/resources/iosxe_bgp_address_family_vpnv4/import.sh b/examples/resources/iosxe_bgp_address_family_vpnv4/import.sh new file mode 100644 index 00000000..635e8a2a --- /dev/null +++ b/examples/resources/iosxe_bgp_address_family_vpnv4/import.sh @@ -0,0 +1 @@ +terraform import iosxe_bgp_address_family_vpnv4.example "," diff --git a/examples/resources/iosxe_bgp_address_family_vpnv4/resource.tf b/examples/resources/iosxe_bgp_address_family_vpnv4/resource.tf new file mode 100644 index 00000000..fbd6ff31 --- /dev/null +++ b/examples/resources/iosxe_bgp_address_family_vpnv4/resource.tf @@ -0,0 +1,4 @@ +resource "iosxe_bgp_address_family_vpnv4" "example" { + asn = "65000" + af_name = "unicast" +} diff --git a/examples/resources/iosxe_bgp_address_family_vpnv6/import.sh b/examples/resources/iosxe_bgp_address_family_vpnv6/import.sh new file mode 100644 index 00000000..8687a9fa --- /dev/null +++ b/examples/resources/iosxe_bgp_address_family_vpnv6/import.sh @@ -0,0 +1 @@ +terraform import iosxe_bgp_address_family_vpnv6.example "," diff --git a/examples/resources/iosxe_bgp_address_family_vpnv6/resource.tf b/examples/resources/iosxe_bgp_address_family_vpnv6/resource.tf new file mode 100644 index 00000000..e21137d7 --- /dev/null +++ b/examples/resources/iosxe_bgp_address_family_vpnv6/resource.tf @@ -0,0 +1,4 @@ +resource "iosxe_bgp_address_family_vpnv6" "example" { + asn = "65000" + af_name = "unicast" +} diff --git a/gen/definitions/bgp_address_family_vpnv4.yaml b/gen/definitions/bgp_address_family_vpnv4.yaml new file mode 100644 index 00000000..6e9dd8c7 --- /dev/null +++ b/gen/definitions/bgp_address_family_vpnv4.yaml @@ -0,0 +1,16 @@ +--- +name: BGP Address Family VPNv4 +path: /Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=%v]/address-family/no-vrf/vpnv4[af-name=%s] +doc_category: BGP +attributes: + - yang_name: id + tf_name: asn + example: 65000 + - yang_name: af-name + example: unicast +test_prerequisites: + - path: /Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=65000] + attributes: + - name: id + value: 65000 + diff --git a/gen/definitions/bgp_address_family_vpnv6.yaml b/gen/definitions/bgp_address_family_vpnv6.yaml new file mode 100644 index 00000000..90cf7da7 --- /dev/null +++ b/gen/definitions/bgp_address_family_vpnv6.yaml @@ -0,0 +1,16 @@ +--- +name: BGP Address Family VPNv6 +path: /Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=%v]/address-family/no-vrf/vpnv6[af-name=%s] +doc_category: BGP +attributes: + - yang_name: id + tf_name: asn + example: 65000 + - yang_name: af-name + example: unicast +test_prerequisites: + - path: /Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=65000] + attributes: + - name: id + value: 65000 + diff --git a/internal/provider/data_source_iosxe_bgp_address_family_vpnv4.go b/internal/provider/data_source_iosxe_bgp_address_family_vpnv4.go new file mode 100644 index 00000000..f79a7f88 --- /dev/null +++ b/internal/provider/data_source_iosxe_bgp_address_family_vpnv4.go @@ -0,0 +1,151 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "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" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &BGPAddressFamilyVPNv4DataSource{} + _ datasource.DataSourceWithConfigure = &BGPAddressFamilyVPNv4DataSource{} +) + +func NewBGPAddressFamilyVPNv4DataSource() datasource.DataSource { + return &BGPAddressFamilyVPNv4DataSource{} +} + +type BGPAddressFamilyVPNv4DataSource struct { + data *IosxeProviderData +} + +func (d *BGPAddressFamilyVPNv4DataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_bgp_address_family_vpnv4" +} + +func (d *BGPAddressFamilyVPNv4DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: "This data source can read the BGP Address Family VPNv4 configuration.", + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The path of the retrieved object.", + Computed: true, + }, + "asn": schema.StringAttribute{ + MarkdownDescription: "", + Required: true, + }, + "af_name": schema.StringAttribute{ + MarkdownDescription: "", + Required: true, + }, + }, + } +} + +func (d *BGPAddressFamilyVPNv4DataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.data = req.ProviderData.(*IosxeProviderData) +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin read + +func (d *BGPAddressFamilyVPNv4DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config BGPAddressFamilyVPNv4Data + + // Read config + diags := req.Config.Get(ctx, &config) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.getPath())) + + device, ok := d.data.Devices[config.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", config.Device.ValueString())) + return + } + + if device.Protocol == "restconf" { + res, err := device.RestconfClient.GetData(config.getPath()) + if res.StatusCode == 404 { + config = BGPAddressFamilyVPNv4Data{Device: config.Device} + } else { + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", config.getPath(), err)) + return + } + + config.fromBody(ctx, res.Res) + } + } else { + // Serialize NETCONF operations when reuse disabled (concurrent reads allowed when reuse enabled) + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, false) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + filter := helpers.GetXpathFilter(config.getXPath()) + res, err := device.NetconfClient.GetConfig(ctx, "running", filter) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", config.getPath(), err)) + return + } + + config.fromBodyXML(ctx, res.Res) + } + + config.Id = types.StringValue(config.getPath()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.getPath())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read diff --git a/internal/provider/data_source_iosxe_bgp_address_family_vpnv4_test.go b/internal/provider/data_source_iosxe_bgp_address_family_vpnv4_test.go new file mode 100644 index 00000000..59f226a4 --- /dev/null +++ b/internal/provider/data_source_iosxe_bgp_address_family_vpnv4_test.go @@ -0,0 +1,82 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSource + +func TestAccDataSourceIosxeBGPAddressFamilyVPNv4(t *testing.T) { + var checks []resource.TestCheckFunc + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceIosxeBGPAddressFamilyVPNv4PrerequisitesConfig + testAccDataSourceIosxeBGPAddressFamilyVPNv4Config(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccDataSourceIosxeBGPAddressFamilyVPNv4PrerequisitesConfig = ` +resource "iosxe_yang" "PreReq0" { + path = "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=65000]" + attributes = { + "id" = "65000" + } +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig + +func testAccDataSourceIosxeBGPAddressFamilyVPNv4Config() string { + config := `resource "iosxe_bgp_address_family_vpnv4" "test" {` + "\n" + config += ` delete_mode = "attributes"` + "\n" + config += ` asn = "65000"` + "\n" + config += ` af_name = "unicast"` + "\n" + config += ` depends_on = [iosxe_yang.PreReq0, ]` + "\n" + config += `}` + "\n" + + config += ` + data "iosxe_bgp_address_family_vpnv4" "test" { + asn = "65000" + af_name = "unicast" + depends_on = [iosxe_bgp_address_family_vpnv4.test] + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/data_source_iosxe_bgp_address_family_vpnv6.go b/internal/provider/data_source_iosxe_bgp_address_family_vpnv6.go new file mode 100644 index 00000000..ef71ed65 --- /dev/null +++ b/internal/provider/data_source_iosxe_bgp_address_family_vpnv6.go @@ -0,0 +1,151 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "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" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &BGPAddressFamilyVPNv6DataSource{} + _ datasource.DataSourceWithConfigure = &BGPAddressFamilyVPNv6DataSource{} +) + +func NewBGPAddressFamilyVPNv6DataSource() datasource.DataSource { + return &BGPAddressFamilyVPNv6DataSource{} +} + +type BGPAddressFamilyVPNv6DataSource struct { + data *IosxeProviderData +} + +func (d *BGPAddressFamilyVPNv6DataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_bgp_address_family_vpnv6" +} + +func (d *BGPAddressFamilyVPNv6DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: "This data source can read the BGP Address Family VPNv6 configuration.", + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The path of the retrieved object.", + Computed: true, + }, + "asn": schema.StringAttribute{ + MarkdownDescription: "", + Required: true, + }, + "af_name": schema.StringAttribute{ + MarkdownDescription: "", + Required: true, + }, + }, + } +} + +func (d *BGPAddressFamilyVPNv6DataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.data = req.ProviderData.(*IosxeProviderData) +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin read + +func (d *BGPAddressFamilyVPNv6DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config BGPAddressFamilyVPNv6Data + + // Read config + diags := req.Config.Get(ctx, &config) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.getPath())) + + device, ok := d.data.Devices[config.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", config.Device.ValueString())) + return + } + + if device.Protocol == "restconf" { + res, err := device.RestconfClient.GetData(config.getPath()) + if res.StatusCode == 404 { + config = BGPAddressFamilyVPNv6Data{Device: config.Device} + } else { + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", config.getPath(), err)) + return + } + + config.fromBody(ctx, res.Res) + } + } else { + // Serialize NETCONF operations when reuse disabled (concurrent reads allowed when reuse enabled) + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, false) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + filter := helpers.GetXpathFilter(config.getXPath()) + res, err := device.NetconfClient.GetConfig(ctx, "running", filter) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", config.getPath(), err)) + return + } + + config.fromBodyXML(ctx, res.Res) + } + + config.Id = types.StringValue(config.getPath()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.getPath())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read diff --git a/internal/provider/data_source_iosxe_bgp_address_family_vpnv6_test.go b/internal/provider/data_source_iosxe_bgp_address_family_vpnv6_test.go new file mode 100644 index 00000000..c1be7402 --- /dev/null +++ b/internal/provider/data_source_iosxe_bgp_address_family_vpnv6_test.go @@ -0,0 +1,82 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSource + +func TestAccDataSourceIosxeBGPAddressFamilyVPNv6(t *testing.T) { + var checks []resource.TestCheckFunc + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceIosxeBGPAddressFamilyVPNv6PrerequisitesConfig + testAccDataSourceIosxeBGPAddressFamilyVPNv6Config(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccDataSourceIosxeBGPAddressFamilyVPNv6PrerequisitesConfig = ` +resource "iosxe_yang" "PreReq0" { + path = "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=65000]" + attributes = { + "id" = "65000" + } +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig + +func testAccDataSourceIosxeBGPAddressFamilyVPNv6Config() string { + config := `resource "iosxe_bgp_address_family_vpnv6" "test" {` + "\n" + config += ` delete_mode = "attributes"` + "\n" + config += ` asn = "65000"` + "\n" + config += ` af_name = "unicast"` + "\n" + config += ` depends_on = [iosxe_yang.PreReq0, ]` + "\n" + config += `}` + "\n" + + config += ` + data "iosxe_bgp_address_family_vpnv6" "test" { + asn = "65000" + af_name = "unicast" + depends_on = [iosxe_bgp_address_family_vpnv6.test] + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/model_iosxe_bgp_address_family_vpnv4.go b/internal/provider/model_iosxe_bgp_address_family_vpnv4.go new file mode 100644 index 00000000..172738ac --- /dev/null +++ b/internal/provider/model_iosxe_bgp_address_family_vpnv4.go @@ -0,0 +1,236 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "net/url" + "regexp" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-netconf" + "github.com/netascode/xmldot" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin types +type BGPAddressFamilyVPNv4 struct { + Device types.String `tfsdk:"device"` + Id types.String `tfsdk:"id"` + DeleteMode types.String `tfsdk:"delete_mode"` + Asn types.String `tfsdk:"asn"` + AfName types.String `tfsdk:"af_name"` +} + +type BGPAddressFamilyVPNv4Data struct { + Device types.String `tfsdk:"device"` + Id types.String `tfsdk:"id"` + Asn types.String `tfsdk:"asn"` + AfName types.String `tfsdk:"af_name"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath + +func (data BGPAddressFamilyVPNv4) getPath() string { + return fmt.Sprintf("Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp=%v/address-family/no-vrf/vpnv4=%s", url.QueryEscape(fmt.Sprintf("%v", data.Asn.ValueString())), url.QueryEscape(fmt.Sprintf("%v", data.AfName.ValueString()))) +} + +func (data BGPAddressFamilyVPNv4Data) getPath() string { + return fmt.Sprintf("Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp=%v/address-family/no-vrf/vpnv4=%s", url.QueryEscape(fmt.Sprintf("%v", data.Asn.ValueString())), url.QueryEscape(fmt.Sprintf("%v", data.AfName.ValueString()))) +} + +// if last path element has a key -> remove it +func (data BGPAddressFamilyVPNv4) getPathShort() string { + path := data.getPath() + re := regexp.MustCompile(`(.*)=[^\/]*$`) + matches := re.FindStringSubmatch(path) + if len(matches) <= 1 { + return path + } + return matches[1] +} + +// getXPath returns the XPath for NETCONF operations +func (data BGPAddressFamilyVPNv4) getXPath() string { + path := "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=%v]/address-family/no-vrf/vpnv4[af-name=%s]" + path = fmt.Sprintf(path, fmt.Sprintf("%v", data.Asn.ValueString()), fmt.Sprintf("%v", data.AfName.ValueString())) + return path +} + +func (data BGPAddressFamilyVPNv4Data) getXPath() string { + path := "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=%v]/address-family/no-vrf/vpnv4[af-name=%s]" + path = fmt.Sprintf(path, fmt.Sprintf("%v", data.Asn.ValueString()), fmt.Sprintf("%v", data.AfName.ValueString())) + return path +} + +// End of section. //template:end getPath + +// Section below is generated&owned by "gen/generator.go". //template:begin toBody + +func (data BGPAddressFamilyVPNv4) toBody(ctx context.Context) string { + body := `{"` + helpers.LastElement(data.getPath()) + `":{}}` + if !data.AfName.IsNull() && !data.AfName.IsUnknown() { + body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"af-name", data.AfName.ValueString()) + } + return body +} + +// End of section. //template:end toBody + +// Section below is generated&owned by "gen/generator.go". //template:begin toBodyXML + +func (data BGPAddressFamilyVPNv4) toBodyXML(ctx context.Context) string { + body := netconf.Body{} + if !data.AfName.IsNull() && !data.AfName.IsUnknown() { + body = helpers.SetFromXPath(body, data.getXPath()+"/af-name", data.AfName.ValueString()) + } + bodyString, err := body.String() + if err != nil { + tflog.Error(ctx, fmt.Sprintf("Error converting body to string: %s", err)) + } + return bodyString +} + +// End of section. //template:end toBodyXML + +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBody + +func (data *BGPAddressFamilyVPNv4) updateFromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } + if value := res.Get(prefix + "af-name"); value.Exists() && !data.AfName.IsNull() { + data.AfName = types.StringValue(value.String()) + } else { + data.AfName = types.StringNull() + } +} + +// End of section. //template:end updateFromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBodyXML + +func (data *BGPAddressFamilyVPNv4) updateFromBodyXML(ctx context.Context, res xmldot.Result) { + if value := helpers.GetFromXPath(res, "data"+data.getXPath()+"/af-name"); value.Exists() && !data.AfName.IsNull() { + data.AfName = types.StringValue(value.String()) + } else { + data.AfName = types.StringNull() + } +} + +// End of section. //template:end updateFromBodyXML + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBody + +func (data *BGPAddressFamilyVPNv4) fromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } +} + +// End of section. //template:end fromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyData + +func (data *BGPAddressFamilyVPNv4Data) fromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } +} + +// End of section. //template:end fromBodyData + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyXML + +func (data *BGPAddressFamilyVPNv4) fromBodyXML(ctx context.Context, res xmldot.Result) { +} + +// End of section. //template:end fromBodyXML + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyDataXML + +func (data *BGPAddressFamilyVPNv4Data) fromBodyXML(ctx context.Context, res xmldot.Result) { +} + +// End of section. //template:end fromBodyDataXML + +// Section below is generated&owned by "gen/generator.go". //template:begin getDeletedItems + +func (data *BGPAddressFamilyVPNv4) getDeletedItems(ctx context.Context, state BGPAddressFamilyVPNv4) []string { + deletedItems := make([]string, 0) + + return deletedItems +} + +// End of section. //template:end getDeletedItems + +// Section below is generated&owned by "gen/generator.go". //template:begin addDeletedItemsXML + +func (data *BGPAddressFamilyVPNv4) addDeletedItemsXML(ctx context.Context, state BGPAddressFamilyVPNv4, body string) string { + b := netconf.NewBody(body) + + b = helpers.CleanupRedundantRemoveOperations(b) + return b.Res() +} + +// End of section. //template:end addDeletedItemsXML + +// Section below is generated&owned by "gen/generator.go". //template:begin getEmptyLeafsDelete + +func (data *BGPAddressFamilyVPNv4) getEmptyLeafsDelete(ctx context.Context) []string { + emptyLeafsDelete := make([]string, 0) + + return emptyLeafsDelete +} + +// End of section. //template:end getEmptyLeafsDelete + +// Section below is generated&owned by "gen/generator.go". //template:begin getDeletePaths + +func (data *BGPAddressFamilyVPNv4) getDeletePaths(ctx context.Context) []string { + var deletePaths []string + + return deletePaths +} + +// End of section. //template:end getDeletePaths + +// Section below is generated&owned by "gen/generator.go". //template:begin addDeletePathsXML + +func (data *BGPAddressFamilyVPNv4) addDeletePathsXML(ctx context.Context, body string) string { + b := netconf.NewBody(body) + + b = helpers.CleanupRedundantRemoveOperations(b) + return b.Res() +} + +// End of section. //template:end addDeletePathsXML diff --git a/internal/provider/model_iosxe_bgp_address_family_vpnv6.go b/internal/provider/model_iosxe_bgp_address_family_vpnv6.go new file mode 100644 index 00000000..0601b288 --- /dev/null +++ b/internal/provider/model_iosxe_bgp_address_family_vpnv6.go @@ -0,0 +1,236 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "net/url" + "regexp" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-netconf" + "github.com/netascode/xmldot" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin types +type BGPAddressFamilyVPNv6 struct { + Device types.String `tfsdk:"device"` + Id types.String `tfsdk:"id"` + DeleteMode types.String `tfsdk:"delete_mode"` + Asn types.String `tfsdk:"asn"` + AfName types.String `tfsdk:"af_name"` +} + +type BGPAddressFamilyVPNv6Data struct { + Device types.String `tfsdk:"device"` + Id types.String `tfsdk:"id"` + Asn types.String `tfsdk:"asn"` + AfName types.String `tfsdk:"af_name"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath + +func (data BGPAddressFamilyVPNv6) getPath() string { + return fmt.Sprintf("Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp=%v/address-family/no-vrf/vpnv6=%s", url.QueryEscape(fmt.Sprintf("%v", data.Asn.ValueString())), url.QueryEscape(fmt.Sprintf("%v", data.AfName.ValueString()))) +} + +func (data BGPAddressFamilyVPNv6Data) getPath() string { + return fmt.Sprintf("Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp=%v/address-family/no-vrf/vpnv6=%s", url.QueryEscape(fmt.Sprintf("%v", data.Asn.ValueString())), url.QueryEscape(fmt.Sprintf("%v", data.AfName.ValueString()))) +} + +// if last path element has a key -> remove it +func (data BGPAddressFamilyVPNv6) getPathShort() string { + path := data.getPath() + re := regexp.MustCompile(`(.*)=[^\/]*$`) + matches := re.FindStringSubmatch(path) + if len(matches) <= 1 { + return path + } + return matches[1] +} + +// getXPath returns the XPath for NETCONF operations +func (data BGPAddressFamilyVPNv6) getXPath() string { + path := "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=%v]/address-family/no-vrf/vpnv6[af-name=%s]" + path = fmt.Sprintf(path, fmt.Sprintf("%v", data.Asn.ValueString()), fmt.Sprintf("%v", data.AfName.ValueString())) + return path +} + +func (data BGPAddressFamilyVPNv6Data) getXPath() string { + path := "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=%v]/address-family/no-vrf/vpnv6[af-name=%s]" + path = fmt.Sprintf(path, fmt.Sprintf("%v", data.Asn.ValueString()), fmt.Sprintf("%v", data.AfName.ValueString())) + return path +} + +// End of section. //template:end getPath + +// Section below is generated&owned by "gen/generator.go". //template:begin toBody + +func (data BGPAddressFamilyVPNv6) toBody(ctx context.Context) string { + body := `{"` + helpers.LastElement(data.getPath()) + `":{}}` + if !data.AfName.IsNull() && !data.AfName.IsUnknown() { + body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"af-name", data.AfName.ValueString()) + } + return body +} + +// End of section. //template:end toBody + +// Section below is generated&owned by "gen/generator.go". //template:begin toBodyXML + +func (data BGPAddressFamilyVPNv6) toBodyXML(ctx context.Context) string { + body := netconf.Body{} + if !data.AfName.IsNull() && !data.AfName.IsUnknown() { + body = helpers.SetFromXPath(body, data.getXPath()+"/af-name", data.AfName.ValueString()) + } + bodyString, err := body.String() + if err != nil { + tflog.Error(ctx, fmt.Sprintf("Error converting body to string: %s", err)) + } + return bodyString +} + +// End of section. //template:end toBodyXML + +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBody + +func (data *BGPAddressFamilyVPNv6) updateFromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } + if value := res.Get(prefix + "af-name"); value.Exists() && !data.AfName.IsNull() { + data.AfName = types.StringValue(value.String()) + } else { + data.AfName = types.StringNull() + } +} + +// End of section. //template:end updateFromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBodyXML + +func (data *BGPAddressFamilyVPNv6) updateFromBodyXML(ctx context.Context, res xmldot.Result) { + if value := helpers.GetFromXPath(res, "data"+data.getXPath()+"/af-name"); value.Exists() && !data.AfName.IsNull() { + data.AfName = types.StringValue(value.String()) + } else { + data.AfName = types.StringNull() + } +} + +// End of section. //template:end updateFromBodyXML + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBody + +func (data *BGPAddressFamilyVPNv6) fromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } +} + +// End of section. //template:end fromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyData + +func (data *BGPAddressFamilyVPNv6Data) fromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } +} + +// End of section. //template:end fromBodyData + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyXML + +func (data *BGPAddressFamilyVPNv6) fromBodyXML(ctx context.Context, res xmldot.Result) { +} + +// End of section. //template:end fromBodyXML + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyDataXML + +func (data *BGPAddressFamilyVPNv6Data) fromBodyXML(ctx context.Context, res xmldot.Result) { +} + +// End of section. //template:end fromBodyDataXML + +// Section below is generated&owned by "gen/generator.go". //template:begin getDeletedItems + +func (data *BGPAddressFamilyVPNv6) getDeletedItems(ctx context.Context, state BGPAddressFamilyVPNv6) []string { + deletedItems := make([]string, 0) + + return deletedItems +} + +// End of section. //template:end getDeletedItems + +// Section below is generated&owned by "gen/generator.go". //template:begin addDeletedItemsXML + +func (data *BGPAddressFamilyVPNv6) addDeletedItemsXML(ctx context.Context, state BGPAddressFamilyVPNv6, body string) string { + b := netconf.NewBody(body) + + b = helpers.CleanupRedundantRemoveOperations(b) + return b.Res() +} + +// End of section. //template:end addDeletedItemsXML + +// Section below is generated&owned by "gen/generator.go". //template:begin getEmptyLeafsDelete + +func (data *BGPAddressFamilyVPNv6) getEmptyLeafsDelete(ctx context.Context) []string { + emptyLeafsDelete := make([]string, 0) + + return emptyLeafsDelete +} + +// End of section. //template:end getEmptyLeafsDelete + +// Section below is generated&owned by "gen/generator.go". //template:begin getDeletePaths + +func (data *BGPAddressFamilyVPNv6) getDeletePaths(ctx context.Context) []string { + var deletePaths []string + + return deletePaths +} + +// End of section. //template:end getDeletePaths + +// Section below is generated&owned by "gen/generator.go". //template:begin addDeletePathsXML + +func (data *BGPAddressFamilyVPNv6) addDeletePathsXML(ctx context.Context, body string) string { + b := netconf.NewBody(body) + + b = helpers.CleanupRedundantRemoveOperations(b) + return b.Res() +} + +// End of section. //template:end addDeletePathsXML diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 08de7ff6..149098f5 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -608,6 +608,8 @@ func (p *IosxeProvider) Resources(ctx context.Context) []func() resource.Resourc NewBGPAddressFamilyIPv6Resource, NewBGPAddressFamilyIPv6VRFResource, NewBGPAddressFamilyL2VPNResource, + NewBGPAddressFamilyVPNv4Resource, + NewBGPAddressFamilyVPNv6Resource, NewBGPIPv4MVPNNeighborResource, NewBGPIPv4UnicastNeighborResource, NewBGPIPv4UnicastVRFNeighborResource, @@ -722,6 +724,8 @@ func (p *IosxeProvider) DataSources(ctx context.Context) []func() datasource.Dat NewBGPAddressFamilyIPv6DataSource, NewBGPAddressFamilyIPv6VRFDataSource, NewBGPAddressFamilyL2VPNDataSource, + NewBGPAddressFamilyVPNv4DataSource, + NewBGPAddressFamilyVPNv6DataSource, NewBGPIPv4MVPNNeighborDataSource, NewBGPIPv4UnicastNeighborDataSource, NewBGPIPv4UnicastVRFNeighborDataSource, diff --git a/internal/provider/resource_iosxe_bgp_address_family_vpnv4.go b/internal/provider/resource_iosxe_bgp_address_family_vpnv4.go new file mode 100644 index 00000000..ddf6f9f3 --- /dev/null +++ b/internal/provider/resource_iosxe_bgp_address_family_vpnv4.go @@ -0,0 +1,519 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "strings" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-netconf" + "github.com/netascode/go-restconf" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure provider defined types fully satisfy framework interfaces +var ( + _ resource.Resource = &BGPAddressFamilyVPNv4Resource{} + _ resource.ResourceWithImportState = &BGPAddressFamilyVPNv4Resource{} +) + +func NewBGPAddressFamilyVPNv4Resource() resource.Resource { + return &BGPAddressFamilyVPNv4Resource{} +} + +type BGPAddressFamilyVPNv4Resource struct { + data *IosxeProviderData +} + +func (r *BGPAddressFamilyVPNv4Resource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_bgp_address_family_vpnv4" +} + +func (r *BGPAddressFamilyVPNv4Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: "This resource can manage the BGP Address Family VPNv4 configuration.", + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The path of the object.", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "delete_mode": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Configure behavior when deleting/destroying the resource. Either delete the entire object (YANG container) being managed, or only delete the individual resource attributes configured explicitly and leave everything else as-is. Default value is `all`.").AddStringEnumDescription("all", "attributes").String, + Optional: true, + Validators: []validator.String{ + stringvalidator.OneOf("all", "attributes"), + }, + }, + "asn": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("").String, + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "af_name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("").AddStringEnumDescription("flowspec", "multicast", "unicast").String, + Required: true, + Validators: []validator.String{ + stringvalidator.OneOf("flowspec", "multicast", "unicast"), + }, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + }, + } +} + +func (r *BGPAddressFamilyVPNv4Resource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.data = req.ProviderData.(*IosxeProviderData) +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin create + +func (r *BGPAddressFamilyVPNv4Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan BGPAddressFamilyVPNv4 + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Create", plan.getPath())) + + device, ok := r.data.Devices[plan.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", plan.Device.ValueString())) + return + } + + if device.Managed { + if device.Protocol == "restconf" { + // Create object + body := plan.toBody(ctx) + + emptyLeafsDelete := plan.getEmptyLeafsDelete(ctx) + tflog.Debug(ctx, fmt.Sprintf("List of empty leafs to delete: %+v", emptyLeafsDelete)) + + if YangPatch { + edits := []restconf.YangPatchEdit{restconf.NewYangPatchEdit("merge", plan.getPath(), restconf.Body{Str: body})} + for _, i := range emptyLeafsDelete { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.RestconfClient.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object, got error: %s", err)) + return + } + } else { + res, err := device.RestconfClient.PatchData(plan.getPathShort(), body) + if len(res.Errors.Error) > 0 && res.Errors.Error[0].ErrorMessage == "patch to a nonexistent resource" { + _, err = device.RestconfClient.PutData(plan.getPath(), body) + } + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PATCH, %s), got error: %s", plan.getPathShort(), err)) + return + } + for _, i := range emptyLeafsDelete { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + } + } else { + // Serialize NETCONF operations when reuse disabled, or writes when reuse enabled + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := plan.toBodyXML(ctx) + + if err := helpers.EditConfig(ctx, device.NetconfClient, body, device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } + + plan.Id = types.StringValue(plan.getPath()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.getPath())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end create + +// Section below is generated&owned by "gen/generator.go". //template:begin read + +func (r *BGPAddressFamilyVPNv4Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state BGPAddressFamilyVPNv4 + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", state.Id.ValueString())) + + device, ok := r.data.Devices[state.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", state.Device.ValueString())) + return + } + + if device.Managed { + imp, diags := helpers.IsFlagImporting(ctx, req) + if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() { + return + } + + if device.Protocol == "restconf" { + res, err := device.RestconfClient.GetData(state.Id.ValueString()) + if res.StatusCode == 404 { + state = BGPAddressFamilyVPNv4{Device: state.Device, Id: state.Id} + } else { + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", state.Id.ValueString(), err)) + return + } + + // After `terraform import` we switch to a full read. + if imp { + state.fromBody(ctx, res.Res) + } else { + state.updateFromBody(ctx, res.Res) + } + } + } else { + // Serialize NETCONF operations when reuse disabled (concurrent reads allowed when reuse enabled) + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, false) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + filter := helpers.GetXpathFilter(state.getXPath()) + res, err := device.NetconfClient.GetConfig(ctx, "running", filter) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", state.getPath(), err)) + return + } + + if helpers.IsGetConfigResponseEmpty(&res) && helpers.IsListPath(state.getXPath()) { + tflog.Debug(ctx, fmt.Sprintf("%s: Resource does not exist", state.Id.ValueString())) + resp.State.RemoveResource(ctx) + return + } + + // After `terraform import` we switch to a full read. + if imp { + state.fromBodyXML(ctx, res.Res) + } else { + state.updateFromBodyXML(ctx, res.Res) + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Id.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end read + +// Section below is generated&owned by "gen/generator.go". //template:begin update + +func (r *BGPAddressFamilyVPNv4Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state BGPAddressFamilyVPNv4 + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + // Read state + diags = req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) + + device, ok := r.data.Devices[plan.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", plan.Device.ValueString())) + return + } + + if device.Managed { + if device.Protocol == "restconf" { + body := plan.toBody(ctx) + + deletedItems := plan.getDeletedItems(ctx, state) + tflog.Debug(ctx, fmt.Sprintf("Removed items to delete: %+v", deletedItems)) + + emptyLeafsDelete := plan.getEmptyLeafsDelete(ctx) + tflog.Debug(ctx, fmt.Sprintf("List of empty leafs to delete: %+v", emptyLeafsDelete)) + + if YangPatch { + var edits []restconf.YangPatchEdit + for _, i := range deletedItems { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + edits = append(edits, restconf.NewYangPatchEdit("merge", plan.getPath(), restconf.Body{Str: body})) + for _, i := range emptyLeafsDelete { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.RestconfClient.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update object, got error: %s", err)) + return + } + } else { + for _, i := range deletedItems { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + res, err := device.RestconfClient.PatchData(plan.getPathShort(), body) + if len(res.Errors.Error) > 0 && res.Errors.Error[0].ErrorMessage == "patch to a nonexistent resource" { + _, err = device.RestconfClient.PutData(plan.getPath(), body) + } + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PATCH, %s), got error: %s", plan.getPathShort(), err)) + return + } + for _, i := range emptyLeafsDelete { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + } + } else { + // Serialize NETCONF operations when reuse disabled, or writes when reuse enabled + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := plan.toBodyXML(ctx) + body = plan.addDeletedItemsXML(ctx, state, body) + + if err := helpers.EditConfig(ctx, device.NetconfClient, body, device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end update + +// Section below is generated&owned by "gen/generator.go". //template:begin delete + +func (r *BGPAddressFamilyVPNv4Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state BGPAddressFamilyVPNv4 + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString())) + + device, ok := r.data.Devices[state.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", state.Device.ValueString())) + return + } + + if device.Managed { + deleteMode := "all" + if state.DeleteMode.ValueString() == "all" { + deleteMode = "all" + } else if state.DeleteMode.ValueString() == "attributes" { + deleteMode = "attributes" + } + + if deleteMode == "all" { + if device.Protocol == "restconf" { + res, err := device.RestconfClient.DeleteData(state.Id.ValueString()) + if err != nil && res.StatusCode != 404 && res.StatusCode != 400 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", state.Id.ValueString(), err)) + return + } + } else { + // NETCONF - Serialize write operations + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := netconf.Body{} + body = helpers.RemoveFromXPath(body, state.getXPath()) + + if err := helpers.EditConfig(ctx, device.NetconfClient, body.Res(), device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } else { + if device.Protocol == "restconf" { + deletePaths := state.getDeletePaths(ctx) + tflog.Debug(ctx, fmt.Sprintf("Paths to delete: %+v", deletePaths)) + + if YangPatch { + edits := []restconf.YangPatchEdit{} + for _, i := range deletePaths { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.RestconfClient.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object, got error: %s", err)) + return + } + } else { + for _, i := range deletePaths { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + } + } + } + } else { + // NETCONF - Serialize write operations + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := state.addDeletePathsXML(ctx, "") + + if err := helpers.EditConfig(ctx, device.NetconfClient, body, device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString())) + + resp.State.RemoveResource(ctx) +} + +// End of section. //template:end delete + +// Section below is generated&owned by "gen/generator.go". //template:begin import + +func (r *BGPAddressFamilyVPNv4Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + idParts := strings.Split(req.ID, ",") + idParts = helpers.RemoveEmptyStrings(idParts) + + if len(idParts) != 2 && len(idParts) != 3 { + expectedIdentifier := "Expected import identifier with format: ','" + expectedIdentifier += " or ',,'" + resp.Diagnostics.AddError( + "Unexpected Import Identifier", + fmt.Sprintf("%s. Got: %q", expectedIdentifier, req.ID), + ) + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("asn"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("af_name"), idParts[1])...) + if len(idParts) == 3 { + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("device"), idParts[len(idParts)-1])...) + } + + // construct path for 'id' attribute + var state BGPAddressFamilyVPNv4 + diags := resp.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), state.getPath())...) + + helpers.SetFlagImporting(ctx, true, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end import diff --git a/internal/provider/resource_iosxe_bgp_address_family_vpnv4_test.go b/internal/provider/resource_iosxe_bgp_address_family_vpnv4_test.go new file mode 100644 index 00000000..ba1a93f5 --- /dev/null +++ b/internal/provider/resource_iosxe_bgp_address_family_vpnv4_test.go @@ -0,0 +1,114 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAcc + +func TestAccIosxeBGPAddressFamilyVPNv4(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("iosxe_bgp_address_family_vpnv4.test", "af_name", "unicast")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccIosxeBGPAddressFamilyVPNv4PrerequisitesConfig + testAccIosxeBGPAddressFamilyVPNv4Config_minimum(), + }, + { + Config: testAccIosxeBGPAddressFamilyVPNv4PrerequisitesConfig + testAccIosxeBGPAddressFamilyVPNv4Config_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + { + ResourceName: "iosxe_bgp_address_family_vpnv4.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: iosxeBGPAddressFamilyVPNv4ImportStateIdFunc("iosxe_bgp_address_family_vpnv4.test"), + ImportStateVerifyIgnore: []string{}, + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAcc + +// Section below is generated&owned by "gen/generator.go". //template:begin importStateIdFunc + +func iosxeBGPAddressFamilyVPNv4ImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + primary := s.RootModule().Resources[resourceName].Primary + Asn := primary.Attributes["asn"] + AfName := primary.Attributes["af_name"] + + return fmt.Sprintf("%s,%s", Asn, AfName), nil + } +} + +// End of section. //template:end importStateIdFunc + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccIosxeBGPAddressFamilyVPNv4PrerequisitesConfig = ` +resource "iosxe_yang" "PreReq0" { + path = "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=65000]" + attributes = { + "id" = "65000" + } +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigMinimal + +func testAccIosxeBGPAddressFamilyVPNv4Config_minimum() string { + config := `resource "iosxe_bgp_address_family_vpnv4" "test" {` + "\n" + config += ` asn = "65000"` + "\n" + config += ` af_name = "unicast"` + "\n" + config += ` depends_on = [iosxe_yang.PreReq0, ]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigMinimal + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigAll + +func testAccIosxeBGPAddressFamilyVPNv4Config_all() string { + config := `resource "iosxe_bgp_address_family_vpnv4" "test" {` + "\n" + config += ` asn = "65000"` + "\n" + config += ` af_name = "unicast"` + "\n" + config += ` depends_on = [iosxe_yang.PreReq0, ]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigAll diff --git a/internal/provider/resource_iosxe_bgp_address_family_vpnv6.go b/internal/provider/resource_iosxe_bgp_address_family_vpnv6.go new file mode 100644 index 00000000..9b9fc259 --- /dev/null +++ b/internal/provider/resource_iosxe_bgp_address_family_vpnv6.go @@ -0,0 +1,519 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "strings" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-netconf" + "github.com/netascode/go-restconf" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure provider defined types fully satisfy framework interfaces +var ( + _ resource.Resource = &BGPAddressFamilyVPNv6Resource{} + _ resource.ResourceWithImportState = &BGPAddressFamilyVPNv6Resource{} +) + +func NewBGPAddressFamilyVPNv6Resource() resource.Resource { + return &BGPAddressFamilyVPNv6Resource{} +} + +type BGPAddressFamilyVPNv6Resource struct { + data *IosxeProviderData +} + +func (r *BGPAddressFamilyVPNv6Resource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_bgp_address_family_vpnv6" +} + +func (r *BGPAddressFamilyVPNv6Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: "This resource can manage the BGP Address Family VPNv6 configuration.", + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The path of the object.", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "delete_mode": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Configure behavior when deleting/destroying the resource. Either delete the entire object (YANG container) being managed, or only delete the individual resource attributes configured explicitly and leave everything else as-is. Default value is `all`.").AddStringEnumDescription("all", "attributes").String, + Optional: true, + Validators: []validator.String{ + stringvalidator.OneOf("all", "attributes"), + }, + }, + "asn": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("").String, + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "af_name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("").AddStringEnumDescription("flowspec", "multicast", "unicast").String, + Required: true, + Validators: []validator.String{ + stringvalidator.OneOf("flowspec", "multicast", "unicast"), + }, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + }, + } +} + +func (r *BGPAddressFamilyVPNv6Resource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.data = req.ProviderData.(*IosxeProviderData) +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin create + +func (r *BGPAddressFamilyVPNv6Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan BGPAddressFamilyVPNv6 + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Create", plan.getPath())) + + device, ok := r.data.Devices[plan.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", plan.Device.ValueString())) + return + } + + if device.Managed { + if device.Protocol == "restconf" { + // Create object + body := plan.toBody(ctx) + + emptyLeafsDelete := plan.getEmptyLeafsDelete(ctx) + tflog.Debug(ctx, fmt.Sprintf("List of empty leafs to delete: %+v", emptyLeafsDelete)) + + if YangPatch { + edits := []restconf.YangPatchEdit{restconf.NewYangPatchEdit("merge", plan.getPath(), restconf.Body{Str: body})} + for _, i := range emptyLeafsDelete { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.RestconfClient.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object, got error: %s", err)) + return + } + } else { + res, err := device.RestconfClient.PatchData(plan.getPathShort(), body) + if len(res.Errors.Error) > 0 && res.Errors.Error[0].ErrorMessage == "patch to a nonexistent resource" { + _, err = device.RestconfClient.PutData(plan.getPath(), body) + } + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PATCH, %s), got error: %s", plan.getPathShort(), err)) + return + } + for _, i := range emptyLeafsDelete { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + } + } else { + // Serialize NETCONF operations when reuse disabled, or writes when reuse enabled + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := plan.toBodyXML(ctx) + + if err := helpers.EditConfig(ctx, device.NetconfClient, body, device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } + + plan.Id = types.StringValue(plan.getPath()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.getPath())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end create + +// Section below is generated&owned by "gen/generator.go". //template:begin read + +func (r *BGPAddressFamilyVPNv6Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state BGPAddressFamilyVPNv6 + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", state.Id.ValueString())) + + device, ok := r.data.Devices[state.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", state.Device.ValueString())) + return + } + + if device.Managed { + imp, diags := helpers.IsFlagImporting(ctx, req) + if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() { + return + } + + if device.Protocol == "restconf" { + res, err := device.RestconfClient.GetData(state.Id.ValueString()) + if res.StatusCode == 404 { + state = BGPAddressFamilyVPNv6{Device: state.Device, Id: state.Id} + } else { + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", state.Id.ValueString(), err)) + return + } + + // After `terraform import` we switch to a full read. + if imp { + state.fromBody(ctx, res.Res) + } else { + state.updateFromBody(ctx, res.Res) + } + } + } else { + // Serialize NETCONF operations when reuse disabled (concurrent reads allowed when reuse enabled) + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, false) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + filter := helpers.GetXpathFilter(state.getXPath()) + res, err := device.NetconfClient.GetConfig(ctx, "running", filter) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", state.getPath(), err)) + return + } + + if helpers.IsGetConfigResponseEmpty(&res) && helpers.IsListPath(state.getXPath()) { + tflog.Debug(ctx, fmt.Sprintf("%s: Resource does not exist", state.Id.ValueString())) + resp.State.RemoveResource(ctx) + return + } + + // After `terraform import` we switch to a full read. + if imp { + state.fromBodyXML(ctx, res.Res) + } else { + state.updateFromBodyXML(ctx, res.Res) + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Id.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end read + +// Section below is generated&owned by "gen/generator.go". //template:begin update + +func (r *BGPAddressFamilyVPNv6Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state BGPAddressFamilyVPNv6 + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + // Read state + diags = req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) + + device, ok := r.data.Devices[plan.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", plan.Device.ValueString())) + return + } + + if device.Managed { + if device.Protocol == "restconf" { + body := plan.toBody(ctx) + + deletedItems := plan.getDeletedItems(ctx, state) + tflog.Debug(ctx, fmt.Sprintf("Removed items to delete: %+v", deletedItems)) + + emptyLeafsDelete := plan.getEmptyLeafsDelete(ctx) + tflog.Debug(ctx, fmt.Sprintf("List of empty leafs to delete: %+v", emptyLeafsDelete)) + + if YangPatch { + var edits []restconf.YangPatchEdit + for _, i := range deletedItems { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + edits = append(edits, restconf.NewYangPatchEdit("merge", plan.getPath(), restconf.Body{Str: body})) + for _, i := range emptyLeafsDelete { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.RestconfClient.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update object, got error: %s", err)) + return + } + } else { + for _, i := range deletedItems { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + res, err := device.RestconfClient.PatchData(plan.getPathShort(), body) + if len(res.Errors.Error) > 0 && res.Errors.Error[0].ErrorMessage == "patch to a nonexistent resource" { + _, err = device.RestconfClient.PutData(plan.getPath(), body) + } + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PATCH, %s), got error: %s", plan.getPathShort(), err)) + return + } + for _, i := range emptyLeafsDelete { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + } + } else { + // Serialize NETCONF operations when reuse disabled, or writes when reuse enabled + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := plan.toBodyXML(ctx) + body = plan.addDeletedItemsXML(ctx, state, body) + + if err := helpers.EditConfig(ctx, device.NetconfClient, body, device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end update + +// Section below is generated&owned by "gen/generator.go". //template:begin delete + +func (r *BGPAddressFamilyVPNv6Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state BGPAddressFamilyVPNv6 + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString())) + + device, ok := r.data.Devices[state.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", state.Device.ValueString())) + return + } + + if device.Managed { + deleteMode := "all" + if state.DeleteMode.ValueString() == "all" { + deleteMode = "all" + } else if state.DeleteMode.ValueString() == "attributes" { + deleteMode = "attributes" + } + + if deleteMode == "all" { + if device.Protocol == "restconf" { + res, err := device.RestconfClient.DeleteData(state.Id.ValueString()) + if err != nil && res.StatusCode != 404 && res.StatusCode != 400 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", state.Id.ValueString(), err)) + return + } + } else { + // NETCONF - Serialize write operations + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := netconf.Body{} + body = helpers.RemoveFromXPath(body, state.getXPath()) + + if err := helpers.EditConfig(ctx, device.NetconfClient, body.Res(), device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } else { + if device.Protocol == "restconf" { + deletePaths := state.getDeletePaths(ctx) + tflog.Debug(ctx, fmt.Sprintf("Paths to delete: %+v", deletePaths)) + + if YangPatch { + edits := []restconf.YangPatchEdit{} + for _, i := range deletePaths { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.RestconfClient.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object, got error: %s", err)) + return + } + } else { + for _, i := range deletePaths { + res, err := device.RestconfClient.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + } + } + } + } else { + // NETCONF - Serialize write operations + locked := helpers.AcquireNetconfLock(&device.NetconfOpMutex, device.ReuseConnection, true) + if locked { + defer device.NetconfOpMutex.Unlock() + } + defer helpers.CloseNetconfConnection(ctx, device.NetconfClient, device.ReuseConnection) + + body := state.addDeletePathsXML(ctx, "") + + if err := helpers.EditConfig(ctx, device.NetconfClient, body, device.AutoCommit); err != nil { + resp.Diagnostics.AddError("Client Error", err.Error()) + return + } + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString())) + + resp.State.RemoveResource(ctx) +} + +// End of section. //template:end delete + +// Section below is generated&owned by "gen/generator.go". //template:begin import + +func (r *BGPAddressFamilyVPNv6Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + idParts := strings.Split(req.ID, ",") + idParts = helpers.RemoveEmptyStrings(idParts) + + if len(idParts) != 2 && len(idParts) != 3 { + expectedIdentifier := "Expected import identifier with format: ','" + expectedIdentifier += " or ',,'" + resp.Diagnostics.AddError( + "Unexpected Import Identifier", + fmt.Sprintf("%s. Got: %q", expectedIdentifier, req.ID), + ) + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("asn"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("af_name"), idParts[1])...) + if len(idParts) == 3 { + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("device"), idParts[len(idParts)-1])...) + } + + // construct path for 'id' attribute + var state BGPAddressFamilyVPNv6 + diags := resp.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), state.getPath())...) + + helpers.SetFlagImporting(ctx, true, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end import diff --git a/internal/provider/resource_iosxe_bgp_address_family_vpnv6_test.go b/internal/provider/resource_iosxe_bgp_address_family_vpnv6_test.go new file mode 100644 index 00000000..00f919a9 --- /dev/null +++ b/internal/provider/resource_iosxe_bgp_address_family_vpnv6_test.go @@ -0,0 +1,114 @@ +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. +// All rights reserved. +// +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://mozilla.org/MPL/2.0/ +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: MPL-2.0 + +// Code generated by "gen/generator.go"; DO NOT EDIT. + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAcc + +func TestAccIosxeBGPAddressFamilyVPNv6(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("iosxe_bgp_address_family_vpnv6.test", "af_name", "unicast")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccIosxeBGPAddressFamilyVPNv6PrerequisitesConfig + testAccIosxeBGPAddressFamilyVPNv6Config_minimum(), + }, + { + Config: testAccIosxeBGPAddressFamilyVPNv6PrerequisitesConfig + testAccIosxeBGPAddressFamilyVPNv6Config_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + { + ResourceName: "iosxe_bgp_address_family_vpnv6.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: iosxeBGPAddressFamilyVPNv6ImportStateIdFunc("iosxe_bgp_address_family_vpnv6.test"), + ImportStateVerifyIgnore: []string{}, + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAcc + +// Section below is generated&owned by "gen/generator.go". //template:begin importStateIdFunc + +func iosxeBGPAddressFamilyVPNv6ImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + primary := s.RootModule().Resources[resourceName].Primary + Asn := primary.Attributes["asn"] + AfName := primary.Attributes["af_name"] + + return fmt.Sprintf("%s,%s", Asn, AfName), nil + } +} + +// End of section. //template:end importStateIdFunc + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccIosxeBGPAddressFamilyVPNv6PrerequisitesConfig = ` +resource "iosxe_yang" "PreReq0" { + path = "/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp[id=65000]" + attributes = { + "id" = "65000" + } +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigMinimal + +func testAccIosxeBGPAddressFamilyVPNv6Config_minimum() string { + config := `resource "iosxe_bgp_address_family_vpnv6" "test" {` + "\n" + config += ` asn = "65000"` + "\n" + config += ` af_name = "unicast"` + "\n" + config += ` depends_on = [iosxe_yang.PreReq0, ]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigMinimal + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigAll + +func testAccIosxeBGPAddressFamilyVPNv6Config_all() string { + config := `resource "iosxe_bgp_address_family_vpnv6" "test" {` + "\n" + config += ` asn = "65000"` + "\n" + config += ` af_name = "unicast"` + "\n" + config += ` depends_on = [iosxe_yang.PreReq0, ]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigAll