add ip_address

This commit is contained in:
Ayu Demura 2016-07-22 10:57:54 -04:00
parent 3099fc7b1c
commit 806939ccdb
4 changed files with 233 additions and 15 deletions

View File

@ -0,0 +1,130 @@
package packet
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/packethost/packngo"
)
func resourcePacketIPAddress() *schema.Resource {
return &schema.Resource{
Create: resourcePacketIPAddressCreate,
Read: resourcePacketIPAddressRead,
Delete: resourcePacketIPAddressDelete,
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
},
"gateway": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"family": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"netmask": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"cidr": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"public": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"assigned_to": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"created": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"updated": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourcePacketIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*packngo.Client)
createRequest := &packngo.IPAddressAssignRequest{
Address: d.Get("address").(string),
}
device_id := ""
if attr, ok := d.GetOk("instance_id"); ok {
device_id = attr.(string)
}
newIPAddress, _, err := client.Ips.Assign(device_id, createRequest)
if err != nil {
return friendlyError(err)
}
d.SetId(newIPAddress.ID)
return resourcePacketIPAddressRead(d, meta)
}
func resourcePacketIPAddressRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*packngo.Client)
ip_address, _, err := client.Ips.Get(d.Id())
if err != nil {
err = friendlyError(err)
// If the volume somehow already destroyed, mark as succesfully gone.
if isNotFound(err) {
d.SetId("")
return nil
}
return err
}
d.Set("address", ip_address.Address)
d.Set("gateway", ip_address.Gateway)
d.Set("network", ip_address.Network)
d.Set("family", ip_address.AddressFamily)
d.Set("netmask", ip_address.Netmask)
d.Set("public", ip_address.Public)
d.Set("cidr", ip_address.Cidr)
d.Set("assigned_to", ip_address.AssignedTo)
d.Set("created", ip_address.Created)
d.Set("updated", ip_address.Updated)
return nil
}
func resourcePacketIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*packngo.Client)
if _, err := client.Ips.Unassign(d.Id()); err != nil {
return friendlyError(err)
}
return nil
}

View File

@ -0,0 +1,86 @@
package packet
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/packethost/packngo"
)
func TestAccPacketIPAddress_Basic(t *testing.T) {
var ip_address packngo.IPAddress
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPacketIPAddressDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPacketIPAddressConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckPacketIPAddressExists("packet_ip_address.foobar", &ip_address),
testAccCheckPacketIPAddressAttributes(&ip_address),
resource.TestCheckResourceAttr(
"packet_ip_address.foobar", "address", "foobar"),
),
},
},
})
}
func testAccCheckPacketIPAddressDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*packngo.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "packet_ip_address" {
continue
}
if _, _, err := client.Ips.Get(rs.Primary.ID); err == nil {
return fmt.Errorf("IPAddress cstill exists")
}
}
return nil
}
func testAccCheckPacketIPAddressAttributes(ip_address *packngo.IPAddress) resource.TestCheckFunc {
return func(s *terraform.State) error {
if ip_address.Address != "foobar" {
return fmt.Errorf("Bad address: %s", ip_address.Address)
}
return nil
}
}
func testAccCheckPacketIPAddressExists(n string, ip_address *packngo.IPAddress) 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 Record ID is set")
}
client := testAccProvider.Meta().(*packngo.Client)
foundIPAddress, _, err := client.Ips.Get(rs.Primary.ID)
if err != nil {
return err
}
if foundIPAddress.ID != rs.Primary.ID {
return fmt.Errorf("Record not found: %v - %v", rs.Primary.ID, foundIPAddress)
}
*ip_address = *foundIPAddress
return nil
}
}
var testAccCheckPacketIPAddressConfig_basic = fmt.Sprintf(`
resource "packet_ip_address" "foobar" {
address = "foobar"
}`)

View File

@ -152,7 +152,7 @@ func resourcePacketVolumeCreate(d *schema.ResourceData, meta interface{}) error
func resourcePacketVolumeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*packngo.Client)
device, _, err := client.Volumes.Get(d.Id())
volume, _, err := client.Volumes.Get(d.Id())
if err != nil {
err = friendlyError(err)
@ -165,25 +165,25 @@ func resourcePacketVolumeRead(d *schema.ResourceData, meta interface{}) error {
return err
}
d.Set("name", device.Name)
d.Set("description", device.Description)
d.Set("size", device.Size)
d.Set("plan", device.Plan.Slug)
d.Set("facility", device.Facility.Code)
d.Set("state", device.State)
d.Set("billing_cycle", device.BillingCycle)
d.Set("locked", device.Locked)
d.Set("created", device.Created)
d.Set("updated", device.Updated)
d.Set("name", volume.Name)
d.Set("description", volume.Description)
d.Set("size", volume.Size)
d.Set("plan", volume.Plan.Slug)
d.Set("facility", volume.Facility.Code)
d.Set("state", volume.State)
d.Set("billing_cycle", volume.BillingCycle)
d.Set("locked", volume.Locked)
d.Set("created", volume.Created)
d.Set("updated", volume.Updated)
snapshot_policies := make([]*packngo.SnapshotPolicy, 0, len(device.SnapshotPolicies))
for _, snapshot_policy := range device.SnapshotPolicies {
snapshot_policies := make([]*packngo.SnapshotPolicy, 0, len(volume.SnapshotPolicies))
for _, snapshot_policy := range volume.SnapshotPolicies {
snapshot_policies = append(snapshot_policies, snapshot_policy)
}
d.Set("snapshot_policies", snapshot_policies)
attachments := make([]*packngo.Attachment, 0, len(device.Attachments))
for _, attachment := range device.Attachments {
attachments := make([]*packngo.Attachment, 0, len(volume.Attachments))
for _, attachment := range volume.Attachments {
attachments = append(attachments, attachment)
}
d.Set("attachments", attachments)

View File

@ -22,6 +22,8 @@ type IPAddress struct {
Public bool `json:"public"`
Cidr int `json:"cidr"`
AssignedTo map[string]string `json:"assigned_to"`
Created string `json:"created_at,omitempty"`
Updated string `json:"updated_at,omitempty"`
Href string `json:"href"`
}