From 0a26c12cb9338901eb246c81165311c2be0f39dd Mon Sep 17 00:00:00 2001 From: Uzair Ali <72073401+uzaxirr@users.noreply.github.com> Date: Thu, 1 Aug 2024 14:26:26 +0530 Subject: [PATCH] Add UUID valadation to disk_image --- civo/instances/resource_instance.go | 9 +++++---- internal/utils/utils.go | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/civo/instances/resource_instance.go b/civo/instances/resource_instance.go index 6d334dcd..f51fd547 100644 --- a/civo/instances/resource_instance.go +++ b/civo/instances/resource_instance.go @@ -61,10 +61,11 @@ func ResourceInstance() *schema.Resource { Description: "This must be the ID of the network from the network listing (optional; default network used when not specified)", }, "disk_image": { - Type: schema.TypeString, - Required: true, - Description: "The ID for the disk image to use to build the instance", - ForceNew: true, + Type: schema.TypeString, + 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, diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 270de824..de05c297 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -240,3 +240,12 @@ 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) { + value := v.(string) + if matched, _ := regexp.MatchString(`^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$`, value); !matched { + errors = append(errors, fmt.Errorf("%q must be a valid UUID", k)) + } + return +}