Skip to content

Commit

Permalink
fix(ibm_is_share): 404 error fix on datasource (#5770)
Browse files Browse the repository at this point in the history
* 404 error fix on ibm_is_source_share datasource

* fix(ibm_is_share): 404 error fix on datasource
  • Loading branch information
uibm authored Nov 21, 2024
1 parent 2a51ec1 commit c15509e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
11 changes: 2 additions & 9 deletions ibm/service/vpc/data_source_ibm_is_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,15 +552,8 @@ func dataSourceIbmIsShareRead(context context.Context, d *schema.ResourceData, m

shareItem, response, err := vpcClient.GetShareWithContext(context, getShareOptions)
if err != nil {
if response != nil {
if response.StatusCode == 404 {
d.SetId("")
}
log.Printf("[DEBUG] GetShareWithContext failed %s\n%s", err, response)
return nil
}
log.Printf("[DEBUG] GetShareWithContext failed %s\n", err)
return diag.FromErr(err)
log.Printf("[DEBUG] GetShareWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] GetShareWithContext failed %s\n%s", err, response))
}
share = shareItem
} else if shareName != "" {
Expand Down
21 changes: 21 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vpc_test

import (
"fmt"
"regexp"
"testing"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
Expand Down Expand Up @@ -39,6 +40,19 @@ func TestAccIbmIsShareDataSourceBasic(t *testing.T) {
},
})
}
func TestAccIbmIsShareDataSource404(t *testing.T) {
shareId := "8843-5fr454ft-f6-4565-9555-5f889f5f3f7777"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIbmIsShareDataSourceConfig404(shareId),
ExpectError: regexp.MustCompile("GetShareWithContext failed"),
},
},
})
}

func TestAccIbmIsShareDataSourceAllArgs(t *testing.T) {
shareName := fmt.Sprintf("tf-fs-name-%d", acctest.RandIntRange(10, 100))
Expand Down Expand Up @@ -93,6 +107,13 @@ func testAccCheckIbmIsShareDataSourceConfigBasic(name string) string {
}
`, name, acc.ShareProfileName)
}
func testAccCheckIbmIsShareDataSourceConfig404(id string) string {
return fmt.Sprintf(`
data "ibm_is_share" "is_share" {
share = "%s"
}
`, id)
}

func testAccCheckIbmIsShareDataSourceConfig(vpcName, shareName string, shareSize int, shareTargetName string) string {
return fmt.Sprintf(`
Expand Down
11 changes: 2 additions & 9 deletions ibm/service/vpc/data_source_ibm_is_source_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,8 @@ func dataSourceIbmIsSourceShareRead(context context.Context, d *schema.ResourceD

share, response, err := vpcClient.GetShareSourceWithContext(context, getShareSourceOptions)
if err != nil {
if response != nil {
if response.StatusCode == 404 {
d.SetId("")
}
log.Printf("[DEBUG] GetShareWithContext failed %s\n%s", err, response)
return nil
}
log.Printf("[DEBUG] GetShareWithContext failed %s\n", err)
return diag.FromErr(fmt.Errorf("[DEBUG] GetShareWithContext failed %s\n", err))
log.Printf("[DEBUG] GetShareSourceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] GetShareSourceWithContext failed %s\n%s", err, response))
}

d.SetId(*share.ID)
Expand Down
23 changes: 23 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_source_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vpc_test

import (
"fmt"
"regexp"
"testing"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
Expand Down Expand Up @@ -54,3 +55,25 @@ func testAccCheckIbmIsSourceShareDataSourceConfigBasic(vpcname, vpcname1, shareN
}
`)
}
func TestAccIbmIsSourceShareDataSource404(t *testing.T) {
srId := "8843-5fr454ft-f6-4565-9555-5f889f5f3f7777"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIbmIsSourceShareDataSourceConfig404(srId),
ExpectError: regexp.MustCompile("GetShareSourceWithContext failed"),
},
},
})
}

func testAccCheckIbmIsSourceShareDataSourceConfig404(srId string) string {
return fmt.Sprintf(`
data "ibm_is_source_share" "test" {
share_replica = "%s"
}
`, srId)
}

0 comments on commit c15509e

Please sign in to comment.