Skip to content

Commit

Permalink
[Refactor] Conform to changes in SDRS protected instances in Gopher (#…
Browse files Browse the repository at this point in the history
…2667)

[Refactor] Conform to changes in SDRS protected instances in Gopher

Summary of the Pull Request
PR Checklist

 Tests added/passed.

Acceptance Steps Performed
=== RUN   TestAccSdrsProtectedInstanceV1_basic
--- PASS: TestAccSdrsProtectedInstanceV1_basic (385.93s)
PASS

Reviewed-by: Anton Sidelnikov
  • Loading branch information
muneeb-jan authored Oct 1, 2024
1 parent 8486038 commit e77eca0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ modules-dev/
*.test
localrc2
localrc
*.env
.vscode

# Test exclusions
!command/test-fixtures/**/*.tfstate
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20240930091858-58426cf7fd21
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241001094048-fbd948f2ab7e
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.21.0
golang.org/x/sync v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20240930091858-58426cf7fd21 h1:Lbky8Fv3ICHrNHUpRHOOpuVajwWAMR1PZi4WkXIOaf4=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20240930091858-58426cf7fd21/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241001094048-fbd948f2ab7e h1:JT2bfxV3X4Pb+1H3rVts1olmWCqtiXb3PI+9z334WBU=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241001094048-fbd948f2ab7e/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@ import (
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
)

func getProtectedInstancesResourceFunc(cfg *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := cfg.SdrsV1Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating SDRS Client: %s", err)
}
return protectedinstances.Get(client, state.Primary.ID)
}

func TestAccSdrsProtectedInstanceV1_basic(t *testing.T) {
var instance protectedinstances.Instance
resourceName := "opentelekomcloud_sdrs_protected_instance_v1.instance_1"

rc := common.InitResourceCheck(
resourceName,
&instance,
getProtectedInstancesResourceFunc,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
PreCheck: func() {
common.TestAccPreCheck(t)
},
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccSdrsProtectedInstanceV1Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccSdrsProtectedInstanceV1Basic,
Check: resource.ComposeTestCheckFunc(
testAccSdrsProtectedInstanceV1Exists(resourceName, &instance),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceName, "name", "instance_create"),
resource.TestCheckResourceAttr(resourceName, "description", "some interesting description"),
resource.TestCheckResourceAttr(resourceName, "tags.muh", "value-create"),
Expand All @@ -34,7 +50,7 @@ func TestAccSdrsProtectedInstanceV1_basic(t *testing.T) {
{
Config: testAccSdrsProtectedInstanceV1Update,
Check: resource.ComposeTestCheckFunc(
testAccSdrsProtectedInstanceV1Exists(resourceName, &instance),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceName, "name", "instance_update"),
resource.TestCheckResourceAttr(resourceName, "tags.muh", "value-update"),
),
Expand All @@ -55,7 +71,7 @@ func testAccSdrsProtectedInstanceV1Destroy(s *terraform.State) error {
continue
}

_, err := protectedinstances.Get(client, rs.Primary.ID).Extract()
_, err := protectedinstances.Get(client, rs.Primary.ID)
if err == nil {
return fmt.Errorf("SDRS protected instance still exists")
}
Expand All @@ -64,38 +80,6 @@ func testAccSdrsProtectedInstanceV1Destroy(s *terraform.State) error {
return nil
}

func testAccSdrsProtectedInstanceV1Exists(n string, instance *protectedinstances.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.SdrsV1Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud SDRS client: %s", err)
}

found, err := protectedinstances.Get(client, rs.Primary.ID).Extract()
if err != nil {
return err
}

if found.ID != rs.Primary.ID {
return fmt.Errorf("SDRS protectiongroup not found")
}

*instance = *found

return nil
}
}

var testAccSdrsProtectedInstanceV1Basic = fmt.Sprintf(`
%s
%s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func resourceSdrsProtectedInstanceV1Create(ctx context.Context, d *schema.Resour
IpAddress: d.Get("ip_address").(string),
}

job, err := protectedinstances.Create(client, createOpts).ExtractJobResponse()
job, err := protectedinstances.Create(client, createOpts)
if err != nil {
return fmterr.Errorf("error creating OpenTelekomcomCloud SDRS Protected Instance: %w", err)
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func resourceSdrsProtectedInstanceV1Read(ctx context.Context, d *schema.Resource
return fmterr.Errorf(errCreationV1Client, err)
}

instance, err := protectedinstances.Get(client, d.Id()).Extract()
instance, err := protectedinstances.Get(client, d.Id())
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
Expand Down Expand Up @@ -200,7 +200,7 @@ func resourceSdrsProtectedInstanceV1Update(ctx context.Context, d *schema.Resour

if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
_, err = protectedinstances.Update(client, d.Id(), updateOpts).Extract()
_, err = protectedinstances.Update(client, d.Id(), updateOpts)
if err != nil {
return fmterr.Errorf("error updating OpenTelekomCloud SDRS Protected Instance: %w", err)
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func resourceSdrsProtectedInstanceV1Delete(ctx context.Context, d *schema.Resour
DeleteTargetEip: &deleteTargetEIP,
}

job, err := protectedinstances.Delete(client, d.Id(), deleteOpts).ExtractJobResponse()
job, err := protectedinstances.Delete(client, d.Id(), deleteOpts)
if err != nil {
return fmterr.Errorf("error deleting OpenTelekomCloud SDRS Protected Instance: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func resourceSDRSReplicationAttachV1Read(ctx context.Context, d *schema.Resource

instanceID := d.Get("instance_id").(string)
replicationID := d.Get("replication_id").(string)
n, err := protectedinstances.Get(client, instanceID).Extract()
n, err := protectedinstances.Get(client, instanceID)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
**[SDRS]** Refactor SDRS protected instances (`#2667 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2667>`_)

0 comments on commit e77eca0

Please sign in to comment.