Skip to content

Commit

Permalink
Add volume type validation for EVS v3 (#823)
Browse files Browse the repository at this point in the history
Add volume type validation for EVS v3

Summary of the Pull Request
Add AZ-based plan-phase volume type validation for opentelekomcloud_evs_volume_v3
PR Checklist

 Refers to: #817
 Tests added/passed.
 Schema updated.

Acceptance Steps Performed
=== RUN   TestAccEvsStorageV3Volume_basic
--- PASS: TestAccEvsStorageV3Volume_basic (35.86s)
=== RUN   TestAccEvsStorageV3Volume_tags
--- PASS: TestAccEvsStorageV3Volume_tags (42.65s)
=== RUN   TestAccEvsStorageV3Volume_image
--- PASS: TestAccEvsStorageV3Volume_image (33.65s)
=== RUN   TestAccEvsStorageV3Volume_timeout
--- PASS: TestAccEvsStorageV3Volume_timeout (25.68s)
=== RUN   TestAccEvsStorageV3Volume_volumeType
--- PASS: TestAccEvsStorageV3Volume_volumeType (2.18s)
PASS

Process finished with exit code 0

Reviewed-by: Anton Sidelnikov <None>
Reviewed-by: Rodion Gyrbu <fpsoff@outlook.com>
Reviewed-by: None <None>
  • Loading branch information
outcatcher authored Feb 1, 2021
1 parent 1fef3fe commit f563b76
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 45 deletions.
46 changes: 25 additions & 21 deletions opentelekomcloud/resource_opentelekomcloud_evs_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"log"
"time"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

volumes_v2 "github.com/opentelekomcloud/gophertelekomcloud/openstack/blockstorage/v2/volumes"
volumesV2 "github.com/opentelekomcloud/gophertelekomcloud/openstack/blockstorage/v2/volumes"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/evs/v3/volumes"
)

Expand All @@ -29,6 +30,8 @@ func resourceEvsStorageVolumeV3() *schema.Resource {
Delete: schema.DefaultTimeout(3 * time.Minute),
},

CustomizeDiff: validateVolumeType("volume_type"),

Schema: map[string]*schema.Schema{
"backup_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -70,9 +73,6 @@ func resourceEvsStorageVolumeV3() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"SATA", "SAS", "SSD", "co-p1", "uh-l1",
}, true),
},
"device_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -145,11 +145,11 @@ func resourceEvsVolumeV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
blockStorageClient, err := config.blockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud EVS storage client: %s", err)
return fmt.Errorf("error creating OpenTelekomCloud EVS storage client: %s", err)
}

if !hasFilledOpt(d, "backup_id") && !hasFilledOpt(d, "size") {
return fmt.Errorf("Missing required argument: 'size' is required, but no definition was found.")
return fmt.Errorf("missing required argument: 'size' is required, but no definition was found")
}
tags := resourceContainerTags(d)
createOpts := &volumes.CreateOpts{
Expand Down Expand Up @@ -179,7 +179,7 @@ func resourceEvsVolumeV3Create(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Create Options: %#v", createOpts)
v, err := volumes.Create(blockStorageClient, createOpts).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud EVS volume: %s", err)
return fmt.Errorf("error creating OpenTelekomCloud EVS volume: %s", err)
}
log.Printf("[INFO] Volume Job ID: %s", v.JobID)

Expand All @@ -201,14 +201,14 @@ func resourceEvsVolumeV3Create(d *schema.ResourceData, meta interface{}) error {
d.SetId(id)
return resourceEvsVolumeV3Read(d, meta)
}
return fmt.Errorf("Unexpected conversion error in resourceEvsVolumeV3Create.")
return fmt.Errorf("unexpected conversion error in resourceEvsVolumeV3Create")
}

func resourceEvsVolumeV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
blockStorageClient, err := config.blockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud EVS storage client: %s", err)
return fmt.Errorf("error creating OpenTelekomCloud EVS storage client: %s", err)
}

