github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/digitalocean/godo" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceDigitalOceanFloatingIp() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceDigitalOceanFloatingIpCreate, 16 Read: resourceDigitalOceanFloatingIpRead, 17 Delete: resourceDigitalOceanFloatingIpDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "ip_address": &schema.Schema{ 21 Type: schema.TypeString, 22 Optional: true, 23 Computed: true, 24 }, 25 26 "region": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "droplet_id": &schema.Schema{ 33 Type: schema.TypeInt, 34 Optional: true, 35 ForceNew: true, 36 }, 37 }, 38 } 39 } 40 41 func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface{}) error { 42 client := meta.(*godo.Client) 43 44 log.Printf("[INFO] Create a FloatingIP In a Region") 45 regionOpts := &godo.FloatingIPCreateRequest{ 46 Region: d.Get("region").(string), 47 } 48 49 log.Printf("[DEBUG] FloatingIP Create: %#v", regionOpts) 50 floatingIp, _, err := client.FloatingIPs.Create(regionOpts) 51 if err != nil { 52 return fmt.Errorf("Error creating FloatingIP: %s", err) 53 } 54 55 d.SetId(floatingIp.IP) 56 57 if v, ok := d.GetOk("droplet_id"); ok { 58 59 log.Printf("[INFO] Assigning the Floating IP to the Droplet %d", v.(int)) 60 action, _, err := client.FloatingIPActions.Assign(d.Id(), v.(int)) 61 if err != nil { 62 return fmt.Errorf( 63 "Error Assigning FloatingIP (%s) to the droplet: %s", d.Id(), err) 64 } 65 66 _, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID) 67 if unassignedErr != nil { 68 return fmt.Errorf( 69 "Error waiting for FloatingIP (%s) to be Assigned: %s", d.Id(), unassignedErr) 70 } 71 } 72 73 return resourceDigitalOceanFloatingIpRead(d, meta) 74 } 75 76 func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error { 77 client := meta.(*godo.Client) 78 79 log.Printf("[INFO] Reading the details of the FloatingIP %s", d.Id()) 80 floatingIp, _, err := client.FloatingIPs.Get(d.Id()) 81 if err != nil { 82 return fmt.Errorf("Error retrieving FloatingIP: %s", err) 83 } 84 85 if floatingIp.Droplet != nil { 86 log.Printf("[INFO] A droplet was detected on the FloatingIP so setting the Region based on the Droplet") 87 log.Printf("[INFO] The region of the Droplet is %s", floatingIp.Droplet.Region.Slug) 88 d.Set("region", floatingIp.Droplet.Region.Slug) 89 } else { 90 d.Set("region", floatingIp.Region.Slug) 91 } 92 93 d.Set("ip_address", floatingIp.IP) 94 95 return nil 96 } 97 98 func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface{}) error { 99 client := meta.(*godo.Client) 100 101 if _, ok := d.GetOk("droplet_id"); ok { 102 log.Printf("[INFO] Unassigning the Floating IP from the Droplet") 103 action, _, err := client.FloatingIPActions.Unassign(d.Id()) 104 if err != nil { 105 return fmt.Errorf( 106 "Error Unassigning FloatingIP (%s) from the droplet: %s", d.Id(), err) 107 } 108 109 _, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID) 110 if unassignedErr != nil { 111 return fmt.Errorf( 112 "Error waiting for FloatingIP (%s) to be unassigned: %s", d.Id(), unassignedErr) 113 } 114 } 115 116 log.Printf("[INFO] Deleting FloatingIP: %s", d.Id()) 117 _, err := client.FloatingIPs.Delete(d.Id()) 118 if err != nil { 119 return fmt.Errorf("Error deleting FloatingIP: %s", err) 120 } 121 122 d.SetId("") 123 return nil 124 } 125 126 func waitForFloatingIPReady( 127 d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}, actionId int) (interface{}, error) { 128 log.Printf( 129 "[INFO] Waiting for FloatingIP (%s) to have %s of %s", 130 d.Id(), attribute, target) 131 132 stateConf := &resource.StateChangeConf{ 133 Pending: pending, 134 Target: target, 135 Refresh: newFloatingIPStateRefreshFunc(d, attribute, meta, actionId), 136 Timeout: 60 * time.Minute, 137 Delay: 10 * time.Second, 138 MinTimeout: 3 * time.Second, 139 140 NotFoundChecks: 60, 141 } 142 143 return stateConf.WaitForState() 144 } 145 146 func newFloatingIPStateRefreshFunc( 147 d *schema.ResourceData, attribute string, meta interface{}, actionId int) resource.StateRefreshFunc { 148 client := meta.(*godo.Client) 149 return func() (interface{}, string, error) { 150 151 log.Printf("[INFO] Assigning the Floating IP to the Droplet") 152 action, _, err := client.FloatingIPActions.Get(d.Id(), actionId) 153 if err != nil { 154 return nil, "", fmt.Errorf("Error retrieving FloatingIP (%s) ActionId (%d): %s", d.Id(), actionId, err) 155 } 156 157 log.Printf("[INFO] The FloatingIP Action Status is %s", action.Status) 158 return &action, action.Status, nil 159 } 160 }