mirror of https://github.com/hashicorp/terraform
parent
7f908e0ac6
commit
28b7b53be6
@ -0,0 +1,160 @@
|
||||
package cloudstack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/xanzy/go-cloudstack/cloudstack"
|
||||
)
|
||||
|
||||
func resourceCloudStackSecondaryIPAddress() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceCloudStackSecondaryIPAddressCreate,
|
||||
Read: resourceCloudStackSecondaryIPAddressRead,
|
||||
Delete: resourceCloudStackSecondaryIPAddressDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"ipaddress": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"nicid": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"virtual_machine": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceCloudStackSecondaryIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
cs := meta.(*cloudstack.CloudStackClient)
|
||||
|
||||
nicid := d.Get("nicid").(string)
|
||||
if nicid == "" {
|
||||
// Retrieve the virtual_machine UUID
|
||||
virtualmachineid, e := retrieveUUID(cs, "virtual_machine", d.Get("virtual_machine").(string))
|
||||
if e != nil {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
// Get the virtual machine details
|
||||
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
|
||||
if err != nil {
|
||||
if count == 0 {
|
||||
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
nicid = vm.Nic[0].Id
|
||||
}
|
||||
|
||||
// Create a new parameter struct
|
||||
p := cs.Nic.NewAddIpToNicParams(nicid)
|
||||
|
||||
if addr := d.Get("ipaddress").(string); addr != "" {
|
||||
p.SetIpaddress(addr)
|
||||
}
|
||||
|
||||
ip, err := cs.Nic.AddIpToNic(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(ip.Id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceCloudStackSecondaryIPAddressRead(d *schema.ResourceData, meta interface{}) error {
|
||||
cs := meta.(*cloudstack.CloudStackClient)
|
||||
|
||||
// Retrieve the virtual_machine UUID
|
||||
virtualmachineid, e := retrieveUUID(cs, "virtual_machine", d.Get("virtual_machine").(string))
|
||||
if e != nil {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
// Get the virtual machine details
|
||||
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
|
||||
if err != nil {
|
||||
if count == 0 {
|
||||
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
nicid := d.Get("nicid").(string)
|
||||
if nicid == "" {
|
||||
nicid = vm.Nic[0].Id
|
||||
}
|
||||
|
||||
p := cs.Nic.NewListNicsParams(virtualmachineid)
|
||||
p.SetNicid(nicid)
|
||||
|
||||
l, err := cs.Nic.ListNics(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Count == 0 {
|
||||
log.Printf("[DEBUG] NIC %s does no longer exist", d.Get("nicid").(string))
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
|
||||
if l.Count > 1 {
|
||||
return fmt.Errorf("Found more then one possible result: %v", l.Nics)
|
||||
}
|
||||
|
||||
for _, ip := range l.Nics[0].Secondaryip {
|
||||
if ip.Id == d.Id() {
|
||||
d.Set("ipaddress", ip.Ipaddress)
|
||||
d.Set("nicid", l.Nics[0].Id)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] IP %s no longer exist", d.Get("ipaddress").(string))
|
||||
d.SetId("")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceCloudStackSecondaryIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
cs := meta.(*cloudstack.CloudStackClient)
|
||||
|
||||
// Create a new parameter struct
|
||||
p := cs.Nic.NewRemoveIpFromNicParams(d.Id())
|
||||
|
||||
log.Printf("[INFO] Removing secondary IP address: %s", d.Get("ipaddress").(string))
|
||||
if _, err := cs.Nic.RemoveIpFromNic(p); err != nil {
|
||||
// This is a very poor way to be told the UUID does no longer exist :(
|
||||
if strings.Contains(err.Error(), fmt.Sprintf(
|
||||
"Invalid parameter id value=%s due to incorrect long value format, "+
|
||||
"or entity does not exist", d.Id())) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("Error removing secondary IP address: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,225 @@
|
||||
package cloudstack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/xanzy/go-cloudstack/cloudstack"
|
||||
)
|
||||
|
||||
func TestAccCloudStackSecondaryIPAddress_basic(t *testing.T) {
|
||||
var ip cloudstack.AddIpToNicResponse
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckCloudStackSecondaryIPAddressDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCloudStackSecondaryIPAddress_basic,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckCloudStackSecondaryIPAddressExists(
|
||||
"cloudstack_secondary_ipaddress.foo", &ip),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccCloudStackSecondaryIPAddress_fixedIP(t *testing.T) {
|
||||
var ip cloudstack.AddIpToNicResponse
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckCloudStackSecondaryIPAddressDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCloudStackSecondaryIPAddress_fixedIP,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckCloudStackSecondaryIPAddressExists(
|
||||
"cloudstack_secondary_ipaddress.foo", &ip),
|
||||
testAccCheckCloudStackSecondaryIPAddressAttributes(&ip),
|
||||
resource.TestCheckResourceAttr(
|
||||
"cloudstack_secondary_ipaddress.foo", "ipaddress", CLOUDSTACK_NETWORK_1_IPADDRESS),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckCloudStackSecondaryIPAddressExists(
|
||||
n string, ip *cloudstack.AddIpToNicResponse) 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 IP address ID is set")
|
||||
}
|
||||
|
||||
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
||||
|
||||
// Retrieve the virtual_machine UUID
|
||||
virtualmachineid, e := retrieveUUID(
|
||||
cs, "virtual_machine", rs.Primary.Attributes["virtual_machine"])
|
||||
if e != nil {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
// Get the virtual machine details
|
||||
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
|
||||
if err != nil {
|
||||
if count == 0 {
|
||||
return fmt.Errorf("Instance not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
nicid := rs.Primary.Attributes["nicid"]
|
||||
if nicid == "" {
|
||||
nicid = vm.Nic[0].Id
|
||||
}
|
||||
|
||||
p := cs.Nic.NewListNicsParams(virtualmachineid)
|
||||
p.SetNicid(nicid)
|
||||
|
||||
l, err := cs.Nic.ListNics(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Count == 0 {
|
||||
return fmt.Errorf("NIC not found")
|
||||
}
|
||||
|
||||
if l.Count > 1 {
|
||||
return fmt.Errorf("Found more then one possible result: %v", l.Nics)
|
||||
}
|
||||
|
||||
for _, sip := range l.Nics[0].Secondaryip {
|
||||
if sip.Id == rs.Primary.ID {
|
||||
ip.Ipaddress = sip.Ipaddress
|
||||
ip.Nicid = l.Nics[0].Id
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("IP address not found")
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckCloudStackSecondaryIPAddressAttributes(
|
||||
ip *cloudstack.AddIpToNicResponse) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
|
||||
if ip.Ipaddress != CLOUDSTACK_NETWORK_1_IPADDRESS {
|
||||
return fmt.Errorf("Bad IP address: %s", ip.Ipaddress)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckCloudStackSecondaryIPAddressDestroy(s *terraform.State) error {
|
||||
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "cloudstack_secondary_ipaddress" {
|
||||
continue
|
||||
}
|
||||
|
||||
if rs.Primary.ID == "" {
|
||||
return fmt.Errorf("No IP address ID is set")
|
||||
}
|
||||
|
||||
// Retrieve the virtual_machine UUID
|
||||
virtualmachineid, e := retrieveUUID(
|
||||
cs, "virtual_machine", rs.Primary.Attributes["virtual_machine"])
|
||||
if e != nil {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
// Get the virtual machine details
|
||||
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
|
||||
if err != nil {
|
||||
if count == 0 {
|
||||
return fmt.Errorf("Instance not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
nicid := rs.Primary.Attributes["nicid"]
|
||||
if nicid == "" {
|
||||
nicid = vm.Nic[0].Id
|
||||
}
|
||||
|
||||
p := cs.Nic.NewListNicsParams(virtualmachineid)
|
||||
p.SetNicid(nicid)
|
||||
|
||||
l, err := cs.Nic.ListNics(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Count == 0 {
|
||||
return fmt.Errorf("NIC not found")
|
||||
}
|
||||
|
||||
if l.Count > 1 {
|
||||
return fmt.Errorf("Found more then one possible result: %v", l.Nics)
|
||||
}
|
||||
|
||||
for _, sip := range l.Nics[0].Secondaryip {
|
||||
if sip.Id == rs.Primary.ID {
|
||||
return fmt.Errorf("IP address %s still exists", rs.Primary.ID)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var testAccCloudStackSecondaryIPAddress_basic = fmt.Sprintf(`
|
||||
resource "cloudstack_instance" "foobar" {
|
||||
name = "terraform-test"
|
||||
service_offering= "%s"
|
||||
network = "%s"
|
||||
template = "%s"
|
||||
zone = "%s"
|
||||
expunge = true
|
||||
}
|
||||
|
||||
resource "cloudstack_secondary_ipaddress" "foo" {
|
||||
virtual_machine = "${cloudstack_instance.foobar.id}"
|
||||
}
|
||||
`,
|
||||
CLOUDSTACK_SERVICE_OFFERING_1,
|
||||
CLOUDSTACK_NETWORK_1,
|
||||
CLOUDSTACK_TEMPLATE,
|
||||
CLOUDSTACK_ZONE)
|
||||
|
||||
var testAccCloudStackSecondaryIPAddress_fixedIP = fmt.Sprintf(`
|
||||
resource "cloudstack_instance" "foobar" {
|
||||
name = "terraform-test"
|
||||
service_offering= "%s"
|
||||
network = "%s"
|
||||
template = "%s"
|
||||
zone = "%s"
|
||||
expunge = true
|
||||
}
|
||||
|
||||
resource "cloudstack_secondary_ipaddress" "foo" {
|
||||
ipaddress = "%s"
|
||||
virtual_machine = "${cloudstack_instance.foobar.id}"
|
||||
}`,
|
||||
CLOUDSTACK_SERVICE_OFFERING_1,
|
||||
CLOUDSTACK_NETWORK_1,
|
||||
CLOUDSTACK_TEMPLATE,
|
||||
CLOUDSTACK_ZONE,
|
||||
CLOUDSTACK_NETWORK_1_IPADDRESS)
|
||||
@ -0,0 +1,41 @@
|
||||
---
|
||||
layout: "cloudstack"
|
||||
page_title: "CloudStack: cloudstack_secondary_ipaddress"
|
||||
sidebar_current: "docs-cloudstack-resource-secondary-ipaddress"
|
||||
description: |-
|
||||
Assigns a secondary IP to a NIC.
|
||||
---
|
||||
|
||||
# cloudstack\_secondary\_ipaddress
|
||||
|
||||
Assigns a secondary IP to a NIC.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
resource "cloudstack_secondary_ipaddress" "default" {
|
||||
virtual_machine = "server-1"
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `ipaddress` - (Optional) The IP address to attach the to NIC. If not supplied
|
||||
an IP address will be selected randomly. Changing this forces a new resource
|
||||
to be created.
|
||||
|
||||
* `nicid` - (Optional) The ID of the NIC to which you want to attach the
|
||||
secondary IP address. Changing this forces a new resource to be
|
||||
created (defaults to the ID of the primary NIC)
|
||||
|
||||
* `virtual_machine` - (Required) The name or ID of the virtual machine to which
|
||||
you want to attach the secondary IP address. Changing this forces a new
|
||||
resource to be created.
|
||||
|
||||
## Attributes Reference
|
||||
|
||||
The following attributes are exported:
|
||||
|
||||
* `id` - The secondary IP address ID.
|
||||
Loading…
Reference in new issue