github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/clc/resource_clc_group_test.go (about) 1 package clc 2 3 import ( 4 "fmt" 5 "testing" 6 7 clc "github.com/CenturyLinkCloud/clc-sdk" 8 "github.com/CenturyLinkCloud/clc-sdk/group" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 // things to test: 14 // resolves to existing group 15 // does not nuke a group w/ no parents (root group) 16 // change a name on a group 17 18 func TestAccGroupBasic(t *testing.T) { 19 var resp group.Response 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckGroupDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccCheckGroupConfigBasic, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckGroupExists("clc_group.acc_test_group", &resp), 29 testAccCheckGroupParent(&resp, "Default Group"), 30 resource.TestCheckResourceAttr( 31 "clc_group.acc_test_group", "name", "okcomputer"), 32 resource.TestCheckResourceAttr( 33 "clc_group.acc_test_group", "location_id", testAccDC), 34 ), 35 }, 36 resource.TestStep{ 37 Config: testAccCheckGroupConfigUpdate, 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckGroupExists("clc_group.acc_test_group", &resp), 40 testAccCheckGroupParent(&resp, "Default Group"), 41 resource.TestCheckResourceAttr( 42 "clc_group.acc_test_group", "name", "foobar"), 43 resource.TestCheckResourceAttr( 44 "clc_group.acc_test_group", "location_id", testAccDC), 45 ), 46 }, 47 resource.TestStep{ 48 Config: testAccCheckGroupConfigReparent, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckGroupExists("clc_group.acc_test_group", &resp), 51 testAccCheckGroupParent(&resp, "reparent"), 52 resource.TestCheckResourceAttr( 53 "clc_group.acc_test_group", "name", "foobar"), 54 resource.TestCheckResourceAttr( 55 "clc_group.acc_test_group", "location_id", testAccDC), 56 ), 57 }, 58 }, 59 }) 60 } 61 62 func testAccCheckGroupDestroy(s *terraform.State) error { 63 client := testAccProvider.Meta().(*clc.Client) 64 for _, rs := range s.RootModule().Resources { 65 if rs.Type != "clc_group" { 66 continue 67 } 68 _, err := client.Group.Get(rs.Primary.ID) 69 if err == nil { 70 return fmt.Errorf("Group still exists") 71 } 72 } 73 return nil 74 } 75 76 func testAccCheckGroupParent(resp *group.Response, expectedName string) resource.TestCheckFunc { 77 return func(s *terraform.State) error { 78 client := testAccProvider.Meta().(*clc.Client) 79 ok, l := resp.Links.GetLink("parentGroup") 80 if !ok { 81 return fmt.Errorf("Missing parent group: %v", resp) 82 } 83 parent, err := client.Group.Get(l.ID) 84 if err != nil { 85 return fmt.Errorf("Failed fetching parent %v: %v", l.ID, err) 86 } 87 if parent.Name != expectedName { 88 return fmt.Errorf("Incorrect parent found:'%v' expected:'%v'", parent.Name, expectedName) 89 } 90 // would be good to test parent but we'd have to make a bunch of calls 91 return nil 92 } 93 } 94 95 func testAccCheckGroupExists(n string, resp *group.Response) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 rs, ok := s.RootModule().Resources[n] 98 if !ok { 99 return fmt.Errorf("Not found: %s", n) 100 } 101 if rs.Primary.ID == "" { 102 return fmt.Errorf("No Group ID is set") 103 } 104 105 client := testAccProvider.Meta().(*clc.Client) 106 g, err := client.Group.Get(rs.Primary.ID) 107 if err != nil { 108 return err 109 } 110 111 if g.ID != rs.Primary.ID { 112 return fmt.Errorf("Group not found") 113 } 114 *resp = *g 115 return nil 116 } 117 } 118 119 const testAccCheckGroupConfigBasic = ` 120 variable "dc" { default = "IL1" } 121 122 resource "clc_group" "acc_test_group" { 123 location_id = "${var.dc}" 124 name = "okcomputer" 125 description = "mishaps happening" 126 parent = "Default Group" 127 }` 128 129 const testAccCheckGroupConfigUpdate = ` 130 variable "dc" { default = "IL1" } 131 132 resource "clc_group" "acc_test_group" { 133 location_id = "${var.dc}" 134 name = "foobar" 135 description = "update test" 136 parent = "Default Group" 137 }` 138 139 const testAccCheckGroupConfigReparent = ` 140 variable "dc" { default = "IL1" } 141 142 resource "clc_group" "acc_test_group_reparent" { 143 location_id = "${var.dc}" 144 name = "reparent" 145 description = "introduce a parent group in place" 146 parent = "Default Group" 147 } 148 149 resource "clc_group" "acc_test_group" { 150 location_id = "${var.dc}" 151 name = "foobar" 152 description = "update test" 153 parent = "${clc_group.acc_test_group_reparent.id}" 154 } 155 `