Skip to content

Commit b13b827

Browse files
authored
Various improvements in civo_instance rersource (#226)
* forceNew on diskImg * Destroy and Create new instance upon change in region or networkID * check on SSH Key Update * Allow updating Hostname in civo_instance
1 parent 407ade6 commit b13b827

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

Diff for: civo/instances/resource_instance.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ func ResourceInstance() *schema.Resource {
2323
"region": {
2424
Type: schema.TypeString,
2525
Optional: true,
26+
ForceNew: true,
2627
Description: "The region for the instance, if not declare we use the region in declared in the provider",
2728
},
2829
"hostname": {
2930
Type: schema.TypeString,
3031
Optional: true,
3132
Computed: true,
3233
Description: "A fully qualified domain name that should be set as the instance's hostname",
33-
ForceNew: true,
3434
ValidateFunc: utils.ValidateNameSize,
3535
},
3636
"reverse_dns": {
@@ -55,6 +55,7 @@ func ResourceInstance() *schema.Resource {
5555
Type: schema.TypeString,
5656
Optional: true,
5757
Computed: true,
58+
ForceNew: true,
5859
Description: "This must be the ID of the network from the network listing (optional; default network used when not specified)",
5960
},
6061
"template": {
@@ -363,7 +364,7 @@ func resourceInstanceRead(_ context.Context, d *schema.ResourceData, m interface
363364
d.Set("initial_password", resp.InitialPassword)
364365
d.Set("source_type", resp.SourceType)
365366
d.Set("source_id", resp.SourceID)
366-
d.Set("sshkey_id", resp.SSHKey)
367+
d.Set("sshkey_id", resp.SSHKeyID)
367368
d.Set("tags", resp.Tags)
368369
d.Set("private_ip", resp.PrivateIP)
369370
d.Set("public_ip", resp.PublicIP)
@@ -429,25 +430,32 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
429430
}
430431
}
431432

432-
// if has note we add to the instance
433-
if d.HasChange("notes") {
433+
// if notes or hostname have changed, add them to the instance
434+
if d.HasChange("notes") || d.HasChange("hostname") {
434435
notes := d.Get("notes").(string)
436+
hostname := d.Get("hostname").(string)
437+
435438
instance, err := apiClient.GetInstance(d.Id())
436439
if err != nil {
437440
// check if the instance no longer exists.
438441
return diag.Errorf("[ERR] instance %s not found", d.Id())
439442
}
440443

441-
instance.Notes = notes
444+
if d.HasChange("notes") {
445+
instance.Notes = notes
446+
}
447+
if d.HasChange("hostname") {
448+
instance.Hostname = hostname
449+
}
442450

443-
log.Printf("[INFO] adding notes to the instance %s", d.Id())
451+
log.Printf("[INFO] updating instance %s", d.Id())
444452
_, err = apiClient.UpdateInstance(instance)
445453
if err != nil {
446-
return diag.Errorf("[ERR] an error occurred while adding a note to the instance %s", d.Id())
454+
return diag.Errorf("[ERR] an error occurred while updating notes or hostname of the instance %s", d.Id())
447455
}
448456
}
449457

450-
// if a firewall is declare we update the instance
458+
// if a firewall is declared we update the instance
451459
if d.HasChange("firewall_id") {
452460
firewallID := d.Get("firewall_id").(string)
453461

@@ -459,6 +467,14 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
459467
}
460468
}
461469

470+
if d.HasChange("initial_user") {
471+
return diag.Errorf("[ERR] updating initial_user is not supported")
472+
}
473+
474+
if d.HasChange("sshkey_id") {
475+
return diag.Errorf("[ERR] updating sshkey_id is not supported")
476+
}
477+
462478
// if tags is declare we update the instance with the tags
463479
if d.HasChange("tags") {
464480
tfTags := d.Get("tags").(*schema.Set).List()

0 commit comments

Comments
 (0)