provider/openstack: Fix Creation of Empty Tags

This commit fixes a bug where images would be created with empty tags.
Acceptance tests were also tidied up.
This commit is contained in:
Joe Topjian 2017-02-19 18:44:42 +00:00
parent b3b9247ad0
commit 96d2ac21d4
3 changed files with 73 additions and 21 deletions

View File

@ -7,7 +7,7 @@ import (
)
func TestAccImagesImageV2_importBasic(t *testing.T) {
resourceName := "openstack_images_image_v2.foo"
resourceName := "openstack_images_image_v2.image_1"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },

View File

@ -181,12 +181,12 @@ func resourceImagesImageV2Create(d *schema.ResourceData, meta interface{}) error
Visibility: &visibility,
}
if tags := d.Get("tags"); tags != nil {
ts := tags.([]interface{})
createOpts.Tags = make([]string, len(ts))
for _, v := range ts {
createOpts.Tags = append(createOpts.Tags, v.(string))
if v, ok := d.GetOk("tags"); ok {
var tags []string
for _, tag := range v.([]interface{}) {
tags = append(tags, tag.(string))
}
createOpts.Tags = tags
}
d.Partial(true)
@ -273,7 +273,7 @@ func resourceImagesImageV2Read(d *schema.ResourceData, meta interface{}) error {
d.Set("name", img.Name)
d.Set("protected", img.Protected)
d.Set("size_bytes", img.SizeBytes)
d.Set("tags", remove_empty(img.Tags))
d.Set("tags", resourceImagesImageV2RemoveEmptyTags(img.Tags))
d.Set("visibility", img.Visibility)
return nil
@ -476,7 +476,7 @@ func resourceImagesImageV2RefreshFunc(client *gophercloud.ServiceClient, id stri
}
}
func remove_empty(s []string) []string {
func resourceImagesImageV2RemoveEmptyTags(s []string) []string {
var r []string
for _, str := range s {
if str != "" {

View File

@ -8,10 +8,11 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"strings"
)
func TestAccImagesImageV2_basic(t *testing.T) {
var image images.Image
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
@ -20,7 +21,15 @@ func TestAccImagesImageV2_basic(t *testing.T) {
resource.TestStep{
Config: testAccImagesImageV2_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckImagesImageV2Exists(t, "openstack_images_image_v2.foo"),
testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image),
resource.TestCheckResourceAttr(
"openstack_images_image_v2.image_1", "name", "Rancher TerraformAccTest"),
resource.TestCheckResourceAttr(
"openstack_images_image_v2.image_1", "container_format", "bare"),
resource.TestCheckResourceAttr(
"openstack_images_image_v2.image_1", "disk_format", "qcow2"),
resource.TestCheckResourceAttr(
"openstack_images_image_v2.image_1", "schema", "/v2/schemas/image"),
),
},
},
@ -28,6 +37,8 @@ func TestAccImagesImageV2_basic(t *testing.T) {
}
func TestAccImagesImageV2_with_tags(t *testing.T) {
var image images.Image
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
@ -36,7 +47,10 @@ func TestAccImagesImageV2_with_tags(t *testing.T) {
resource.TestStep{
Config: testAccImagesImageV2_with_tags,
Check: resource.ComposeTestCheckFunc(
testAccCheckImagesImageV2HasTags(t, "openstack_images_image_v2.foo"),
testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image),
testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "foo"),
testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "bar"),
testAccCheckImagesImageV2TagCount("openstack_images_image_v2.image_1", 2),
),
},
},
@ -47,7 +61,7 @@ func testAccCheckImagesImageV2Destroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
imageClient, err := config.imageV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("(testAccCheckImagesImageV2Destroy) Error creating OpenStack Image: %s", err)
return fmt.Errorf("Error creating OpenStack Image: %s", err)
}
for _, rs := range s.RootModule().Resources {
@ -64,7 +78,7 @@ func testAccCheckImagesImageV2Destroy(s *terraform.State) error {
return nil
}
func testAccCheckImagesImageV2Exists(t *testing.T, n string) resource.TestCheckFunc {
func testAccCheckImagesImageV2Exists(n string, image *images.Image) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
@ -78,7 +92,7 @@ func testAccCheckImagesImageV2Exists(t *testing.T, n string) resource.TestCheckF
config := testAccProvider.Meta().(*Config)
imageClient, err := config.imageV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("(testAccCheckImagesImageV2Destroy) Error creating OpenStack Image: %s", err)
return fmt.Errorf("Error creating OpenStack Image: %s", err)
}
found, err := images.Get(imageClient, rs.Primary.ID).Extract()
@ -90,11 +104,13 @@ func testAccCheckImagesImageV2Exists(t *testing.T, n string) resource.TestCheckF
return fmt.Errorf("Image not found")
}
*image = *found
return nil
}
}
func testAccCheckImagesImageV2HasTags(t *testing.T, n string) resource.TestCheckFunc {
func testAccCheckImagesImageV2HasTag(n, tag string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
@ -108,7 +124,7 @@ func testAccCheckImagesImageV2HasTags(t *testing.T, n string) resource.TestCheck
config := testAccProvider.Meta().(*Config)
imageClient, err := config.imageV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("(testAccCheckImagesImageV2Destroy) Error creating OpenStack Image: %s", err)
return fmt.Errorf("Error creating OpenStack Image: %s", err)
}
found, err := images.Get(imageClient, rs.Primary.ID).Extract()
@ -120,16 +136,52 @@ func testAccCheckImagesImageV2HasTags(t *testing.T, n string) resource.TestCheck
return fmt.Errorf("Image not found")
}
tags := strings.Join(found.Tags, "")
if tags != "foobar" && tags != "barfoo" {
return fmt.Errorf("Image tags are %#v and should be \"foo, bar\"", found.Tags)
for _, v := range found.Tags {
if tag == v {
return nil
}
}
return fmt.Errorf("Tag not found: %s", tag)
}
}
func testAccCheckImagesImageV2TagCount(n string, expected int) 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 := testAccProvider.Meta().(*Config)
imageClient, err := config.imageV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating OpenStack Image: %s", err)
}
found, err := images.Get(imageClient, rs.Primary.ID).Extract()
if err != nil {
return err
}
if found.ID != rs.Primary.ID {
return fmt.Errorf("Image not found")
}
if len(found.Tags) != expected {
return fmt.Errorf("Expecting %d tags, found %d", expected, len(found.Tags))
}
return nil
}
}
var testAccImagesImageV2_basic = `
resource "openstack_images_image_v2" "foo" {
resource "openstack_images_image_v2" "image_1" {
name = "Rancher TerraformAccTest"
image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img"
container_format = "bare"
@ -137,7 +189,7 @@ var testAccImagesImageV2_basic = `
}`
var testAccImagesImageV2_with_tags = `
resource "openstack_images_image_v2" "foo" {
resource "openstack_images_image_v2" "image_1" {
name = "Rancher TerraformAccTest"
image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img"
container_format = "bare"