Fix the acceptance tests and some cosmetic tweaks (#8598)

This commit is contained in:
Sander van Harmelen 2016-09-01 11:19:37 +02:00 committed by GitHub
parent 0835b64456
commit 1a85d06843
4 changed files with 39 additions and 76 deletions

View File

@ -66,9 +66,6 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CLOUDSTACK_2ND_NIC_NETWORK"); v == "" { if v := os.Getenv("CLOUDSTACK_2ND_NIC_NETWORK"); v == "" {
t.Fatal("CLOUDSTACK_2ND_NIC_NETWORK must be set for acceptance tests") t.Fatal("CLOUDSTACK_2ND_NIC_NETWORK must be set for acceptance tests")
} }
if v := os.Getenv("CLOUDSTACK_AFFINITY_GROUP_TYPE"); v == "" {
t.Fatal("CLOUDSTACK_AFFINITY_GROUP_TYPE must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_DISK_OFFERING_1"); v == "" { if v := os.Getenv("CLOUDSTACK_DISK_OFFERING_1"); v == "" {
t.Fatal("CLOUDSTACK_DISK_OFFERING_1 must be set for acceptance tests") t.Fatal("CLOUDSTACK_DISK_OFFERING_1 must be set for acceptance tests")
} }
@ -149,9 +146,6 @@ func testAccPreCheck(t *testing.T) {
} }
} }
// Name of an affinity group type
var CLOUDSTACK_AFFINITY_GROUP_TYPE = os.Getenv("CLOUDSTACK_AFFINITY_GROUP_TYPE")
// Name of a valid disk offering // Name of a valid disk offering
var CLOUDSTACK_DISK_OFFERING_1 = os.Getenv("CLOUDSTACK_DISK_OFFERING_1") var CLOUDSTACK_DISK_OFFERING_1 = os.Getenv("CLOUDSTACK_DISK_OFFERING_1")

View File

