@ -25,8 +25,7 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
"region" : & schema . Schema {
Type : schema . TypeString ,
Optional : true ,
Computed : true ,
Required : true ,
ForceNew : true ,
} ,
@ -42,33 +41,41 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
func resourceDigitalOceanFloatingIpCreate ( d * schema . ResourceData , meta interface { } ) error {
client := meta . ( * godo . Client )
// Build up our creation options
opts := & godo . FloatingIPCreateRequest { }
if v , ok := d . GetOk ( "region" ) ; ok {
log . Printf ( "[INFO] Create a FloatingIP for a region" )
opts . Region = v . ( string )
}
if v , ok := d . GetOk ( "droplet_id" ) ; ok {
log . Printf ( "[INFO] Found a droplet_id to try and attach to the FloatingIP" )
opts . DropletID = v . ( int )
log . Printf ( "[INFO] Create a FloatingIP In a Region" )
regionOpts := & godo . FloatingIPCreateRequest {
Region : d . Get ( "region" ) . ( string ) ,
}
log . Printf ( "[DEBUG] FloatingIP Create: %#v" , opts)
floatingIp , _ , err := client . FloatingIPs . Create ( opts)
log . Printf ( "[DEBUG] FloatingIP Create: %#v" , regionOpts )
floatingIp , _ , err := client . FloatingIPs . Create ( regionOpts )
if err != nil {
return fmt . Errorf ( "Error creating FloatingIP: %s" , err )
}
d . SetId ( floatingIp . IP )
if v , ok := d . GetOk ( "droplet_id" ) ; ok {
log . Printf ( "[INFO] Assigning the Floating IP to the Droplet %s" , v . ( int ) )
action , _ , err := client . FloatingIPActions . Assign ( d . Id ( ) , v . ( int ) )
if err != nil {
return fmt . Errorf (
"Error Assigning FloatingIP (%s) to the droplet: %s" , d . Id ( ) , err )
}
_ , unassignedErr := waitForFloatingIPReady ( d , "completed" , [ ] string { "new" , "in-progress" } , "status" , meta , action . ID )
if unassignedErr != nil {
return fmt . Errorf (
"Error waiting for FloatingIP (%s) to be Assigned: %s" , d . Id ( ) , unassignedErr )
}
}
return resourceDigitalOceanFloatingIpRead ( d , meta )
}
func resourceDigitalOceanFloatingIpRead ( d * schema . ResourceData , meta interface { } ) error {
client := meta . ( * godo . Client )
time . Sleep ( 7 * time . Second )
log . Printf ( "[INFO] Reading the details of the FloatingIP %s" , d . Id ( ) )
floatingIp , _ , err := client . FloatingIPs . Get ( d . Id ( ) )
if err != nil {
@ -98,7 +105,7 @@ func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface
"Error Unassigning FloatingIP (%s) from the droplet: %s" , d . Id ( ) , err )
}
_ , unassignedErr := waitForFloatingIPReady ( d , "completed" , [ ] string { "new" }, "status" , meta , action . ID )
_ , unassignedErr := waitForFloatingIPReady ( d , "completed" , [ ] string { "new" , "in-progress" }, "status" , meta , action . ID )
if unassignedErr != nil {
return fmt . Errorf (
"Error waiting for FloatingIP (%s) to be unassigned: %s" , d . Id ( ) , unassignedErr )
@ -116,7 +123,7 @@ func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface
}
func waitForFloatingIPReady (
d * schema . ResourceData , target string , pending [ ] string , attribute string , meta interface { } , action int ) ( interface { } , error ) {
d * schema . ResourceData , target string , pending [ ] string , attribute string , meta interface { } , action Id int ) ( interface { } , error ) {
log . Printf (
"[INFO] Waiting for FloatingIP (%s) to have %s of %s" ,
d . Id ( ) , attribute , target )
@ -124,7 +131,7 @@ func waitForFloatingIPReady(
stateConf := & resource . StateChangeConf {
Pending : pending ,
Target : target ,
Refresh : newFloatingIPStateRefreshFunc ( d , attribute , meta , action ) ,
Refresh : newFloatingIPStateRefreshFunc ( d , attribute , meta , action Id ) ,
Timeout : 60 * time . Minute ,
Delay : 10 * time . Second ,
MinTimeout : 3 * time . Second ,
@ -136,15 +143,17 @@ func waitForFloatingIPReady(
}
func newFloatingIPStateRefreshFunc (
d * schema . ResourceData , attribute string , meta interface { } , action int ) resource . StateRefreshFunc {
d * schema . ResourceData , attribute string , meta interface { } , action Id int ) resource . StateRefreshFunc {
client := meta . ( * godo . Client )
return func ( ) ( interface { } , string , error ) {
floatingIP , _ , err := client . FloatingIPActions . Get ( d . Id ( ) , action )
log . Printf ( "[INFO] Assigning the Floating IP to the Droplet" )
action , _ , err := client . FloatingIPActions . Get ( d . Id ( ) , actionId )
if err != nil {
return nil , "" , fmt . Errorf ( "Error retrieving FloatingIP Action: %s" , err )
return nil , "" , fmt . Errorf ( "Error retrieving FloatingIP (%s) ActionId (%d) : %s", d . Id ( ) , actionId , err )
}
log . Printf ( "[INFO] The FloatingIP A ssigned Status is %s", floatingIP . Status )
return & flo atingIP , flo atingIP . Status , nil
log . Printf ( "[INFO] The FloatingIP A ction Status is %s", action . Status )
return & ac tio n, ac tio n. Status , nil
}
}