Fix refresing an IP when it’s no longer associated

This commit is contained in:
Sander van Harmelen 2016-07-12 20:23:10 +02:00
parent 29ce2df873
commit 28543694de
2 changed files with 43 additions and 24 deletions

View File

@ -252,9 +252,12 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
p.SetKeypair(keypair.(string)) p.SetKeypair(keypair.(string))
} }
if ud, err := getUserData(d, cs); err != nil { if userData, ok := d.GetOk("user_data"); ok {
ud, err := getUserData(userData.(string), cs.HTTPGETOnly)
if err != nil {
return err return err
} else if len(ud) > 0 { }
p.SetUserdata(ud) p.SetUserdata(ud)
} }
@ -438,6 +441,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
d.SetPartial("service_offering") d.SetPartial("service_offering")
} }
// Check if the affinity group IDs have changed and if so, update the IDs
if d.HasChange("affinity_group_ids") { if d.HasChange("affinity_group_ids") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id()) p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{} groups := []string{}
@ -451,6 +455,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
p.SetAffinitygroupids(groups) p.SetAffinitygroupids(groups)
} }
// Check if the affinity group names have changed and if so, update the names
if d.HasChange("affinity_group_names") { if d.HasChange("affinity_group_names") {
p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id()) p := cs.AffinityGroup.NewUpdateVMAffinityGroupParams(d.Id())
groups := []string{} groups := []string{}
@ -464,6 +469,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
p.SetAffinitygroupids(groups) p.SetAffinitygroupids(groups)
} }
// Check if the keypair has changed and if so, update the keypair
if d.HasChange("keypair") { if d.HasChange("keypair") {
log.Printf("[DEBUG] SSH keypair changed for %s, starting update", name) log.Printf("[DEBUG] SSH keypair changed for %s, starting update", name)
@ -478,10 +484,11 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
d.SetPartial("keypair") d.SetPartial("keypair")
} }
// Check if the user data has changed and if so, update the user data
if d.HasChange("user_data") { if d.HasChange("user_data") {
log.Printf("[DEBUG] user_data changed for %s, starting update", name) 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 { if err != nil {
return err return err
} }
@ -534,16 +541,14 @@ func resourceCloudStackInstanceDelete(d *schema.ResourceData, meta interface{})
return nil return nil
} }
// getUserData returns user_data as a base64 encoded string. An empty // getUserData returns the user data as a base64 encoded string
// string is returned if unset. func getUserData(userData string, httpGetOnly bool) (string, error) {
func getUserData(d *schema.ResourceData, cs *cloudstack.CloudStackClient) (string, error) { ud := base64.StdEncoding.EncodeToString([]byte(userData))
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 // deployVirtualMachine uses POST by default, so max userdata is 32K
maxUD := 32768 maxUD := 32768
if cs.HTTPGETOnly { if httpGetOnly {
// deployVirtualMachine using GET instead, so max userdata is 2K // deployVirtualMachine using GET instead, so max userdata is 2K
maxUD = 2048 maxUD = 2048
} }
@ -555,7 +560,4 @@ func getUserData(d *schema.ResourceData, cs *cloudstack.CloudStackClient) (strin
} }
return ud, nil return ud, nil
}
return "", nil
} }

View File

@ -2,6 +2,7 @@ package cloudstack
import ( import (
"fmt" "fmt"
"log"
"sync" "sync"
"time" "time"
@ -173,6 +174,22 @@ func createPortForward(d *schema.ResourceData, meta interface{}, forward map[str
func resourceCloudStackPortForwardRead(d *schema.ResourceData, meta interface{}) error { func resourceCloudStackPortForwardRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient) 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 // Get all the forwards from the running environment
p := cs.Firewall.NewListPortForwardingRulesParams() p := cs.Firewall.NewListPortForwardingRulesParams()
p.SetIpaddressid(d.Id()) p.SetIpaddressid(d.Id())