@ -25,6 +25,7 @@ func resourceCloudStackAffinityGroup() *schema.Resource {
"description": &schema.Schema{ "description": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: true,
ForceNew: true, ForceNew: true,
}, },
@ -49,13 +50,14 @@ func resourceCloudStackAffinityGroupCreate(d *schema.ResourceData, meta interfac
name := d.Get("name").(string) name := d.Get("name").(string)
affinityGroupType := d.Get("type").(string) affinityGroupType := d.Get("type").(string)
log.Printf("[DEBUG] creating affinity group with name %s of type %s", name, affinityGroupType) // Create a new parameter struct
p := cs.AffinityGroup.NewCreateAffinityGroupParams(name, affinityGroupType) p := cs.AffinityGroup.NewCreateAffinityGroupParams(name, affinityGroupType)
// Set the description // Set the description
if description, ok := d.GetOk("description"); ok { if description, ok := d.GetOk("description"); ok {
p.SetDescription(description.(string)) p.SetDescription(description.(string))
} else {
p.SetDescription(name)
} }
// If there is a project supplied, we retrieve and set the project id // If there is a project supplied, we retrieve and set the project id
@ -63,12 +65,13 @@ func resourceCloudStackAffinityGroupCreate(d *schema.ResourceData, meta interfac
return err return err
} }
log.Printf("[DEBUG] Creating affinity group %s", name)
r, err := cs.AffinityGroup.CreateAffinityGroup(p) r, err := cs.AffinityGroup.CreateAffinityGroup(p)
if err != nil { if err != nil {
return err return err
} }
log.Printf("[DEBUG] New affinity group successfully created") log.Printf("[DEBUG] Affinity group %s successfully created", name)
d.SetId(r.Id) d.SetId(r.Id)
return resourceCloudStackAffinityGroupRead(d, meta) return resourceCloudStackAffinityGroupRead(d, meta)
@ -77,20 +80,24 @@ func resourceCloudStackAffinityGroupCreate(d *schema.ResourceData, meta interfac
func resourceCloudStackAffinityGroupRead(d *schema.ResourceData, meta interface{}) error { func resourceCloudStackAffinityGroupRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient) cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] looking for affinity group with name %s", d.Id()) log.Printf("[DEBUG] Rerieving affinity group %s", d.Get("name").(string))
// Get the affinity group details // Get the affinity group details
ag, count, err := cs.AffinityGroup.GetAffinityGroupByID(d.Id(), cloudstack.WithProject(d.Get("project").(string))) ag, count, err := cs.AffinityGroup.GetAffinityGroupByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil { if err != nil {
if count == 0 { if count == 0 {
log.Printf("[DEBUG] Affinity group %s does not longer exist", d.Id()) log.Printf("[DEBUG] Affinity group %s does not longer exist", d.Get("name").(string))
d.SetId("") d.SetId("")
return nil return nil
} }
return err return err
} }
//Affinity group name is unique in a cloudstack account so dont need to check for multiple // Update the config
d.Set("name", ag.Name) d.Set("name", ag.Name)
d.Set("description", ag.Description) d.Set("description", ag.Description)
d.Set("type", ag.Type) d.Set("type", ag.Type)
@ -103,8 +110,6 @@ func resourceCloudStackAffinityGroupDelete(d *schema.ResourceData, meta interfac
// Create a new parameter struct // Create a new parameter struct
p := cs.AffinityGroup.NewDeleteAffinityGroupParams() p := cs.AffinityGroup.NewDeleteAffinityGroupParams()
// Set id
p.SetId(d.Id()) p.SetId(d.Id())
// If there is a project supplied, we retrieve and set the project id // If there is a project supplied, we retrieve and set the project id
@ -112,7 +117,7 @@ func resourceCloudStackAffinityGroupDelete(d *schema.ResourceData, meta interfac
return err return err
} }
// Remove the affinity group // Delete the affinity group
_, err := cs.AffinityGroup.DeleteAffinityGroup(p) _, err := cs.AffinityGroup.DeleteAffinityGroup(p)
if err != nil { if err != nil {
// This is a very poor way to be told the ID does no longer exist :( // This is a very poor way to be told the ID does no longer exist :(
@ -121,6 +126,7 @@ func resourceCloudStackAffinityGroupDelete(d *schema.ResourceData, meta interfac
"or entity does not exist", d.Id())) { "or entity does not exist", d.Id())) {
return nil return nil
} }
return fmt.Errorf("Error deleting affinity group: %s", err) return fmt.Errorf("Error deleting affinity group: %s", err)
} }

View File

