diff --git a/CHANGELOG.md b/CHANGELOG.md index f6a3db0..33f0cfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.1.16 (unreleased) +- Add Device Unreachability Warning to `catalystcenter_fabric_l3_handoff_ip_transit` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/150) +- Add `catalystcenter_fabric_l2_virtual_network` resource and data source - Remove `max_async_wait_time` attribute and use timeout from `CC_MAX_TIMEOUT` - Modify `catalystcenter_deploy_template` resource to support deploying composite templates - Modify `catalystcenter_template_version` resource to use versioned template id as `id` and remove data_source diff --git a/docs/data-sources/fabric_l2_virtual_network.md b/docs/data-sources/fabric_l2_virtual_network.md new file mode 100644 index 0000000..d1f5662 --- /dev/null +++ b/docs/data-sources/fabric_l2_virtual_network.md @@ -0,0 +1,36 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "catalystcenter_fabric_l2_virtual_network Data Source - terraform-provider-catalystcenter" +subcategory: "SDA" +description: |- + This data source can read the Fabric L2 Virtual Network. +--- + +# catalystcenter_fabric_l2_virtual_network (Data Source) + +This data source can read the Fabric L2 Virtual Network. + +## Example Usage + +```terraform +data "catalystcenter_fabric_l2_virtual_network" "example" { + fabric_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1" + vlan_name = "VLAN401" +} +``` + + +## Schema + +### Required + +- `fabric_id` (String) ID of the fabric this layer 2 virtual network is to be assigned to +- `vlan_name` (String) Name of the VLAN of the layer 2 virtual network. Must contain only alphanumeric characters, underscores, and hyphens + +### Read-Only + +- `associated_l3_virtual_network_name` (String) Name of the layer 3 virtual network associated with the layer 2 virtual network. This field is provided to support requests related to virtual network anchoring. The layer 3 virtual network must have already been added to the fabric before association. This field must either be present in all payload elements or none +- `fabric_enabled_wireless` (Boolean) Set to true to enable wireless. Default is false +- `id` (String) The id of the object +- `traffic_type` (String) The type of traffic that is served +- `vlan_id` (Number) ID of the VLAN of the layer 2 virtual network. Allowed VLAN range is 2-4093 except for reserved VLANs 1002-1005, and 2046. If deploying on a fabric zone, this vlanId must match the vlanId of the corresponding layer 2 virtual network on the fabric site diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index 422897b..945044a 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -9,6 +9,8 @@ description: |- ## 0.1.16 (unreleased) +- Add Device Unreachability Warning to `catalystcenter_fabric_l3_handoff_ip_transit` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/150) +- Add `catalystcenter_fabric_l2_virtual_network` resource and data source - Remove `max_async_wait_time` attribute and use timeout from `CC_MAX_TIMEOUT` - Modify `catalystcenter_deploy_template` resource to support deploying composite templates - Modify `catalystcenter_template_version` resource to use versioned template id as `id` and remove data_source diff --git a/docs/resources/fabric_l2_virtual_network.md b/docs/resources/fabric_l2_virtual_network.md new file mode 100644 index 0000000..f6857a3 --- /dev/null +++ b/docs/resources/fabric_l2_virtual_network.md @@ -0,0 +1,51 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "catalystcenter_fabric_l2_virtual_network Resource - terraform-provider-catalystcenter" +subcategory: "SDA" +description: |- + This resource can manage a Fabric L2 Virtual Network. +--- + +# catalystcenter_fabric_l2_virtual_network (Resource) + +This resource can manage a Fabric L2 Virtual Network. + +## Example Usage + +```terraform +resource "catalystcenter_fabric_l2_virtual_network" "example" { + fabric_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1" + vlan_name = "VLAN401" + vlan_id = 401 + traffic_type = "DATA" + fabric_enabled_wireless = false +} +``` + + +## Schema + +### Required + +- `fabric_id` (String) ID of the fabric this layer 2 virtual network is to be assigned to +- `traffic_type` (String) The type of traffic that is served + - Choices: `DATA`, `VOICE` +- `vlan_name` (String) Name of the VLAN of the layer 2 virtual network. Must contain only alphanumeric characters, underscores, and hyphens + +### Optional + +- `associated_l3_virtual_network_name` (String) Name of the layer 3 virtual network associated with the layer 2 virtual network. This field is provided to support requests related to virtual network anchoring. The layer 3 virtual network must have already been added to the fabric before association. This field must either be present in all payload elements or none +- `fabric_enabled_wireless` (Boolean) Set to true to enable wireless. Default is false +- `vlan_id` (Number) ID of the VLAN of the layer 2 virtual network. Allowed VLAN range is 2-4093 except for reserved VLANs 1002-1005, and 2046. If deploying on a fabric zone, this vlanId must match the vlanId of the corresponding layer 2 virtual network on the fabric site + +### Read-Only + +- `id` (String) The id of the object + +## Import + +Import is supported using the following syntax: + +```shell +terraform import catalystcenter_fabric_l2_virtual_network.example "," +``` diff --git a/examples/data-sources/catalystcenter_fabric_l2_virtual_network/data-source.tf b/examples/data-sources/catalystcenter_fabric_l2_virtual_network/data-source.tf new file mode 100644 index 0000000..30dd167 --- /dev/null +++ b/examples/data-sources/catalystcenter_fabric_l2_virtual_network/data-source.tf @@ -0,0 +1,4 @@ +data "catalystcenter_fabric_l2_virtual_network" "example" { + fabric_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1" + vlan_name = "VLAN401" +} diff --git a/examples/resources/catalystcenter_fabric_l2_virtual_network/import.sh b/examples/resources/catalystcenter_fabric_l2_virtual_network/import.sh new file mode 100644 index 0000000..8968cc6 --- /dev/null +++ b/examples/resources/catalystcenter_fabric_l2_virtual_network/import.sh @@ -0,0 +1 @@ +terraform import catalystcenter_fabric_l2_virtual_network.example "," diff --git a/examples/resources/catalystcenter_fabric_l2_virtual_network/resource.tf b/examples/resources/catalystcenter_fabric_l2_virtual_network/resource.tf new file mode 100644 index 0000000..c11323a --- /dev/null +++ b/examples/resources/catalystcenter_fabric_l2_virtual_network/resource.tf @@ -0,0 +1,7 @@ +resource "catalystcenter_fabric_l2_virtual_network" "example" { + fabric_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1" + vlan_name = "VLAN401" + vlan_id = 401 + traffic_type = "DATA" + fabric_enabled_wireless = false +} diff --git a/gen/definitions/anycast_gateway.yaml b/gen/definitions/anycast_gateway.yaml index 9556bd1..c5a4bf1 100644 --- a/gen/definitions/anycast_gateway.yaml +++ b/gen/definitions/anycast_gateway.yaml @@ -146,7 +146,6 @@ test_prerequisites: | resource "catalystcenter_area" "test" { name = "Area1" parent_name = "Global" - depends_on = [catalystcenter_ip_pool.test] } resource "catalystcenter_ip_pool" "test" { name = "MyPool1" @@ -166,7 +165,6 @@ test_prerequisites: | site_id = catalystcenter_area.test.id pub_sub_enabled = false authentication_profile_name = "No Authentication" - depends_on = [catalystcenter_area.test] } resource "catalystcenter_fabric_virtual_network" "test" { virtual_network_name = "SDA_VN1" diff --git a/gen/definitions/fabric_l2_virtual_network.yaml b/gen/definitions/fabric_l2_virtual_network.yaml new file mode 100644 index 0000000..ed94bd8 --- /dev/null +++ b/gen/definitions/fabric_l2_virtual_network.yaml @@ -0,0 +1,72 @@ +--- +name: Fabric L2 Virtual Network +rest_endpoint: /dna/intent/api/v1/sda/layer2VirtualNetworks +id_from_query_path: response.0 +id_from_query_path_attribute: id +import_no_id: true +data_source_no_id: true +skip_minimum_test: true +put_id_include_path: "0.id" +put_no_id: true +doc_category: SDA +attributes: + - model_name: fabricId + requires_replace: true + data_path: '0' + query_param: true + response_data_path: response.0.fabricId + mandatory: true + description: ID of the fabric this layer 2 virtual network is to be assigned to + type: String + example: 5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1 + test_value: catalystcenter_fabric_site.test.id + - model_name: vlanName + requires_replace: true + data_path: '0' + query_param: true + response_data_path: response.0.vlanName + type: String + mandatory: true + description: Name of the VLAN of the layer 2 virtual network. Must contain only alphanumeric characters, underscores, and hyphens + example: VLAN401 + - model_name: vlanId + requires_replace: true + data_path: '0' + response_data_path: response.0.vlanId + type: Int64 + description: ID of the VLAN of the layer 2 virtual network. Allowed VLAN range is 2-4093 except for reserved VLANs 1002-1005, and 2046. If deploying on a fabric zone, this vlanId must match the vlanId of the corresponding layer 2 virtual network on the fabric site + example: 401 + - model_name: trafficType + data_path: '0' + response_data_path: response.0.trafficType + type: String + enum_values: [DATA, VOICE] + mandatory: true + description: The type of traffic that is served + example: DATA + - model_name: isFabricEnabledWireless + data_path: '0' + response_data_path: response.0.isFabricEnabledWireless + tf_name: fabric_enabled_wireless + type: Bool + description: Set to true to enable wireless. Default is false + example: false + - model_name: associatedLayer3VirtualNetworkName + requires_replace: true + data_path: '0' + response_data_path: response.0.associatedLayer3VirtualNetworkName + tf_name: associated_l3_virtual_network_name + type: String + description: Name of the layer 3 virtual network associated with the layer 2 virtual network. This field is provided to support requests related to virtual network anchoring. The layer 3 virtual network must have already been added to the fabric before association. This field must either be present in all payload elements or none + example: SDA_VN1 + exclude_test: true +test_prerequisites: | + resource "catalystcenter_area" "test" { + name = "Area1" + parent_name = "Global" + } + resource "catalystcenter_fabric_site" "test" { + site_id = catalystcenter_area.test.id + pub_sub_enabled = false + authentication_profile_name = "No Authentication" + } diff --git a/internal/provider/data_source_catalystcenter_anycast_gateway_test.go b/internal/provider/data_source_catalystcenter_anycast_gateway_test.go index 1635adb..ac49402 100644 --- a/internal/provider/data_source_catalystcenter_anycast_gateway_test.go +++ b/internal/provider/data_source_catalystcenter_anycast_gateway_test.go @@ -58,7 +58,6 @@ const testAccDataSourceCcAnycastGatewayPrerequisitesConfig = ` resource "catalystcenter_area" "test" { name = "Area1" parent_name = "Global" - depends_on = [catalystcenter_ip_pool.test] } resource "catalystcenter_ip_pool" "test" { name = "MyPool1" @@ -78,7 +77,6 @@ resource "catalystcenter_fabric_site" "test" { site_id = catalystcenter_area.test.id pub_sub_enabled = false authentication_profile_name = "No Authentication" - depends_on = [catalystcenter_area.test] } resource "catalystcenter_fabric_virtual_network" "test" { virtual_network_name = "SDA_VN1" diff --git a/internal/provider/data_source_catalystcenter_fabric_l2_virtual_network.go b/internal/provider/data_source_catalystcenter_fabric_l2_virtual_network.go new file mode 100644 index 0000000..1a1eda2 --- /dev/null +++ b/internal/provider/data_source_catalystcenter_fabric_l2_virtual_network.go @@ -0,0 +1,131 @@ +// 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 + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "net/url" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-log/tflog" + cc "github.com/netascode/go-catalystcenter" +) + +// 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 = &FabricL2VirtualNetworkDataSource{} + _ datasource.DataSourceWithConfigure = &FabricL2VirtualNetworkDataSource{} +) + +func NewFabricL2VirtualNetworkDataSource() datasource.DataSource { + return &FabricL2VirtualNetworkDataSource{} +} + +type FabricL2VirtualNetworkDataSource struct { + client *cc.Client +} + +func (d *FabricL2VirtualNetworkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_fabric_l2_virtual_network" +} + +func (d *FabricL2VirtualNetworkDataSource) 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 Fabric L2 Virtual Network.", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the object", + Computed: true, + }, + "fabric_id": schema.StringAttribute{ + MarkdownDescription: "ID of the fabric this layer 2 virtual network is to be assigned to", + Required: true, + }, + "vlan_name": schema.StringAttribute{ + MarkdownDescription: "Name of the VLAN of the layer 2 virtual network. Must contain only alphanumeric characters, underscores, and hyphens", + Required: true, + }, + "vlan_id": schema.Int64Attribute{ + MarkdownDescription: "ID of the VLAN of the layer 2 virtual network. Allowed VLAN range is 2-4093 except for reserved VLANs 1002-1005, and 2046. If deploying on a fabric zone, this vlanId must match the vlanId of the corresponding layer 2 virtual network on the fabric site", + Computed: true, + }, + "traffic_type": schema.StringAttribute{ + MarkdownDescription: "The type of traffic that is served", + Computed: true, + }, + "fabric_enabled_wireless": schema.BoolAttribute{ + MarkdownDescription: "Set to true to enable wireless. Default is false", + Computed: true, + }, + "associated_l3_virtual_network_name": schema.StringAttribute{ + MarkdownDescription: "Name of the layer 3 virtual network associated with the layer 2 virtual network. This field is provided to support requests related to virtual network anchoring. The layer 3 virtual network must have already been added to the fabric before association. This field must either be present in all payload elements or none", + Computed: true, + }, + }, + } +} + +func (d *FabricL2VirtualNetworkDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.client = req.ProviderData.(*CcProviderData).Client +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin read +func (d *FabricL2VirtualNetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config FabricL2VirtualNetwork + + // 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.Id.String())) + + params := "" + params += "?fabricId=" + url.QueryEscape(config.FabricId.ValueString()) + "&vlanName=" + url.QueryEscape(config.VlanName.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)) + return + } + + config.fromBody(ctx, res) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.Id.ValueString())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read diff --git a/internal/provider/data_source_catalystcenter_fabric_l2_virtual_network_test.go b/internal/provider/data_source_catalystcenter_fabric_l2_virtual_network_test.go new file mode 100644 index 0000000..cfd6529 --- /dev/null +++ b/internal/provider/data_source_catalystcenter_fabric_l2_virtual_network_test.go @@ -0,0 +1,86 @@ +// 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 + +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 TestAccDataSourceCcFabricL2VirtualNetwork(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_fabric_l2_virtual_network.test", "vlan_name", "VLAN401")) + checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_fabric_l2_virtual_network.test", "vlan_id", "401")) + checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_fabric_l2_virtual_network.test", "traffic_type", "DATA")) + checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_fabric_l2_virtual_network.test", "fabric_enabled_wireless", "false")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceCcFabricL2VirtualNetworkPrerequisitesConfig + testAccDataSourceCcFabricL2VirtualNetworkConfig(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccDataSourceCcFabricL2VirtualNetworkPrerequisitesConfig = ` +resource "catalystcenter_area" "test" { + name = "Area1" + parent_name = "Global" +} +resource "catalystcenter_fabric_site" "test" { + site_id = catalystcenter_area.test.id + pub_sub_enabled = false + authentication_profile_name = "No Authentication" +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig +func testAccDataSourceCcFabricL2VirtualNetworkConfig() string { + config := `resource "catalystcenter_fabric_l2_virtual_network" "test" {` + "\n" + config += ` fabric_id = catalystcenter_fabric_site.test.id` + "\n" + config += ` vlan_name = "VLAN401"` + "\n" + config += ` vlan_id = 401` + "\n" + config += ` traffic_type = "DATA"` + "\n" + config += ` fabric_enabled_wireless = false` + "\n" + config += `}` + "\n" + + config += ` + data "catalystcenter_fabric_l2_virtual_network" "test" { + fabric_id = catalystcenter_fabric_site.test.id + vlan_name = "VLAN401" + depends_on = [catalystcenter_fabric_l2_virtual_network.test] + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/model_catalystcenter_fabric_l2_virtual_network.go b/internal/provider/model_catalystcenter_fabric_l2_virtual_network.go new file mode 100644 index 0000000..11492f8 --- /dev/null +++ b/internal/provider/model_catalystcenter_fabric_l2_virtual_network.go @@ -0,0 +1,182 @@ +// 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 + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/types" + "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 FabricL2VirtualNetwork struct { + Id types.String `tfsdk:"id"` + FabricId types.String `tfsdk:"fabric_id"` + VlanName types.String `tfsdk:"vlan_name"` + VlanId types.Int64 `tfsdk:"vlan_id"` + TrafficType types.String `tfsdk:"traffic_type"` + FabricEnabledWireless types.Bool `tfsdk:"fabric_enabled_wireless"` + AssociatedL3VirtualNetworkName types.String `tfsdk:"associated_l3_virtual_network_name"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath +func (data FabricL2VirtualNetwork) getPath() string { + return "/dna/intent/api/v1/sda/layer2VirtualNetworks" +} + +// End of section. //template:end getPath + +// Section below is generated&owned by "gen/generator.go". //template:begin getPathDelete + +// End of section. //template:end getPathDelete + +// Section below is generated&owned by "gen/generator.go". //template:begin toBody +func (data FabricL2VirtualNetwork) toBody(ctx context.Context, state FabricL2VirtualNetwork) string { + body := "" + put := false + if state.Id.ValueString() != "" { + put = true + body, _ = sjson.Set(body, "0.id", state.Id.ValueString()) + } + _ = put + if !data.FabricId.IsNull() { + body, _ = sjson.Set(body, "0.fabricId", data.FabricId.ValueString()) + } + if !data.VlanName.IsNull() { + body, _ = sjson.Set(body, "0.vlanName", data.VlanName.ValueString()) + } + if !data.VlanId.IsNull() { + body, _ = sjson.Set(body, "0.vlanId", data.VlanId.ValueInt64()) + } + if !data.TrafficType.IsNull() { + body, _ = sjson.Set(body, "0.trafficType", data.TrafficType.ValueString()) + } + if !data.FabricEnabledWireless.IsNull() { + body, _ = sjson.Set(body, "0.isFabricEnabledWireless", data.FabricEnabledWireless.ValueBool()) + } + if !data.AssociatedL3VirtualNetworkName.IsNull() { + body, _ = sjson.Set(body, "0.associatedLayer3VirtualNetworkName", data.AssociatedL3VirtualNetworkName.ValueString()) + } + return body +} + +// End of section. //template:end toBody + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBody +func (data *FabricL2VirtualNetwork) fromBody(ctx context.Context, res gjson.Result) { + // Retrieve the 'id' attribute, if Data Source doesn't require id + if value := res.Get("response.0.id"); value.Exists() { + data.Id = types.StringValue(value.String()) + } else { + data.Id = types.StringNull() + } + if value := res.Get("response.0.fabricId"); value.Exists() { + data.FabricId = types.StringValue(value.String()) + } else { + data.FabricId = types.StringNull() + } + if value := res.Get("response.0.vlanName"); value.Exists() { + data.VlanName = types.StringValue(value.String()) + } else { + data.VlanName = types.StringNull() + } + if value := res.Get("response.0.vlanId"); value.Exists() { + data.VlanId = types.Int64Value(value.Int()) + } else { + data.VlanId = types.Int64Null() + } + if value := res.Get("response.0.trafficType"); value.Exists() { + data.TrafficType = types.StringValue(value.String()) + } else { + data.TrafficType = types.StringNull() + } + if value := res.Get("response.0.isFabricEnabledWireless"); value.Exists() { + data.FabricEnabledWireless = types.BoolValue(value.Bool()) + } else { + data.FabricEnabledWireless = types.BoolNull() + } + if value := res.Get("response.0.associatedLayer3VirtualNetworkName"); value.Exists() { + data.AssociatedL3VirtualNetworkName = types.StringValue(value.String()) + } else { + data.AssociatedL3VirtualNetworkName = types.StringNull() + } +} + +// End of section. //template:end fromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBody +func (data *FabricL2VirtualNetwork) updateFromBody(ctx context.Context, res gjson.Result) { + if value := res.Get("response.0.fabricId"); value.Exists() && !data.FabricId.IsNull() { + data.FabricId = types.StringValue(value.String()) + } else { + data.FabricId = types.StringNull() + } + if value := res.Get("response.0.vlanName"); value.Exists() && !data.VlanName.IsNull() { + data.VlanName = types.StringValue(value.String()) + } else { + data.VlanName = types.StringNull() + } + if value := res.Get("response.0.vlanId"); value.Exists() && !data.VlanId.IsNull() { + data.VlanId = types.Int64Value(value.Int()) + } else { + data.VlanId = types.Int64Null() + } + if value := res.Get("response.0.trafficType"); value.Exists() && !data.TrafficType.IsNull() { + data.TrafficType = types.StringValue(value.String()) + } else { + data.TrafficType = types.StringNull() + } + if value := res.Get("response.0.isFabricEnabledWireless"); value.Exists() && !data.FabricEnabledWireless.IsNull() { + data.FabricEnabledWireless = types.BoolValue(value.Bool()) + } else { + data.FabricEnabledWireless = types.BoolNull() + } + if value := res.Get("response.0.associatedLayer3VirtualNetworkName"); value.Exists() && !data.AssociatedL3VirtualNetworkName.IsNull() { + data.AssociatedL3VirtualNetworkName = types.StringValue(value.String()) + } else { + data.AssociatedL3VirtualNetworkName = types.StringNull() + } +} + +// End of section. //template:end updateFromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin isNull +func (data *FabricL2VirtualNetwork) isNull(ctx context.Context, res gjson.Result) bool { + if !data.VlanId.IsNull() { + return false + } + if !data.TrafficType.IsNull() { + return false + } + if !data.FabricEnabledWireless.IsNull() { + return false + } + if !data.AssociatedL3VirtualNetworkName.IsNull() { + return false + } + return true +} + +// End of section. //template:end isNull diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9071a16..9a875fd 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -288,6 +288,7 @@ func (p *CcProvider) Resources(ctx context.Context) []func() resource.Resource { NewFabricAuthenticationProfileResource, NewFabricDeviceResource, NewFabricL2HandoffResource, + NewFabricL2VirtualNetworkResource, NewFabricL3HandoffIPTransitResource, NewFabricPortAssignmentResource, NewFabricProvisionDeviceResource, @@ -346,6 +347,7 @@ func (p *CcProvider) DataSources(ctx context.Context) []func() datasource.DataSo NewFabricAuthenticationProfileDataSource, NewFabricDeviceDataSource, NewFabricL2HandoffDataSource, + NewFabricL2VirtualNetworkDataSource, NewFabricL3HandoffIPTransitDataSource, NewFabricPortAssignmentDataSource, NewFabricProvisionDeviceDataSource, diff --git a/internal/provider/resource_catalystcenter_anycast_gateway_test.go b/internal/provider/resource_catalystcenter_anycast_gateway_test.go index dd673fc..6cae5de 100644 --- a/internal/provider/resource_catalystcenter_anycast_gateway_test.go +++ b/internal/provider/resource_catalystcenter_anycast_gateway_test.go @@ -60,7 +60,6 @@ const testAccCcAnycastGatewayPrerequisitesConfig = ` resource "catalystcenter_area" "test" { name = "Area1" parent_name = "Global" - depends_on = [catalystcenter_ip_pool.test] } resource "catalystcenter_ip_pool" "test" { name = "MyPool1" @@ -80,7 +79,6 @@ resource "catalystcenter_fabric_site" "test" { site_id = catalystcenter_area.test.id pub_sub_enabled = false authentication_profile_name = "No Authentication" - depends_on = [catalystcenter_area.test] } resource "catalystcenter_fabric_virtual_network" "test" { virtual_network_name = "SDA_VN1" diff --git a/internal/provider/resource_catalystcenter_fabric_l2_virtual_network.go b/internal/provider/resource_catalystcenter_fabric_l2_virtual_network.go new file mode 100644 index 0000000..439bc1b --- /dev/null +++ b/internal/provider/resource_catalystcenter_fabric_l2_virtual_network.go @@ -0,0 +1,280 @@ +// 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 + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "net/url" + "strings" + + "github.com/CiscoDevNet/terraform-provider-catalystcenter/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/int64planmodifier" + "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" + cc "github.com/netascode/go-catalystcenter" +) + +// 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 = &FabricL2VirtualNetworkResource{} +var _ resource.ResourceWithImportState = &FabricL2VirtualNetworkResource{} + +func NewFabricL2VirtualNetworkResource() resource.Resource { + return &FabricL2VirtualNetworkResource{} +} + +type FabricL2VirtualNetworkResource struct { + client *cc.Client +} + +func (r *FabricL2VirtualNetworkResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_fabric_l2_virtual_network" +} + +func (r *FabricL2VirtualNetworkResource) 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: helpers.NewAttributeDescription("This resource can manage a Fabric L2 Virtual Network.").String, + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the object", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "fabric_id": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("ID of the fabric this layer 2 virtual network is to be assigned to").String, + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "vlan_name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Name of the VLAN of the layer 2 virtual network. Must contain only alphanumeric characters, underscores, and hyphens").String, + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "vlan_id": schema.Int64Attribute{ + MarkdownDescription: helpers.NewAttributeDescription("ID of the VLAN of the layer 2 virtual network. Allowed VLAN range is 2-4093 except for reserved VLANs 1002-1005, and 2046. If deploying on a fabric zone, this vlanId must match the vlanId of the corresponding layer 2 virtual network on the fabric site").String, + Optional: true, + PlanModifiers: []planmodifier.Int64{ + int64planmodifier.RequiresReplace(), + }, + }, + "traffic_type": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("The type of traffic that is served").AddStringEnumDescription("DATA", "VOICE").String, + Required: true, + Validators: []validator.String{ + stringvalidator.OneOf("DATA", "VOICE"), + }, + }, + "fabric_enabled_wireless": schema.BoolAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Set to true to enable wireless. Default is false").String, + Optional: true, + }, + "associated_l3_virtual_network_name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Name of the layer 3 virtual network associated with the layer 2 virtual network. This field is provided to support requests related to virtual network anchoring. The layer 3 virtual network must have already been added to the fabric before association. This field must either be present in all payload elements or none").String, + Optional: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + }, + } +} + +func (r *FabricL2VirtualNetworkResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.client = req.ProviderData.(*CcProviderData).Client +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin create +func (r *FabricL2VirtualNetworkResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan FabricL2VirtualNetwork + + // 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.Id.ValueString())) + + // Create object + body := plan.toBody(ctx, FabricL2VirtualNetwork{}) + + params := "" + res, err := r.client.Post(plan.getPath()+params, body) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", "POST", err, res.String())) + return + } + params = "" + params += "?fabricId=" + url.QueryEscape(plan.FabricId.ValueString()) + "&vlanName=" + url.QueryEscape(plan.VlanName.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("response.0.id").String()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end create + +// Section below is generated&owned by "gen/generator.go". //template:begin read +func (r *FabricL2VirtualNetworkResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state FabricL2VirtualNetwork + + // 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.String())) + + params := "" + params += "?fabricId=" + url.QueryEscape(state.FabricId.ValueString()) + "&vlanName=" + url.QueryEscape(state.VlanName.ValueString()) + res, err := r.client.Get(state.getPath() + params) + if err != nil && strings.Contains(err.Error(), "StatusCode 404") { + resp.State.RemoveResource(ctx) + return + } else if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (GET), got error: %s, %s", err, res.String())) + return + } + + // If every attribute is set to null we are dealing with an import operation and therefore reading all attributes + if state.isNull(ctx, res) { + state.fromBody(ctx, res) + } else { + state.updateFromBody(ctx, res) + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Id.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read + +// Section below is generated&owned by "gen/generator.go". //template:begin update +func (r *FabricL2VirtualNetworkResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state FabricL2VirtualNetwork + + // 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())) + + body := plan.toBody(ctx, state) + params := "" + res, err := r.client.Put(plan.getPath()+params, body) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PUT), got error: %s, %s", err, res.String())) + 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 *FabricL2VirtualNetworkResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state FabricL2VirtualNetwork + + // 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())) + res, err := r.client.Delete(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString())) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String())) + 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 *FabricL2VirtualNetworkResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + idParts := strings.Split(req.ID, ",") + + if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + resp.Diagnostics.AddError( + "Unexpected Import Identifier", + fmt.Sprintf("Expected import identifier with format: ,. Got: %q", req.ID), + ) + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("fabric_id"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("vlan_name"), idParts[1])...) +} + +// End of section. //template:end import diff --git a/internal/provider/resource_catalystcenter_fabric_l2_virtual_network_test.go b/internal/provider/resource_catalystcenter_fabric_l2_virtual_network_test.go new file mode 100644 index 0000000..db143e4 --- /dev/null +++ b/internal/provider/resource_catalystcenter_fabric_l2_virtual_network_test.go @@ -0,0 +1,92 @@ +// 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 + +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 testAcc +func TestAccCcFabricL2VirtualNetwork(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_fabric_l2_virtual_network.test", "vlan_name", "VLAN401")) + checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_fabric_l2_virtual_network.test", "vlan_id", "401")) + checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_fabric_l2_virtual_network.test", "traffic_type", "DATA")) + checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_fabric_l2_virtual_network.test", "fabric_enabled_wireless", "false")) + + var steps []resource.TestStep + steps = append(steps, resource.TestStep{ + Config: testAccCcFabricL2VirtualNetworkPrerequisitesConfig + testAccCcFabricL2VirtualNetworkConfig_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: steps, + }) +} + +// End of section. //template:end testAcc + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccCcFabricL2VirtualNetworkPrerequisitesConfig = ` +resource "catalystcenter_area" "test" { + name = "Area1" + parent_name = "Global" +} +resource "catalystcenter_fabric_site" "test" { + site_id = catalystcenter_area.test.id + pub_sub_enabled = false + authentication_profile_name = "No Authentication" +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigMinimal +func testAccCcFabricL2VirtualNetworkConfig_minimum() string { + config := `resource "catalystcenter_fabric_l2_virtual_network" "test" {` + "\n" + config += ` fabric_id = catalystcenter_fabric_site.test.id` + "\n" + config += ` vlan_name = "VLAN401"` + "\n" + config += ` traffic_type = "DATA"` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigMinimal + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigAll +func testAccCcFabricL2VirtualNetworkConfig_all() string { + config := `resource "catalystcenter_fabric_l2_virtual_network" "test" {` + "\n" + config += ` fabric_id = catalystcenter_fabric_site.test.id` + "\n" + config += ` vlan_name = "VLAN401"` + "\n" + config += ` vlan_id = 401` + "\n" + config += ` traffic_type = "DATA"` + "\n" + config += ` fabric_enabled_wireless = false` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigAll diff --git a/internal/provider/resource_catalystcenter_fabric_l3_handoff_ip_transit.go b/internal/provider/resource_catalystcenter_fabric_l3_handoff_ip_transit.go index 0587c19..f12433a 100644 --- a/internal/provider/resource_catalystcenter_fabric_l3_handoff_ip_transit.go +++ b/internal/provider/resource_catalystcenter_fabric_l3_handoff_ip_transit.go @@ -170,7 +170,6 @@ func (r *FabricL3HandoffIPTransitResource) Configure(_ context.Context, req reso // End of section. //template:end model -// Section below is generated&owned by "gen/generator.go". //template:begin create func (r *FabricL3HandoffIPTransitResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { var plan FabricL3HandoffIPTransit @@ -189,8 +188,14 @@ func (r *FabricL3HandoffIPTransitResource) Create(ctx context.Context, req resou params := "" res, err := r.client.Post(plan.getPath()+params, body) if err != nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", "POST", err, res.String())) - return + errorCode := res.Get("response.errorCode").String() + if errorCode == "NCDP10000" { + // Log a warning and continue execution when device is unreachable + resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s).", errorCode)) + } else { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", "POST", err, res.String())) + return + } } params = "" params += "?networkDeviceId=" + url.QueryEscape(plan.NetworkDeviceId.ValueString()) + "&fabricId=" + url.QueryEscape(plan.FabricId.ValueString()) @@ -207,8 +212,6 @@ func (r *FabricL3HandoffIPTransitResource) Create(ctx context.Context, req resou resp.Diagnostics.Append(diags...) } -// End of section. //template:end create - // Section below is generated&owned by "gen/generator.go". //template:begin read func (r *FabricL3HandoffIPTransitResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var state FabricL3HandoffIPTransit diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index 422897b..945044a 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -9,6 +9,8 @@ description: |- ## 0.1.16 (unreleased) +- Add Device Unreachability Warning to `catalystcenter_fabric_l3_handoff_ip_transit` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/150) +- Add `catalystcenter_fabric_l2_virtual_network` resource and data source - Remove `max_async_wait_time` attribute and use timeout from `CC_MAX_TIMEOUT` - Modify `catalystcenter_deploy_template` resource to support deploying composite templates - Modify `catalystcenter_template_version` resource to use versioned template id as `id` and remove data_source