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

feat: implement random_ip resource #519

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

Conversation

bschaatsbergen
Copy link
Member

@bschaatsbergen bschaatsbergen commented Jan 20, 2024

Closes #517

What: Implements the random_ip resource. The resource random_ip generates a random IP address from a given CIDR range based on the address type specified.

Example usage

IPv4:

resource "random_ip" "example" {
  address_type = "ipv4"
  cidr_range   = "192.168.0.0/24"
}

IPv6:

resource "random_ip" "example" {
  address_type = "ipv6"
  cidr_range   = "2001:db8::/32"
}

Using a count:

resource "random_ip" "example" {
  count        = 5
  address_type = "ipv6"
  cidr_range   = "2001:db8::/32"
}

output "random_ipv6_addresses" {
  value = random_ip.example[*].result
}

Using a count followed by a distinct:

resource "random_ip" "example" {
  count        = 50
  address_type = "ipv4"
  cidr_range   = "192.168.1.0/28"
}

output "random_distinct_ipv4_addresses" {
  value = distinct(random_ip.example[*].result)
}

Output from make testacc:

 $ TF_ACC=1 go test -v -cover -timeout 120m -run "TestAccResourceIP_"
=== RUN   TestAccResourceIP_ipv4
--- PASS: TestAccResourceIP_ipv4 (0.47s)
=== RUN   TestAccResourceIP_ipv4_quadZero
--- PASS: TestAccResourceIP_ipv4_quadZero (0.47s)
=== RUN   TestAccResourceIP_ipv4_largestPrefix
--- PASS: TestAccResourceIP_ipv4_largestPrefix (0.47s)
=== RUN   TestAccResourceIP_ipv4_smallestPrefix
--- PASS: TestAccResourceIP_ipv4_smallestPrefix (0.48s)
=== RUN   TestAccResourceIP_ipv6
--- PASS: TestAccResourceIP_ipv6 (0.48s)
=== RUN   TestAccResourceIP_ipv6_zeroCompression
--- PASS: TestAccResourceIP_ipv6_zeroCompression (0.47s)
=== RUN   TestAccResourceIP_ipv6_largestPrefix
--- PASS: TestAccResourceIP_ipv6_largestPrefix (0.47s)
=== RUN   TestAccResourceIP_ipv6_smallestPrefix
--- PASS: TestAccResourceIP_ipv6_smallestPrefix (0.47s)
=== RUN   TestAccResourceIP_Keepers_Keep_EmptyMap
=== PAUSE TestAccResourceIP_Keepers_Keep_EmptyMap
=== RUN   TestAccResourceIP_Keepers_Keep_EmptyMapToNullValue
=== PAUSE TestAccResourceIP_Keepers_Keep_EmptyMapToNullValue
=== RUN   TestAccResourceIP_Keepers_Keep_NullMap
=== PAUSE TestAccResourceIP_Keepers_Keep_NullMap
=== RUN   TestAccResourceIP_Keepers_Keep_NullMapToNullValue
=== PAUSE TestAccResourceIP_Keepers_Keep_NullMapToNullValue
=== RUN   TestAccResourceIP_Keepers_Keep_NullValue
=== PAUSE TestAccResourceIP_Keepers_Keep_NullValue
=== RUN   TestAccResourceIP_Keepers_Keep_NullValues
=== PAUSE TestAccResourceIP_Keepers_Keep_NullValues
=== RUN   TestAccResourceIP_Keepers_Keep_Value
=== PAUSE TestAccResourceIP_Keepers_Keep_Value
=== RUN   TestAccResourceIP_Keepers_Keep_Values
=== PAUSE TestAccResourceIP_Keepers_Keep_Values
=== RUN   TestAccResourceIP_Keepers_Replace_EmptyMapToValue
=== PAUSE TestAccResourceIP_Keepers_Replace_EmptyMapToValue
=== RUN   TestAccResourceIP_Keepers_Replace_NullMapToValue
=== PAUSE TestAccResourceIP_Keepers_Replace_NullMapToValue
=== RUN   TestAccResourceIP_Keepers_Replace_NullValueToValue
=== PAUSE TestAccResourceIP_Keepers_Replace_NullValueToValue
=== RUN   TestAccResourceIP_Keepers_Replace_ValueToEmptyMap
=== PAUSE TestAccResourceIP_Keepers_Replace_ValueToEmptyMap
=== RUN   TestAccResourceIP_Keepers_Replace_ValueToNullMap
=== PAUSE TestAccResourceIP_Keepers_Replace_ValueToNullMap
=== RUN   TestAccResourceIP_Keepers_Replace_ValueToNullValue
=== PAUSE TestAccResourceIP_Keepers_Replace_ValueToNullValue
=== RUN   TestAccResourceIP_Keepers_Replace_ValueToNewValue
=== PAUSE TestAccResourceIP_Keepers_Replace_ValueToNewValue
=== CONT  TestAccResourceIP_Keepers_Keep_EmptyMap
=== CONT  TestAccResourceIP_Keepers_Replace_EmptyMapToValue
=== CONT  TestAccResourceIP_Keepers_Replace_ValueToNullMap
=== CONT  TestAccResourceIP_Keepers_Keep_NullValue
=== CONT  TestAccResourceIP_Keepers_Keep_Values
=== CONT  TestAccResourceIP_Keepers_Keep_NullMapToNullValue
=== CONT  TestAccResourceIP_Keepers_Replace_NullValueToValue
=== CONT  TestAccResourceIP_Keepers_Replace_ValueToEmptyMap
=== CONT  TestAccResourceIP_Keepers_Keep_Value
=== CONT  TestAccResourceIP_Keepers_Keep_NullMap
=== CONT  TestAccResourceIP_Keepers_Replace_NullMapToValue
=== CONT  TestAccResourceIP_Keepers_Keep_NullValues
=== CONT  TestAccResourceIP_Keepers_Keep_EmptyMapToNullValue
=== CONT  TestAccResourceIP_Keepers_Replace_ValueToNewValue
=== CONT  TestAccResourceIP_Keepers_Replace_ValueToNullValue
--- PASS: TestAccResourceIP_Keepers_Keep_Values (1.29s)
--- PASS: TestAccResourceIP_Keepers_Keep_EmptyMap (1.33s)
--- PASS: TestAccResourceIP_Keepers_Keep_NullValue (1.33s)
--- PASS: TestAccResourceIP_Keepers_Keep_Value (1.36s)
--- PASS: TestAccResourceIP_Keepers_Keep_NullValues (1.37s)
--- PASS: TestAccResourceIP_Keepers_Keep_NullMap (1.37s)
--- PASS: TestAccResourceIP_Keepers_Keep_NullMapToNullValue (1.41s)
--- PASS: TestAccResourceIP_Keepers_Replace_NullValueToValue (1.42s)
--- PASS: TestAccResourceIP_Keepers_Replace_ValueToNullValue (1.42s)
--- PASS: TestAccResourceIP_Keepers_Replace_NullMapToValue (1.42s)
--- PASS: TestAccResourceIP_Keepers_Replace_EmptyMapToValue (1.42s)
--- PASS: TestAccResourceIP_Keepers_Replace_ValueToNullMap (1.42s)
--- PASS: TestAccResourceIP_Keepers_Replace_ValueToEmptyMap (1.43s)
--- PASS: TestAccResourceIP_Keepers_Replace_ValueToNewValue (1.43s)
--- PASS: TestAccResourceIP_Keepers_Keep_EmptyMapToNullValue (1.43s)
PASS
coverage: 10.8% of statements
ok      github.com/terraform-providers/terraform-provider-random/internal/provider      5.217s

