Skip to content

Commit

Permalink
Various improvements in civo_instance rersource (#226)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
uzaxirr authored Jun 19, 2024
1 parent 407ade6 commit b13b827
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions civo/instances/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func ResourceInstance() *schema.Resource {
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The region for the instance, if not declare we use the region in declared in the provider",
},
"hostname": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "A fully qualified domain name that should be set as the instance's hostname",
ForceNew: true,
ValidateFunc: utils.ValidateNameSize,
},
"reverse_dns": {
Expand All @@ -55,6 +55,7 @@ func ResourceInstance() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "This must be the ID of the network from the network listing (optional; default network used when not specified)",
},
"template": {
Expand Down Expand Up @@ -363,7 +364,7 @@ func resourceInstanceRead(_ context.Context, d *schema.ResourceData, m interface
d.Set("initial_password", resp.InitialPassword)
d.Set("source_type", resp.SourceType)
d.Set("source_id", resp.SourceID)
d.Set("sshkey_id", resp.SSHKey)
d.Set("sshkey_id", resp.SSHKeyID)
d.Set("tags", resp.Tags)
d.Set("private_ip", resp.PrivateIP)
d.Set("public_ip", resp.PublicIP)
Expand Down Expand Up @@ -429,25 +430,32 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
}
}

// if has note we add to the instance
if d.HasChange("notes") {
// if notes or hostname have changed, add them to the instance
if d.HasChange("notes") || d.HasChange("hostname") {
notes := d.Get("notes").(string)
hostname := d.Get("hostname").(string)

instance, err := apiClient.GetInstance(d.Id())
if err != nil {
// check if the instance no longer exists.
return diag.Errorf("[ERR] instance %s not found", d.Id())
}

instance.Notes = notes
if d.HasChange("notes") {
instance.Notes = notes
}
if d.HasChange("hostname") {
instance.Hostname = hostname
}

log.Printf("[INFO] adding notes to the instance %s", d.Id())
log.Printf("[INFO] updating instance %s", d.Id())
_, err = apiClient.UpdateInstance(instance)
if err != nil {
return diag.Errorf("[ERR] an error occurred while adding a note to the instance %s", d.Id())
return diag.Errorf("[ERR] an error occurred while updating notes or hostname of the instance %s", d.Id())
}
}

// if a firewall is declare we update the instance
// if a firewall is declared we update the instance
if d.HasChange("firewall_id") {
firewallID := d.Get("firewall_id").(string)

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

if d.HasChange("initial_user") {
return diag.Errorf("[ERR] updating initial_user is not supported")
}

if d.HasChange("sshkey_id") {
return diag.Errorf("[ERR] updating sshkey_id is not supported")
}

// if tags is declare we update the instance with the tags
if d.HasChange("tags") {
tfTags := d.Get("tags").(*schema.Set).List()
Expand Down

0 comments on commit b13b827

Please sign in to comment.