github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_secondary_ipaddress_test.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/xanzy/go-cloudstack/cloudstack" 10 ) 11 12 func TestAccCloudStackSecondaryIPAddress_basic(t *testing.T) { 13 var ip cloudstack.AddIpToNicResponse 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckCloudStackSecondaryIPAddressDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCloudStackSecondaryIPAddress_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckCloudStackSecondaryIPAddressExists( 24 "cloudstack_secondary_ipaddress.foo", &ip), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccCloudStackSecondaryIPAddress_fixedIP(t *testing.T) { 32 var ip cloudstack.AddIpToNicResponse 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccCheckCloudStackSecondaryIPAddressDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testAccCloudStackSecondaryIPAddress_fixedIP, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckCloudStackSecondaryIPAddressExists( 43 "cloudstack_secondary_ipaddress.foo", &ip), 44 testAccCheckCloudStackSecondaryIPAddressAttributes(&ip), 45 resource.TestCheckResourceAttr( 46 "cloudstack_secondary_ipaddress.foo", "ipaddress", CLOUDSTACK_NETWORK_1_IPADDRESS), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func testAccCheckCloudStackSecondaryIPAddressExists( 54 n string, ip *cloudstack.AddIpToNicResponse) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 rs, ok := s.RootModule().Resources[n] 57 if !ok { 58 return fmt.Errorf("Not found: %s", n) 59 } 60 61 if rs.Primary.ID == "" { 62 return fmt.Errorf("No IP address ID is set") 63 } 64 65 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 66 67 // Retrieve the virtual_machine ID 68 virtualmachineid, e := retrieveID( 69 cs, "virtual_machine", rs.Primary.Attributes["virtual_machine"]) 70 if e != nil { 71 return e.Error() 72 } 73 74 // Get the virtual machine details 75 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid) 76 if err != nil { 77 if count == 0 { 78 return fmt.Errorf("Instance not found") 79 } 80 return err 81 } 82 83 nicid := rs.Primary.Attributes["nicid"] 84 if nicid == "" { 85 nicid = vm.Nic[0].Id 86 } 87 88 p := cs.Nic.NewListNicsParams(virtualmachineid) 89 p.SetNicid(nicid) 90 91 l, err := cs.Nic.ListNics(p) 92 if err != nil { 93 return err 94 } 95 96 if l.Count == 0 { 97 return fmt.Errorf("NIC not found") 98 } 99 100 if l.Count > 1 { 101 return fmt.Errorf("Found more then one possible result: %v", l.Nics) 102 } 103 104 for _, sip := range l.Nics[0].Secondaryip { 105 if sip.Id == rs.Primary.ID { 106 ip.Ipaddress = sip.Ipaddress 107 ip.Nicid = l.Nics[0].Id 108 return nil 109 } 110 } 111 112 return fmt.Errorf("IP address not found") 113 } 114 } 115 116 func testAccCheckCloudStackSecondaryIPAddressAttributes( 117 ip *cloudstack.AddIpToNicResponse) resource.TestCheckFunc { 118 return func(s *terraform.State) error { 119 120 if ip.Ipaddress != CLOUDSTACK_NETWORK_1_IPADDRESS { 121 return fmt.Errorf("Bad IP address: %s", ip.Ipaddress) 122 } 123 return nil 124 } 125 } 126 127 func testAccCheckCloudStackSecondaryIPAddressDestroy(s *terraform.State) error { 128 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 129 130 for _, rs := range s.RootModule().Resources { 131 if rs.Type != "cloudstack_secondary_ipaddress" { 132 continue 133 } 134 135 if rs.Primary.ID == "" { 136 return fmt.Errorf("No IP address ID is set") 137 } 138 139 // Retrieve the virtual_machine ID 140 virtualmachineid, e := retrieveID( 141 cs, "virtual_machine", rs.Primary.Attributes["virtual_machine"]) 142 if e != nil { 143 return e.Error() 144 } 145 146 // Get the virtual machine details 147 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid) 148 if err != nil { 149 if count == 0 { 150 return fmt.Errorf("Instance not found") 151 } 152 return err 153 } 154 155 nicid := rs.Primary.Attributes["nicid"] 156 if nicid == "" { 157 nicid = vm.Nic[0].Id 158 } 159 160 p := cs.Nic.NewListNicsParams(virtualmachineid) 161 p.SetNicid(nicid) 162 163 l, err := cs.Nic.ListNics(p) 164 if err != nil { 165 return err 166 } 167 168 if l.Count == 0 { 169 return fmt.Errorf("NIC not found") 170 } 171 172 if l.Count > 1 { 173 return fmt.Errorf("Found more then one possible result: %v", l.Nics) 174 } 175 176 for _, sip := range l.Nics[0].Secondaryip { 177 if sip.Id == rs.Primary.ID { 178 return fmt.Errorf("IP address %s still exists", rs.Primary.ID) 179 } 180 } 181 182 return nil 183 } 184 185 return nil 186 } 187 188 var testAccCloudStackSecondaryIPAddress_basic = fmt.Sprintf(` 189 resource "cloudstack_instance" "foobar" { 190 name = "terraform-test" 191 service_offering= "%s" 192 network = "%s" 193 template = "%s" 194 zone = "%s" 195 expunge = true 196 } 197 198 resource "cloudstack_secondary_ipaddress" "foo" { 199 virtual_machine = "${cloudstack_instance.foobar.id}" 200 } 201 `, 202 CLOUDSTACK_SERVICE_OFFERING_1, 203 CLOUDSTACK_NETWORK_1, 204 CLOUDSTACK_TEMPLATE, 205 CLOUDSTACK_ZONE) 206 207 var testAccCloudStackSecondaryIPAddress_fixedIP = fmt.Sprintf(` 208 resource "cloudstack_instance" "foobar" { 209 name = "terraform-test" 210 service_offering= "%s" 211 network = "%s" 212 template = "%s" 213 zone = "%s" 214 expunge = true 215 } 216 217 resource "cloudstack_secondary_ipaddress" "foo" { 218 ipaddress = "%s" 219 virtual_machine = "${cloudstack_instance.foobar.id}" 220 }`, 221 CLOUDSTACK_SERVICE_OFFERING_1, 222 CLOUDSTACK_NETWORK_1, 223 CLOUDSTACK_TEMPLATE, 224 CLOUDSTACK_ZONE, 225 CLOUDSTACK_NETWORK_1_IPADDRESS)