@github-actions github-actions bot added size/L and removed size/M labels Jan 20, 2024
@bschaatsbergen bschaatsbergen marked this pull request as ready for review January 21, 2024 17:25
@bschaatsbergen bschaatsbergen requested a review from a team as a code owner January 21, 2024 17:25
@bschaatsbergen bschaatsbergen marked this pull request as draft January 21, 2024 17:36
@bschaatsbergen bschaatsbergen marked this pull request as ready for review January 21, 2024 17:37
@github-actions github-actions bot added size/XL and removed size/L labels Jan 21, 2024
@bschaatsbergen
Copy link
Member Author

bschaatsbergen commented Jan 21, 2024

Hey @bendbennett and @bflad, this should be ready for review, I really enjoyed working on this provider resource 😊
The only question I have is: we currently set the id to -, as there's no value to set the internal id with. What do you suggest?

@mvanholsteijn
Copy link

@bschaatsbergen why not set the ID to the unique identifier of the generated IP address: the ip address itself.

@bschaatsbergen
Copy link
Member Author

bschaatsbergen commented Jan 22, 2024

@bschaatsbergen why not set the ID to the unique identifier of the generated IP address: the ip address itself.

Hmm, I'm just not entirely sure if it might conflict with how Terraform Core deals with internal IDs containing special characters. I'll check it out – thanks for bringing it up, @mvanholsteijn!

@bendbennett
Copy link
Contributor

Hi @bschaatsbergen 👋

Thank you for submitting this PR.

As the random provider is widely used across the community, we'd like to leave this open to assess the level of community interest before making a decision regarding the addition of the new random_ip resource that you've added. Thanks.

@bschaatsbergen
Copy link
Member Author

Hi @bschaatsbergen 👋

Thank you for submitting this PR.

As the random provider is widely used across the community, we'd like to leave this open to assess the level of community interest before making a decision regarding the addition of the new random_ip resource that you've added. Thanks.

Thanks for getting back to me @bendbennett, appreciate it. Agreed, lets see :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: random IP from CIDR range
3 participants