Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements in civo_instance rersource #226

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 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 All @@ -71,6 +72,7 @@ func ResourceInstance() *schema.Resource {
Computed: true,
ExactlyOneOf: []string{"template", "disk_image"},
Description: "The ID for the disk image to use to build the instance",
ForceNew: true,
},
"initial_user": {
Type: schema.TypeString,
Expand Down Expand Up @@ -362,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 @@ -428,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 @@ -458,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
Loading