@ -2,7 +2,6 @@ package cloudstack
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
@ -19,18 +18,18 @@ func TestAccCloudStackAffinityGroup_basic(t *testing.T) {
CheckDestroy: testAccCheckCloudStackAffinityGroupDestroy, CheckDestroy: testAccCheckCloudStackAffinityGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackAffinityGroupPair, Config: testAccCloudStackAffinityGroup,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackAffinityGroupExists("terraform-test-affinity-group", &affinityGroup), testAccCheckCloudStackAffinityGroupExists("cloudstack_affinity_group.foo", &affinityGroup),
testAccCheckCloudStackAffinityGroupAttributes(&affinityGroup), testAccCheckCloudStackAffinityGroupAttributes(&affinityGroup),
testAccCheckCloudStackAffinityGroupCreateAttributes("terraform-test-affinity-group"),
), ),
}, },
}, },
}) })
} }
func testAccCheckCloudStackAffinityGroupExists(n string, affinityGroup *cloudstack.AffinityGroup) resource.TestCheckFunc { func testAccCheckCloudStackAffinityGroupExists(
n string, affinityGroup *cloudstack.AffinityGroup) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
@ -42,19 +41,17 @@ func testAccCheckCloudStackAffinityGroupExists(n string, affinityGroup *cloudsta
} }
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
p := cs.AffinityGroup.NewListAffinityGroupsParams() ag, _, err := cs.AffinityGroup.GetAffinityGroupByID(rs.Primary.ID)
p.SetName(rs.Primary.ID)
list, err := cs.AffinityGroup.ListAffinityGroups(p)
if err != nil { if err != nil {
return err return err
} }
if list.Count != 1 || list.AffinityGroups[0].Name != rs.Primary.ID { if ag.Id != rs.Primary.ID {
return fmt.Errorf("Affinity group not found") return fmt.Errorf("Affinity group not found")
} }
*affinityGroup = *list.AffinityGroups[0] *affinityGroup = *ag
return nil return nil
} }
@ -64,40 +61,16 @@ func testAccCheckCloudStackAffinityGroupAttributes(
affinityGroup *cloudstack.AffinityGroup) resource.TestCheckFunc { affinityGroup *cloudstack.AffinityGroup) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if affinityGroup.Type != CLOUDSTACK_AFFINITY_GROUP_TYPE { if affinityGroup.Name != "terraform-affinity-group" {
return fmt.Errorf("Affinity group: Attribute type expected %s, got %s", return fmt.Errorf("Bad name: %s", affinityGroup.Name)
CLOUDSTACK_AFFINITY_GROUP_TYPE, affinityGroup.Type)
} }
return nil if affinityGroup.Description != "terraform-affinity-group" {
} return fmt.Errorf("Bad description: %s", affinityGroup.Description)
}
func testAccCheckCloudStackAffinityGroupCreateAttributes(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
found := false
for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudstack_affinity_group" {
continue
}
if rs.Primary.ID != name {
continue
}
if !strings.Contains(rs.Primary.Attributes["description"], "terraform-test-description") {
return fmt.Errorf(
"Affiity group: Attribute description expected 'terraform-test-description' to be present, got %s",
rs.Primary.Attributes["description"])
}
found = true
break
} }
if !found { if affinityGroup.Type != "host anti-affinity" {
return fmt.Errorf("Could not find affinity group %s", name) return fmt.Errorf("Bad type: %s", affinityGroup.Type)
} }
return nil return nil
@ -116,27 +89,17 @@ func testAccCheckCloudStackAffinityGroupDestroy(s *terraform.State) error {
return fmt.Errorf("No affinity group ID is set") return fmt.Errorf("No affinity group ID is set")
} }
p := cs.AffinityGroup.NewListAffinityGroupsParams() _, _, err := cs.AffinityGroup.GetAffinityGroupByID(rs.Primary.ID)
p.SetName(rs.Primary.ID) if err == nil {
return fmt.Errorf("Affinity group %s still exists", rs.Primary.ID)
r, err := cs.AffinityGroup.ListAffinityGroups(p)
if err != nil {
return err
}
for i := 0; i < r.Count; i++ {
if r.AffinityGroups[i].Id == rs.Primary.ID {
return fmt.Errorf("Affinity group %s still exists", rs.Primary.ID)
}
} }
} }
return nil return nil
} }
var testAccCloudStackAffinityGroupPair = fmt.Sprintf(` var testAccCloudStackAffinityGroup = fmt.Sprintf(`
resource "cloudstack_affinity_group" "foo" { resource "cloudstack_affinity_group" "foo" {
name = "terraform-test-affinty-group" name = "terraform-affinity-group"
type = "%s" type = "host anti-affinity"
description = "terraform-test-description" }`)
}`, CLOUDSTACK_AFFINITY_GROUP_TYPE)

View File

@ -14,9 +14,8 @@ Creates an affinity group.
``` ```
resource "cloudstack_affinity_group" "default" { resource "cloudstack_affinity_group" "default" {
name = "myGroup" name = "test-affinity-group"
type = "anti-affinity" type = "host anti-affinity"
project = "myProject"
} }
``` ```
@ -40,3 +39,4 @@ The following arguments are supported:
The following attributes are exported: The following attributes are exported:
* `id` - The id of the affinity group. * `id` - The id of the affinity group.
* `description` - The description of the affinity group.