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

New Data Source: azurerm_public_ips #304

Merged
merged 15 commits into from Mar 7, 2018

Conversation

hbuckle
Copy link
Contributor

@hbuckle hbuckle commented Sep 4, 2017

Adds a new data source to retrieve the IDs of unassigned public IP addresses from a resource group. We have a pool of public IP addresses that have been pre-whitelisted by third parties so this will allow us to pick them up dynamically instead of needing to hardcode the IDs into our configuration.
This is my first time working with go and terraform so any feedback is appreciated!

@tombuildsstuff
Copy link
Member

Hey @hbuckle

Thanks for opening this PR :)

Taking a look at this, I think this Data Source might be more useful were it to return a list of Public IP's (and most of their properties) rather than just their ID's. We can apply the same filter logic to this so that it's possible to return only Public IP's which aren't attached to a VM/LB etc (and thus your workflow should still work 😄) - but the advantage here would be that the Data Source would work for multiple use-cases, here's an example of what I mean:

data "azurerm_public_ips" "test" {
  resource_group_name = "foo"
  attached = false
}

output "public_ip_address_ids" {
  value = "${data.azurerm_public_ips.*.id}"
}

What do you think? :)

Thanks!

@hbuckle
Copy link
Contributor Author

hbuckle commented Sep 10, 2017

I have started looking at adding more properties, the only issue is that DNS settings are optional for public IPs, and for dynamic IPs they will only have an IP address if they are assigned. What's the right way to handle this, just add empty values to the results?

@tombuildsstuff
Copy link
Member

Hey @hbuckle

Sorry for the delayed response here - I'd missed we'd not replied to this!

I have started looking at adding more properties, the only issue is that DNS settings are optional for public IPs, and for dynamic IPs they will only have an IP address if they are assigned. What's the right way to handle this, just add empty values to the results?

So if there's no value we can either not set the object (in the case of a simple type like a String/Integer) - or set an empty value (in the case of a complex object). For instance, in the azurerm_public_ip resource we only set the fqdn if there's a value - otherwise (given it's a string) where it's not being set, it means that it'll get a default value of an empty string.

Hopefully that helps? :)

Thanks!

@hbuckle
Copy link
Contributor Author

hbuckle commented Oct 8, 2017

I've updated the data source so that it returns a list of public IPs. Each public IP is represented by a map with the IP's properties.

@hbuckle
Copy link
Contributor Author

hbuckle commented Oct 31, 2017

Hi - does the updated version fit in with the suggested changes? Is it possible to merge?

@tombuildsstuff
Copy link
Member

hey @hbuckle - sorry for the delayed review here - I'm taking another look at this now :)

Copy link
Member

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

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

Hey @hbuckle

Apologies for the delay in reviewing this PR - I've taken a look through and left some comments inline.

Thanks!

"public_ips": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeMap},
Copy link
Member

Choose a reason for hiding this comment

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

Given this is a list - we need to specify each schema for the fields as in this example - could we update this to match?

if resp.StatusCode == http.StatusNotFound {
d.SetId("")
}
return fmt.Errorf("Error making Read request on Azure resource group %s: %s", resGroup, err)
Copy link
Member

Choose a reason for hiding this comment

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

could we make these formatting arguments%q and %+v?

attachedOnly := d.Get("attached").(bool)
resp, err := publicIPClient.List(resGroup)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
Copy link
Member

Choose a reason for hiding this comment

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

can we update this to use the helper method utils.ResponseWasNotFound(resp) - which also handles connection drops?

publicIPClient := meta.(*ArmClient).publicIPClient

resGroup := d.Get("resource_group_name").(string)
minimumCount, minimumCountOk := d.GetOk("minimum_count")
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove this field?

