Skip to content

Commit

Permalink
Add fabric l3 virtual network resource and data source
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Dec 19, 2024
1 parent 24743e9 commit 023203c
Show file tree
Hide file tree
Showing 15 changed files with 848 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.18 (unreleased)

- Add `catalystcenter_fabric_l3_virtual_network` resource and data source

## 0.1.17

- BREAKING CHANGE: Rename `auth_key_sae_ext_plus_tf` attribute of `catalystcenter_wireless_ssid` resource to `auth_key_sae_ext_plus_ft`
Expand Down
34 changes: 34 additions & 0 deletions docs/data-sources/fabric_l3_virtual_network.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "catalystcenter_fabric_l3_virtual_network Data Source - terraform-provider-catalystcenter"
subcategory: "SDA"
description: |-
This data source can read the Fabric L3 Virtual Network.
---

# catalystcenter_fabric_l3_virtual_network (Data Source)

This data source can read the Fabric L3 Virtual Network.

## Example Usage

```terraform
data "catalystcenter_fabric_l3_virtual_network" "example" {
virtual_network_name = "MyL3VN"
fabric_ids = ["5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"]
anchored_site_id = ""
}
```

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

### Required

- `anchored_site_id` (String) Fabric ID of the fabric site this layer 3 virtual network is to be anchored at.
- `fabric_ids` (Set of String) IDs of the fabrics this layer 3 virtual network is to be assigned to.
- `virtual_network_name` (String) Name of the layer 3 virtual network.

### Read-Only

- `id` (String) The id of the object
4 changes: 4 additions & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ description: |-

# Changelog

## 0.1.18 (unreleased)

- Add `catalystcenter_fabric_l3_virtual_network` resource and data source

## 0.1.17

- BREAKING CHANGE: Rename `auth_key_sae_ext_plus_tf` attribute of `catalystcenter_wireless_ssid` resource to `auth_key_sae_ext_plus_ft`
Expand Down
44 changes: 44 additions & 0 deletions docs/resources/fabric_l3_virtual_network.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "catalystcenter_fabric_l3_virtual_network Resource - terraform-provider-catalystcenter"
subcategory: "SDA"
description: |-
This resource can manage a Fabric L3 Virtual Network.
---

# catalystcenter_fabric_l3_virtual_network (Resource)

This resource can manage a Fabric L3 Virtual Network.

## Example Usage

```terraform
resource "catalystcenter_fabric_l3_virtual_network" "example" {
virtual_network_name = "MyL3VN"
fabric_ids = ["5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"]
}
```

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

### Required

- `virtual_network_name` (String) Name of the layer 3 virtual network.

### Optional

- `anchored_site_id` (String) Fabric ID of the fabric site this layer 3 virtual network is to be anchored at.
- `fabric_ids` (Set of String) IDs of the fabrics this layer 3 virtual network is to be assigned to.

### Read-Only

- `id` (String) The id of the object

## Import

Import is supported using the following syntax:

```shell
terraform import catalystcenter_fabric_l3_virtual_network.example "<virtual_network_name>,<fabric_ids>,<anchored_site_id>"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "catalystcenter_fabric_l3_virtual_network" "example" {
virtual_network_name = "MyL3VN"
fabric_ids = ["5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"]
anchored_site_id = ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import catalystcenter_fabric_l3_virtual_network.example "<virtual_network_name>,<fabric_ids>,<anchored_site_id>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "catalystcenter_fabric_l3_virtual_network" "example" {
virtual_network_name = "MyL3VN"
fabric_ids = ["5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"]
}
50 changes: 50 additions & 0 deletions gen/definitions/fabric_l3_virtual_network.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
name: Fabric L3 Virtual Network
rest_endpoint: /dna/intent/api/v1/sda/layer3VirtualNetworks
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: virtualNetworkName
requires_replace: true
data_path: "0"
query_param: true
response_data_path: response.0.virtualNetworkName
type: String
mandatory: true
description: Name of the layer 3 virtual network.
example: MyL3VN
- model_name: fabricIds
requires_replace: true
data_path: "0"
query_param: true
response_data_path: response.0.fabricIds
description: IDs of the fabrics this layer 3 virtual network is to be assigned to.
type: Set
element_type: String
example: 5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1
test_value: "[catalystcenter_fabric_site.test.id]"
- model_name: anchoredSiteId
requires_replace: true
data_path: "0"
query_param: true
response_data_path: response.0.anchoredSiteId
description: Fabric ID of the fabric site this layer 3 virtual network is to be anchored at.
type: String
example: ""
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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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-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 the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &FabricL3VirtualNetworkDataSource{}
_ datasource.DataSourceWithConfigure = &FabricL3VirtualNetworkDataSource{}
)

func NewFabricL3VirtualNetworkDataSource() datasource.DataSource {
return &FabricL3VirtualNetworkDataSource{}
}

type FabricL3VirtualNetworkDataSource struct {
client *cc.Client
}

func (d *FabricL3VirtualNetworkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_fabric_l3_virtual_network"
}

func (d *FabricL3VirtualNetworkDataSource) 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 L3 Virtual Network.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The id of the object",
Computed: true,
},
"virtual_network_name": schema.StringAttribute{
MarkdownDescription: "Name of the layer 3 virtual network.",
Required: true,
},
"fabric_ids": schema.SetAttribute{
MarkdownDescription: "IDs of the fabrics this layer 3 virtual network is to be assigned to.",
ElementType: types.StringType,
Required: true,
},
"anchored_site_id": schema.StringAttribute{
MarkdownDescription: "Fabric ID of the fabric site this layer 3 virtual network is to be anchored at.",
Required: true,
},
},
}
}

func (d *FabricL3VirtualNetworkDataSource) 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

func (d *FabricL3VirtualNetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config FabricL3VirtualNetwork

// 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()))

var fabricIds []string
config.FabricIds.ElementsAs(ctx, &fabricIds, false)
params := ""
params += "?virtualNetworkName=" + url.QueryEscape(config.VirtualNetworkName.ValueString())
if len(fabricIds) > 0 {
params += "&fabricIds=" + url.QueryEscape(fabricIds[0])
}
if !config.AnchoredSiteId.IsNull() && config.AnchoredSiteId.ValueString() != "" {
params += "&anchoredSiteId=" + url.QueryEscape(config.AnchoredSiteId.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...)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 TestAccDataSourceCcFabricL3VirtualNetwork(t *testing.T) {
var checks []resource.TestCheckFunc
checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_fabric_l3_virtual_network.test", "virtual_network_name", "MyL3VN"))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCcFabricL3VirtualNetworkPrerequisitesConfig + testAccDataSourceCcFabricL3VirtualNetworkConfig(),
Check: resource.ComposeTestCheckFunc(checks...),
},
},
})
}

// End of section. //template:end testAccDataSource

// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites
const testAccDataSourceCcFabricL3VirtualNetworkPrerequisitesConfig = `
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 testAccDataSourceCcFabricL3VirtualNetworkConfig() string {
config := `resource "catalystcenter_fabric_l3_virtual_network" "test" {` + "\n"
config += ` virtual_network_name = "MyL3VN"` + "\n"
config += ` fabric_ids = [catalystcenter_fabric_site.test.id]` + "\n"
config += `}` + "\n"

config += `
data "catalystcenter_fabric_l3_virtual_network" "test" {
virtual_network_name = "MyL3VN"
fabric_ids = [catalystcenter_fabric_site.test.id]
anchored_site_id = ""
depends_on = [catalystcenter_fabric_l3_virtual_network.test]
}
`
return config
}

// End of section. //template:end testAccDataSourceConfig
Loading

0 comments on commit 023203c

Please sign in to comment.