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 == "" {
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 == "" {
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
var CLOUDSTACK_DISK_OFFERING_1 = os.Getenv("CLOUDSTACK_DISK_OFFERING_1")

View File

@ -25,6 +25,7 @@ func resourceCloudStackAffinityGroup() *schema.Resource {
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
@ -49,13 +50,14 @@ func resourceCloudStackAffinityGroupCreate(d *schema.ResourceData, meta interfac
name := d.Get("name").(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)
// Set the description
if description, ok := d.GetOk("description"); ok {
p.SetDescription(description.(string))
} else {
p.SetDescription(name)
}
// 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
}
log.Printf("[DEBUG] Creating affinity group %s", name)
r, err := cs.AffinityGroup.CreateAffinityGroup(p)
if err != nil {
return err
}
log.Printf("[DEBUG] New affinity group successfully created")
log.Printf("[DEBUG] Affinity group %s successfully created", name)
d.SetId(r.Id)
return resourceCloudStackAffinityGroupRead(d, meta)
@ -77,20 +80,24 @@ func resourceCloudStackAffinityGroupCreate(d *schema.ResourceData, meta interfac
func resourceCloudStackAffinityGroupRead(d *schema.ResourceData, meta interface{}) error {
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
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 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("")
return nil
}
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("description", ag.Description)
d.Set("type", ag.Type)
@ -103,8 +110,6 @@ func resourceCloudStackAffinityGroupDelete(d *schema.ResourceData, meta interfac
// Create a new parameter struct
p := cs.AffinityGroup.NewDeleteAffinityGroupParams()
// Set id
p.SetId(d.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
}
// Remove the affinity group
// Delete the affinity group
_, err := cs.AffinityGroup.DeleteAffinityGroup(p)
if err != nil {
// 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())) {
return nil
}
return fmt.Errorf("Error deleting affinity group: %s", err)
}

View File

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

View File

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