From 61cbcb2834e1bccb0284429fbb64bb6ca48c32f5 Mon Sep 17 00:00:00 2001 From: Chris Archibald Date: Mon, 14 Oct 2024 15:06:36 -0700 Subject: [PATCH] add old name support for network --- .../network_ip_interface_data_source.go | 9 ++ .../network_ip_interface_resource.go | 9 ++ ...etwork_ip_interface_resource_alias_test.go | 94 +++++++++++ .../network_ip_interfaces_data_source.go | 9 ++ .../network_ip_route_data_source.go | 9 ++ .../networking/network_ip_route_resource.go | 9 ++ .../network_ip_route_resource_alias_test.go | 148 ++++++++++++++++++ .../network_ip_routes_data_source.go | 9 ++ internal/provider/provider.go | 6 + 9 files changed, 302 insertions(+) create mode 100644 internal/provider/networking/network_ip_interface_resource_alias_test.go create mode 100644 internal/provider/networking/network_ip_route_resource_alias_test.go diff --git a/internal/provider/networking/network_ip_interface_data_source.go b/internal/provider/networking/network_ip_interface_data_source.go index 95c93b3b..ac9460be 100644 --- a/internal/provider/networking/network_ip_interface_data_source.go +++ b/internal/provider/networking/network_ip_interface_data_source.go @@ -27,6 +27,15 @@ func NewIPInterfaceDataSource() datasource.DataSource { } } +// NewIPInterfaceDataSourceAlias is a helper function to simplify the provider implementation. +func NewIPInterfaceDataSourceAlias() datasource.DataSource { + return &IPInterfaceDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "networking_ip_interface_data_source", + }, + } +} + // IPInterfaceDataSource defines the data source implementation. type IPInterfaceDataSource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/networking/network_ip_interface_resource.go b/internal/provider/networking/network_ip_interface_resource.go index 907cef08..5755f342 100644 --- a/internal/provider/networking/network_ip_interface_resource.go +++ b/internal/provider/networking/network_ip_interface_resource.go @@ -32,6 +32,15 @@ func NewIPInterfaceResource() resource.Resource { } } +// NewIPInterfaceResourceAlias is a helper function to simplify the provider implementation. +func NewIPInterfaceResourceAlias() resource.Resource { + return &IPInterfaceResource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "networking_ip_interface_resource", + }, + } +} + // IPInterfaceResource defines the resource implementation. type IPInterfaceResource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/networking/network_ip_interface_resource_alias_test.go b/internal/provider/networking/network_ip_interface_resource_alias_test.go new file mode 100644 index 00000000..797e220c --- /dev/null +++ b/internal/provider/networking/network_ip_interface_resource_alias_test.go @@ -0,0 +1,94 @@ +package networking_test + +import ( + "fmt" + "os" + "regexp" + "testing" + + ntest "github.com/netapp/terraform-provider-netapp-ontap/internal/provider" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccNetworkIpInterfaceResourceAlias(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { ntest.TestAccPreCheck(t) }, + ProtoV6ProviderFactories: ntest.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // non-existant SVM return code 2621462. Must happen before create/read + { + Config: testAccNetworkIPInterfaceResourceConfigAlias("non-existant", "10.10.10.10", "ontap_cluster_1-01"), + ExpectError: regexp.MustCompile("2621462"), + }, + // non-existant home node + { + Config: testAccNetworkIPInterfaceResourceConfigAlias("svm0", "10.10.10.10", "non-existant_home_node"), + ExpectError: regexp.MustCompile("393271"), + }, + // Create and Read + { + Config: testAccNetworkIPInterfaceResourceConfigAlias("svm0", "10.10.10.10", "ontap_cluster_1-01"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_interface_resource.example", "name", "test-interface"), + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_interface_resource.example", "svm_name", "svm0"), + ), + }, + // Update and Read + { + Config: testAccNetworkIPInterfaceResourceConfigAlias("svm0", "10.10.10.20", "ontap_cluster_1-01"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_interface_resource.example", "name", "test-interface"), + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_interface_resource.example", "ip.address", "10.10.10.20"), + ), + }, + // Test importing a resource + { + ResourceName: "netapp-ontap_networking_ip_interface_resource.example", + ImportState: true, + ImportStateId: fmt.Sprintf("%s,%s,%s", "test-interface", "svm0", "cluster4"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_interface_resource.example", "name", "test-interface"), + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_interface_resource.example", "ip.address", "10.10.10.20"), + ), + }, + }, + }) +} + +func testAccNetworkIPInterfaceResourceConfigAlias(svmName, address, homeNode string) string { + host := os.Getenv("TF_ACC_NETAPP_HOST") + admin := os.Getenv("TF_ACC_NETAPP_USER") + password := os.Getenv("TF_ACC_NETAPP_PASS") + if host == "" || admin == "" || password == "" { + fmt.Println("TF_ACC_NETAPP_HOST, TF_ACC_NETAPP_USER, and TF_ACC_NETAPP_PASS must be set for acceptance tests") + os.Exit(1) + } + return fmt.Sprintf(` +provider "netapp-ontap" { + connection_profiles = [ + { + name = "cluster4" + hostname = "%s" + username = "%s" + password = "%s" + validate_certs = false + }, + ] +} + +resource "netapp-ontap_networking_ip_interface_resource" "example" { + cx_profile_name = "cluster4" + name = "test-interface" + svm_name = "%s" + ip = { + address = "%s" + netmask = 18 + } + location = { + home_port = "e0d" + home_node = "%s" + } +} +`, host, admin, password, svmName, address, homeNode) +} diff --git a/internal/provider/networking/network_ip_interfaces_data_source.go b/internal/provider/networking/network_ip_interfaces_data_source.go index 2ad3e291..09eb4402 100644 --- a/internal/provider/networking/network_ip_interfaces_data_source.go +++ b/internal/provider/networking/network_ip_interfaces_data_source.go @@ -27,6 +27,15 @@ func NewIPInterfacesDataSource() datasource.DataSource { } } +// NewIPInterfacesDataSourceAlias is a helper function to simplify the provider implementation. +func NewIPInterfacesDataSourceAlias() datasource.DataSource { + return &IPInterfacesDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "networking_ip_interfaces_data_source", + }, + } +} + // IPInterfacesDataSource defines the data source implementation. type IPInterfacesDataSource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/networking/network_ip_route_data_source.go b/internal/provider/networking/network_ip_route_data_source.go index 9e1ea554..0968d68a 100644 --- a/internal/provider/networking/network_ip_route_data_source.go +++ b/internal/provider/networking/network_ip_route_data_source.go @@ -26,6 +26,15 @@ func NewIPRouteDataSource() datasource.DataSource { } } +// NewIPRouteDataSourceAlias is a helper function to simplify the provider implementation. +func NewIPRouteDataSourceAlias() datasource.DataSource { + return &IPRouteDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "networking_ip_route_data_source", + }, + } +} + // IPRouteDataSource defines the data source implementation. type IPRouteDataSource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/networking/network_ip_route_resource.go b/internal/provider/networking/network_ip_route_resource.go index 3e4da86e..99b18196 100644 --- a/internal/provider/networking/network_ip_route_resource.go +++ b/internal/provider/networking/network_ip_route_resource.go @@ -38,6 +38,15 @@ func NewIPRouteResource() resource.Resource { } } +// NewIPRouteResourceAlias is a helper function to simplify the provider implementation. +func NewIPRouteResourceAlias() resource.Resource { + return &IPRouteResource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "networking_ip_route_resource", + }, + } +} + // IPRouteResource defines the resource implementation. type IPRouteResource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/networking/network_ip_route_resource_alias_test.go b/internal/provider/networking/network_ip_route_resource_alias_test.go new file mode 100644 index 00000000..0c82b5d2 --- /dev/null +++ b/internal/provider/networking/network_ip_route_resource_alias_test.go @@ -0,0 +1,148 @@ +package networking_test + +import ( + "fmt" + "os" + "regexp" + "testing" + + ntest "github.com/netapp/terraform-provider-netapp-ontap/internal/provider" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccNetworkIpRouteResourceAlias(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { ntest.TestAccPreCheck(t) }, + ProtoV6ProviderFactories: ntest.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Missing Required argument + { + Config: testAccNetworkIPIRouteResourceConfigAliasMissingVars("non-existent"), + ExpectError: regexp.MustCompile("Missing required argument"), + }, + // Non existent SVM + { + Config: testAccNetworkIPIRouteResourceConfigAlias("non-existent"), + ExpectError: regexp.MustCompile("2621462"), + }, + // Test create with no gateway + { + Config: testAccNetworkIPIRouteResourceConfigAlias("ansibleSVM"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_route_resource.example", "svm_name", "ansibleSVM"), + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_route_resource.example", "destination.address", "0.0.0.0"), + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_route_resource.example", "destination.netmask", "0"), + ), + }, + // test create with a gateway + { + Config: testAccNetworkIPIRouteResourceWithGatewayConfigAlias("ansibleSVM", "10.10.10.254", 20), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_route_resource.example", "svm_name", "ansibleSVM"), + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_route_resource.example", "destination.address", "10.10.10.254"), + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_route_resource.example", "destination.netmask", "20"), + ), + }, + // Import and read + { + ResourceName: "netapp-ontap_networking_ip_route_resource.example", + ImportState: true, + ImportStateId: fmt.Sprintf("%s,%s,%s", "carchi-test", "10.10.10.254", "cluster4"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_networking_ip_route_resource.example", "svm_name", "carchi-test"), + ), + }, + }, + }) +} + +func testAccNetworkIPIRouteResourceConfigAlias(svmName string) string { + host := os.Getenv("TF_ACC_NETAPP_HOST") + admin := os.Getenv("TF_ACC_NETAPP_USER") + password := os.Getenv("TF_ACC_NETAPP_PASS") + if host == "" || admin == "" || password == "" { + fmt.Println("TF_ACC_NETAPP_HOST, TF_ACC_NETAPP_USER, and TF_ACC_NETAPP_PASS must be set for acceptance tests") + os.Exit(1) + } + return fmt.Sprintf(` +provider "netapp-ontap" { + connection_profiles = [ + { + name = "cluster4" + hostname = "%s" + username = "%s" + password = "%s" + validate_certs = false + }, + ] +} + +resource "netapp-ontap_networking_ip_route_resource" "example" { + cx_profile_name = "cluster4" + svm_name = "%s" + gateway = "10.10.10.1" +} +`, host, admin, password, svmName) +} + +func testAccNetworkIPIRouteResourceWithGatewayConfigAlias(svmName string, address string, netmask int) string { + host := os.Getenv("TF_ACC_NETAPP_HOST") + admin := os.Getenv("TF_ACC_NETAPP_USER") + password := os.Getenv("TF_ACC_NETAPP_PASS") + if host == "" || admin == "" || password == "" { + fmt.Println("TF_ACC_NETAPP_HOST, TF_ACC_NETAPP_USER, and TF_ACC_NETAPP_PASS must be set for acceptance tests") + os.Exit(1) + } + return fmt.Sprintf(` +provider "netapp-ontap" { + connection_profiles = [ + { + name = "cluster4" + hostname = "%s" + username = "%s" + password = "%s" + validate_certs = false + }, + ] +} + +resource "netapp-ontap_networking_ip_route_resource" "example" { + cx_profile_name = "cluster4" + svm_name = "%s" + gateway = "10.10.10.1" + destination = { + address = "%s" + netmask = %d + } +} +`, host, admin, password, svmName, address, netmask) +} + +func testAccNetworkIPIRouteResourceConfigAliasMissingVars(svmName string) string { + host := os.Getenv("TF_ACC_NETAPP_HOST") + admin := os.Getenv("TF_ACC_NETAPP_USER") + password := os.Getenv("TF_ACC_NETAPP_PASS") + if host == "" || admin == "" || password == "" { + fmt.Println("TF_ACC_NETAPP_HOST, TF_ACC_NETAPP_USER, and TF_ACC_NETAPP_PASS must be set for acceptance tests") + os.Exit(1) + } + return fmt.Sprintf(` +provider "netapp-ontap" { + connection_profiles = [ + { + name = "cluster4" + hostname = "%s" + username = "%s" + password = "%s" + validate_certs = false + }, + ] +} + +resource "netapp-ontap_networking_ip_route_resource" "example" { + cx_profile_name = "cluster4" + svm_name = "%s" +} +`, host, admin, password, svmName) +} diff --git a/internal/provider/networking/network_ip_routes_data_source.go b/internal/provider/networking/network_ip_routes_data_source.go index 5f3dcdc4..d8741fc7 100644 --- a/internal/provider/networking/network_ip_routes_data_source.go +++ b/internal/provider/networking/network_ip_routes_data_source.go @@ -26,6 +26,15 @@ func NewIPRoutesDataSource() datasource.DataSource { } } +// NewIPRoutesDataSourceAlias is a helper function to simplify the provider implementation. +func NewIPRoutesDataSourceAlias() datasource.DataSource { + return &IPRoutesDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "networking_ip_routes_data_source", + }, + } +} + // IPRoutesDataSource defines the data source implementation. type IPRoutesDataSource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 708b6da7..224932e2 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -259,6 +259,8 @@ func (p *ONTAPProvider) Resources(ctx context.Context) []func() resource.Resourc cluster.NewClusterScheduleResourceAlias, name_services.NewNameServicesDNSResourceAlias, name_services.NewNameServicesLDAPResourceAlias, + networking.NewIPInterfaceResourceAlias, + networking.NewIPRouteResourceAlias, } } @@ -351,6 +353,10 @@ func (p *ONTAPProvider) DataSources(ctx context.Context) []func() datasource.Dat name_services.NewNameServicesDNSsDataSourceAlias, name_services.NewNameServicesLDAPDataSourceAlias, name_services.NewNameServicesLDAPsDataSourceAlias, + networking.NewIPInterfaceDataSourceAlias, + networking.NewIPInterfacesDataSourceAlias, + networking.NewIPRouteDataSourceAlias, + networking.NewIPRoutesDataSourceAlias, } }