diff --git a/powerstore/constants.go b/powerstore/constants.go index 286866fd..6e55f14c 100644 --- a/powerstore/constants.go +++ b/powerstore/constants.go @@ -117,6 +117,9 @@ const ( //CreateVolumeGroupDetailErrorMsg specifies error caused when invalid attribute values are provided CreateVolumeGroupDetailErrorMsg = "Error creating volume group" + //CreateVolumeErrorMsg specifies error caused when invalid attribute values are provided + CreateVolumeErrorMsg = "Error creating volume" + //CreateResourceMissingErrorMsg specifies error caused when required attribute value is not provided CreateResourceMissingErrorMsg = "Missing required argument" diff --git a/powerstore/datasource_host_group_test.go b/powerstore/datasource_host_group_test.go index 4bde500b..87fc5275 100644 --- a/powerstore/datasource_host_group_test.go +++ b/powerstore/datasource_host_group_test.go @@ -26,7 +26,7 @@ import ( ) // Test to Fetch Host Groups -func TestAccHostGroup_FetchHostGroup(t *testing.T) { +func TestAccHostGroupDs_FetchHostGroup(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/datasource_host_test.go b/powerstore/datasource_host_test.go index 403236d8..225cebd9 100644 --- a/powerstore/datasource_host_test.go +++ b/powerstore/datasource_host_test.go @@ -26,7 +26,7 @@ import ( ) // Test to Fetch Host details -func TestAccHost_FetchHost(t *testing.T) { +func TestAccHostDs_FetchHost(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } @@ -49,7 +49,7 @@ func TestAccHost_FetchHost(t *testing.T) { } // Test to fetch Host - Negative -func TestAccHost_FetchHostNegative(t *testing.T) { +func TestAccHostDs_FetchHostNegative(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/datasource_protection_policy_test.go b/powerstore/datasource_protection_policy_test.go index a0eca4b9..c19b94c4 100644 --- a/powerstore/datasource_protection_policy_test.go +++ b/powerstore/datasource_protection_policy_test.go @@ -26,11 +26,10 @@ import ( ) // Test to Fetch Protection Policy -func TestAccProtectionPolicy_FetchPolicy(t *testing.T) { +func TestAccProtectionPolicyDs_FetchPolicy(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testProviderFactory, @@ -49,7 +48,7 @@ func TestAccProtectionPolicy_FetchPolicy(t *testing.T) { } // Test to Fetch Protection Policy- Negative -func TestAccProtectionPolicy_FetchPolicyNegative(t *testing.T) { +func TestAccProtectionPolicyDs_FetchPolicyNegative(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/datasource_snapshotrule_test.go b/powerstore/datasource_snapshotrule_test.go index 4e340b23..d413161a 100644 --- a/powerstore/datasource_snapshotrule_test.go +++ b/powerstore/datasource_snapshotrule_test.go @@ -26,7 +26,7 @@ import ( ) // Test to Fetch SnapshotRule -func TestAccSnapshotRule_FetchSnapshotRule(t *testing.T) { +func TestAccSnapshotRuleDs_FetchSnapshotRule(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/datasource_volume_group_snapshot_test.go b/powerstore/datasource_volume_group_snapshot_test.go index f7a9b781..6623437f 100644 --- a/powerstore/datasource_volume_group_snapshot_test.go +++ b/powerstore/datasource_volume_group_snapshot_test.go @@ -26,7 +26,7 @@ import ( ) // Test to Fetch Volume Group snapshots -func TestAccVolumeGroupSnapshot_FetchVolumeGroupSnapshot(t *testing.T) { +func TestAccVolumeGroupSnapshotDs_FetchVolumeGroupSnapshot(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/datasource_volume_group_test.go b/powerstore/datasource_volume_group_test.go index 4d096821..ee6c191d 100644 --- a/powerstore/datasource_volume_group_test.go +++ b/powerstore/datasource_volume_group_test.go @@ -26,7 +26,7 @@ import ( ) // Test to Fetch Volume Groups -func TestAccVolumeGroup_FetchVolumeGroup(t *testing.T) { +func TestAccVolumeGroupDs_FetchVolumeGroup(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/datasource_volume_snapshot_test.go b/powerstore/datasource_volume_snapshot_test.go index ab3fa187..f9da7411 100644 --- a/powerstore/datasource_volume_snapshot_test.go +++ b/powerstore/datasource_volume_snapshot_test.go @@ -26,7 +26,7 @@ import ( ) // Test to Fetch Volume Snapshot -func TestAccVolume_FetchVolumeSnapshot(t *testing.T) { +func TestAccVolumeDs_FetchVolumeSnapshot(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/datasource_volume_test.go b/powerstore/datasource_volume_test.go index bcc7d867..18ffa86d 100644 --- a/powerstore/datasource_volume_test.go +++ b/powerstore/datasource_volume_test.go @@ -26,7 +26,7 @@ import ( ) // Test to Fetch Volume -func TestAccVolume_FetchVolume(t *testing.T) { +func TestAccVolumeDs_FetchVolume(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("Dont run with units tests because it will try to create the context") } diff --git a/powerstore/provider_test.go b/powerstore/provider_test.go index 5ace06d6..9b2b1cc6 100644 --- a/powerstore/provider_test.go +++ b/powerstore/provider_test.go @@ -30,36 +30,38 @@ import ( // var testAccProviders map[string]func() tfsdk.Provider var testProvider provider.Provider var testProviderFactory map[string]func() (tfprotov6.ProviderServer, error) -var endpoint = os.Getenv("POWERSTORE_ENDPOINT") -var username = os.Getenv("POWERSTORE_USERNAME") -var password = os.Getenv("POWERSTORE_PASSWORD") -var hostID = os.Getenv("HOST_ID") -var hostIDRead = os.Getenv("HOST_ID_READ") -var hostGroupID = os.Getenv("HOST_GROUP_ID") -var volumeGroupID = os.Getenv("VOLUME_GROUP_ID") -var hostName = os.Getenv("HOST_NAME") -var hostNameRead = os.Getenv("HOST_NAME_READ") -var hostGroupName = os.Getenv("HOST_GROUP_NAME") -var volumeID = os.Getenv("VOLUME_ID") -var volumeName = os.Getenv("VOLUME_NAME") -var snapshotRuleID = os.Getenv("SNAPSHOT_RULE_ID") -var replicationRuleID = os.Getenv("REPLICATION_RULE_ID") -var snapshotRuleName = os.Getenv("SNAPSHOT_RULE_NAME") -var replicationRuleName = os.Getenv("REPLICATION_RULE_NAME") -var policyName = os.Getenv("PROTECTION_POLICY_NAME") -var policyID = os.Getenv("PROTECTION_POLICY_ID") -var volumeGroupName = os.Getenv("VOLUME_GROUP_NAME") -var volumeGroupSnapshotName = os.Getenv("VOLUME_GROUP_SNAPSHOT_NAME") -var volumeGroupSnapshotID = os.Getenv("VOLUME_GROUP_SNAPSHOT_ID") -var volumeSnapshotID = os.Getenv("VOLUME_SNAPSHOT_ID") -var volumeSnapshotName = os.Getenv("VOLUME_SNAPSHOT_NAME") + +var endpoint = setDefault(os.Getenv("POWERSTORE_ENDPOINT"), "http://localhost:3003/api/rest") +var username = setDefault(os.Getenv("POWERSTORE_USERNAME"), "test") +var password = setDefault(os.Getenv("POWERSTORE_PASSWORD"), "test") +var hostID = setDefault(os.Getenv("HOST_ID"), "tfacc_host_id") +var hostIDRead = setDefault(os.Getenv("HOST_ID_READ"), "tfacc_host_id") +var hostGroupID = setDefault(os.Getenv("HOST_GROUP_ID"), "tfacc_host_group_id") +var volumeGroupID = setDefault(os.Getenv("VOLUME_GROUP_ID"), "tfacc_volume_group_id") +var hostName = setDefault(os.Getenv("HOST_NAME"), "tfacc_host_name") +var hostNameRead = setDefault(os.Getenv("HOST_NAME_READ"), "tfacc_host_name") +var hostGroupName = setDefault(os.Getenv("HOST_GROUP_NAME"), "tfacc_host_group_name") +var volumeID = setDefault(os.Getenv("VOLUME_ID"), "tfacc_volume_id") +var volumeName = setDefault(os.Getenv("VOLUME_NAME"), "tfacc_volume_name") +var snapshotRuleID = setDefault(os.Getenv("SNAPSHOT_RULE_ID"), "tfacc_snapshot_rule_id") +var replicationRuleID = setDefault(os.Getenv("REPLICATION_RULE_ID"), "tfacc_replication_rule_id") +var snapshotRuleName = setDefault(os.Getenv("SNAPSHOT_RULE_NAME"), "tfacc_snapshot_rule_name") +var replicationRuleName = setDefault(os.Getenv("REPLICATION_RULE_NAME"), "tfacc_replication_rule_name") +var policyName = setDefault(os.Getenv("PROTECTION_POLICY_NAME"), "tfacc_policy_name") +var policyID = setDefault(os.Getenv("PROTECTION_POLICY_ID"), "tfacc_policy_id") +var volumeGroupName = setDefault(os.Getenv("VOLUME_GROUP_NAME"), "tfacc_volume_group_name") +var volumeGroupSnapshotName = setDefault(os.Getenv("VOLUME_GROUP_SNAPSHOT_NAME"), "tfacc_volume_group_snapshot_name") +var volumeGroupSnapshotID = setDefault(os.Getenv("VOLUME_GROUP_SNAPSHOT_ID"), "tfacc_volume_group_snapshot_id") +var volumeSnapshotID = setDefault(os.Getenv("VOLUME_SNAPSHOT_ID"), "tfacc_volume_snapshot_id") +var volumeSnapshotName = setDefault(os.Getenv("VOLUME_SNAPSHOT_NAME"), "tfacc_volume_snapshot_name") var ProviderConfigForTesting = `` func init() { - username := os.Getenv("POWERSTORE_USERNAME") - password := os.Getenv("POWERSTORE_PASSWORD") - endpoint := os.Getenv("POWERSTORE_ENDPOINT") + + username := username + password := password + endpoint := endpoint insecure := "true" ProviderConfigForTesting = fmt.Sprintf(` @@ -78,16 +80,23 @@ func init() { } func testAccPreCheck(t *testing.T) { - if v := os.Getenv("POWERSTORE_USERNAME"); v == "" { + if v := username; v == "" { t.Fatal("POWERSTORE_USERNAME must be set for acceptance tests") } - if v := os.Getenv("POWERSTORE_PASSWORD"); v == "" { + if v := password; v == "" { t.Fatal("POWERSTORE_PASSWORD must be set for acceptance tests") } - if v := os.Getenv("POWERSTORE_ENDPOINT"); v == "" { + if v := endpoint; v == "" { t.Fatal("POWERSTORE_ENDPOINT must be set for acceptance tests") } } + +func setDefault(osInput string, defaultStr string) string { + if osInput == "" { + return defaultStr + } + return osInput +} diff --git a/powerstore/resource_host_group_test.go b/powerstore/resource_host_group_test.go index 626440df..e3b9b27b 100644 --- a/powerstore/resource_host_group_test.go +++ b/powerstore/resource_host_group_test.go @@ -45,6 +45,29 @@ func TestAccHostGroup_Create(t *testing.T) { resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID), ), }, + // Import Testing + { + Config: ProviderConfigForTesting + HostGroupParamsCreate, + ResourceName: "powerstore_hostgroup.test", + ImportState: true, + ExpectError: nil, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "test_hostgroup", s[0].Attributes["name"]) + assert.Equal(t, "Test Create Host Group", s[0].Attributes["description"]) + assert.Equal(t, hostID, s[0].Attributes["host_ids.0"]) + return nil + }, + }, + // Update Testing + { + Config: ProviderConfigForTesting + HostGroupParamsUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"), + resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Update Host Group"), + resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID), + ), + }, + // Remove host before cleanup { Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost, }, @@ -167,100 +190,6 @@ func TestAccHostGroup_CreateWithHostIDAndName(t *testing.T) { }) } -// Test to Update existing HostGroup Params -func TestAccHostGroup_Update(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProviderConfigForTesting + HostGroupParamsCreate, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"), - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Create Host Group"), - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID), - ), - }, - { - Config: ProviderConfigForTesting + HostGroupParamsUpdate, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"), - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Update Host Group"), - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID), - ), - }, - { - Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost, - }, - }, - }) -} - -// Test to Update existing HostGroup Params -func TestAccHostGroup_UpdateRemoveHost(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProviderConfigForTesting + HostGroupParamsCreate, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"), - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Create Host Group"), - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID), - ), - }, - { - Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"), - resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Create Host Group"), - ), - }, - }, - }) -} - -// Test to import host group successfully -func TestAccHostGroup_ImportSuccess(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProviderConfigForTesting + HostGroupParamsCreate, - }, - { - Config: ProviderConfigForTesting + HostGroupParamsCreate, - ResourceName: "powerstore_hostgroup.test", - ImportState: true, - ExpectError: nil, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "test_hostgroup", s[0].Attributes["name"]) - assert.Equal(t, "Test Create Host Group", s[0].Attributes["description"]) - assert.Equal(t, hostID, s[0].Attributes["host_ids.0"]) - return nil - }, - }, - { - Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost, - }, - }, - }) -} - // Negative - Test to import host group func TestAccHostGroup_ImportFailure(t *testing.T) { if os.Getenv("TF_ACC") == "" { diff --git a/powerstore/resource_host_test.go b/powerstore/resource_host_test.go index 062be763..75aee440 100644 --- a/powerstore/resource_host_test.go +++ b/powerstore/resource_host_test.go @@ -45,6 +45,18 @@ func TestAccHost_Create(t *testing.T) { resource.TestCheckResourceAttr("powerstore_host.test", "description", "Test Host Resource"), ), }, + { + Config: ProviderConfigForTesting + HostParamsCreate, + ResourceName: "powerstore_host.test", + ImportState: true, + ExpectError: nil, + ImportStateVerify: true, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "tf_host_acc_new", s[0].Attributes["name"]) + assert.Equal(t, "Linux", s[0].Attributes["os_type"]) + return nil + }, + }, }, }) } @@ -210,37 +222,6 @@ func TestAccHost_ImportFailure(t *testing.T) { }) } -// Test to import successfully -func TestAccHost_ImportSuccess(t *testing.T) { - - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProviderConfigForTesting + HostParamsCreate, - }, - { - Config: ProviderConfigForTesting + HostParamsCreate, - ResourceName: "powerstore_host.test", - ImportState: true, - ExpectError: nil, - ImportStateVerify: true, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "tf_host_acc_new", s[0].Attributes["name"]) - assert.Equal(t, "Linux", s[0].Attributes["os_type"]) - return nil - }, - }, - }, - }) - -} - var HostParamsCreate = ` resource "powerstore_host" "test" { name = "tf_host_acc_new" diff --git a/powerstore/resource_protection_policy_test.go b/powerstore/resource_protection_policy_test.go index 2859872d..5de50fe3 100644 --- a/powerstore/resource_protection_policy_test.go +++ b/powerstore/resource_protection_policy_test.go @@ -46,6 +46,20 @@ func TestAccProtectionPolicy_Create(t *testing.T) { resource.TestCheckResourceAttr("powerstore_protectionpolicy.test", "snapshot_rule_ids.0", snapshotRuleID), ), }, + { + Config: ProtectionPolicyParamsCreate, + ResourceName: "powerstore_protectionpolicy.test", + ImportState: true, + ExpectError: nil, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "protectionpolicy_acc_new", s[0].Attributes["name"]) + assert.Equal(t, "Test CreateProtectionPolicy", s[0].Attributes["description"]) + assert.Equal(t, "Protection", s[0].Attributes["type"]) + assert.Equal(t, replicationRuleID, s[0].Attributes["replication_rule_ids.0"]) + assert.Equal(t, snapshotRuleID, s[0].Attributes["snapshot_rule_ids.0"]) + return nil + }, + }, }, }) } @@ -220,37 +234,6 @@ func TestAccProtectionPolicy_ImportFailure(t *testing.T) { }) } -// Test to import protection policy successfully -func TestAccProtectionPolicy_ImportSuccess(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProtectionPolicyParamsCreate, - }, - { - Config: ProtectionPolicyParamsCreate, - ResourceName: "powerstore_protectionpolicy.test", - ImportState: true, - ExpectError: nil, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "protectionpolicy_acc_new", s[0].Attributes["name"]) - assert.Equal(t, "Test CreateProtectionPolicy", s[0].Attributes["description"]) - assert.Equal(t, "Protection", s[0].Attributes["type"]) - assert.Equal(t, replicationRuleID, s[0].Attributes["replication_rule_ids.0"]) - assert.Equal(t, snapshotRuleID, s[0].Attributes["snapshot_rule_ids.0"]) - return nil - }, - }, - }, - }) -} - var ProtectionPolicyParamsCreate = ` provider "powerstore" { username = "` + username + `" diff --git a/powerstore/resource_snapshot_test.go b/powerstore/resource_snapshot_test.go index 07da8215..f577366f 100644 --- a/powerstore/resource_snapshot_test.go +++ b/powerstore/resource_snapshot_test.go @@ -45,6 +45,17 @@ func TestAccVolumeSnapshot_Create(t *testing.T) { resource.TestCheckResourceAttr("powerstore_volume_snapshot.test", "description", "Test Snapshot Resource"), ), }, + // Import Testing + { + Config: ProviderConfigForTesting + SnapParamsCreate, + ResourceName: "powerstore_volume_snapshot.test", + ImportState: true, + ExpectError: nil, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "tf_snap_acc", s[0].Attributes["name"]) + return nil + }, + }, }, }) } @@ -231,34 +242,6 @@ func TestAccVolumeSnapshot_ImportFailure(t *testing.T) { }) } -// Test to import successfully -func TestAccVolumeSnapshot_ImportSuccess(t *testing.T) { - - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProviderConfigForTesting + SnapParamsCreate, - }, - { - Config: ProviderConfigForTesting + SnapParamsCreate, - ResourceName: "powerstore_volume_snapshot.test", - ImportState: true, - ExpectError: nil, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "tf_snap_acc", s[0].Attributes["name"]) - return nil - }, - }, - }, - }) -} - var SnapParamsCreate = ` resource "powerstore_volume_snapshot" "test" { name = "tf_snap_acc" diff --git a/powerstore/resource_storagecontainer.go b/powerstore/resource_storagecontainer.go index 20ac1066..ff506813 100644 --- a/powerstore/resource_storagecontainer.go +++ b/powerstore/resource_storagecontainer.go @@ -20,11 +20,12 @@ package powerstore import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-framework/path" "log" client "terraform-provider-powerstore/client" "terraform-provider-powerstore/models" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/dell/gopowerstore" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -154,7 +155,7 @@ func (r *resourceStorageContainer) Create(ctx context.Context, req resource.Crea if err1 != nil { resp.Diagnostics.AddError( "Error getting Storage Container after creation", - "Could not get Storage Container, unexpected error: "+err.Error(), + "Could not get Storage Container, unexpected error: "+err1.Error(), ) return } diff --git a/powerstore/resource_storagecontainer_test.go b/powerstore/resource_storagecontainer_test.go index c4a07312..a68fa77b 100644 --- a/powerstore/resource_storagecontainer_test.go +++ b/powerstore/resource_storagecontainer_test.go @@ -46,6 +46,20 @@ func TestAccStorageContainer_Create(t *testing.T) { resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "high_water_mark", "70"), ), }, + // Import test + { + Config: StorageContainerParamsCreate, + ResourceName: "powerstore_storagecontainer.test", + ImportState: true, + ExpectError: nil, + ImportStateVerify: true, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "scterraform_acc", s[0].Attributes["name"]) + assert.Equal(t, "10737418240", s[0].Attributes["quota"]) + assert.Equal(t, "SCSI", s[0].Attributes["storage_protocol"]) + return nil + }, + }, }, }) } @@ -78,6 +92,11 @@ func TestAccStorageContainer_Update(t *testing.T) { resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "high_water_mark", "60"), ), }, + // Test to update existing StorageContainer params but will result in error + { + Config: StorageContainerParamsCreateServerError, + ExpectError: regexp.MustCompile(UpdateSCDetailErrorMsg), + }, }, }) } @@ -108,33 +127,6 @@ func TestAccStorageContainer_CreateWithInvalidValues(t *testing.T) { } } -// Test to update existing StorageContainer params but will result in error -func TestAccStorageContainer_UpdateError(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: StorageContainerParamsCreate, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "name", "scterraform_acc"), - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "quota", "10737418240"), - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "storage_protocol", "SCSI"), - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "high_water_mark", "70"), - ), - }, - { - Config: StorageContainerParamsCreateServerError, - ExpectError: regexp.MustCompile(UpdateSCDetailErrorMsg), - }, - }, - }) -} - // Test to import resource but resulting in error func TestAccStorageContainer_ImportFailure(t *testing.T) { if os.Getenv("TF_ACC") == "" { @@ -156,44 +148,6 @@ func TestAccStorageContainer_ImportFailure(t *testing.T) { }) } -// Test to import successfully -func TestAccStorageContainer_ImportSuccess(t *testing.T) { - - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: StorageContainerParamsCreate, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "name", "scterraform_acc"), - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "quota", "10737418240"), - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "storage_protocol", "SCSI"), - resource.TestCheckResourceAttr("powerstore_storagecontainer.test", "high_water_mark", "70"), - ), - }, - { - Config: StorageContainerParamsCreate, - ResourceName: "powerstore_storagecontainer.test", - ImportState: true, - ExpectError: nil, - ImportStateVerify: true, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "scterraform_acc", s[0].Attributes["name"]) - assert.Equal(t, "10737418240", s[0].Attributes["quota"]) - assert.Equal(t, "SCSI", s[0].Attributes["storage_protocol"]) - return nil - }, - }, - }, - }) - -} - var StorageContainerParamsCreate = ` provider "powerstore" { username = "` + username + `" diff --git a/powerstore/resource_volume.go b/powerstore/resource_volume.go index 4b2a0c0c..111e804e 100644 --- a/powerstore/resource_volume.go +++ b/powerstore/resource_volume.go @@ -439,7 +439,7 @@ func (r volumeResource) Create(ctx context.Context, req resource.CreateRequest, volGroupMapping, err := r.client.PStoreClient.GetVolumeGroupsByVolumeID(context.Background(), volCreateResponse.ID) if err != nil { resp.Diagnostics.AddError( - "Error fetching volume host mapping", + "Error fetching volume group mapping", "Could not create volume, unexpected error: "+err.Error(), ) return @@ -569,7 +569,7 @@ func (r volumeResource) Update(ctx context.Context, req resource.UpdateRequest, } // Get Host Mapping from volume ID - hostMapping, err := r.client.PStoreClient.GetHostVolumeMappingByVolumeID(context.Background(), volID) + hostMapping, err := r.client.PStoreClient.GetHostVolumeMappingByVolumeID(context.Background(), volResponse.ID) if err != nil { resp.Diagnostics.AddError( "Error fetching volume host mapping", @@ -579,7 +579,7 @@ func (r volumeResource) Update(ctx context.Context, req resource.UpdateRequest, } // Get Volume Group Mapping details from API - volGroupMapping, err := r.client.PStoreClient.GetVolumeGroupsByVolumeID(context.Background(), volID) + volGroupMapping, err := r.client.PStoreClient.GetVolumeGroupsByVolumeID(context.Background(), volResponse.ID) if err != nil { resp.Diagnostics.AddError( "Error fetching volume host mapping", diff --git a/powerstore/resource_volume_group_snapshot_test.go b/powerstore/resource_volume_group_snapshot_test.go index 628129c3..def04334 100644 --- a/powerstore/resource_volume_group_snapshot_test.go +++ b/powerstore/resource_volume_group_snapshot_test.go @@ -18,12 +18,13 @@ limitations under the License. package powerstore import ( - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - "github.com/stretchr/testify/assert" "os" "regexp" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/stretchr/testify/assert" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) @@ -44,6 +45,18 @@ func TestAccVolumeGroupSnapshot_Create(t *testing.T) { resource.TestCheckResourceAttr("powerstore_volumegroup_snapshot.test", "description", "Test Snapshot Resource"), ), }, + // Import Test + { + Config: ProviderConfigForTesting + VolumeGroupSnapParamsCreate, + ResourceName: "powerstore_volumegroup_snapshot.test", + ImportState: true, + ExpectError: nil, + ImportStateVerify: true, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "test_snap", s[0].Attributes["name"]) + return nil + }, + }, }, }) } @@ -223,35 +236,6 @@ func TestAccVolumeGroupSnapshot_ImportFailure(t *testing.T) { }) } -// Test to import successfully -func TestAccVolumeGroupSnapshot_ImportSuccess(t *testing.T) { - - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProviderConfigForTesting + VolumeGroupSnapParamsCreate, - }, - { - Config: ProviderConfigForTesting + VolumeGroupSnapParamsCreate, - ResourceName: "powerstore_volumegroup_snapshot.test", - ImportState: true, - ExpectError: nil, - ImportStateVerify: true, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "test_snap", s[0].Attributes["name"]) - return nil - }, - }, - }, - }) -} - var VolumeGroupSnapParamsCreate = ` resource "powerstore_volumegroup_snapshot" "test" { name = "test_snap" diff --git a/powerstore/resource_volume_group_test.go b/powerstore/resource_volume_group_test.go index 6264c78b..779df612 100644 --- a/powerstore/resource_volume_group_test.go +++ b/powerstore/resource_volume_group_test.go @@ -45,6 +45,19 @@ func TestAccVolumeGroup_Create(t *testing.T) { resource.TestCheckResourceAttr("powerstore_volumegroup.test", "is_write_order_consistent", "false"), ), }, + // Import test + { + Config: ProviderConfigForTesting + VolumeGroupParamsCreate, + ResourceName: "powerstore_volumegroup.test", + ImportState: true, + ExpectError: nil, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "tf_volume_group_new", s[0].Attributes["name"]) + assert.Equal(t, "Creating Volume Group", s[0].Attributes["description"]) + assert.Equal(t, "false", s[0].Attributes["is_write_order_consistent"]) + return nil + }, + }, }, }) } @@ -409,35 +422,6 @@ func TestAccVolumeGroup_UpdateRemoveVolume(t *testing.T) { }) } -// Test to import volume group successfully -func TestAccVolumeGroup_ImportSuccess(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: ProviderConfigForTesting + VolumeGroupParamsCreate, - }, - { - Config: ProviderConfigForTesting + VolumeGroupParamsCreate, - ResourceName: "powerstore_volumegroup.test", - ImportState: true, - ExpectError: nil, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "tf_volume_group_new", s[0].Attributes["name"]) - assert.Equal(t, "Creating Volume Group", s[0].Attributes["description"]) - assert.Equal(t, "false", s[0].Attributes["is_write_order_consistent"]) - return nil - }, - }, - }, - }) -} - // Negative - Test to import volume group func TestAccVolumeGroup_ImportFailure(t *testing.T) { if os.Getenv("TF_ACC") == "" { diff --git a/powerstore/resource_volume_test.go b/powerstore/resource_volume_test.go index 89fc49cd..51ed261b 100644 --- a/powerstore/resource_volume_test.go +++ b/powerstore/resource_volume_test.go @@ -18,13 +18,10 @@ limitations under the License. package powerstore import ( - "context" - "fmt" "os" "regexp" "testing" - "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" @@ -46,6 +43,20 @@ func TestAccVolume_CreateVolume(t *testing.T) { resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "size", "2.5"), resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "capacity_unit", "GB")), }, + // Import Succes Test + { + Config: VolumeParams, + ResourceName: "powerstore_volume.volume_create_test", + ImportState: true, + ExpectError: nil, + ImportStateVerify: true, + ImportStateCheck: func(s []*terraform.InstanceState) error { + assert.Equal(t, "test_acc_cvol", s[0].Attributes["name"]) + assert.Equal(t, "2.5", s[0].Attributes["size"]) + assert.Equal(t, "GB", s[0].Attributes["capacity_unit"]) + return nil + }, + }, }, }) } @@ -86,7 +97,7 @@ func TestAccVolume_CreateVolumeWithMBInInt(t *testing.T) { Steps: []resource.TestStep{ { Config: VolumeParamsWithMBInInt, - Check: resource.ComposeTestCheckFunc(checkCreateVolume(t, testProvider, "test_acc_cvol_mb")), + Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("powerstore_volume.volume_create_test_mb", "name", "test_acc_cvol_mb")), }, }, }) @@ -104,7 +115,7 @@ func TestAccVolume_CreateVolumeWithTBInFloat(t *testing.T) { Steps: []resource.TestStep{ { Config: VolumeParamsWithTBInFloat, - Check: resource.ComposeTestCheckFunc(checkCreateVolume(t, testProvider, "test_acc_cvol_tb_float")), + Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("powerstore_volume.volume_create_test_tb_float", "name", "test_acc_cvol_tb_float")), }, }, }) @@ -255,7 +266,7 @@ func TestAccVolume_UpdateVolumeInvalidApplianceID(t *testing.T) { resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "size", "2.5"), resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "capacity_unit", "GB"), resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "appliance_id", "Z1")), - ExpectError: regexp.MustCompile(InvalidAppliaceErrorMsg), + ExpectError: regexp.MustCompile(CreateVolumeErrorMsg), }, }, }) @@ -315,35 +326,20 @@ func TestAccVolume_UpdateVolumeGroupID(t *testing.T) { { Config: VolumeParams, }, + // Add Volume Group { Config: VolumeParamsWithVolumeGroupID, Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "name", "test_acc_cvol"), resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "size", "2.5"), resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "capacity_unit", "GB")), }, - }, - }) -} - -// Test to update Volume Group ID in volume resource -func TestAccVolume_DetachVolumeGroupID(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ + // Remove Volume Group { Config: VolumeParamsWithVolumeGroupID, Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "name", "test_acc_cvol"), resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "size", "2.5"), resource.TestCheckResourceAttr("powerstore_volume.volume_create_test", "capacity_unit", "GB")), }, - { - Config: VolumeParams, - }, }, }) } @@ -503,53 +499,6 @@ func TestAccVolume_ImportFailure(t *testing.T) { }) } -// Test to import successfully -func TestAccVolume_ImportSuccess(t *testing.T) { - - if os.Getenv("TF_ACC") == "" { - t.Skip("Dont run with units tests because it will try to create the context") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProtoV6ProviderFactories: testProviderFactory, - Steps: []resource.TestStep{ - { - Config: VolumeParams, - }, - { - Config: VolumeParams, - ResourceName: "powerstore_volume.volume_create_test", - ImportState: true, - ExpectError: nil, - ImportStateVerify: true, - ImportStateCheck: func(s []*terraform.InstanceState) error { - assert.Equal(t, "test_acc_cvol", s[0].Attributes["name"]) - assert.Equal(t, "2.5", s[0].Attributes["size"]) - assert.Equal(t, "GB", s[0].Attributes["capacity_unit"]) - return nil - }, - }, - }, - }) - -} - -func checkCreateVolume(t *testing.T, p provider.Provider, volName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - providers := p.(*Pstoreprovider) - _, err := providers.client.PStoreClient.GetVolumeByName(context.Background(), volName) - if err != nil { - return fmt.Errorf("failed to fetch volume") - } - - if providers.client.PStoreClient == nil { - return fmt.Errorf("provider not configured") - } - return nil - } -} - var VolumeParams = ` provider "powerstore" { username = "` + username + `" @@ -653,7 +602,7 @@ provider "powerstore" { } resource "powerstore_volume" "volume_create_test" { - name = "test_acc_cvol" + name = "test_acc_cvol_tb_float" size = 2.5 capacity_unit = "TB" }