Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ims): refactor ims_evs_data_image resource code style #6618

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,56 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/chnsz/golangsdk/openstack/ims/v2/cloudimages"
"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func getEvsDataImageResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
var (
region = acceptance.HW_REGION_NAME
product = "ims"
httpUrl = "v2/cloudimages"
)

client, err := cfg.NewServiceClient(product, region)
if err != nil {
return nil, fmt.Errorf("error creating IMS client: %s", err)
}

getPath := client.Endpoint + httpUrl
getPath += fmt.Sprintf("?id=%s", state.Primary.ID)
getOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}

getResp, err := client.Request("GET", getPath, &getOpt)
if err != nil {
return nil, fmt.Errorf("error retrieving IMS EVS data image: %s", err)
}

getRespBody, err := utils.FlattenResponse(getResp)
if err != nil {
return nil, err
}

image := utils.PathSearch("images[0]", getRespBody, nil)
// If the list API return empty, then return `404` error code.
if image == nil {
return nil, golangsdk.ErrDefault404{}
}

return image, nil
}

func TestAccEvsDataImage_basic(t *testing.T) {
var (
image cloudimages.Image
image interface{}
rName = acceptance.RandomAccResourceName()
rNameUpdate = rName + "-update"
resourceName = "huaweicloud_ims_evs_data_image.test"
Expand All @@ -25,7 +66,7 @@ func TestAccEvsDataImage_basic(t *testing.T) {
rc := acceptance.InitResourceCheck(
resourceName,
&image,
getImsImageResourceFunc,
getEvsDataImageResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -101,6 +142,27 @@ func testAccEvsDataImage_base(rName string) string {
return fmt.Sprintf(`
%[1]s

data "huaweicloud_availability_zones" "test" {}

data "huaweicloud_compute_flavors" "test" {
availability_zone = data.huaweicloud_availability_zones.test.names[0]
performance_type = "normal"
cpu_core_count = 2
memory_size = 4
}

resource "huaweicloud_compute_instance" "test" {
name = "%[2]s"
image_name = "Ubuntu 18.04 server 64bit"
flavor_id = data.huaweicloud_compute_flavors.test.ids[0]
security_group_ids = [huaweicloud_networking_secgroup.test.id]
availability_zone = data.huaweicloud_availability_zones.test.names[0]

network {
uuid = huaweicloud_vpc_subnet.test.id
}
}

resource "huaweicloud_evs_volume" "test" {
name = "%[2]s"
volume_type = "GPSSD"
Expand All @@ -109,7 +171,7 @@ resource "huaweicloud_evs_volume" "test" {
size = 100
charging_mode = "postPaid"
}
`, testAccEcsSystemImage_base(rName), rName)
`, common.TestBaseNetwork(rName), rName)
}

func testAccEvsDataImage_basic(rName string) string {
Expand All @@ -136,6 +198,7 @@ func testAccEvsDataImage_update1(rName, rNameUpdate, migrateEpsId string) string
resource "huaweicloud_ims_evs_data_image" "test" {
name = "%[2]s"
volume_id = huaweicloud_evs_volume.test.id
description = ""
enterprise_project_id = "%[3]s"

tags = {
Expand Down
Loading
Loading