filteredIps = append(filteredIps, element)
}
}
if minimumCountOk && len(filteredIps) < minimumCount.(int) {
Copy link
Member

Choose a reason for hiding this comment

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

I think now this is a more generic Data Source - that we can remove this?

m["fqdn"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn
} else {
m["fqdn"] = ""
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove the else statement here - since it'll be set to an empty string automatically?

if element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != nil && *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != "" {
m["domain_name_label"] = *element.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel
} else {
m["domain_name_label"] = ""
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove the else statement here - since it'll be set to an empty string automatically?

for _, element := range filteredIps {
m := make(map[string]string)
m["public_ip_address_id"] = *element.ID
m["name"] = *element.Name
Copy link
Member

Choose a reason for hiding this comment

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

can we add an if check around this, in-case it's nil?

var results []map[string]string
for _, element := range filteredIps {
m := make(map[string]string)
m["public_ip_address_id"] = *element.ID
Copy link
Member

Choose a reason for hiding this comment

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

can we add an if check around this, in-case it's nil?


* `resource_group_name` - (Required) Specifies the name of the resource group.
* `attached` - (Required) Whether to return public IPs that are attached or not.
* `minimum_count` - (Optional) Specifies the minimum number of IP addresses that
Copy link
Member

Choose a reason for hiding this comment

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

(as above) can we remove this field?

@hbuckle
Copy link
Contributor Author

hbuckle commented Feb 1, 2018

Sorry for the delay, finally got around to updating this

@hbuckle
Copy link
Contributor Author

hbuckle commented Mar 5, 2018

@tombuildsstuff any chance of going over this again?

@tombuildsstuff
Copy link
Member

@hbuckle sorry, we're still trying to work through the backlog of pending PR reviews; I'll take a look into this shortly

@tombuildsstuff
Copy link
Member

tombuildsstuff commented Mar 6, 2018

@hbuckle heads up that I've just rebased this on top of master so it merges cleanly, taking a look now :)

@tombuildsstuff tombuildsstuff self-assigned this Mar 6, 2018
```
$ acctests azurerm TestAccDataSourceAzureRMPublicIPs_
=== RUN   TestAccDataSourceAzureRMPublicIPs_basic
--- PASS: TestAccDataSourceAzureRMPublicIPs_basic (85.74s)
=== RUN   TestAccDataSourceAzureRMPublicIPs_mixed
--- PASS: TestAccDataSourceAzureRMPublicIPs_mixed (82.73s)
=== RUN   TestAccDataSourceAzureRMPublicIPs_count
--- PASS: TestAccDataSourceAzureRMPublicIPs_count (79.26s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	247.767s
```
@tombuildsstuff
Copy link
Member

@hbuckle I hope you don't mind, but I've pushed some commits to add support for also filtering by the prefix of the name and the allocation_type for the Public IP Address in order to expand the use-case of this data source

Tests pass:

```
$ acctests azurerm TestAccDataSourceAzureRMPublicIPs_
=== RUN   TestAccDataSourceAzureRMPublicIPs_namePrefix
--- PASS: TestAccDataSourceAzureRMPublicIPs_namePrefix (86.67s)
=== RUN   TestAccDataSourceAzureRMPublicIPs_assigned
--- PASS: TestAccDataSourceAzureRMPublicIPs_assigned (90.43s)
=== RUN   TestAccDataSourceAzureRMPublicIPs_allocationType
--- PASS: TestAccDataSourceAzureRMPublicIPs_allocationType (82.07s)
```
@tombuildsstuff tombuildsstuff added this to the 1.3.0 milestone Mar 6, 2018
@tombuildsstuff tombuildsstuff dismissed their stale review March 6, 2018 22:51

pushed changes

@tombuildsstuff tombuildsstuff changed the title Public IP IDs data source New Data Source: azurerm_public_ips Mar 6, 2018
Copy link
Member

@mbfrahry mbfrahry left a comment

Choose a reason for hiding this comment

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

LGTM minus the one comment and if the tests pass

## Argument Reference

* `resource_group_name` - (Required) Specifies the name of the resource group.
* `attached` - (Optional) Should we only return IP Addresses which are attached to a device (VM/LB) etc?
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't frame this as a question and instead have it read like Returns IP Address that are attached to a device or not attached to a device or something to that effect.

@tombuildsstuff tombuildsstuff merged commit fc98c0a into hashicorp:master Mar 7, 2018
tombuildsstuff added a commit that referenced this pull request Mar 7, 2018
@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@hashicorp hashicorp locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants