Skip to content

Commit

Permalink
vultr: enable instance and IP importing
Browse files Browse the repository at this point in the history
  • Loading branch information
squat committed Apr 10, 2018
1 parent 644b95d commit 49fa7f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
12 changes: 10 additions & 2 deletions vultr/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func resourceInstance() *schema.Resource {
Read: resourceInstanceRead,
Update: resourceInstanceUpdate,
Delete: resourceInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"application_id": {
Expand Down Expand Up @@ -209,18 +212,23 @@ func resourceInstanceRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error getting instance (%s): %v", d.Id(), err)
}

d.Set("application_id", instance.AppID)
d.Set("cost_per_month", instance.Cost)
d.Set("default_password", instance.DefaultPassword)
d.Set("disk", instance.Disk)
if instance.FirewallGroupID == "" {
d.Set("firewall_group_id", 0)
d.Set("firewall_group_id", "0")
} else {
d.Set("firewall_group_id", instance.FirewallGroupID)
}
d.Set("ipv4_address", instance.MainIP)
d.Set("ipv4_private_address", instance.InternalIP)
d.Set("name", instance.Name)
d.Set("os_id", instance.OSID)
osID, err := strconv.Atoi(instance.OSID)
if err != nil {
return fmt.Errorf("OS ID must be an integer: %v", err)
}
d.Set("os_id", osID)
d.Set("plan_id", instance.PlanID)
d.Set("power_status", instance.PowerStatus)
d.Set("ram", instance.RAM)
Expand Down
33 changes: 29 additions & 4 deletions vultr/resource_ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"strings"

"github.com/JamesClonk/vultr/lib"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -15,6 +16,9 @@ func resourceIPV4() *schema.Resource {
Read: resourceIPV4Read,
Update: resourceIPV4Update,
Delete: resourceIPV4Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"gateway": {
Expand All @@ -41,6 +45,7 @@ func resourceIPV4() *schema.Resource {
"reboot": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"reverse_dns": {
Expand Down Expand Up @@ -91,21 +96,26 @@ func resourceIPV4Create(d *schema.ResourceData, meta interface{}) error {
return errors.New("Error finding created IPv4 address")
}

d.SetId(ip.IP)
d.SetId(fmt.Sprintf("%s/%s", instance, ip.IP))

return resourceIPV4Read(d, meta)
}

func resourceIPV4Read(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)

ips, err := client.ListIPv4(d.Get("instance_id").(string))
instance, id, err := idFromData(d)
if err != nil {
return err
}

ips, err := client.ListIPv4(instance)
if err != nil {
return fmt.Errorf("Error getting IPv4 addresses: %v", err)
}
var ip *lib.IPv4
for i := range ips {
if ips[i].IP == d.Id() {
if ips[i].IP == id {
ip = &ips[i]
break
}
Expand All @@ -132,11 +142,26 @@ func resourceIPV4Update(d *schema.ResourceData, meta interface{}) error {
func resourceIPV4Delete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)

instance, id, err := idFromData(d)
if err != nil {
return err
}

log.Printf("[INFO] Destroying IPv4 address (%s)", d.Id())

if err := client.DeleteIPv4(d.Get("instance_id").(string), d.Id()); err != nil {
if err := client.DeleteIPv4(instance, id); err != nil {
return fmt.Errorf("Error destroying IPv4 address (%s): %v", d.Id(), err)
}

return nil
}

// idFromData returns the IPv4 id components from the ResourceData ID,
// which are, in order: the IPv4's associated instance, the actual IP, and any error.
func idFromData(d *schema.ResourceData) (string, string, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return "", "", errors.New("Error parsing IPv4 ID: ID should be of form <instance-id>/<ip-address>")
}
return parts[0], parts[1], nil
}

0 comments on commit 49fa7f4

Please sign in to comment.