From cb1cb2a3eae3aa6d01eee2902d3391f80b148884 Mon Sep 17 00:00:00 2001 From: Jorge Gomez Velasquez Date: Mon, 18 Dec 2023 10:54:54 -0500 Subject: [PATCH] Add vpc peerlink resource (#196) --- CHANGELOG.md | 1 + docs/data-sources/vpc_peerlink.md | 33 +++ docs/guides/changelog.md | 5 + docs/guides/supported_objects.md | 1 + docs/resources/vpc_keepalive.md | 6 + docs/resources/vpc_peerlink.md | 51 +++++ .../nxos_vpc_peerlink/data-source.tf | 2 + .../resources/nxos_vpc_peerlink/import.sh | 1 + .../resources/nxos_vpc_peerlink/resource.tf | 3 + gen/definitions/vpc_keepalive.yaml | 4 +- gen/definitions/vpc_peerlink.yaml | 43 ++++ .../provider/data_source_nxos_vpc_peerlink.go | 108 ++++++++++ .../data_source_nxos_vpc_peerlink_test.go | 100 +++++++++ internal/provider/model_nxos_vpc_peerlink.go | 59 ++++++ internal/provider/provider.go | 2 + .../provider/resource_nxos_vpc_keepalive.go | 2 +- .../provider/resource_nxos_vpc_peerlink.go | 196 ++++++++++++++++++ .../resource_nxos_vpc_peerlink_test.go | 111 ++++++++++ templates/guides/changelog.md.tmpl | 5 + templates/guides/supported_objects.md.tmpl | 1 + 20 files changed, 732 insertions(+), 2 deletions(-) create mode 100644 docs/data-sources/vpc_peerlink.md create mode 100644 docs/resources/vpc_peerlink.md create mode 100644 examples/data-sources/nxos_vpc_peerlink/data-source.tf create mode 100644 examples/resources/nxos_vpc_peerlink/import.sh create mode 100644 examples/resources/nxos_vpc_peerlink/resource.tf create mode 100644 gen/definitions/vpc_peerlink.yaml create mode 100644 internal/provider/data_source_nxos_vpc_peerlink.go create mode 100644 internal/provider/data_source_nxos_vpc_peerlink_test.go create mode 100644 internal/provider/model_nxos_vpc_peerlink.go create mode 100644 internal/provider/resource_nxos_vpc_peerlink.go create mode 100644 internal/provider/resource_nxos_vpc_peerlink_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 989cc439..fc318189 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.5.2 (unreleased) - Add `nxos_vpc_keepalive` resource and data source +- Add `nxos_vpc_peerlink` resource and data source ## 0.5.1 diff --git a/docs/data-sources/vpc_peerlink.md b/docs/data-sources/vpc_peerlink.md new file mode 100644 index 00000000..368e2a5b --- /dev/null +++ b/docs/data-sources/vpc_peerlink.md @@ -0,0 +1,33 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "nxos_vpc_peerlink Data Source - terraform-provider-nxos" +subcategory: "vPC" +description: |- + This data source can read the vPC peerlink configuration. + API Documentation: vpcPeerLink https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:PeerLink/ +--- + +# nxos_vpc_peerlink (Data Source) + +This data source can read the vPC peerlink configuration. + +- API Documentation: [vpcPeerLink](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:PeerLink/) + +## Example Usage + +```terraform +data "nxos_vpc_peerlink" "example" { +} +``` + + +## Schema + +### Optional + +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The distinguished name of the object. +- `port_channel_id` (String) vPC Peerlink port-channel id. diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index e3d21e3f..28c13dfd 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -7,6 +7,11 @@ description: |- # Changelog +## 0.5.2 (unreleased) + +- Add `nxos_vpc_keepalive` resource and data source +- Add `nxos_vpc_peerlink` resource and data source + ## 0.5.1 - Add `nxos_bgp_advertised_prefix` resource and data source diff --git a/docs/guides/supported_objects.md b/docs/guides/supported_objects.md index 18f0f6c5..8886b242 100644 --- a/docs/guides/supported_objects.md +++ b/docs/guides/supported_objects.md @@ -133,6 +133,7 @@ For the following DME objects a corresponding Terraform resource and data source | [vpcInst](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:Inst/) | [nxos_vpc_instance](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_instance) | [nxos_vpc_instance](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_instance) | | [vpcIf](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:If/) | [nxos_vpc_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_interface) | [nxos_vpc_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_interface) | | [vpcKeepalive](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:Keepalive/) | [nxos_vpc_keepalive](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_keepalive) | [nxos_vpc_keepalive](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_keepalive) | +| [vpcPeerLink](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:PeerLink/) | [nxos_vpc_peerlink](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_peerlink) | [nxos_vpc_peerlink](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_peerlink) | | [l3Inst](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Layer%203/l3:Inst/) | [nxos_vrf](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vrf) | [nxos_vrf](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vrf) | | [rtctrlDomAf](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtctrl:DomAf/) | [nxos_vrf_address_family](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vrf_address_family) | [nxos_vrf_address_family](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vrf_address_family) | | [rtctrlRttEntry](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtctrl:RttEntry/) | [nxos_vrf_route_target](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vrf_route_target) | [nxos_vrf_route_target](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vrf_route_target) | diff --git a/docs/resources/vpc_keepalive.md b/docs/resources/vpc_keepalive.md index 45fe8ebc..5bb15e50 100644 --- a/docs/resources/vpc_keepalive.md +++ b/docs/resources/vpc_keepalive.md @@ -7,6 +7,8 @@ description: |- API Documentation: vpcKeepalive https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:Keepalive/ Parent resources nxosvpcdomain https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_domain + Child resources + nxosvpcpeerlink https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_peerlink --- # nxos_vpc_keepalive (Resource) @@ -19,6 +21,10 @@ This resource can manage the vPC keepalive configuration. - [nxos_vpc_domain](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_domain) +### Child resources + +- [nxos_vpc_peerlink](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_peerlink) + ## Example Usage ```terraform diff --git a/docs/resources/vpc_peerlink.md b/docs/resources/vpc_peerlink.md new file mode 100644 index 00000000..fa0e5a9d --- /dev/null +++ b/docs/resources/vpc_peerlink.md @@ -0,0 +1,51 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "nxos_vpc_peerlink Resource - terraform-provider-nxos" +subcategory: "vPC" +description: |- + This resource can manage the vPC peerlink configuration. + API Documentation: vpcPeerLink https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:PeerLink/ + Parent resources + nxosvpckeepalive https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_keepalive +--- + +# nxos_vpc_peerlink (Resource) + +This resource can manage the vPC peerlink configuration. + +- API Documentation: [vpcPeerLink](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:PeerLink/) + +### Parent resources + +- [nxos_vpc_keepalive](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_keepalive) + +## Example Usage + +```terraform +resource "nxos_vpc_peerlink" "example" { + port_channel_id = "po1" +} +``` + + +## Schema + +### Required + +- `port_channel_id` (String) vPC Peerlink port-channel id. + +### Optional + +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The distinguished name of the object. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import nxos_vpc_peerlink.example "sys/vpc/inst/dom/keepalive/peerlink" +``` diff --git a/examples/data-sources/nxos_vpc_peerlink/data-source.tf b/examples/data-sources/nxos_vpc_peerlink/data-source.tf new file mode 100644 index 00000000..58913833 --- /dev/null +++ b/examples/data-sources/nxos_vpc_peerlink/data-source.tf @@ -0,0 +1,2 @@ +data "nxos_vpc_peerlink" "example" { +} diff --git a/examples/resources/nxos_vpc_peerlink/import.sh b/examples/resources/nxos_vpc_peerlink/import.sh new file mode 100644 index 00000000..d23ed5b0 --- /dev/null +++ b/examples/resources/nxos_vpc_peerlink/import.sh @@ -0,0 +1 @@ +terraform import nxos_vpc_peerlink.example "sys/vpc/inst/dom/keepalive/peerlink" diff --git a/examples/resources/nxos_vpc_peerlink/resource.tf b/examples/resources/nxos_vpc_peerlink/resource.tf new file mode 100644 index 00000000..300a8552 --- /dev/null +++ b/examples/resources/nxos_vpc_peerlink/resource.tf @@ -0,0 +1,3 @@ +resource "nxos_vpc_peerlink" "example" { + port_channel_id = "po1" +} diff --git a/gen/definitions/vpc_keepalive.yaml b/gen/definitions/vpc_keepalive.yaml index f9ca7cfe..2c0e699e 100644 --- a/gen/definitions/vpc_keepalive.yaml +++ b/gen/definitions/vpc_keepalive.yaml @@ -8,6 +8,8 @@ doc_path: System/vpc:Keepalive/ doc_category: vPC parents: - vPC Domain +children: + - vPC Peerlink attributes: - nxos_name: destIp tf_name: destination_ip @@ -118,4 +120,4 @@ test_prerequisites: dependencies: [0] - dn: sys/vpc/inst/dom class_name: vpcDom - dependencies: [1] \ No newline at end of file + dependencies: [1] diff --git a/gen/definitions/vpc_peerlink.yaml b/gen/definitions/vpc_peerlink.yaml new file mode 100644 index 00000000..df101ae4 --- /dev/null +++ b/gen/definitions/vpc_peerlink.yaml @@ -0,0 +1,43 @@ +--- +name: vPC Peerlink +class_name: vpcPeerLink +dn: sys/vpc/inst/dom/keepalive/peerlink +ds_description: This data source can read the vPC peerlink configuration. +res_description: This resource can manage the vPC peerlink configuration. +doc_path: System/vpc:PeerLink/ +doc_category: vPC +parents: + - vPC Keepalive +attributes: + - nxos_name: id + tf_name: port_channel_id + type: String + mandatory: true + description: "vPC Peerlink port-channel id." + example: "po1" +test_prerequisites: + - dn: sys/fm/vpc + class_name: fmVpc + no_delete: true + attributes: + - name: adminSt + value: enabled + - dn: sys/vpc/inst + class_name: vpcInst + dependencies: [0] + - dn: sys/vpc/inst/dom + class_name: vpcDom + dependencies: [1] + - dn: sys/vpc/inst/dom/keepalive + class_name: vpcKeepalive + dependencies: [2] + - dn: sys/fm/lacp + class_name: fmLacp + no_delete: true + attributes: + - adminSt: enabled + - dn: sys/intf/aggr-[123] + class_name: pcAggrIf + dependencies: [4] + attributes: + - id: po1 \ No newline at end of file diff --git a/internal/provider/data_source_nxos_vpc_peerlink.go b/internal/provider/data_source_nxos_vpc_peerlink.go new file mode 100644 index 00000000..b2d4820e --- /dev/null +++ b/internal/provider/data_source_nxos_vpc_peerlink.go @@ -0,0 +1,108 @@ +// 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 + +import ( + "context" + "fmt" + + "github.com/CiscoDevNet/terraform-provider-nxos/internal/provider/helpers" + "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" + "github.com/netascode/go-nxos" +) + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &VPCPeerlinkDataSource{} + _ datasource.DataSourceWithConfigure = &VPCPeerlinkDataSource{} +) + +func NewVPCPeerlinkDataSource() datasource.DataSource { + return &VPCPeerlinkDataSource{} +} + +type VPCPeerlinkDataSource struct { + clients map[string]*nxos.Client +} + +func (d *VPCPeerlinkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_vpc_peerlink" +} + +func (d *VPCPeerlinkDataSource) 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: helpers.NewResourceDescription("This data source can read the vPC peerlink configuration.", "vpcPeerLink", "System/vpc:PeerLink/").String, + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The distinguished name of the object.", + Computed: true, + }, + "port_channel_id": schema.StringAttribute{ + MarkdownDescription: "vPC Peerlink port-channel id.", + Computed: true, + }, + }, + } +} + +func (d *VPCPeerlinkDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.clients = req.ProviderData.(map[string]*nxos.Client) +} + +func (d *VPCPeerlinkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config VPCPeerlink + + // 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.getDn())) + + queries := []func(*nxos.Req){} + res, err := d.clients[config.Device.ValueString()].GetDn(config.getDn(), queries...) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err)) + return + } + + config.fromBody(res, true) + config.Dn = types.StringValue(config.getDn()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.getDn())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} diff --git a/internal/provider/data_source_nxos_vpc_peerlink_test.go b/internal/provider/data_source_nxos_vpc_peerlink_test.go new file mode 100644 index 00000000..2d484d39 --- /dev/null +++ b/internal/provider/data_source_nxos_vpc_peerlink_test.go @@ -0,0 +1,100 @@ +// 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 + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccDataSourceNxosVPCPeerlink(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceNxosVPCPeerlinkPrerequisitesConfig + testAccDataSourceNxosVPCPeerlinkConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.nxos_vpc_peerlink.test", "port_channel_id", "po1"), + ), + }, + }, + }) +} + +const testAccDataSourceNxosVPCPeerlinkPrerequisitesConfig = ` +resource "nxos_rest" "PreReq0" { + dn = "sys/fm/vpc" + class_name = "fmVpc" + delete = false + content = { + adminSt = "enabled" + } +} + +resource "nxos_rest" "PreReq1" { + dn = "sys/vpc/inst" + class_name = "vpcInst" + depends_on = [nxos_rest.PreReq0, ] +} + +resource "nxos_rest" "PreReq2" { + dn = "sys/vpc/inst/dom" + class_name = "vpcDom" + depends_on = [nxos_rest.PreReq1, ] +} + +resource "nxos_rest" "PreReq3" { + dn = "sys/vpc/inst/dom/keepalive" + class_name = "vpcKeepalive" + depends_on = [nxos_rest.PreReq2, ] +} + +resource "nxos_rest" "PreReq4" { + dn = "sys/fm/lacp" + class_name = "fmLacp" + content = { + = "" + } +} + +resource "nxos_rest" "PreReq5" { + dn = "sys/intf/aggr-[123]" + class_name = "pcAggrIf" + content = { + = "" + } + depends_on = [nxos_rest.PreReq4, ] +} + +` + +const testAccDataSourceNxosVPCPeerlinkConfig = ` + +resource "nxos_vpc_peerlink" "test" { + port_channel_id = "po1" + depends_on = [nxos_rest.PreReq0, nxos_rest.PreReq1, nxos_rest.PreReq2, nxos_rest.PreReq3, nxos_rest.PreReq4, nxos_rest.PreReq5, ] +} + +data "nxos_vpc_peerlink" "test" { + depends_on = [nxos_vpc_peerlink.test] +} +` diff --git a/internal/provider/model_nxos_vpc_peerlink.go b/internal/provider/model_nxos_vpc_peerlink.go new file mode 100644 index 00000000..ffe941a4 --- /dev/null +++ b/internal/provider/model_nxos_vpc_peerlink.go @@ -0,0 +1,59 @@ +// 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 + +import ( + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/netascode/go-nxos" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +type VPCPeerlink struct { + Device types.String `tfsdk:"device"` + Dn types.String `tfsdk:"id"` + PortChannelId types.String `tfsdk:"port_channel_id"` +} + +func (data VPCPeerlink) getDn() string { + return "sys/vpc/inst/dom/keepalive/peerlink" +} + +func (data VPCPeerlink) getClassName() string { + return "vpcPeerLink" +} + +func (data VPCPeerlink) toBody() nxos.Body { + body := "" + body, _ = sjson.Set(body, data.getClassName()+".attributes", map[string]interface{}{}) + if (!data.PortChannelId.IsUnknown() && !data.PortChannelId.IsNull()) || true { + body, _ = sjson.Set(body, data.getClassName()+".attributes."+"id", data.PortChannelId.ValueString()) + } + + return nxos.Body{body} +} + +func (data *VPCPeerlink) fromBody(res gjson.Result, all bool) { + if !data.PortChannelId.IsNull() || all { + data.PortChannelId = types.StringValue(res.Get(data.getClassName() + ".attributes.id").String()) + } else { + data.PortChannelId = types.StringNull() + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index c7e9879c..f9a3e0da 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -393,6 +393,7 @@ func (p *NxosProvider) Resources(ctx context.Context) []func() resource.Resource NewVPCInstanceResource, NewVPCInterfaceResource, NewVPCKeepaliveResource, + NewVPCPeerlinkResource, NewVRFResource, NewVRFAddressFamilyResource, NewVRFRouteTargetResource, @@ -527,6 +528,7 @@ func (p *NxosProvider) DataSources(ctx context.Context) []func() datasource.Data NewVPCInstanceDataSource, NewVPCInterfaceDataSource, NewVPCKeepaliveDataSource, + NewVPCPeerlinkDataSource, NewVRFDataSource, NewVRFAddressFamilyDataSource, NewVRFRouteTargetDataSource, diff --git a/internal/provider/resource_nxos_vpc_keepalive.go b/internal/provider/resource_nxos_vpc_keepalive.go index 4ee085b2..7e82623f 100644 --- a/internal/provider/resource_nxos_vpc_keepalive.go +++ b/internal/provider/resource_nxos_vpc_keepalive.go @@ -55,7 +55,7 @@ func (r *VPCKeepaliveResource) Metadata(ctx context.Context, req resource.Metada func (r *VPCKeepaliveResource) 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.NewResourceDescription("This resource can manage the vPC keepalive configuration.", "vpcKeepalive", "System/vpc:Keepalive/").AddParents("vpc_domain").String, + MarkdownDescription: helpers.NewResourceDescription("This resource can manage the vPC keepalive configuration.", "vpcKeepalive", "System/vpc:Keepalive/").AddParents("vpc_domain").AddChildren("vpc_peerlink").String, Attributes: map[string]schema.Attribute{ "device": schema.StringAttribute{ diff --git a/internal/provider/resource_nxos_vpc_peerlink.go b/internal/provider/resource_nxos_vpc_peerlink.go new file mode 100644 index 00000000..64998bb1 --- /dev/null +++ b/internal/provider/resource_nxos_vpc_peerlink.go @@ -0,0 +1,196 @@ +// 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 + +import ( + "context" + "fmt" + + "github.com/CiscoDevNet/terraform-provider-nxos/internal/provider/helpers" + "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/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-nxos" +) + +// Ensure provider defined types fully satisfy framework interfaces +var _ resource.Resource = &VPCPeerlinkResource{} +var _ resource.ResourceWithImportState = &VPCPeerlinkResource{} + +func NewVPCPeerlinkResource() resource.Resource { + return &VPCPeerlinkResource{} +} + +type VPCPeerlinkResource struct { + clients map[string]*nxos.Client +} + +func (r *VPCPeerlinkResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_vpc_peerlink" +} + +func (r *VPCPeerlinkResource) 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.NewResourceDescription("This resource can manage the vPC peerlink configuration.", "vpcPeerLink", "System/vpc:PeerLink/").AddParents("vpc_keepalive").String, + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The distinguished name of the object.", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "port_channel_id": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("vPC Peerlink port-channel id.").String, + Required: true, + }, + }, + } +} + +func (r *VPCPeerlinkResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + // Prevent panic if the provider has not been configured. + if req.ProviderData == nil { + return + } + + r.clients = req.ProviderData.(map[string]*nxos.Client) +} + +func (r *VPCPeerlinkResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan VPCPeerlink + + // 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.getDn())) + + // Post object + body := plan.toBody() + _, err := r.clients[plan.Device.ValueString()].Post(plan.getDn(), body.Str) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to post object, got error: %s", err)) + return + } + + plan.Dn = types.StringValue(plan.getDn()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.getDn())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +func (r *VPCPeerlinkResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state VPCPeerlink + + // 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.Dn.ValueString())) + + queries := []func(*nxos.Req){nxos.Query("rsp-prop-include", "config-only")} + res, err := r.clients[state.Device.ValueString()].GetDn(state.Dn.ValueString(), queries...) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err)) + return + } + + state.fromBody(res, false) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Dn.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) +} + +func (r *VPCPeerlinkResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan VPCPeerlink + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.getDn())) + + body := plan.toBody() + _, err := r.clients[plan.Device.ValueString()].Post(plan.getDn(), body.Str) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update object, got error: %s", err)) + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.getDn())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +func (r *VPCPeerlinkResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state VPCPeerlink + + // 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.Dn.ValueString())) + + res, err := r.clients[state.Device.ValueString()].DeleteDn(state.Dn.ValueString()) + if err != nil { + errCode := res.Get("imdata.0.error.attributes.code").Str + // Ignore errors of type "Cannot delete object" + if errCode != "1" && errCode != "107" { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update object, got error: %s", err)) + return + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Dn.ValueString())) + + resp.State.RemoveResource(ctx) +} + +func (r *VPCPeerlinkResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +} diff --git a/internal/provider/resource_nxos_vpc_peerlink_test.go b/internal/provider/resource_nxos_vpc_peerlink_test.go new file mode 100644 index 00000000..d14dc639 --- /dev/null +++ b/internal/provider/resource_nxos_vpc_peerlink_test.go @@ -0,0 +1,111 @@ +// 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 + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccNxosVPCPeerlink(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccNxosVPCPeerlinkPrerequisitesConfig + testAccNxosVPCPeerlinkConfig_all(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("nxos_vpc_peerlink.test", "port_channel_id", "po1"), + ), + }, + { + ResourceName: "nxos_vpc_peerlink.test", + ImportState: true, + ImportStateId: "sys/vpc/inst/dom/keepalive/peerlink", + }, + }, + }) +} + +const testAccNxosVPCPeerlinkPrerequisitesConfig = ` +resource "nxos_rest" "PreReq0" { + dn = "sys/fm/vpc" + class_name = "fmVpc" + delete = false + content = { + adminSt = "enabled" + } +} + +resource "nxos_rest" "PreReq1" { + dn = "sys/vpc/inst" + class_name = "vpcInst" + depends_on = [nxos_rest.PreReq0, ] +} + +resource "nxos_rest" "PreReq2" { + dn = "sys/vpc/inst/dom" + class_name = "vpcDom" + depends_on = [nxos_rest.PreReq1, ] +} + +resource "nxos_rest" "PreReq3" { + dn = "sys/vpc/inst/dom/keepalive" + class_name = "vpcKeepalive" + depends_on = [nxos_rest.PreReq2, ] +} + +resource "nxos_rest" "PreReq4" { + dn = "sys/fm/lacp" + class_name = "fmLacp" + content = { + = "" + } +} + +resource "nxos_rest" "PreReq5" { + dn = "sys/intf/aggr-[123]" + class_name = "pcAggrIf" + content = { + = "" + } + depends_on = [nxos_rest.PreReq4, ] +} + +` + +func testAccNxosVPCPeerlinkConfig_minimum() string { + return ` + resource "nxos_vpc_peerlink" "test" { + port_channel_id = "po1" + depends_on = [nxos_rest.PreReq0, nxos_rest.PreReq1, nxos_rest.PreReq2, nxos_rest.PreReq3, nxos_rest.PreReq4, nxos_rest.PreReq5, ] + } + ` +} + +func testAccNxosVPCPeerlinkConfig_all() string { + return ` + resource "nxos_vpc_peerlink" "test" { + port_channel_id = "po1" + depends_on = [nxos_rest.PreReq0, nxos_rest.PreReq1, nxos_rest.PreReq2, nxos_rest.PreReq3, nxos_rest.PreReq4, nxos_rest.PreReq5, ] + } + ` +} diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index e3d21e3f..28c13dfd 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -7,6 +7,11 @@ description: |- # Changelog +## 0.5.2 (unreleased) + +- Add `nxos_vpc_keepalive` resource and data source +- Add `nxos_vpc_peerlink` resource and data source + ## 0.5.1 - Add `nxos_bgp_advertised_prefix` resource and data source diff --git a/templates/guides/supported_objects.md.tmpl b/templates/guides/supported_objects.md.tmpl index 18f0f6c5..8886b242 100644 --- a/templates/guides/supported_objects.md.tmpl +++ b/templates/guides/supported_objects.md.tmpl @@ -133,6 +133,7 @@ For the following DME objects a corresponding Terraform resource and data source | [vpcInst](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:Inst/) | [nxos_vpc_instance](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_instance) | [nxos_vpc_instance](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_instance) | | [vpcIf](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:If/) | [nxos_vpc_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_interface) | [nxos_vpc_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_interface) | | [vpcKeepalive](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:Keepalive/) | [nxos_vpc_keepalive](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_keepalive) | [nxos_vpc_keepalive](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_keepalive) | +| [vpcPeerLink](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/System/vpc:PeerLink/) | [nxos_vpc_peerlink](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vpc_peerlink) | [nxos_vpc_peerlink](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vpc_peerlink) | | [l3Inst](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Layer%203/l3:Inst/) | [nxos_vrf](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vrf) | [nxos_vrf](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vrf) | | [rtctrlDomAf](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtctrl:DomAf/) | [nxos_vrf_address_family](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vrf_address_family) | [nxos_vrf_address_family](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vrf_address_family) | | [rtctrlRttEntry](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtctrl:RttEntry/) | [nxos_vrf_route_target](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/vrf_route_target) | [nxos_vrf_route_target](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/vrf_route_target) |