Skip to content

Commit

Permalink
Add UUID valadation to disk_image
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Aug 1, 2024
1 parent 8738236 commit 0a26c12
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions civo/instances/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0a26c12

Please sign in to comment.