v, err := volumes.Get(blockStorageClient, d.Id()).Extract()
Expand All @@ -218,14 +218,18 @@ func resourceEvsVolumeV3Read(d *schema.ResourceData, meta interface{}) error {

log.Printf("[DEBUG] Retrieved volume %s: %+v", d.Id(), v)

d.Set("size", v.Size)
d.Set("description", v.Description)
d.Set("availability_zone", v.AvailabilityZone)
d.Set("name", v.Name)
d.Set("snapshot_id", v.SnapshotID)
d.Set("source_vol_id", v.SourceVolID)
d.Set("volume_type", v.VolumeType)
d.Set("wwn", v.WWN)
mErr := multierror.Append(
d.Set("size", v.Size),
d.Set("description", v.Description),
d.Set("availability_zone", v.AvailabilityZone),
d.Set("name", v.Name),
d.Set("snapshot_id", v.SnapshotID),
d.Set("volume_type", v.VolumeType),
d.Set("wwn", v.WWN),
)
if err := mErr.ErrorOrNil(); err != nil {
return err
}

// set tags
tags := make(map[string]string)
Expand Down Expand Up @@ -257,17 +261,17 @@ func resourceEvsVolumeV3Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
blockStorageClient, err := config.blockStorageV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud block storage client: %s", err)
return fmt.Errorf("error creating OpenTelekomCloud block storage client: %s", err)
}

updateOpts := volumes_v2.UpdateOpts{
updateOpts := volumesV2.UpdateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}

_, err = volumes_v2.Update(blockStorageClient, d.Id(), updateOpts).Extract()
_, err = volumesV2.Update(blockStorageClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenTelekomCloud volume: %s", err)
return fmt.Errorf("error updating OpenTelekomCloud volume: %s", err)
}

if d.HasChange("tags") {
Expand Down
71 changes: 47 additions & 24 deletions opentelekomcloud/resource_opentelekomcloud_evs_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opentelekomcloud

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -100,11 +101,26 @@ func TestAccEvsStorageV3Volume_timeout(t *testing.T) {
})
}

func TestAccEvsStorageV3Volume_volumeType(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEvsStorageV3VolumeDestroy,
Steps: []resource.TestStep{
{
Config: testAccEvsStorageV3Volume_volumeType,
PlanOnly: true,
ExpectError: regexp.MustCompile(`volume type .+ doesn't exist`),
},
},
})
}

func testAccCheckEvsStorageV3VolumeDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
blockStorageClient, err := config.blockStorageV3Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud evs storage client: %s", err)
return fmt.Errorf("error creating OpenTelekomCloud evs storage client: %s", err)
}

for _, rs := range s.RootModule().Resources {
Expand All @@ -114,7 +130,7 @@ func testAccCheckEvsStorageV3VolumeDestroy(s *terraform.State) error {

_, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("Volume still exists")
return fmt.Errorf("volume still exists")
}
}

Expand All @@ -125,17 +141,17 @@ func testAccCheckEvsStorageV3VolumeExists(n string, volume *volumes.Volume) reso
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
return fmt.Errorf("not found: %s", n)
}

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

config := testAccProvider.Meta().(*Config)
blockStorageClient, err := config.blockStorageV3Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud evs storage client: %s", err)
return fmt.Errorf("error creating OpenTelekomCloud evs storage client: %s", err)
}

found, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
Expand All @@ -144,7 +160,7 @@ func testAccCheckEvsStorageV3VolumeExists(n string, volume *volumes.Volume) reso
}

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

*volume = *found
Expand All @@ -157,17 +173,17 @@ func testAccCheckEvsStorageV3VolumeTags(n string, k string, v string) resource.T
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
return fmt.Errorf("not found: %s", n)
}

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

config := testAccProvider.Meta().(*Config)
blockStorageClient, err := config.blockStorageV3Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud block storage client: %s", err)
return fmt.Errorf("error creating OpenTelekomCloud block storage client: %s", err)
}

found, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
Expand All @@ -176,11 +192,11 @@ func testAccCheckEvsStorageV3VolumeTags(n string, k string, v string) resource.T
}

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

if found.Tags == nil {
return fmt.Errorf("No Tags")
return fmt.Errorf("no Tags")
}

for key, value := range found.Tags {
Expand All @@ -191,13 +207,14 @@ func testAccCheckEvsStorageV3VolumeTags(n string, k string, v string) resource.T
if v == value {
return nil
}
return fmt.Errorf("Bad value for %s: %s", k, value)
return fmt.Errorf("bad value for %s: %s", k, value)
}
return fmt.Errorf("Tag not found: %s", k)
return fmt.Errorf("tag not found: %s", k)
}
}

var testAccEvsStorageV3Volume_basic = fmt.Sprintf(`
var (
testAccEvsStorageV3Volume_basic = fmt.Sprintf(`
resource "opentelekomcloud_evs_volume_v3" "volume_1" {
name = "volume_1"
description = "first test volume"
Expand All @@ -206,8 +223,7 @@ resource "opentelekomcloud_evs_volume_v3" "volume_1" {
size = 12
}
`, OS_AVAILABILITY_ZONE)

var testAccEvsStorageV3Volume_update = fmt.Sprintf(`
testAccEvsStorageV3Volume_update = fmt.Sprintf(`
resource "opentelekomcloud_evs_volume_v3" "volume_1" {
name = "volume_1-updated"
description = "first test volume"
Expand All @@ -216,8 +232,7 @@ resource "opentelekomcloud_evs_volume_v3" "volume_1" {
size = 12
}
`, OS_AVAILABILITY_ZONE)

var testAccEvsStorageV3Volume_tags = fmt.Sprintf(`
testAccEvsStorageV3Volume_tags = fmt.Sprintf(`
resource "opentelekomcloud_evs_volume_v3" "volume_tags" {
name = "volume_tags"
description = "test volume with tags"
Expand All @@ -230,8 +245,7 @@ resource "opentelekomcloud_evs_volume_v3" "volume_tags" {
size = 12
}
`, OS_AVAILABILITY_ZONE)

var testAccEvsStorageV3Volume_tags_update = fmt.Sprintf(`
testAccEvsStorageV3Volume_tags_update = fmt.Sprintf(`
resource "opentelekomcloud_evs_volume_v3" "volume_tags" {
name = "volume_tags-updated"
description = "test volume with tags"
Expand All @@ -244,8 +258,7 @@ resource "opentelekomcloud_evs_volume_v3" "volume_tags" {
size = 12
}
`, OS_AVAILABILITY_ZONE)

var testAccEvsStorageV3Volume_image = fmt.Sprintf(`
testAccEvsStorageV3Volume_image = fmt.Sprintf(`
resource "opentelekomcloud_evs_volume_v3" "volume_1" {
name = "volume_1"
availability_zone = "%s"
Expand All @@ -254,8 +267,7 @@ resource "opentelekomcloud_evs_volume_v3" "volume_1" {
image_id = "%s"
}
`, OS_AVAILABILITY_ZONE, OS_IMAGE_ID)

var testAccEvsStorageV3Volume_timeout = fmt.Sprintf(`
testAccEvsStorageV3Volume_timeout = fmt.Sprintf(`
resource "opentelekomcloud_evs_volume_v3" "volume_1" {
name = "volume_1"
description = "first test volume"
Expand All @@ -269,3 +281,14 @@ resource "opentelekomcloud_evs_volume_v3" "volume_1" {
}
}
`, OS_AVAILABILITY_ZONE)

testAccEvsStorageV3Volume_volumeType = fmt.Sprintf(`
resource "opentelekomcloud_evs_volume_v3" "volume_1" {
name = "volume_1"
description = "first test volume"
availability_zone = "%s"
volume_type = "asfddasf"
size = 12
}
`, OS_AVAILABILITY_ZONE)
)

0 comments on commit f563b76

Please sign in to comment.