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 33 commits into
base: main
Choose a base branch
from

Conversation

bschaatsbergen
Copy link
Member

@bschaatsbergen bschaatsbergen commented Jan 20, 2024

Closes #517

The random_ip resource generates a random IP address, either IPv4 or IPv6. By default, it randomly chooses between 0.0.0.0/0 (IPv4) and ::/0 (IPv6). You can influence the IP type by specifying a cidr_range.

Example usage

Basic:

resource "random_ip" "example" {}

IPv4:

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

IPv6:

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

Using a count:

resource "random_ip" "example" {
  count        = 5
  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
  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_basic
--- PASS: TestAccResourceIP_basic (0.33s)
=== 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 force-pushed the f/add-random-ip-resource branch from 3a03f2d to 0665205 Compare January 20, 2024 23:24
@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 :)

@kaspergrubbe
Copy link

This looks really helpful, and that would have helped me tremendously today.

@bschaatsbergen bschaatsbergen force-pushed the f/add-random-ip-resource branch from 4b66768 to 582f62e Compare August 9, 2024 21:39
@bschaatsbergen
Copy link
Member Author

Thank you for your comments on this pull request, @kaspergrubbe! Please remember to 👍🏼 the PR or issue #517 to help bring it to the team’s attention.

@austinvalle austinvalle added new-resource needs-more-input Issues or PRs that need more real-world use-cases before accepting labels Nov 12, 2024
@austinvalle
Copy link
Member

Following up on what @bschaatsbergen said, if you think this would be useful and have a good use-case for random_ip, please share over in the GH issue.

See #517 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-more-input Issues or PRs that need more real-world use-cases before accepting new-resource size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: random IP from CIDR range
5 participants