Skip to content

Commit

Permalink
Fix refresing an IP when it’s no longer associated
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander van Harmelen committed Jul 12, 2016
1 parent 29ce2df commit 2854369
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
48 changes: 25 additions & 23 deletions builtin/providers/cloudstack/resource_cloudstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
p.SetKeypair(keypair.(string))
}

if ud, err := getUserData(d, cs); err != nil {
return err
} else if len(ud) > 0 {
if userData, ok := d.GetOk("user_data"); ok {
ud, err := getUserData(userData.(string), cs.HTTPGETOnly)
if err != nil {
return err
}

p.SetUserdata(ud)
}

Expand Down Expand Up @@ -438,6 +441,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
d.SetPartial("service_offering")
}

// Check if the affinity group IDs have changed and if so, update the IDs
if d.HasChange("affinity_group_ids") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{}
Expand All @@ -451,6 +455,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
p.SetAffinitygroupids(groups)
}

// Check if the affinity group names have changed and if so, update the names
if d.HasChange("affinity_group_names") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{}
Expand All @@ -464,6 +469,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
p.SetAffinitygroupids(groups)
}

// Check if the keypair has changed and if so, update the keypair
if d.HasChange("keypair") {
log.Printf("[DEBUG] SSH keypair changed for %s, starting update", name)

Expand All @@ -478,10 +484,11 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
d.SetPartial("keypair")
}

// Check if the user data has changed and if so, update the user data
if d.HasChange("user_data") {
log.Printf("[DEBUG] user_data changed for %s, starting update", name)

ud, err := getUserData(d, cs)
ud, err := getUserData(d.Get("user_data").(string), cs.HTTPGETOnly)
if err != nil {
return err
}
Expand Down Expand Up @@ -534,28 +541,23 @@ func resourceCloudStackInstanceDelete(d *schema.ResourceData, meta interface{})
return nil
}

// getUserData returns user_data as a base64 encoded string. An empty
// string is returned if unset.
func getUserData(d *schema.ResourceData, cs *cloudstack.CloudStackClient) (string, error) {
if userData, ok := d.GetOk("user_data"); ok {
ud := base64.StdEncoding.EncodeToString([]byte(userData.(string)))

// deployVirtualMachine uses POST by default, so max userdata is 32K
maxUD := 32768
// getUserData returns the user data as a base64 encoded string
func getUserData(userData string, httpGetOnly bool) (string, error) {
ud := base64.StdEncoding.EncodeToString([]byte(userData))

if cs.HTTPGETOnly {
// deployVirtualMachine using GET instead, so max userdata is 2K
maxUD = 2048
}
// deployVirtualMachine uses POST by default, so max userdata is 32K
maxUD := 32768

if len(ud) > maxUD {
return "", fmt.Errorf(
"The supplied user_data contains %d bytes after encoding, "+
"this exeeds the limit of %d bytes", len(ud), maxUD)
}
if httpGetOnly {
// deployVirtualMachine using GET instead, so max userdata is 2K
maxUD = 2048
}

return ud, nil
if len(ud) > maxUD {
return "", fmt.Errorf(
"The supplied user_data contains %d bytes after encoding, "+
"this exeeds the limit of %d bytes", len(ud), maxUD)
}

return "", nil
return ud, nil
}
17 changes: 17 additions & 0 deletions builtin/providers/cloudstack/resource_cloudstack_port_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudstack

import (
"fmt"
"log"
"sync"
"time"

Expand Down Expand Up @@ -173,6 +174,22 @@ func createPortForward(d *schema.ResourceData, meta interface{}, forward map[str
func resourceCloudStackPortForwardRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)

// First check if the IP address is still associated
_, count, err := cs.Address.GetPublicIpAddressByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
log.Printf(
"[DEBUG] IP address with ID %s is no longer associated", d.Id())
d.SetId("")
return nil
}

return err
}

// Get all the forwards from the running environment
p := cs.Firewall.NewListPortForwardingRulesParams()
p.SetIpaddressid(d.Id())
Expand Down

0 comments on commit 2854369

Please sign in to comment.