From bdd0dfaf27c68e60d6484b581df493d3d76a6348 Mon Sep 17 00:00:00 2001 From: Chris Archibald Date: Mon, 14 Oct 2024 16:42:56 -0700 Subject: [PATCH] Add old name support for SVM --- internal/provider/provider.go | 6 + internal/provider/svm/svm_data_source.go | 10 ++ internal/provider/svm/svm_peer_data_source.go | 10 ++ internal/provider/svm/svm_peer_resource.go | 9 ++ .../svm/svm_peer_resource_alias_test.go | 98 ++++++++++++++ .../provider/svm/svm_peers_data_source.go | 10 ++ internal/provider/svm/svm_resource.go | 12 +- .../provider/svm/svm_resource_alias_test.go | 123 ++++++++++++++++++ internal/provider/svm/svms_data_source.go | 10 ++ 9 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 internal/provider/svm/svm_peer_resource_alias_test.go create mode 100644 internal/provider/svm/svm_resource_alias_test.go diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9660c1de..2da4518a 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -281,6 +281,8 @@ func (p *ONTAPProvider) Resources(ctx context.Context) []func() resource.Resourc storage.NewSnapshotPolicyResourceAlias, storage.NewStorageVolumeResourceAlias, storage.NewStorageVolumeSnapshotResourceAlias, + svm.NewSVMPeerResourceAlias, + svm.NewSvmResourceAlias, } } @@ -417,6 +419,10 @@ func (p *ONTAPProvider) DataSources(ctx context.Context) []func() datasource.Dat storage.NewStorageVolumeSnapshotDataSourceAlias, storage.NewStorageVolumeSnapshotsDataSourceAlias, storage.NewStorageVolumesDataSourceAlias, + svm.NewSvmDataSourceAlias, + svm.NewSVMPeerDataSourceAlias, + svm.NewSVMPeersDataSourceAlias, + svm.NewSvmsDataSourceAlias, } } diff --git a/internal/provider/svm/svm_data_source.go b/internal/provider/svm/svm_data_source.go index a7c4eba9..9302d7d1 100644 --- a/internal/provider/svm/svm_data_source.go +++ b/internal/provider/svm/svm_data_source.go @@ -3,6 +3,7 @@ package svm import ( "context" "fmt" + "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -25,6 +26,15 @@ func NewSvmDataSource() datasource.DataSource { } } +// NewSvmDataSourceAlias is a helper function to simplify the provider implementation. +func NewSvmDataSourceAlias() datasource.DataSource { + return &SvmDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "svm_data_source", + }, + } +} + // SvmDataSource defines the data source implementation. type SvmDataSource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/svm/svm_peer_data_source.go b/internal/provider/svm/svm_peer_data_source.go index 7f6e2a9e..a1d5cc58 100644 --- a/internal/provider/svm/svm_peer_data_source.go +++ b/internal/provider/svm/svm_peer_data_source.go @@ -3,6 +3,7 @@ package svm import ( "context" "fmt" + "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection" "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/snapmirror" @@ -26,6 +27,15 @@ func NewSVMPeerDataSource() datasource.DataSource { } } +// NewSVMPeerDataSourceAlias is a helper function to simplify the provider implementation. +func NewSVMPeerDataSourceAlias() datasource.DataSource { + return &SVMPeerDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "svm_peer_data_source", + }, + } +} + // SVMPeerDataSource defines the data source implementation. type SVMPeerDataSource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/svm/svm_peer_resource.go b/internal/provider/svm/svm_peer_resource.go index ac8e821e..7e80ecb5 100644 --- a/internal/provider/svm/svm_peer_resource.go +++ b/internal/provider/svm/svm_peer_resource.go @@ -33,6 +33,15 @@ func NewSVMPeerResource() resource.Resource { } } +// NewSVMPeerResourceAlias is a helper function to simplify the provider implementation. +func NewSVMPeerResourceAlias() resource.Resource { + return &SVMPeersResource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "svm_peers_resource", + }, + } +} + // SVMPeersResource defines the resource implementation. type SVMPeersResource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/svm/svm_peer_resource_alias_test.go b/internal/provider/svm/svm_peer_resource_alias_test.go new file mode 100644 index 00000000..f0d26d74 --- /dev/null +++ b/internal/provider/svm/svm_peer_resource_alias_test.go @@ -0,0 +1,98 @@ +package svm_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 TestAccSvmPeerResourceAlias(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { ntest.TestAccPreCheck(t) }, + ProtoV6ProviderFactories: ntest.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Test cluster peer non existant to do svm peer + { + Config: testAccSvmPeerResourceConfigAlias("testme", "testme2", "abcd", "snapmirror"), + ExpectError: regexp.MustCompile("9895941"), + }, + // Testing in VSIM is failing to peer + // // Create svm peer and read + // { + // Config: testAccSvmPeersResourceConfig("acc_test_peer2", "acc_test2", "swenjuncluster-1", "snapmirror"), + // Check: resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr("netapp-ontap_svm_peers_resources.example", "svm.name", "acc_test_peer2"), + // ), + // }, + // // Update applications + // { + // Config: testAccSvmPeersResourceConfig("acc_test_peer2", "acc_test2", "swenjuncluster-1", "flexcache"), + // Check: resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr("netapp-ontap_svm_peers_resources.example", "applications.0", "flexcache"), + // resource.TestCheckResourceAttr("netapp-ontap_svm_peers_resources.example", "svm.name", "acc_test_peer2"), + // ), + // }, + // Import and read + { + ResourceName: "netapp-ontap_svm_peers_resource.example", + ImportState: true, + ImportStateId: fmt.Sprintf("%s,%s,%s,%s", "terraform", "tf_peer", "swenjuncluster-1", "cluster4"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_svm_peers_resource.example", "svm.name", "snapmirror_dest_dp"), + ), + }, + }, + }) +} +func testAccSvmPeerResourceConfigAlias(svm, peerSvm, peerCluster, applications string) string { + host := os.Getenv("TF_ACC_NETAPP_HOST5") + admin := os.Getenv("TF_ACC_NETAPP_USER") + password := os.Getenv("TF_ACC_NETAPP_PASS") + password2 := os.Getenv("TF_ACC_NETAPP_PASS2") + host2 := os.Getenv("TF_ACC_NETAPP_HOST2") + if host == "" || admin == "" || password == "" { + fmt.Println("TF_ACC_NETAPP_HOST2, TF_ACC_NETAPP_HOST5, TF_ACC_NETAPP_USER, TF_ACC_NETAPP_PASS2 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 + }, + { + name = "cluster3" + hostname = "%s" + username = "%s" + password = "%s" + validate_certs = false + }, + ] +} + +resource "netapp-ontap_svm_peers_resource" "example" { + cx_profile_name = "cluster4" + svm = { + name = "%s" + } + peer = { + svm = { + name = "%s" + } + cluster = { + name = "%s" + } + peer_cx_profile_name = "cluster3" + } + applications = ["%s"] +}`, host, admin, password2, host2, admin, password2, svm, peerSvm, peerCluster, applications) +} diff --git a/internal/provider/svm/svm_peers_data_source.go b/internal/provider/svm/svm_peers_data_source.go index a6dfd593..7945241a 100644 --- a/internal/provider/svm/svm_peers_data_source.go +++ b/internal/provider/svm/svm_peers_data_source.go @@ -3,6 +3,7 @@ package svm import ( "context" "fmt" + "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection" "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/snapmirror" @@ -26,6 +27,15 @@ func NewSVMPeersDataSource() datasource.DataSource { } } +// NewSVMPeersDataSourceAlias is a helper function to simplify the provider implementation. +func NewSVMPeersDataSourceAlias() datasource.DataSource { + return &SVMPeersDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "svm_peers_data_source", + }, + } +} + // SVMPeersDataSource defines the data source implementation. type SVMPeersDataSource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/svm/svm_resource.go b/internal/provider/svm/svm_resource.go index 994ef3ab..f651431e 100644 --- a/internal/provider/svm/svm_resource.go +++ b/internal/provider/svm/svm_resource.go @@ -3,9 +3,10 @@ package svm import ( "context" "fmt" - "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection" "strings" + "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -30,6 +31,15 @@ func NewSvmResource() resource.Resource { } } +// NewSvmResourceAlias is a helper function to simplify the provider implementation. +func NewSvmResourceAlias() resource.Resource { + return &SvmResource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "svm_resource", + }, + } +} + // SvmResource defines the resource implementation. type SvmResource struct { config connection.ResourceOrDataSourceConfig diff --git a/internal/provider/svm/svm_resource_alias_test.go b/internal/provider/svm/svm_resource_alias_test.go new file mode 100644 index 00000000..78eb29aa --- /dev/null +++ b/internal/provider/svm/svm_resource_alias_test.go @@ -0,0 +1,123 @@ +package svm_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 TestAccSvmResourceAlias(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { ntest.TestAccPreCheck(t) }, + ProtoV6ProviderFactories: ntest.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccSvmResourceConfigAlias("tfsvm4", "test", "default"), + Check: resource.ComposeTestCheckFunc( + // Check to see the svm name is correct, + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "name", "tfsvm4"), + // Check to see if Ipspace is set correctly + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "ipspace", "ansibleIpspace_newname"), + // Check that a ID has been set (we don't know what the vaule is as it changes + resource.TestCheckResourceAttrSet("netapp-ontap_svm_resource.example", "id"), + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "comment", "test")), + }, + // Update a comment + { + Config: testAccSvmResourceConfigAlias("tfsvm4", "carchi8py was here", "default"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "comment", "carchi8py was here"), + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "name", "tfsvm4")), + }, + // Update a comment with an empty string + { + Config: testAccSvmResourceConfigAlias("tfsvm4", "", "default"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "comment", ""), + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "name", "tfsvm4")), + }, + // Update snapshot policy default-1weekly and comment "carchi8py was here" + { + Config: testAccSvmResourceConfigAlias("tfsvm4", "carchi8py was here", "default-1weekly"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "comment", "carchi8py was here"), + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "snapshot_policy", "default-1weekly"), + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "name", "tfsvm4")), + }, + // Update snapshot policy with empty string + { + Config: testAccSvmResourceConfigAlias("tfsvm4", "carchi8py was here", ""), + ExpectError: regexp.MustCompile("cannot be updated with empty string"), + }, + // change SVM name + { + Config: testAccSvmResourceConfigAlias("tfsvm3", "carchi8py was here", "default"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "comment", "carchi8py was here"), + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "name", "tfsvm3")), + }, + // Fail if the name already exist + { + Config: testAccSvmResourceConfigAlias("svm5", "carchi8py was here", "default"), + ExpectError: regexp.MustCompile("13434908"), + }, + // Import and read + { + ResourceName: "netapp-ontap_svm_resource.example", + ImportState: true, + ImportStateId: fmt.Sprintf("%s,%s", "ansibleSVM", "cluster4"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netapp-ontap_svm_resource.example", "name", "ansibleSVM"), + ), + }, + }, + }) +} +func testAccSvmResourceConfigAlias(svm, comment string, snapshotPolicy 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_svm_resource" "example" { + cx_profile_name = "cluster4" + name = "%s" + ipspace = "ansibleIpspace_newname" + comment = "%s" + snapshot_policy = "%s" + subtype = "default" + language = "en_us.utf_8" + aggregates = [ + { + name = "aggr1" + }, + { + name = "aggr2" + }, + { + name = "aggr3" + }, + ] + max_volumes = "unlimited" +}`, host, admin, password, svm, comment, snapshotPolicy) +} diff --git a/internal/provider/svm/svms_data_source.go b/internal/provider/svm/svms_data_source.go index 35066881..30323e8c 100644 --- a/internal/provider/svm/svms_data_source.go +++ b/internal/provider/svm/svms_data_source.go @@ -3,6 +3,7 @@ package svm import ( "context" "fmt" + "github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -25,6 +26,15 @@ func NewSvmsDataSource() datasource.DataSource { } } +// NewSvmsDataSourceAlias is a helper function to simplify the provider implementation. +func NewSvmsDataSourceAlias() datasource.DataSource { + return &SvmsDataSource{ + config: connection.ResourceOrDataSourceConfig{ + Name: "svms_data_source", + }, + } +} + // SvmsDataSource defines the data source implementation. type SvmsDataSource struct { config connection.ResourceOrDataSourceConfig