Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource server: improve IP attachment usability #14

Merged
merged 1 commit into from
Jul 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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