From e281918454c95c3f5e21c649d9eaf52173fcbf10 Mon Sep 17 00:00:00 2001 From: Ujjwal Kumar <78945437+ujjwal-ibm@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:32:58 +0530 Subject: [PATCH] fix(is_share): added empty check and moved source_share_crn outside (#5632) --- ibm/service/vpc/resource_ibm_is_share.go | 23 ++++---- ibm/service/vpc/resource_ibm_is_share_test.go | 57 +++++++++++++++++++ 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/ibm/service/vpc/resource_ibm_is_share.go b/ibm/service/vpc/resource_ibm_is_share.go index e48458c171..080699d087 100644 --- a/ibm/service/vpc/resource_ibm_is_share.go +++ b/ibm/service/vpc/resource_ibm_is_share.go @@ -1096,23 +1096,24 @@ func resourceIbmIsShareCreate(context context.Context, d *schema.ResourceData, m sharePrototype.SourceShare = &vpcv1.ShareIdentity{ ID: &sourceShare, } - } else { - sourceShareCRN := d.Get("source_share_crn").(string) - if sourceShareCRN != "" { - sharePrototype.SourceShare = &vpcv1.ShareIdentity{ - CRN: &sourceShareCRN, - } + } + replicationCronSpec := d.Get("replication_cron_spec").(string) + sharePrototype.ReplicationCronSpec = &replicationCronSpec + } else if sourceShareCrnIntf, sShareCrnok := d.GetOk("source_share_crn"); sShareCrnok { + sourceShareCRN := sourceShareCrnIntf.(string) + if sourceShareCRN != "" { + sharePrototype.SourceShare = &vpcv1.ShareIdentity{ + CRN: &sourceShareCRN, } } - replicationCronSpec := d.Get("replication_cron_spec").(string) sharePrototype.ReplicationCronSpec = &replicationCronSpec } else { originShare := d.Get("origin_share") - OriginShareModel := ResourceIBMIsShareMapToShareIdentity(originShare.([]interface{})[0].(map[string]interface{})) - - sharePrototype.OriginShare = OriginShareModel - + if len(originShare.([]interface{})) > 0 { + OriginShareModel := ResourceIBMIsShareMapToShareIdentity(originShare.([]interface{})[0].(map[string]interface{})) + sharePrototype.OriginShare = OriginShareModel + } } if iopsIntf, ok := d.GetOk("iops"); ok { diff --git a/ibm/service/vpc/resource_ibm_is_share_test.go b/ibm/service/vpc/resource_ibm_is_share_test.go index a9f5ce9eb2..e3f0b678cf 100644 --- a/ibm/service/vpc/resource_ibm_is_share_test.go +++ b/ibm/service/vpc/resource_ibm_is_share_test.go @@ -137,6 +137,32 @@ func TestAccIbmIsShareAllArgs(t *testing.T) { }, }) } +func TestAccIbmIsShareAllArgs_EmptyCheck(t *testing.T) { + var conf vpcv1.Share + + name := fmt.Sprintf("tf-fs-name-%d", acctest.RandIntRange(10, 100)) + name2 := fmt.Sprintf("tf-fs-name2-%d", acctest.RandIntRange(10, 100)) + name3 := fmt.Sprintf("tf-fs-name3-%d", acctest.RandIntRange(10, 100)) + name4 := fmt.Sprintf("tf-fs-name4-%d", acctest.RandIntRange(10, 100)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + CheckDestroy: testAccCheckIbmIsShareDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckIbmIsShareAllConfigEmptyCheckConfig(name, name2, name3, name4), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckIbmIsShareExists("ibm_is_share.test-share", conf), + resource.TestCheckResourceAttr("ibm_is_share.test-share", "name", name), + resource.TestCheckResourceAttr("ibm_is_share.test-share2", "name", name2), + resource.TestCheckResourceAttr("ibm_is_share.test-share-replica", "name", name3), + resource.TestCheckResourceAttr("ibm_is_share.test-share2-replica-crn", "name", name4), + ), + }, + }, + }) +} func TestAccIbmIsShareReplicaMain(t *testing.T) { var conf vpcv1.Share @@ -432,6 +458,37 @@ func testAccCheckIbmIsShareConfig(vpcName, name string, size int, shareTergetNam } `, vpcName, name, acc.ShareProfileName, size, shareTergetName) } +func testAccCheckIbmIsShareAllConfigEmptyCheckConfig(sharename1, sharename2, sharename3, sharename4 string) string { + return fmt.Sprintf(` + + resource "ibm_is_share" "test-share" { + name = "%s" + size = 200 + profile = "%s" + zone = "%s" + } + resource "ibm_is_share" "test-share2" { + name = "%s" + size = 200 + profile = "%s" + zone = "%s" + } + resource "ibm_is_share" "test-share-replica" { + name = "%s" + profile = "%s" + zone = "%s" + source_share = ibm_is_share.test-share.id + replication_cron_spec = "0 */6 * * *" + } + resource "ibm_is_share" "test-share2-replica-crn" { + name = "%s" + profile = "%s" + zone = "%s" + source_share_crn = ibm_is_share.test-share2.crn + replication_cron_spec = "0 */5 * * *" + } + `, sharename1, acc.ShareProfileName, acc.ISZoneName, sharename2, acc.ShareProfileName, acc.ISZoneName, sharename3, acc.ShareProfileName, acc.ISZoneName2, sharename4, acc.ShareProfileName, acc.ISZoneName3) +} func testAccCheckIbmIsShareConfigReplica(vpcName, vpcName1, name string, size int, shareTergetName, shareTergetName1, replicaName string) string { return fmt.Sprintf(`