Skip to content

Commit

Permalink
Add fabric l2 handoff resource and data source (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-mazurkiewicz authored Aug 8, 2024
1 parent 2cf53db commit d0f0efa
Show file tree
Hide file tree
Showing 16 changed files with 927 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.1.10 (unreleased)

- Add `fabric_l2_handoff` resource and data source
- BREAKING CHANGE: Modified `wireless_profile` resource to use `/intent/api/v1/wirelessProfiles` API endpoint, this resource now only works with Catalyst Center version 2.3.7.6+
- Add `wireless_device_provision` resource
- Add `fabric_provision_device` resource and data source
Expand Down
36 changes: 36 additions & 0 deletions docs/data-sources/fabric_l2_handoff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "catalystcenter_fabric_l2_handoff Data Source - terraform-provider-catalystcenter"
subcategory: "SDA"
description: |-
This data source can read the Fabric L2 Handoff.
---

# catalystcenter_fabric_l2_handoff (Data Source)

This data source can read the Fabric L2 Handoff.

## Example Usage

```terraform
data "catalystcenter_fabric_l2_handoff" "example" {
id = "76d24097-41c4-4558-a4d0-a8c07ac08470"
network_device_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"
fabric_id = "c4b85bb2-ce3f-4db9-a32b-e439a388ac2f"
}
```

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

### Required

- `fabric_id` (String) ID of the fabric this device belongs to
- `id` (String) The id of the object
- `network_device_id` (String) Network device ID of the fabric device

### Read-Only

- `external_vlan_id` (Number) External VLAN number into which the fabric must be extended. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)
- `interface_name` (String) Interface name of the layer 2 handoff. E.g., GigabitEthernet1/0/4
- `internal_vlan_id` (Number) VLAN number associated with this fabric. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)
1 change: 1 addition & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description: |-

## 0.1.10 (unreleased)

- Add `fabric_l2_handoff` resource and data source
- BREAKING CHANGE: Modified `wireless_profile` resource to use `/intent/api/v1/wirelessProfiles` API endpoint, this resource now only works with Catalyst Center version 2.3.7.6+
- Add `wireless_device_provision` resource
- Add `fabric_provision_device` resource and data source
Expand Down
46 changes: 46 additions & 0 deletions docs/resources/fabric_l2_handoff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "catalystcenter_fabric_l2_handoff Resource - terraform-provider-catalystcenter"
subcategory: "SDA"
description: |-
Manages Layer 2 Handoffs in Fabric Devices
---

# catalystcenter_fabric_l2_handoff (Resource)

Manages Layer 2 Handoffs in Fabric Devices

## Example Usage

```terraform
resource "catalystcenter_fabric_l2_handoff" "example" {
network_device_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"
fabric_id = "c4b85bb2-ce3f-4db9-a32b-e439a388ac2f"
interface_name = "GigabitEthernet1/0/4"
internal_vlan_id = 300
external_vlan_id = 400
}
```

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

### Required

- `external_vlan_id` (Number) External VLAN number into which the fabric must be extended. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)
- `fabric_id` (String) ID of the fabric this device belongs to
- `interface_name` (String) Interface name of the layer 2 handoff. E.g., GigabitEthernet1/0/4
- `internal_vlan_id` (Number) VLAN number associated with this fabric. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)
- `network_device_id` (String) Network device ID of the fabric device

### Read-Only

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

## Import

Import is supported using the following syntax:

