diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ae6c484e..34133c20d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add `ip_domain_lookup_nsap`, `ip_domain_lookup_recursive`, and `ip_domain_lookup_vrfs*` attributes to `iosxe_system` resource and data source - Add `iosxe_evpn_ethernet_segment` resource and data source for managing L2VPN EVPN Ethernet Segment configuration - Add `evpn_ethernet_segments` attribute to `iosxe_interface_ethernet` and `iosxe_interface_port_channel` resources and data sources +- Add `iosxe_interface_igmp` resource and data source for managing IGMP configuration on interfaces - Add `ip_ssh_bulk_mode` and `ip_ssh_bulk_mode_window_size` attributes to `iosxe_system` resource and data source - Add `set_ip_next_hop_unchanged` attribute to `iosxe_route_map` resource and data source - Enhance `set_communities` attribute documentation in `iosxe_route_map` to clarify support for well-known BGP community values (internet, local-AS, no-advertise, no-export, gshut) diff --git a/docs/data-sources/interface_igmp.md b/docs/data-sources/interface_igmp.md new file mode 100644 index 000000000..4b1898f37 --- /dev/null +++ b/docs/data-sources/interface_igmp.md @@ -0,0 +1,37 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "iosxe_interface_igmp Data Source - terraform-provider-iosxe" +subcategory: "Multicast" +description: |- + This data source can read the Interface IGMP configuration. +--- + +# iosxe_interface_igmp (Data Source) + +This data source can read the Interface IGMP configuration. + +## Example Usage + +```terraform +data "iosxe_interface_igmp" "example" { + type = "Loopback" + name = "100" +} +``` + + +## Schema + +### Required + +- `name` (String) +- `type` (String) Interface type + +### Optional + +- `device` (String) A device name from the provider configuration. + +### Read-Only + +- `id` (String) The path of the retrieved object. +- `version` (Number) IGMP version diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index 7fa3bd4df..8ac1b7d4e 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -13,6 +13,7 @@ description: |- - Add `ip_domain_lookup_nsap`, `ip_domain_lookup_recursive`, and `ip_domain_lookup_vrfs*` attributes to `iosxe_system` resource and data source - Add `iosxe_evpn_ethernet_segment` resource and data source for managing L2VPN EVPN Ethernet Segment configuration - Add `evpn_ethernet_segments` attribute to `iosxe_interface_ethernet` and `iosxe_interface_port_channel` resources and data sources +- Add `iosxe_interface_igmp` resource and data source for managing IGMP configuration on interfaces - Add `ip_ssh_bulk_mode` and `ip_ssh_bulk_mode_window_size` attributes to `iosxe_system` resource and data source - Add `set_ip_next_hop_unchanged` attribute to `iosxe_route_map` resource and data source - Enhance `set_communities` attribute documentation in `iosxe_route_map` to clarify support for well-known BGP community values (internet, local-AS, no-advertise, no-export, gshut) diff --git a/docs/resources/interface_igmp.md b/docs/resources/interface_igmp.md new file mode 100644 index 000000000..7a19dc747 --- /dev/null +++ b/docs/resources/interface_igmp.md @@ -0,0 +1,51 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "iosxe_interface_igmp Resource - terraform-provider-iosxe" +subcategory: "Multicast" +description: |- + This resource can manage the Interface IGMP configuration. +--- + +# iosxe_interface_igmp (Resource) + +This resource can manage the Interface IGMP configuration. + +## Example Usage + +```terraform +resource "iosxe_interface_igmp" "example" { + type = "Loopback" + name = "100" + version = 3 +} +``` + + +## Schema + +### Required + +- `name` (String) +- `type` (String) Interface type + - Choices: `FastEthernet`, `GigabitEthernet`, `TwoGigabitEthernet`, `FiveGigabitEthernet`, `TenGigabitEthernet`, `TwentyFiveGigE`, `FortyGigabitEthernet`, `HundredGigE`, `Loopback`, `Vlan`, `Port-channel`, `Port-channel-subinterface/Port-channel`, `Tunnel` + +### Optional + +- `device` (String) A device name from the provider configuration. +- `version` (Number) IGMP version + - Range: `1`-`3` + - Default value: `2` + +### Read-Only + +- `id` (String) The path of the object. + +## Import + +Import is supported using the following syntax: + +The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example: + +```shell +terraform import iosxe_interface_igmp.example "," +``` diff --git a/examples/data-sources/iosxe_interface_igmp/data-source.tf b/examples/data-sources/iosxe_interface_igmp/data-source.tf new file mode 100644 index 000000000..90a10321b --- /dev/null +++ b/examples/data-sources/iosxe_interface_igmp/data-source.tf @@ -0,0 +1,4 @@ +data "iosxe_interface_igmp" "example" { + type = "Loopback" + name = "100" +} diff --git a/examples/resources/iosxe_interface_igmp/import.sh b/examples/resources/iosxe_interface_igmp/import.sh new file mode 100644 index 000000000..5273291a3 --- /dev/null +++ b/examples/resources/iosxe_interface_igmp/import.sh @@ -0,0 +1 @@ +terraform import iosxe_interface_igmp.example "," diff --git a/examples/resources/iosxe_interface_igmp/resource.tf b/examples/resources/iosxe_interface_igmp/resource.tf new file mode 100644 index 000000000..3fba8065a --- /dev/null +++ b/examples/resources/iosxe_interface_igmp/resource.tf @@ -0,0 +1,5 @@ +resource "iosxe_interface_igmp" "example" { + type = "Loopback" + name = "100" + version = 3 +} diff --git a/gen/definitions/interface_igmp.yaml b/gen/definitions/interface_igmp.yaml new file mode 100644 index 000000000..e501b1653 --- /dev/null +++ b/gen/definitions/interface_igmp.yaml @@ -0,0 +1,39 @@ +--- +name: Interface IGMP +path: Cisco-IOS-XE-native:native/interface/%s=%v/ip/Cisco-IOS-XE-igmp:igmp +augment_path: Cisco-IOS-XE-native:native/interface/GigabitEthernet=%v/ip/Cisco-IOS-XE-igmp:igmp +no_delete: true +doc_category: Multicast +attributes: + - yang_name: type + tf_name: type + description: Interface type + type: String + reference: true + enum_values: + - FastEthernet + - GigabitEthernet + - TwoGigabitEthernet + - FiveGigabitEthernet + - TenGigabitEthernet + - TwentyFiveGigE + - FortyGigabitEthernet + - HundredGigE + - Loopback + - Vlan + - Port-channel + - Port-channel-subinterface/Port-channel + - Tunnel + example: Loopback + - yang_name: name + example: 100 + - yang_name: version + description: IGMP version + default_value: 2 + example: 3 + +test_prerequisites: + - path: Cisco-IOS-XE-native:native/interface/Loopback=100 + attributes: + - name: name + value: 100 diff --git a/internal/provider/data_source_iosxe_interface_igmp.go b/internal/provider/data_source_iosxe_interface_igmp.go new file mode 100644 index 000000000..8ab83d8f2 --- /dev/null +++ b/internal/provider/data_source_iosxe_interface_igmp.go @@ -0,0 +1,136 @@ +// 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 + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +// 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 = &InterfaceIGMPDataSource{} + _ datasource.DataSourceWithConfigure = &InterfaceIGMPDataSource{} +) + +func NewInterfaceIGMPDataSource() datasource.DataSource { + return &InterfaceIGMPDataSource{} +} + +type InterfaceIGMPDataSource struct { + data *IosxeProviderData +} + +func (d *InterfaceIGMPDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_interface_igmp" +} + +func (d *InterfaceIGMPDataSource) 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 Interface IGMP configuration.", + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The path of the retrieved object.", + Computed: true, + }, + "type": schema.StringAttribute{ + MarkdownDescription: "Interface type", + Required: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "", + Required: true, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: "IGMP version", + Computed: true, + }, + }, + } +} + +func (d *InterfaceIGMPDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.data = req.ProviderData.(*IosxeProviderData) +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin read + +func (d *InterfaceIGMPDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config InterfaceIGMPData + + // 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.getPath())) + + device, ok := d.data.Devices[config.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", config.Device.ValueString())) + return + } + + res, err := device.Client.GetData(config.getPath()) + if res.StatusCode == 404 { + config = InterfaceIGMPData{Device: config.Device} + } else { + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", config.getPath(), err)) + return + } + + config.fromBody(ctx, res.Res) + } + + config.Id = types.StringValue(config.getPath()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.getPath())) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read diff --git a/internal/provider/data_source_iosxe_interface_igmp_test.go b/internal/provider/data_source_iosxe_interface_igmp_test.go new file mode 100644 index 000000000..2e9dec434 --- /dev/null +++ b/internal/provider/data_source_iosxe_interface_igmp_test.go @@ -0,0 +1,83 @@ +// 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 + +// 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 TestAccDataSourceIosxeInterfaceIGMP(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("data.iosxe_interface_igmp.test", "version", "3")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceIosxeInterfaceIGMPPrerequisitesConfig + testAccDataSourceIosxeInterfaceIGMPConfig(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccDataSourceIosxeInterfaceIGMPPrerequisitesConfig = ` +resource "iosxe_restconf" "PreReq0" { + path = "Cisco-IOS-XE-native:native/interface/Loopback=100" + attributes = { + "name" = "100" + } +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig + +func testAccDataSourceIosxeInterfaceIGMPConfig() string { + config := `resource "iosxe_interface_igmp" "test" {` + "\n" + config += ` type = "Loopback"` + "\n" + config += ` name = "100"` + "\n" + config += ` version = 3` + "\n" + config += ` depends_on = [iosxe_restconf.PreReq0, ]` + "\n" + config += `}` + "\n" + + config += ` + data "iosxe_interface_igmp" "test" { + type = "Loopback" + name = "100" + depends_on = [iosxe_interface_igmp.test] + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/model_iosxe_interface_igmp.go b/internal/provider/model_iosxe_interface_igmp.go new file mode 100644 index 000000000..4ab570068 --- /dev/null +++ b/internal/provider/model_iosxe_interface_igmp.go @@ -0,0 +1,170 @@ +// 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 + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "net/url" + "regexp" + "strconv" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin types +type InterfaceIGMP struct { + Device types.String `tfsdk:"device"` + Id types.String `tfsdk:"id"` + Type types.String `tfsdk:"type"` + Name types.String `tfsdk:"name"` + Version types.Int64 `tfsdk:"version"` +} + +type InterfaceIGMPData struct { + Device types.String `tfsdk:"device"` + Id types.String `tfsdk:"id"` + Type types.String `tfsdk:"type"` + Name types.String `tfsdk:"name"` + Version types.Int64 `tfsdk:"version"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath + +func (data InterfaceIGMP) getPath() string { + return fmt.Sprintf("Cisco-IOS-XE-native:native/interface/%s=%v/ip/Cisco-IOS-XE-igmp:igmp", url.QueryEscape(fmt.Sprintf("%v", data.Type.ValueString())), url.QueryEscape(fmt.Sprintf("%v", data.Name.ValueString()))) +} + +func (data InterfaceIGMPData) getPath() string { + return fmt.Sprintf("Cisco-IOS-XE-native:native/interface/%s=%v/ip/Cisco-IOS-XE-igmp:igmp", url.QueryEscape(fmt.Sprintf("%v", data.Type.ValueString())), url.QueryEscape(fmt.Sprintf("%v", data.Name.ValueString()))) +} + +// if last path element has a key -> remove it +func (data InterfaceIGMP) getPathShort() string { + path := data.getPath() + re := regexp.MustCompile(`(.*)=[^\/]*$`) + matches := re.FindStringSubmatch(path) + if len(matches) <= 1 { + return path + } + return matches[1] +} + +// End of section. //template:end getPath + +// Section below is generated&owned by "gen/generator.go". //template:begin toBody + +func (data InterfaceIGMP) toBody(ctx context.Context) string { + body := `{"` + helpers.LastElement(data.getPath()) + `":{}}` + if !data.Version.IsNull() && !data.Version.IsUnknown() { + body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"version", strconv.FormatInt(data.Version.ValueInt64(), 10)) + } + return body +} + +// End of section. //template:end toBody + +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBody + +func (data *InterfaceIGMP) updateFromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } + if value := res.Get(prefix + "version"); value.Exists() && !data.Version.IsNull() { + data.Version = types.Int64Value(value.Int()) + } else { + data.Version = types.Int64Null() + } +} + +// End of section. //template:end updateFromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBody + +func (data *InterfaceIGMP) fromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } + if value := res.Get(prefix + "version"); value.Exists() { + data.Version = types.Int64Value(value.Int()) + } +} + +// End of section. //template:end fromBody + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyData + +func (data *InterfaceIGMPData) fromBody(ctx context.Context, res gjson.Result) { + prefix := helpers.LastElement(data.getPath()) + "." + if res.Get(helpers.LastElement(data.getPath())).IsArray() { + prefix += "0." + } + if value := res.Get(prefix + "version"); value.Exists() { + data.Version = types.Int64Value(value.Int()) + } +} + +// End of section. //template:end fromBodyData + +// Section below is generated&owned by "gen/generator.go". //template:begin getDeletedItems + +func (data *InterfaceIGMP) getDeletedItems(ctx context.Context, state InterfaceIGMP) []string { + deletedItems := make([]string, 0) + if !state.Version.IsNull() && data.Version.IsNull() { + deletedItems = append(deletedItems, fmt.Sprintf("%v/version", state.getPath())) + } + + return deletedItems +} + +// End of section. //template:end getDeletedItems + +// Section below is generated&owned by "gen/generator.go". //template:begin getEmptyLeafsDelete + +func (data *InterfaceIGMP) getEmptyLeafsDelete(ctx context.Context) []string { + emptyLeafsDelete := make([]string, 0) + + return emptyLeafsDelete +} + +// End of section. //template:end getEmptyLeafsDelete + +// Section below is generated&owned by "gen/generator.go". //template:begin getDeletePaths + +func (data *InterfaceIGMP) getDeletePaths(ctx context.Context) []string { + var deletePaths []string + if !data.Version.IsNull() { + deletePaths = append(deletePaths, fmt.Sprintf("%v/version", data.getPath())) + } + + return deletePaths +} + +// End of section. //template:end getDeletePaths diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 8c8ba8a87..93c51936f 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -417,6 +417,7 @@ func (p *IosxeProvider) Resources(ctx context.Context) []func() resource.Resourc NewFlowMonitorResource, NewFlowRecordResource, NewInterfaceEthernetResource, + NewInterfaceIGMPResource, NewInterfaceLoopbackResource, NewInterfaceMPLSResource, NewInterfaceNVEResource, @@ -522,6 +523,7 @@ func (p *IosxeProvider) DataSources(ctx context.Context) []func() datasource.Dat NewFlowMonitorDataSource, NewFlowRecordDataSource, NewInterfaceEthernetDataSource, + NewInterfaceIGMPDataSource, NewInterfaceLoopbackDataSource, NewInterfaceMPLSDataSource, NewInterfaceNVEDataSource, diff --git a/internal/provider/resource_iosxe_interface_igmp.go b/internal/provider/resource_iosxe_interface_igmp.go new file mode 100644 index 000000000..dcc71949c --- /dev/null +++ b/internal/provider/resource_iosxe_interface_igmp.go @@ -0,0 +1,426 @@ +// 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 + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "regexp" + "strings" + + "github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default" + "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-restconf" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure provider defined types fully satisfy framework interfaces +var ( + _ resource.Resource = &InterfaceIGMPResource{} + _ resource.ResourceWithImportState = &InterfaceIGMPResource{} +) + +func NewInterfaceIGMPResource() resource.Resource { + return &InterfaceIGMPResource{} +} + +type InterfaceIGMPResource struct { + data *IosxeProviderData +} + +func (r *InterfaceIGMPResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_interface_igmp" +} + +func (r *InterfaceIGMPResource) 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: "This resource can manage the Interface IGMP configuration.", + + Attributes: map[string]schema.Attribute{ + "device": schema.StringAttribute{ + MarkdownDescription: "A device name from the provider configuration.", + Optional: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "The path of the object.", + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "type": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Interface type").AddStringEnumDescription("FastEthernet", "GigabitEthernet", "TwoGigabitEthernet", "FiveGigabitEthernet", "TenGigabitEthernet", "TwentyFiveGigE", "FortyGigabitEthernet", "HundredGigE", "Loopback", "Vlan", "Port-channel", "Port-channel-subinterface/Port-channel", "Tunnel").String, + Required: true, + Validators: []validator.String{ + stringvalidator.OneOf("FastEthernet", "GigabitEthernet", "TwoGigabitEthernet", "FiveGigabitEthernet", "TenGigabitEthernet", "TwentyFiveGigE", "FortyGigabitEthernet", "HundredGigE", "Loopback", "Vlan", "Port-channel", "Port-channel-subinterface/Port-channel", "Tunnel"), + }, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "name": schema.StringAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("").String, + Required: true, + Validators: []validator.String{ + stringvalidator.RegexMatches(regexp.MustCompile(`(0|[1-9][0-9]*)(/(0|[1-9][0-9]*))*(\.[0-9]*)?`), ""), + }, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "version": schema.Int64Attribute{ + MarkdownDescription: helpers.NewAttributeDescription("IGMP version").AddIntegerRangeDescription(1, 3).AddDefaultValueDescription("2").String, + Optional: true, + Computed: true, + Validators: []validator.Int64{ + int64validator.Between(1, 3), + }, + Default: int64default.StaticInt64(2), + }, + }, + } +} + +func (r *InterfaceIGMPResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.data = req.ProviderData.(*IosxeProviderData) +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin create + +func (r *InterfaceIGMPResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan InterfaceIGMP + + // 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.getPath())) + + device, ok := r.data.Devices[plan.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", plan.Device.ValueString())) + return + } + + if device.Managed { + // Create object + body := plan.toBody(ctx) + + emptyLeafsDelete := plan.getEmptyLeafsDelete(ctx) + tflog.Debug(ctx, fmt.Sprintf("List of empty leafs to delete: %+v", emptyLeafsDelete)) + + if YangPatch { + edits := []restconf.YangPatchEdit{restconf.NewYangPatchEdit("merge", plan.getPath(), restconf.Body{Str: body})} + for _, i := range emptyLeafsDelete { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.Client.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object, got error: %s", err)) + return + } + } else { + res, err := device.Client.PatchData(plan.getPathShort(), body) + if len(res.Errors.Error) > 0 && res.Errors.Error[0].ErrorMessage == "patch to a nonexistent resource" { + _, err = device.Client.PutData(plan.getPath(), body) + } + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PATCH, %s), got error: %s", plan.getPathShort(), err)) + return + } + for _, i := range emptyLeafsDelete { + res, err := device.Client.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + } + } + + plan.Id = types.StringValue(plan.getPath()) + + tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.getPath())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end create + +// Section below is generated&owned by "gen/generator.go". //template:begin read + +func (r *InterfaceIGMPResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state InterfaceIGMP + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", state.Id.ValueString())) + + device, ok := r.data.Devices[state.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", state.Device.ValueString())) + return + } + + if device.Managed { + res, err := device.Client.GetData(state.Id.ValueString()) + if res.StatusCode == 404 { + state = InterfaceIGMP{Device: state.Device, Id: state.Id} + } else { + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", state.Id.ValueString(), err)) + return + } + + imp, diags := helpers.IsFlagImporting(ctx, req) + if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() { + return + } + + // After `terraform import` we switch to a full read. + if imp { + state.fromBody(ctx, res.Res) + } else { + state.updateFromBody(ctx, res.Res) + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", state.Id.ValueString())) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end read + +// Section below is generated&owned by "gen/generator.go". //template:begin update + +func (r *InterfaceIGMPResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state InterfaceIGMP + + // Read plan + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + // Read state + diags = req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) + + device, ok := r.data.Devices[plan.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", plan.Device.ValueString())) + return + } + + if device.Managed { + body := plan.toBody(ctx) + + deletedItems := plan.getDeletedItems(ctx, state) + tflog.Debug(ctx, fmt.Sprintf("Removed items to delete: %+v", deletedItems)) + + emptyLeafsDelete := plan.getEmptyLeafsDelete(ctx) + tflog.Debug(ctx, fmt.Sprintf("List of empty leafs to delete: %+v", emptyLeafsDelete)) + + if YangPatch { + var edits []restconf.YangPatchEdit + for _, i := range deletedItems { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + edits = append(edits, restconf.NewYangPatchEdit("merge", plan.getPath(), restconf.Body{Str: body})) + for _, i := range emptyLeafsDelete { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.Client.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update object, got error: %s", err)) + return + } + } else { + for _, i := range deletedItems { + res, err := device.Client.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + res, err := device.Client.PatchData(plan.getPathShort(), body) + if len(res.Errors.Error) > 0 && res.Errors.Error[0].ErrorMessage == "patch to a nonexistent resource" { + _, err = device.Client.PutData(plan.getPath(), body) + } + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PATCH, %s), got error: %s", plan.getPathShort(), err)) + return + } + for _, i := range emptyLeafsDelete { + res, err := device.Client.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + return + } + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString())) + + diags = resp.State.Set(ctx, &plan) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end update + +// Section below is generated&owned by "gen/generator.go". //template:begin delete + +func (r *InterfaceIGMPResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state InterfaceIGMP + + // Read state + diags := req.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString())) + + device, ok := r.data.Devices[state.Device.ValueString()] + if !ok { + resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", state.Device.ValueString())) + return + } + + if device.Managed { + deleteMode := "attributes" + + if deleteMode == "all" { + res, err := device.Client.DeleteData(state.Id.ValueString()) + if err != nil && res.StatusCode != 404 && res.StatusCode != 400 { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s", state.Id.ValueString(), err)) + return + } + } else { + deletePaths := state.getDeletePaths(ctx) + tflog.Debug(ctx, fmt.Sprintf("Paths to delete: %+v", deletePaths)) + + if YangPatch { + edits := []restconf.YangPatchEdit{} + for _, i := range deletePaths { + edits = append(edits, restconf.NewYangPatchEdit("remove", i, restconf.Body{})) + } + _, err := device.Client.YangPatchData("", "1", "", edits) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object, got error: %s", err)) + return + } + } else { + for _, i := range deletePaths { + res, err := device.Client.DeleteData(i) + if err != nil && res.StatusCode != 404 { + resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Failed to delete object (%s), got error: %s", i, err)) + } + } + } + } + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString())) + + resp.State.RemoveResource(ctx) +} + +// End of section. //template:end delete + +// Section below is generated&owned by "gen/generator.go". //template:begin import + +func (r *InterfaceIGMPResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + idParts := strings.Split(req.ID, ",") + idParts = helpers.RemoveEmptyStrings(idParts) + + if len(idParts) != 2 && len(idParts) != 3 { + expectedIdentifier := "Expected import identifier with format: ','" + expectedIdentifier += " or ',,'" + resp.Diagnostics.AddError( + "Unexpected Import Identifier", + fmt.Sprintf("%s. Got: %q", expectedIdentifier, req.ID), + ) + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("type"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[1])...) + if len(idParts) == 3 { + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("device"), idParts[len(idParts)-1])...) + } + + // construct path for 'id' attribute + var state InterfaceIGMP + diags := resp.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), state.getPath())...) + + helpers.SetFlagImporting(ctx, true, resp.Private, &resp.Diagnostics) +} + +// End of section. //template:end import diff --git a/internal/provider/resource_iosxe_interface_igmp_test.go b/internal/provider/resource_iosxe_interface_igmp_test.go new file mode 100644 index 000000000..6ab056b1b --- /dev/null +++ b/internal/provider/resource_iosxe_interface_igmp_test.go @@ -0,0 +1,115 @@ +// 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 + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAcc + +func TestAccIosxeInterfaceIGMP(t *testing.T) { + var checks []resource.TestCheckFunc + checks = append(checks, resource.TestCheckResourceAttr("iosxe_interface_igmp.test", "version", "3")) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccIosxeInterfaceIGMPPrerequisitesConfig + testAccIosxeInterfaceIGMPConfig_minimum(), + }, + { + Config: testAccIosxeInterfaceIGMPPrerequisitesConfig + testAccIosxeInterfaceIGMPConfig_all(), + Check: resource.ComposeTestCheckFunc(checks...), + }, + { + ResourceName: "iosxe_interface_igmp.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: iosxeInterfaceIGMPImportStateIdFunc("iosxe_interface_igmp.test"), + ImportStateVerifyIgnore: []string{}, + Check: resource.ComposeTestCheckFunc(checks...), + }, + }, + }) +} + +// End of section. //template:end testAcc + +// Section below is generated&owned by "gen/generator.go". //template:begin importStateIdFunc + +func iosxeInterfaceIGMPImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + primary := s.RootModule().Resources[resourceName].Primary + Type := primary.Attributes["type"] + Name := primary.Attributes["name"] + + return fmt.Sprintf("%s,%s", Type, Name), nil + } +} + +// End of section. //template:end importStateIdFunc + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites +const testAccIosxeInterfaceIGMPPrerequisitesConfig = ` +resource "iosxe_restconf" "PreReq0" { + path = "Cisco-IOS-XE-native:native/interface/Loopback=100" + attributes = { + "name" = "100" + } +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigMinimal + +func testAccIosxeInterfaceIGMPConfig_minimum() string { + config := `resource "iosxe_interface_igmp" "test" {` + "\n" + config += ` type = "Loopback"` + "\n" + config += ` name = "100"` + "\n" + config += ` depends_on = [iosxe_restconf.PreReq0, ]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigMinimal + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccConfigAll + +func testAccIosxeInterfaceIGMPConfig_all() string { + config := `resource "iosxe_interface_igmp" "test" {` + "\n" + config += ` type = "Loopback"` + "\n" + config += ` name = "100"` + "\n" + config += ` version = 3` + "\n" + config += ` depends_on = [iosxe_restconf.PreReq0, ]` + "\n" + config += `}` + "\n" + return config +} + +// End of section. //template:end testAccConfigAll diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index 7fa3bd4df..8ac1b7d4e 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -13,6 +13,7 @@ description: |- - Add `ip_domain_lookup_nsap`, `ip_domain_lookup_recursive`, and `ip_domain_lookup_vrfs*` attributes to `iosxe_system` resource and data source - Add `iosxe_evpn_ethernet_segment` resource and data source for managing L2VPN EVPN Ethernet Segment configuration - Add `evpn_ethernet_segments` attribute to `iosxe_interface_ethernet` and `iosxe_interface_port_channel` resources and data sources +- Add `iosxe_interface_igmp` resource and data source for managing IGMP configuration on interfaces - Add `ip_ssh_bulk_mode` and `ip_ssh_bulk_mode_window_size` attributes to `iosxe_system` resource and data source - Add `set_ip_next_hop_unchanged` attribute to `iosxe_route_map` resource and data source - Enhance `set_communities` attribute documentation in `iosxe_route_map` to clarify support for well-known BGP community values (internet, local-AS, no-advertise, no-export, gshut)