Skip to content

Commit

Permalink
resource(server): fail if ip attachment fails
Browse files Browse the repository at this point in the history
to improve the usability in case the IP does not exist - see #1
  • Loading branch information
nicolai86 committed Jul 19, 2017
1 parent f9874cd commit bb6cc5a
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions scaleway/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ func resourceScalewayServer() *schema.Resource {
}
}

func attachIP(scaleway *api.ScalewayAPI, serverID, IPAddress string) error {
ips, err := scaleway.GetIPS()
if err != nil {
return err
}
for _, ip := range ips.IPS {
if ip.Address == IPAddress {
log.Printf("[DEBUG] Attaching IP %q to server %q\n", ip.ID, serverID)
return scaleway.AttachIP(ip.ID, serverID)
}
}
return fmt.Errorf("Failed to find IP with ip %q to attach", IPAddress)
}

func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway

Expand Down Expand Up @@ -185,18 +199,8 @@ func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
err = waitForServerState(scaleway, id, "running")

if v, ok := d.GetOk("public_ip"); ok {
if ips, err := scaleway.GetIPS(); err != nil {
if err := attachIP(scaleway, d.Id(), v.(string)); err != nil {
return err
} else {
for _, ip := range ips.IPS {
if ip.Address == v.(string) {
log.Printf("[DEBUG] Attaching IP %q to server %q\n", ip.ID, d.Id())
if err := scaleway.AttachIP(ip.ID, d.Id()); err != nil {
return err
}
break
}
}
}
}
}
Expand Down

0 comments on commit bb6cc5a

Please sign in to comment.