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

add uuid function and unit test #63

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

cdituri
Copy link

@cdituri cdituri commented Sep 6, 2024

Adds a provider::assert::uuid() function to use in assertions 🚀 Cheers


PS C:\Go\src\github.com\hashicorp\terraform-provider-assert> go test -v -run "^TestUUID.*" ./...
?       github.com/hashicorp/terraform-provider-assert  [no test files]
=== RUN   TestUUIDFunction
=== PAUSE TestUUIDFunction
=== RUN   TestUUIDFunction_empty_uuid
=== PAUSE TestUUIDFunction_empty_uuid
=== RUN   TestUUIDFunction_falseCases
=== PAUSE TestUUIDFunction_falseCases
=== CONT  TestUUIDFunction
=== CONT  TestUUIDFunction_falseCases
=== CONT  TestUUIDFunction_empty_uuid
--- PASS: TestUUIDFunction_empty_uuid (1.94s)
--- PASS: TestUUIDFunction_falseCases (2.95s)
--- PASS: TestUUIDFunction (3.98s)
PASS
ok      github.com/hashicorp/terraform-provider-assert/internal/provider        (cached)

PS C:\Go\src\github.com\hashicorp\terraform-provider-assert> make test | Select-Object -Last 10
    --- PASS: TestRegexMatchesFunction_falseCases/complex_pattern_with_digit_not_matching (4.98s)
    --- PASS: TestRegexMatchesFunction_falseCases/pattern_with_character_class_not_matching (4.91s)
    --- PASS: TestRegexMatchesFunction_falseCases/pattern_with_non-capturing_group_not_matching (4.17s)
    --- PASS: TestRegexMatchesFunction_falseCases/pattern_with_alternation_not_matching (4.24s)
    --- PASS: TestRegexMatchesFunction_falseCases/pattern_with_optional_group_not_present (3.64s)
--- PASS: TestPositiveFunction_falseCases (13.95s)
--- PASS: TestNegativeFunction_falseCases (13.42s)
PASS
coverage: 89.5% of statements
ok      github.com/hashicorp/terraform-provider-assert/internal/provider        (cached)        coverage: 89.5% of statements

@cdituri cdituri requested a review from a team as a code owner September 6, 2024 23:52
Copy link

hashicorp-cla-app bot commented Sep 6, 2024

CLA assistant check
All committers have signed the CLA.

@bschaatsbergen
Copy link
Member

Hey @cdituri 👋🏼, thank you for submitting this pull request. I’ll review it as soon as possible and get back to you.

Copy link
Author

@cdituri cdituri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapping out packages for google/uuid makes much sense. Thanks for taking a pass at this PR and polishing things up, @bschaatsbergen. Looks great! 🚀

@github-actions github-actions bot added dependencies Pull requests that update a dependency file new-function Pull requests that update or introduce new functions tests labels Sep 30, 2024
Signed-off-by: Bruno Schaatsbergen <[email protected]>
@bschaatsbergen
Copy link
Member

Hey @cdituri,

Thanks for your patience on this PR. I’ve reviewed it a few times and have been experimenting with the implementation. To move forward, I’d like to get input from a few colleagues on which UUID package we should standardize on, as we currently use a mix of HashiCorp’s go-uuid and Google’s uuid.

In the meantime, could you share any specific use cases you envision for this assertion function? Understanding these could help guide our final implementation. Thanks!

@cdituri
Copy link
Author

cdituri commented Nov 10, 2024

Hi @bschaatsbergen. No worries and thank you for the update. Makes sense regarding standardizing on an UUID package. Please see inline for the answer to your question:

In the meantime, could you share any specific use cases you envision for this assertion function? Understanding these could help guide our final implementation. Thanks!

The main use-case is to reduce the error prone repetition of validating UUIDs in native terraform core. UUIDs are ubiquitous in computing and, because of this, terraform-provider-assert is a great place to provide a well-typed mechanism to validate them. Doing so also keeps terraform core clean and unconcerned.

Example

Any terraform provider or module which requires a UUID may benefit from this new function.

Here's an example with AzureRM provider. Today we may validate Azure Resource IDs variables expected to be supplied as inputs using terraform core regex function. This is error prone because the regex for the UUID must be repeated. provider::assert::uuid() frees the user from copying the regex everywhere and then having to ensure it is kept accurate. It also provides stronger safety because its symbolic name is known at terraform validate-time.

Terraform code-listing:
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 4.0"
    }
  }

  required_version = "~> 1.9"
}

variable "tenant_id" {
  type = string 
  description = "The tenant id for some cross-tenant resource"

  validation {
    condition     = can(regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", var.application_object_id))
    error_message = "Invalid tenant id"
  }
}

variable "subscription_id" {
  type = string 
  description = "The subscription associated with some resource's deployment"

  validation {
    condition     = can(regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", var.application_object_id))
    error_message = "Invalid subscription id"
  }
}

variable "application_object_id" {
  type = string 
  description = "The object id of the Azure Entra App Registration to associate to xyz"

  validation {
    condition     = can(regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", var.application_object_id))
    error_message = "Invalid app registration object id"
  }
}

provider "azurerm" {
  subscription_id = var.subscription_id
  features { } 
}

# do more stuff and much things :)

Note: Microsoft, generally, uses GUIDs. GUIDs and UUIDs share the same string representation but differ in byte-order representation. For the purposes of discussion, I'm referring to both as UUIDs.

One can imagine any number of additional scenarios where this kind of validation is useful: from libvirt domain uuid validation to Azure Tenant ID validation to something as bespoke as Cisco UCS UUID Pool ID validation. Due to the ubiquity of UUIDs, the possibilities are endless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file new-function Pull requests that update or introduce new functions tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants