github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 _, ok := d.GetOk("droplet_id"); ok { 86 log.Printf("[INFO] The region of the Droplet is %s", floatingIp.Droplet.Region) 87 d.Set("region", floatingIp.Droplet.Region.Slug) 88 } else { 89 d.Set("region", floatingIp.Region.Slug) 90 } 91 92 d.Set("ip_address", floatingIp.IP) 93 94 return nil 95 } 96 97 func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface{}) error { 98 client := meta.(*godo.Client) 99 100 if _, ok := d.GetOk("droplet_id"); ok { 101 log.Printf("[INFO] Unassigning the Floating IP from the Droplet") 102 action, _, err := client.FloatingIPActions.Unassign(d.Id()) 103 if err != nil { 104 return fmt.Errorf( 105 "Error Unassigning FloatingIP (%s) from the droplet: %s", d.Id(), err) 106 } 107 108 _, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID) 109 if unassignedErr != nil { 110 return fmt.Errorf( 111 "Error waiting for FloatingIP (%s) to be unassigned: %s", d.Id(), unassignedErr) 112 } 113 } 114 115 log.Printf("[INFO] Deleting FloatingIP: %s", d.Id()) 116 _, err := client.FloatingIPs.Delete(d.Id()) 117 if err != nil { 118 return fmt.Errorf("Error deleting FloatingIP: %s", err) 119 } 120 121 d.SetId("") 122 return nil 123 } 124 125 func waitForFloatingIPReady( 126 d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}, actionId int) (interface{}, error) { 127 log.Printf( 128 "[INFO] Waiting for FloatingIP (%s) to have %s of %s", 129 d.Id(), attribute, target) 130 131 stateConf := &resource.StateChangeConf{ 132 Pending: pending, 133 Target: target, 134 Refresh: newFloatingIPStateRefreshFunc(d, attribute, meta, actionId), 135 Timeout: 60 * time.Minute, 136 Delay: 10 * time.Second, 137 MinTimeout: 3 * time.Second, 138 139 NotFoundChecks: 60, 140 } 141 142 return stateConf.WaitForState() 143 } 144 145 func newFloatingIPStateRefreshFunc( 146 d *schema.ResourceData, attribute string, meta interface{}, actionId int) resource.StateRefreshFunc { 147 client := meta.(*godo.Client) 148 return func() (interface{}, string, error) { 149 150 log.Printf("[INFO] Assigning the Floating IP to the Droplet") 151 action, _, err := client.FloatingIPActions.Get(d.Id(), actionId) 152 if err != nil { 153 return nil, "", fmt.Errorf("Error retrieving FloatingIP (%s) ActionId (%d): %s", d.Id(), actionId, err) 154 } 155 156 log.Printf("[INFO] The FloatingIP Action Status is %s", action.Status) 157 return &action, action.Status, nil 158 } 159 }