```shell
terraform import catalystcenter_fabric_l2_handoff.example "<network_device_id>,<fabric_id>,<id>"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "catalystcenter_fabric_l2_handoff" "example" {
id = "76d24097-41c4-4558-a4d0-a8c07ac08470"
network_device_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"
fabric_id = "c4b85bb2-ce3f-4db9-a32b-e439a388ac2f"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import catalystcenter_fabric_l2_handoff.example "<network_device_id>,<fabric_id>,<id>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "catalystcenter_fabric_l2_handoff" "example" {
network_device_id = "5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1"
fabric_id = "c4b85bb2-ce3f-4db9-a32b-e439a388ac2f"
interface_name = "GigabitEthernet1/0/4"
internal_vlan_id = 300
external_vlan_id = 400
}
67 changes: 67 additions & 0 deletions gen/definitions/fabric_l2_handoff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
name: Fabric L2 Handoff
rest_endpoint: /dna/intent/api/v1/sda/fabricDevices/layer2Handoffs
res_description: Manages Layer 2 Handoffs in Fabric Devices
id_from_query_path: response
id_from_query_path_attribute: id
put_id_include_path: 0.id
get_from_all: true
put_no_id: true
doc_category: SDA
test_tags: [SDA]
attributes:
- model_name: networkDeviceId
query_param: true
requires_replace: true
data_path: '0'
response_data_path: networkDeviceId
mandatory: true
description: Network device ID of the fabric device
type: String
example: 5e6f7b3a-2b0b-4a7d-8b1c-0d4b1cd5e1b1
- model_name: fabricId
requires_replace: true
query_param: true
data_path: '0'
response_data_path: fabricId
type: String
mandatory: true
description: ID of the fabric this device belongs to
example: c4b85bb2-ce3f-4db9-a32b-e439a388ac2f
test_value: catalystcenter_fabric_site.test.id
- model_name: interfaceName
requires_replace: true
data_path: '0'
response_data_path: interfaceName
type: String
mandatory: true
description: Interface name of the layer 2 handoff. E.g., GigabitEthernet1/0/4
example: GigabitEthernet1/0/4
- model_name: internalVlanId
data_path: '0'
requires_replace: true
response_data_path: internalVlanId
type: Int64
match_id: true
mandatory: true
description: VLAN number associated with this fabric. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)
example: 300
- model_name: externalVlanId
data_path: '0'
requires_replace: true
response_data_path: externalVlanId
mandatory: true
type: Int64
description: External VLAN number into which the fabric must be extended. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)
example: 400
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"
depends_on = [catalystcenter_area.test]
}
2 changes: 1 addition & 1 deletion gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C
{{- if and .IdFromQueryPathAttribute .IdFromQueryPath (not .GetExtraQueryParams) (not .GetFromAll) }}
plan.Id = types.StringValue(res.Get("{{if eq .IdFromQueryPath "." }}{{else}}{{.IdFromQueryPath}}.{{end}}{{.IdFromQueryPathAttribute}}").String())
{{- else}}
plan.Id = types.StringValue(res.Get("{{.IdFromQueryPath}}.#({{if $id.ResponseModelName}}{{$id.ResponseModelName}}{{else}}{{$id.ModelName}}{{end}}==\""+ plan.{{toGoName $id.TfName}}.Value{{$id.Type}}() +"\").{{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}").String())
plan.Id = types.StringValue(res.Get("{{.IdFromQueryPath}}.#({{if $id.ResponseModelName}}{{$id.ResponseModelName}}{{else}}{{$id.ModelName}}{{end}}==\""+ {{if eq $id.Type "Int64"}}strconv.FormatInt(plan.{{toGoName $id.TfName}}.ValueInt64(), 10){{else}}plan.{{toGoName $id.TfName}}.Value{{$id.Type}}(){{end}} +"\").{{if .IdFromQueryPathAttribute}}{{.IdFromQueryPathAttribute}}{{else}}id{{end}}").String())
{{- end}}
{{- /* If we have an id attribute we will use that as id */}}
{{- else if hasId .Attributes}}
Expand Down
128 changes: 128 additions & 0 deletions internal/provider/data_source_catalystcenter_fabric_l2_handoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// 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 = &FabricL2HandoffDataSource{}
_ datasource.DataSourceWithConfigure = &FabricL2HandoffDataSource{}
)

func NewFabricL2HandoffDataSource() datasource.DataSource {
return &FabricL2HandoffDataSource{}
}

type FabricL2HandoffDataSource struct {
client *cc.Client
}

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

func (d *FabricL2HandoffDataSource) 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 Handoff.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The id of the object",
Required: true,
},
"network_device_id": schema.StringAttribute{
MarkdownDescription: "Network device ID of the fabric device",
Required: true,
},
"fabric_id": schema.StringAttribute{
MarkdownDescription: "ID of the fabric this device belongs to",
Required: true,
},
"interface_name": schema.StringAttribute{
MarkdownDescription: "Interface name of the layer 2 handoff. E.g., GigabitEthernet1/0/4",
Computed: true,
},
"internal_vlan_id": schema.Int64Attribute{
MarkdownDescription: "VLAN number associated with this fabric. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)",
Computed: true,
},
"external_vlan_id": schema.Int64Attribute{
MarkdownDescription: "External VLAN number into which the fabric must be extended. Allowed VLAN range is 2-4094 except for reserved vlans (1, 1002-1005, 2046, 4094)",
Computed: true,
},
},
}
}

func (d *FabricL2HandoffDataSource) 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 *FabricL2HandoffDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config FabricL2Handoff

// 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 += "?networkDeviceId=" + url.QueryEscape(config.NetworkDeviceId.ValueString()) + "&fabricId=" + url.QueryEscape(config.FabricId.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
}
res = res.Get("response.#(id==\"" + config.Id.ValueString() + "\")")

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
Loading

0 comments on commit d0f0efa

Please sign in to comment.