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

rm deprecated template and add UUID validation on disk_image #295

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
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
26 changes: 2 additions & 24 deletions civo/instances/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,12 @@ func ResourceInstance() *schema.Resource {
ForceNew: true,
Description: "This must be the ID of the network from the network listing (optional; default network used when not specified)",
},
"template": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"template", "disk_image"},
Deprecated: "\"template\" attribute is deprecated. Moving forward, please use \"disk_image\" attribute.",
Description: "The ID for the template to use to build the instance",
},
"disk_image": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"template", "disk_image"},
Required: true,
Description: "The ID for the disk image to use to build the instance",
ForceNew: true,
ValidateFunc: utils.ValidateUUID,
},
"initial_user": {
Type: schema.TypeString,
Expand Down Expand Up @@ -244,14 +235,6 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
config.NetworkID = defaultNetwork.ID
}

if attr, ok := d.GetOk("template"); ok {
findTemplate, err := apiClient.FindDiskImage(attr.(string))
if err != nil {
return diag.Errorf("[ERR] failed to get the template: %s", err)
}
config.TemplateID = findTemplate.ID
}

if attr, ok := d.GetOk("disk_image"); ok {
findDiskImage, err := apiClient.FindDiskImage(attr.(string))
if err != nil {
Expand Down Expand Up @@ -374,7 +357,6 @@ func resourceInstanceRead(_ context.Context, d *schema.ResourceData, m interface
d.Set("firewall_id", resp.FirewallID)
d.Set("status", resp.Status)
d.Set("script", resp.Script)
// d.Set("reserved_ipv4", resp.ReservedIPID)
d.Set("created_at", resp.CreatedAt.UTC().String())
d.Set("notes", resp.Notes)
d.Set("disk_image", resp.SourceID)
Expand All @@ -385,10 +367,6 @@ func resourceInstanceRead(_ context.Context, d *schema.ResourceData, m interface
d.Set("public_ip_required", "none")
}

if _, ok := d.GetOk("template"); ok {
d.Set("template", d.Get("template").(string))
}

if d.HasChange("reserved_ipv4") {
_, new := d.GetChange("reserved_ipv4")
newValue := new.(string)
Expand Down
5 changes: 2 additions & 3 deletions docs/resources/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ resource "civo_instance" "example" {
### Required

- `firewall_id` (String) The ID of the firewall to use, from the current list. If left blank or not sent, the default firewall will be used (open to all)

- `disk_image` (String) The ID for the disk image to use to build the instance
-
### Optional

- `disk_image` (String) The ID for the disk image to use to build the instance
- `hostname` (String) A fully qualified domain name that should be set as the instance's hostname
- `initial_user` (String) The name of the initial user created on the server (optional; this will default to the template's default_username and fallback to civo)
- `network_id` (String) This must be the ID of the network from the network listing (optional; default network used when not specified)
Expand All @@ -145,7 +145,6 @@ resource "civo_instance" "example" {
- `tags` (Set of String) An optional list of tags, represented as a key, value pair
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) defines timeouts for cluster creation, read and update, default is 30 minutes for all


<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Expand Down
11 changes: 11 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package utils
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -240,3 +241,13 @@ func ParseErrorResponse(errorMsg string) (*CustomError, error) {
}
return &customErr, nil
}

// ValidateUUID checks if a given string is a UUID or not
func ValidateUUID(v interface{}, k string) (ws []string, errors []error) {
RealHarshThakur marked this conversation as resolved.
Show resolved Hide resolved
value := v.(string)
_, err := uuid.Parse(value)
if err != nil {
errors = append(errors, fmt.Errorf("%q must be a valid UUID", k))
}
return
}
Loading