Skip to content

Commit 2854369

Browse files
author
Sander van Harmelen
committed
Fix refresing an IP when it’s no longer associated
1 parent 29ce2df commit 2854369

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

builtin/providers/cloudstack/resource_cloudstack_instance.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,12 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
252252
p.SetKeypair(keypair.(string))
253253
}
254254

255-
if ud, err := getUserData(d, cs); err != nil {
256-
return err
257-
} else if len(ud) > 0 {
255+
if userData, ok := d.GetOk("user_data"); ok {
256+
ud, err := getUserData(userData.(string), cs.HTTPGETOnly)
257+
if err != nil {
258+
return err
259+
}
260+
258261
p.SetUserdata(ud)
259262
}
260263

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

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

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

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

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

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

484-
ud, err := getUserData(d, cs)
491+
ud, err := getUserData(d.Get("user_data").(string), cs.HTTPGETOnly)
485492
if err != nil {
486493
return err
487494
}
@@ -534,28 +541,23 @@ func resourceCloudStackInstanceDelete(d *schema.ResourceData, meta interface{})
534541
return nil
535542
}
536543

537-
// getUserData returns user_data as a base64 encoded string. An empty
538-
// string is returned if unset.
539-
func getUserData(d *schema.ResourceData, cs *cloudstack.CloudStackClient) (string, error) {
540-
if userData, ok := d.GetOk("user_data"); ok {
541-
ud := base64.StdEncoding.EncodeToString([]byte(userData.(string)))
542-
543-
// deployVirtualMachine uses POST by default, so max userdata is 32K
544-
maxUD := 32768
544+
// getUserData returns the user data as a base64 encoded string
545+
func getUserData(userData string, httpGetOnly bool) (string, error) {
546+
ud := base64.StdEncoding.EncodeToString([]byte(userData))
545547

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

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

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

560-
return "", nil
562+
return ud, nil
561563
}

builtin/providers/cloudstack/resource_cloudstack_port_forward.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cloudstack
22

33
import (
44
"fmt"
5+
"log"
56
"sync"
67
"time"
78

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

177+
// First check if the IP address is still associated
178+
_, count, err := cs.Address.GetPublicIpAddressByID(
179+
d.Id(),
180+
cloudstack.WithProject(d.Get("project").(string)),
181+
)
182+
if err != nil {
183+
if count == 0 {
184+
log.Printf(
185+
"[DEBUG] IP address with ID %s is no longer associated", d.Id())
186+
d.SetId("")
187+
return nil
188+
}
189+
190+
return err
191+
}
192+
176193
// Get all the forwards from the running environment
177194
p := cs.Firewall.NewListPortForwardingRulesParams()
178195
p.SetIpaddressid(d.Id())

0 commit comments

Comments
 (0)