From cf0e08eec649ed69d2b1065ed6dfff78f9536cb5 Mon Sep 17 00:00:00 2001 From: Rafael Muller Date: Tue, 31 Dec 2024 10:04:08 -0500 Subject: [PATCH 1/2] New route map for tags --- .../route_map_rule_entry_match_tag.md | 41 +++ docs/guides/supported_objects.md | 1 + .../route_map_rule_entry_match_tag.md | 63 +++++ .../data-source.tf | 5 + .../import.sh | 1 + .../resource.tf | 5 + .../route_map_rule_entry_match_tag.yaml | 50 ++++ ...rce_nxos_route_map_rule_entry_match_tag.go | 116 +++++++++ ...xos_route_map_rule_entry_match_tag_test.go | 78 ++++++ ...del_nxos_route_map_rule_entry_match_tag.go | 73 ++++++ internal/provider/provider.go | 2 + ...rce_nxos_route_map_rule_entry_match_tag.go | 243 ++++++++++++++++++ ...xos_route_map_rule_entry_match_tag_test.go | 90 +++++++ templates/guides/supported_objects.md.tmpl | 1 + 14 files changed, 769 insertions(+) create mode 100644 docs/data-sources/route_map_rule_entry_match_tag.md create mode 100644 docs/resources/route_map_rule_entry_match_tag.md create mode 100644 examples/data-sources/nxos_route_map_rule_entry_match_tag/data-source.tf create mode 100644 examples/resources/nxos_route_map_rule_entry_match_tag/import.sh create mode 100644 examples/resources/nxos_route_map_rule_entry_match_tag/resource.tf create mode 100644 gen/definitions/route_map_rule_entry_match_tag.yaml create mode 100644 internal/provider/data_source_nxos_route_map_rule_entry_match_tag.go create mode 100644 internal/provider/data_source_nxos_route_map_rule_entry_match_tag_test.go create mode 100644 internal/provider/model_nxos_route_map_rule_entry_match_tag.go create mode 100644 internal/provider/resource_nxos_route_map_rule_entry_match_tag.go create mode 100644 internal/provider/resource_nxos_route_map_rule_entry_match_tag_test.go diff --git a/docs/data-sources/route_map_rule_entry_match_tag.md b/docs/data-sources/route_map_rule_entry_match_tag.md new file mode 100644 index 00000000..30a23bea --- /dev/null +++ b/docs/data-sources/route_map_rule_entry_match_tag.md @@ -0,0 +1,41 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "nxos_route_map_rule_entry_match_tag Data Source - terraform-provider-nxos" +subcategory: "Routing" +description: |- + This data source can read a Match Tag in Route-Map Rule Entry configuration. + API Documentation: rtmapMatchRtTag https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/ +--- + +# nxos_route_map_rule_entry_match_tag (Data Source) + +This data source can read a Match Tag in Route-Map Rule Entry configuration. + +- API Documentation: [rtmapMatchRtTag](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/) + +## Example Usage + +```terraform +data "nxos_route_map_rule_entry_match_tag" "example" { + rule_name = "RULE1" + order = 10 + tag = 12345 +} +``` + + +## Schema + +### Required + +- `order` (Number) Route-Map Rule Entry order. +- `rule_name` (String) Route Map rule name. +- `tag` (Number) Route Map Tag Value + +### Optional + +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The distinguished name of the object. diff --git a/docs/guides/supported_objects.md b/docs/guides/supported_objects.md index ae36e1ab..f30e6fc9 100644 --- a/docs/guides/supported_objects.md +++ b/docs/guides/supported_objects.md @@ -124,6 +124,7 @@ For the following DME objects a corresponding Terraform resource and data source | [rtmapEntry](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:Entry/) | [nxos_route_map_rule_entry](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry) | [nxos_route_map_rule_entry](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry) | | [rtmapMatchRtDst](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtDst/) | [nxos_route_map_rule_entry_match_route](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_route) | [nxos_route_map_rule_entry_match_route](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_match_route) | | [rtmapRsRtDstAtt](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:RsRtDstAtt/) | [nxos_route_map_rule_entry_match_route_prefix_list](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_route_prefix_list) | [nxos_route_map_rule_entry_match_route_prefix_list](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_match_route_prefix_list) | +| [rtmapMatchRtTag](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/) | [nxos_route_map_rule_entry_match_tag](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_tag) | [nxos_route_map_rule_entry_match_tag](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_match_tag) | | [rtmapSetRegComm](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:SetRegComm/) | [nxos_route_map_rule_entry_set_regular_community](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_set_regular_community) | [nxos_route_map_rule_entry_set_regular_community](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_set_regular_community) | | [rtregcomItem](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtregcom:Item/) | [nxos_route_map_rule_entry_set_regular_community_item](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_set_regular_community_item) | [nxos_route_map_rule_entry_set_regular_community_item](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_set_regular_community_item) | | [stpIf](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Discovery%20Protocols/stp:If/) | [nxos_spanning_tree_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/spanning_tree_interface) | [nxos_spanning_tree_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/spanning_tree_interface) | diff --git a/docs/resources/route_map_rule_entry_match_tag.md b/docs/resources/route_map_rule_entry_match_tag.md new file mode 100644 index 00000000..4bcdfd01 --- /dev/null +++ b/docs/resources/route_map_rule_entry_match_tag.md @@ -0,0 +1,63 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "nxos_route_map_rule_entry_match_tag Resource - terraform-provider-nxos" +subcategory: "Routing" +description: |- + This resource can manage a Match Tag in Route-Map Rule Entry configuration. + API Documentation: rtmapMatchRtTag https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/ + Parent resources + nxos_route_map_rule_entry https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry + Child resources + nxos_route_map_rule_entry_match_tag https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_tag +--- + +# nxos_route_map_rule_entry_match_tag (Resource) + +This resource can manage a Match Tag in Route-Map Rule Entry configuration. + +- API Documentation: [rtmapMatchRtTag](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/) + +### Parent resources + +- [nxos_route_map_rule_entry](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry) + +### Child resources + +- [nxos_route_map_rule_entry_match_tag](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_tag) + +## Example Usage + +```terraform +resource "nxos_route_map_rule_entry_match_tag" "example" { + rule_name = "RULE1" + order = 10 + tag = 12345 +} +``` + + +## Schema + +### Required + +- `order` (Number) Route-Map Rule Entry order. + - Range: `0`-`65535` +- `rule_name` (String) Route Map rule name. +- `tag` (Number) Route Map Tag Value + - Range: `0`-`4294967295` + +### 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_route_map_rule_entry_match_tag.example "sys/rpm/rtmap-[RULE1]/ent-[10]/mrttag-[12345]" +``` diff --git a/examples/data-sources/nxos_route_map_rule_entry_match_tag/data-source.tf b/examples/data-sources/nxos_route_map_rule_entry_match_tag/data-source.tf new file mode 100644 index 00000000..8605db20 --- /dev/null +++ b/examples/data-sources/nxos_route_map_rule_entry_match_tag/data-source.tf @@ -0,0 +1,5 @@ +data "nxos_route_map_rule_entry_match_tag" "example" { + rule_name = "RULE1" + order = 10 + tag = 12345 +} diff --git a/examples/resources/nxos_route_map_rule_entry_match_tag/import.sh b/examples/resources/nxos_route_map_rule_entry_match_tag/import.sh new file mode 100644 index 00000000..9c01bf88 --- /dev/null +++ b/examples/resources/nxos_route_map_rule_entry_match_tag/import.sh @@ -0,0 +1 @@ +terraform import nxos_route_map_rule_entry_match_tag.example "sys/rpm/rtmap-[RULE1]/ent-[10]/mrttag-[12345]" diff --git a/examples/resources/nxos_route_map_rule_entry_match_tag/resource.tf b/examples/resources/nxos_route_map_rule_entry_match_tag/resource.tf new file mode 100644 index 00000000..163e025e --- /dev/null +++ b/examples/resources/nxos_route_map_rule_entry_match_tag/resource.tf @@ -0,0 +1,5 @@ +resource "nxos_route_map_rule_entry_match_tag" "example" { + rule_name = "RULE1" + order = 10 + tag = 12345 +} diff --git a/gen/definitions/route_map_rule_entry_match_tag.yaml b/gen/definitions/route_map_rule_entry_match_tag.yaml new file mode 100644 index 00000000..10e1de7b --- /dev/null +++ b/gen/definitions/route_map_rule_entry_match_tag.yaml @@ -0,0 +1,50 @@ +--- +name: Route Map Rule Entry Match Tag +class_name: rtmapMatchRtTag +dn: sys/rpm/rtmap-[%s]/ent-[%v]/mrttag-[%v] +ds_description: This data source can read a Match Tag in Route-Map Rule Entry configuration. +res_description: This resource can manage a Match Tag in Route-Map Rule Entry configuration. +doc_path: Routing%20and%20Forwarding/rtmap:MatchRtTag/ +doc_category: Routing +parents: + - Route Map Rule Entry +children: + - Route Map Rule Entry Match Tag +attributes: + - nxos_name: rtmap + tf_name: rule_name + type: String + id: true + reference_only: true + description: "Route Map rule name." + example: RULE1 + - nxos_name: order + tf_name: order + type: Int64 + description: "Route-Map Rule Entry order." + min_int: 0 + max_int: 65535 + example: 10 + id: true + reference_only: true + - nxos_name: tag + tf_name: tag + type: Int64 + min_int: 0 + max_int: 4294967295 + description: "Route Map Tag Value" + id: true + example: 12345 + mandatory: true +test_prerequisites: + - dn: sys/rpm/rtmap-[RULE1] + class_name: rtmapRule + attributes: + - name: name + value: RULE1 + - dn: sys/rpm/rtmap-[RULE1]/ent-[10] + class_name: rtmapEntry + attributes: + - name: order + value: 10 + dependencies: [0] diff --git a/internal/provider/data_source_nxos_route_map_rule_entry_match_tag.go b/internal/provider/data_source_nxos_route_map_rule_entry_match_tag.go new file mode 100644 index 00000000..d2a42f30 --- /dev/null +++ b/internal/provider/data_source_nxos_route_map_rule_entry_match_tag.go @@ -0,0 +1,116 @@ +// 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 = &RouteMapRuleEntryMatchTagDataSource{} + _ datasource.DataSourceWithConfigure = &RouteMapRuleEntryMatchTagDataSource{} +) + +func NewRouteMapRuleEntryMatchTagDataSource() datasource.DataSource { + return &RouteMapRuleEntryMatchTagDataSource{} +} + +type RouteMapRuleEntryMatchTagDataSource struct { + clients map[string]*nxos.Client +} + +func (d *RouteMapRuleEntryMatchTagDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_route_map_rule_entry_match_tag" +} + +func (d *RouteMapRuleEntryMatchTagDataSource) 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 a Match Tag in Route-Map Rule Entry configuration.", "rtmapMatchRtTag", "Routing%20and%20Forwarding/rtmap:MatchRtTag/").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, + }, + "rule_name": schema.StringAttribute{ + MarkdownDescription: "Route Map rule name.", + Required: true, + }, + "order": schema.Int64Attribute{ + MarkdownDescription: "Route-Map Rule Entry order.", + Required: true, + }, + "tag": schema.Int64Attribute{ + MarkdownDescription: "Route Map Tag Value", + Required: true, + }, + }, + } +} + +func (d *RouteMapRuleEntryMatchTagDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.clients = req.ProviderData.(map[string]*nxos.Client) +} + +func (d *RouteMapRuleEntryMatchTagDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config RouteMapRuleEntryMatchTag + + // 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_route_map_rule_entry_match_tag_test.go b/internal/provider/data_source_nxos_route_map_rule_entry_match_tag_test.go new file mode 100644 index 00000000..f56469e7 --- /dev/null +++ b/internal/provider/data_source_nxos_route_map_rule_entry_match_tag_test.go @@ -0,0 +1,78 @@ +// 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 TestAccDataSourceNxosRouteMapRuleEntryMatchTag(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceNxosRouteMapRuleEntryMatchTagPrerequisitesConfig + testAccDataSourceNxosRouteMapRuleEntryMatchTagConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.nxos_route_map_rule_entry_match_tag.test", "tag", "12345"), + ), + }, + }, + }) +} + +const testAccDataSourceNxosRouteMapRuleEntryMatchTagPrerequisitesConfig = ` +resource "nxos_rest" "PreReq0" { + dn = "sys/rpm/rtmap-[RULE1]" + class_name = "rtmapRule" + content = { + name = "RULE1" + } +} + +resource "nxos_rest" "PreReq1" { + dn = "sys/rpm/rtmap-[RULE1]/ent-[10]" + class_name = "rtmapEntry" + content = { + order = "10" + } + depends_on = [nxos_rest.PreReq0, ] +} + +` + +const testAccDataSourceNxosRouteMapRuleEntryMatchTagConfig = ` + +resource "nxos_route_map_rule_entry_match_tag" "test" { + rule_name = "RULE1" + order = 10 + tag = 12345 + depends_on = [nxos_rest.PreReq0, nxos_rest.PreReq1, ] +} + +data "nxos_route_map_rule_entry_match_tag" "test" { + rule_name = "RULE1" + order = 10 + tag = 12345 + depends_on = [nxos_route_map_rule_entry_match_tag.test] +} +` diff --git a/internal/provider/model_nxos_route_map_rule_entry_match_tag.go b/internal/provider/model_nxos_route_map_rule_entry_match_tag.go new file mode 100644 index 00000000..f20b160c --- /dev/null +++ b/internal/provider/model_nxos_route_map_rule_entry_match_tag.go @@ -0,0 +1,73 @@ +// 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 ( + "fmt" + "strconv" + + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/netascode/go-nxos" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +type RouteMapRuleEntryMatchTag struct { + Device types.String `tfsdk:"device"` + Dn types.String `tfsdk:"id"` + RuleName types.String `tfsdk:"rule_name"` + Order types.Int64 `tfsdk:"order"` + Tag types.Int64 `tfsdk:"tag"` +} + +func (data RouteMapRuleEntryMatchTag) getDn() string { + return fmt.Sprintf("sys/rpm/rtmap-[%s]/ent-[%v]/mrttag-[%v]", data.RuleName.ValueString(), data.Order.ValueInt64(), data.Tag.ValueInt64()) +} + +func (data RouteMapRuleEntryMatchTag) getClassName() string { + return "rtmapMatchRtTag" +} + +func (data RouteMapRuleEntryMatchTag) toBody(statusReplace bool) nxos.Body { + body := "" + body, _ = sjson.Set(body, data.getClassName()+".attributes", map[string]interface{}{}) + if statusReplace { + body, _ = sjson.Set(body, data.getClassName()+".attributes."+"status", "replaced") + } + if (!data.Tag.IsUnknown() && !data.Tag.IsNull()) || true { + body, _ = sjson.Set(body, data.getClassName()+".attributes."+"tag", strconv.FormatInt(data.Tag.ValueInt64(), 10)) + } + + return nxos.Body{body} +} + +func (data *RouteMapRuleEntryMatchTag) fromBody(res gjson.Result, all bool) { + if !data.Tag.IsNull() || all { + data.Tag = types.Int64Value(res.Get(data.getClassName() + ".attributes.tag").Int()) + } else { + data.Tag = types.Int64Null() + } +} + +func (data RouteMapRuleEntryMatchTag) toDeleteBody() nxos.Body { + body := "" + + return nxos.Body{body} +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 3fd8c84d..cf51507e 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -385,6 +385,7 @@ func (p *NxosProvider) Resources(ctx context.Context) []func() resource.Resource NewRouteMapRuleEntryResource, NewRouteMapRuleEntryMatchRouteResource, NewRouteMapRuleEntryMatchRoutePrefixListResource, + NewRouteMapRuleEntryMatchTagResource, NewRouteMapRuleEntrySetRegularCommunityResource, NewRouteMapRuleEntrySetRegularCommunityItemResource, NewSpanningTreeInterfaceResource, @@ -523,6 +524,7 @@ func (p *NxosProvider) DataSources(ctx context.Context) []func() datasource.Data NewRouteMapRuleEntryDataSource, NewRouteMapRuleEntryMatchRouteDataSource, NewRouteMapRuleEntryMatchRoutePrefixListDataSource, + NewRouteMapRuleEntryMatchTagDataSource, NewRouteMapRuleEntrySetRegularCommunityDataSource, NewRouteMapRuleEntrySetRegularCommunityItemDataSource, NewSpanningTreeInterfaceDataSource, diff --git a/internal/provider/resource_nxos_route_map_rule_entry_match_tag.go b/internal/provider/resource_nxos_route_map_rule_entry_match_tag.go new file mode 100644 index 00000000..7357b178 --- /dev/null +++ b/internal/provider/resource_nxos_route_map_rule_entry_match_tag.go @@ -0,0 +1,243 @@ +// 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-validators/int64validator" + "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" + "github.com/netascode/go-nxos" +) + +// Ensure provider defined types fully satisfy framework interfaces +var _ resource.Resource = &RouteMapRuleEntryMatchTagResource{} +var _ resource.ResourceWithImportState = &RouteMapRuleEntryMatchTagResource{} + +func NewRouteMapRuleEntryMatchTagResource() resource.Resource { + return &RouteMapRuleEntryMatchTagResource{} +} + +type RouteMapRuleEntryMatchTagResource struct { + clients map[string]*nxos.Client +} + +func (r *RouteMapRuleEntryMatchTagResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_route_map_rule_entry_match_tag" +} + +func (r *RouteMapRuleEntryMatchTagResource) 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 a Match Tag in Route-Map Rule Entry configuration.", "rtmapMatchRtTag", "Routing%20and%20Forwarding/rtmap:MatchRtTag/").AddParents("route_map_rule_entry").AddChildren("route_map_rule_entry_match_tag").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(), + }, + }, + "rule_name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Route Map rule name.").String, + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "order": schema.Int64Attribute{ + MarkdownDescription: helpers.NewAttributeDescription("Route-Map Rule Entry order.").AddIntegerRangeDescription(0, 65535).String, + Required: true, + Validators: []validator.Int64{ + int64validator.Between(0, 65535), + }, + PlanModifiers: []planmodifier.Int64{ + int64planmodifier.RequiresReplace(), + }, + }, + "tag": schema.Int64Attribute{ + MarkdownDescription: helpers.NewAttributeDescription("Route Map Tag Value").AddIntegerRangeDescription(0, 4294967295).String, + Required: true, + Validators: []validator.Int64{ + int64validator.Between(0, 4294967295), + }, + PlanModifiers: []planmodifier.Int64{ + int64planmodifier.RequiresReplace(), + }, + }, + }, + } +} + +func (r *RouteMapRuleEntryMatchTagResource) 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 *RouteMapRuleEntryMatchTagResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan RouteMapRuleEntryMatchTag + + // 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(false) + _, 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...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +func (r *RouteMapRuleEntryMatchTagResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state RouteMapRuleEntryMatchTag + + // 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 + } + + imp, diags := helpers.IsFlagImporting(ctx, req) + if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() { + return + } + state.fromBody(res, imp) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Dn.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +func (r *RouteMapRuleEntryMatchTagResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan RouteMapRuleEntryMatchTag + + // 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(false) + + _, 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 *RouteMapRuleEntryMatchTagResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state RouteMapRuleEntryMatchTag + + // 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())) + + body := state.toDeleteBody() + + if len(body.Str) > 0 { + _, err := r.clients[state.Device.ValueString()].Post(state.getDn(), body.Str) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update object, got error: %s", err)) + return + } + } else { + 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 delete object, got error: %s", err)) + return + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Dn.ValueString())) + + resp.State.RemoveResource(ctx) +} + +func (r *RouteMapRuleEntryMatchTagResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) + + helpers.SetFlagImporting(ctx, true, resp.Private, &resp.Diagnostics) +} diff --git a/internal/provider/resource_nxos_route_map_rule_entry_match_tag_test.go b/internal/provider/resource_nxos_route_map_rule_entry_match_tag_test.go new file mode 100644 index 00000000..f4e79d0b --- /dev/null +++ b/internal/provider/resource_nxos_route_map_rule_entry_match_tag_test.go @@ -0,0 +1,90 @@ +// 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 TestAccNxosRouteMapRuleEntryMatchTag(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccNxosRouteMapRuleEntryMatchTagPrerequisitesConfig + testAccNxosRouteMapRuleEntryMatchTagConfig_all(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("nxos_route_map_rule_entry_match_tag.test", "rule_name", "RULE1"), + resource.TestCheckResourceAttr("nxos_route_map_rule_entry_match_tag.test", "order", "10"), + resource.TestCheckResourceAttr("nxos_route_map_rule_entry_match_tag.test", "tag", "12345"), + ), + }, + { + ResourceName: "nxos_route_map_rule_entry_match_tag.test", + ImportState: true, + ImportStateId: "sys/rpm/rtmap-[RULE1]/ent-[10]/mrttag-[12345]", + }, + }, + }) +} + +const testAccNxosRouteMapRuleEntryMatchTagPrerequisitesConfig = ` +resource "nxos_rest" "PreReq0" { + dn = "sys/rpm/rtmap-[RULE1]" + class_name = "rtmapRule" + content = { + name = "RULE1" + } +} + +resource "nxos_rest" "PreReq1" { + dn = "sys/rpm/rtmap-[RULE1]/ent-[10]" + class_name = "rtmapEntry" + content = { + order = "10" + } + depends_on = [nxos_rest.PreReq0, ] +} + +` + +func testAccNxosRouteMapRuleEntryMatchTagConfig_minimum() string { + return ` + resource "nxos_route_map_rule_entry_match_tag" "test" { + rule_name = "RULE1" + order = 10 + tag = 12345 + depends_on = [nxos_rest.PreReq0, nxos_rest.PreReq1, ] + } + ` +} + +func testAccNxosRouteMapRuleEntryMatchTagConfig_all() string { + return ` + resource "nxos_route_map_rule_entry_match_tag" "test" { + rule_name = "RULE1" + order = 10 + tag = 12345 + depends_on = [nxos_rest.PreReq0, nxos_rest.PreReq1, ] + } + ` +} diff --git a/templates/guides/supported_objects.md.tmpl b/templates/guides/supported_objects.md.tmpl index ae36e1ab..f30e6fc9 100644 --- a/templates/guides/supported_objects.md.tmpl +++ b/templates/guides/supported_objects.md.tmpl @@ -124,6 +124,7 @@ For the following DME objects a corresponding Terraform resource and data source | [rtmapEntry](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:Entry/) | [nxos_route_map_rule_entry](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry) | [nxos_route_map_rule_entry](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry) | | [rtmapMatchRtDst](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtDst/) | [nxos_route_map_rule_entry_match_route](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_route) | [nxos_route_map_rule_entry_match_route](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_match_route) | | [rtmapRsRtDstAtt](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:RsRtDstAtt/) | [nxos_route_map_rule_entry_match_route_prefix_list](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_route_prefix_list) | [nxos_route_map_rule_entry_match_route_prefix_list](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_match_route_prefix_list) | +| [rtmapMatchRtTag](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/) | [nxos_route_map_rule_entry_match_tag](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_match_tag) | [nxos_route_map_rule_entry_match_tag](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_match_tag) | | [rtmapSetRegComm](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:SetRegComm/) | [nxos_route_map_rule_entry_set_regular_community](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_set_regular_community) | [nxos_route_map_rule_entry_set_regular_community](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_set_regular_community) | | [rtregcomItem](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtregcom:Item/) | [nxos_route_map_rule_entry_set_regular_community_item](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry_set_regular_community_item) | [nxos_route_map_rule_entry_set_regular_community_item](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/route_map_rule_entry_set_regular_community_item) | | [stpIf](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Discovery%20Protocols/stp:If/) | [nxos_spanning_tree_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/spanning_tree_interface) | [nxos_spanning_tree_interface](https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/data-sources/spanning_tree_interface) | From db4a2970ae1ffea055b4c5896bd0126049b88b3d Mon Sep 17 00:00:00 2001 From: Rafael Muller Date: Thu, 9 Jan 2025 08:06:53 -0500 Subject: [PATCH 2/2] Fix space issue --- docs/resources/route_map_rule_entry_match_tag.md | 4 ++-- gen/definitions/route_map_rule_entry_match_tag.yaml | 2 +- .../provider/resource_nxos_route_map_rule_entry_match_tag.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/resources/route_map_rule_entry_match_tag.md b/docs/resources/route_map_rule_entry_match_tag.md index 4bcdfd01..1fa36357 100644 --- a/docs/resources/route_map_rule_entry_match_tag.md +++ b/docs/resources/route_map_rule_entry_match_tag.md @@ -3,7 +3,7 @@ page_title: "nxos_route_map_rule_entry_match_tag Resource - terraform-provider-nxos" subcategory: "Routing" description: |- - This resource can manage a Match Tag in Route-Map Rule Entry configuration. + This resource can manage a Match Tag in Route-Map Rule Entry configuration. API Documentation: rtmapMatchRtTag https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/ Parent resources nxos_route_map_rule_entry https://registry.terraform.io/providers/CiscoDevNet/nxos/latest/docs/resources/route_map_rule_entry @@ -13,7 +13,7 @@ description: |- # nxos_route_map_rule_entry_match_tag (Resource) -This resource can manage a Match Tag in Route-Map Rule Entry configuration. +This resource can manage a Match Tag in Route-Map Rule Entry configuration. - API Documentation: [rtmapMatchRtTag](https://pubhub.devnetcloud.com/media/dme-docs-10-2-2/docs/Routing%20and%20Forwarding/rtmap:MatchRtTag/) diff --git a/gen/definitions/route_map_rule_entry_match_tag.yaml b/gen/definitions/route_map_rule_entry_match_tag.yaml index 10e1de7b..35c6f338 100644 --- a/gen/definitions/route_map_rule_entry_match_tag.yaml +++ b/gen/definitions/route_map_rule_entry_match_tag.yaml @@ -3,7 +3,7 @@ name: Route Map Rule Entry Match Tag class_name: rtmapMatchRtTag dn: sys/rpm/rtmap-[%s]/ent-[%v]/mrttag-[%v] ds_description: This data source can read a Match Tag in Route-Map Rule Entry configuration. -res_description: This resource can manage a Match Tag in Route-Map Rule Entry configuration. +res_description: This resource can manage a Match Tag in Route-Map Rule Entry configuration. doc_path: Routing%20and%20Forwarding/rtmap:MatchRtTag/ doc_category: Routing parents: diff --git a/internal/provider/resource_nxos_route_map_rule_entry_match_tag.go b/internal/provider/resource_nxos_route_map_rule_entry_match_tag.go index 7357b178..392bfe96 100644 --- a/internal/provider/resource_nxos_route_map_rule_entry_match_tag.go +++ b/internal/provider/resource_nxos_route_map_rule_entry_match_tag.go @@ -56,7 +56,7 @@ func (r *RouteMapRuleEntryMatchTagResource) Metadata(ctx context.Context, req re func (r *RouteMapRuleEntryMatchTagResource) 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 a Match Tag in Route-Map Rule Entry configuration.", "rtmapMatchRtTag", "Routing%20and%20Forwarding/rtmap:MatchRtTag/").AddParents("route_map_rule_entry").AddChildren("route_map_rule_entry_match_tag").String, + MarkdownDescription: helpers.NewResourceDescription("This resource can manage a Match Tag in Route-Map Rule Entry configuration.", "rtmapMatchRtTag", "Routing%20and%20Forwarding/rtmap:MatchRtTag/").AddParents("route_map_rule_entry").AddChildren("route_map_rule_entry_match_tag").String, Attributes: map[string]schema.Attribute{ "device": schema.StringAttribute{