hashicorp / terraform-provider-random

Utility provider that supports the use of randomness within Terraform configurations.
https://registry.terraform.io/providers/hashicorp/random/latest
Mozilla Public License 2.0
203 stars 117 forks source link

feat: implement `random_ip` resource #519

Open bschaatsbergen opened 10 months ago

bschaatsbergen commented 10 months ago

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
bschaatsbergen commented 10 months ago

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 commented 10 months ago

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

bschaatsbergen commented 10 months ago

@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 commented 10 months ago

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 _randomip resource that you've added. Thanks.

bschaatsbergen commented 10 months ago

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 _randomip resource that you've added. Thanks.

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

kaspergrubbe commented 3 months ago

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

bschaatsbergen commented 3 months ago

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 commented 2 weeks ago

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 https://github.com/hashicorp/terraform-provider-random/issues/517#issuecomment-2471737848