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 name_regex to azurerm_image data source #642

Merged
merged 4 commits into from
Jan 18, 2018
Merged

Add name_regex to azurerm_image data source #642

merged 4 commits into from
Jan 18, 2018

Conversation

paultyng
Copy link
Contributor

Fixes #454

data "azurerm_image" "test2" {
name_regex = "^[a-z]+-acctest-\\d+"
name_descending = true
resource_group_name = "${azurerm_resource_group.test.name}${substr(azurerm_image.abc.name, 0, 0)}${substr(azurerm_image.def.name, 0, 0)}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a hack to deal with depends_on and data source issues, is there a better way to do this?

Copy link
Contributor

Choose a reason for hiding this comment

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

alas not really - some tests create the config in one step then apply the data source in another; I think there's a core bug about that?

Copy link
Contributor

@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 @paultyng

Thanks for this PR - I've taken a look through and left some comments inline but this is off to a good start.

My main question is if it's worth mirroring the behaviour of the AWS AMI Data Source - in that, if multiple images are specified should we fail, rather than picking the default?

Thanks!

Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateNameRegex,
Copy link
Contributor

Choose a reason for hiding this comment

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

we should probably add a ConflictsWith attribute to this for the name field?

Copy link
Contributor

Choose a reason for hiding this comment

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

I might be wrong - but I think there's a helper that does this in the validation namespace?

"name": {
Type: schema.TypeString,
Required: true,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

we should probably add a ConflictsWith attribute to this for the name_regex field?

* `name` - (Required) The name of the Image.
* `name` - (Optional) The name of the Image.
* `name_regex` - (Optional) Regex pattern of the image to match.
* `name_descending` - (Optional) By default when matching by regex, images are sorted by name in ascending order and the first match is chosen, to sort descending, set this flag.
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be instead worth making this a most_recent flag as in the AWS data source?

data "azurerm_image" "test2" {
name_regex = "^[a-z]+-acctest-\\d+"
name_descending = true
resource_group_name = "${azurerm_resource_group.test.name}${substr(azurerm_image.abc.name, 0, 0)}${substr(azurerm_image.def.name, 0, 0)}"
Copy link
Contributor

Choose a reason for hiding this comment

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

alas not really - some tests create the config in one step then apply the data source in another; I think there's a core bug about that?

os_disk {
os_type = "Linux"
os_state = "Generalized"
blob_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should be able to make this ${azurerm_virtual_machine.storage_os_disk.0.blob_uri}?

os_disk {
os_type = "Linux"
os_state = "Generalized"
blob_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should be able to make this ${azurerm_virtual_machine.storage_os_disk.0.blob_uri} - which means we could remove the depends_on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

possibly, the bulk of this test was just copied from the existing one, I will update / test


if len(list) > 1 {
desc := d.Get("name_descending").(bool)
log.Printf("[DEBUG] arm_image - multiple results found and `name_descending` is set to: %t", desc)
Copy link
Contributor

Choose a reason for hiding this comment

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

for the AWS provider we fail in this case unless most_recent is specified - is it worth mirroring that behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we had the ability to do most recent i would agree and would have just matched AWS, without that, since we are sorting by name, either ascending or descending, we may need an additional attribute to specify if a single match is expected, but we could do that. I had assumed that if you are using the regex you intend to match from a list, but that could be an bad assumption.

desc := d.Get("name_descending").(bool)
log.Printf("[DEBUG] arm_image - multiple results found and `name_descending` is set to: %t", desc)

sort.Slice(list, func(i, j int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

it'd be good to add an acceptance test for this behaviour too?

@paultyng
Copy link
Contributor Author

Maybe its worth just waiting until they surface created date on the images?

Alternatively we could possibly infer created date via the blobs? It would be very chatty, and kind of a hack, but could use the latest date of the disk blobs?

@paultyng
Copy link
Contributor Author

paultyng commented Jan 5, 2018

image

@tombuildsstuff tombuildsstuff added this to the 1.0.2 milestone Jan 10, 2018
Copy link
Contributor

@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 @paultyng

Sorry for the delayed re-review of this - taking a look through this LGTM - the final thing before merging this would be to rename the name_descending field to sort_descending to make it clearer what it does (and also support sorting by additional fields in the future). Other than that this LGTM :)

Thanks!

ValidateFunc: validation.ValidateRegexp,
ConflictsWith: []string{"name"},
},
"name_descending": {
Copy link
Contributor

Choose a reason for hiding this comment

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

after chatting with @radeksimko - can we rename this sort_descending? it's possible we'll filter by additional fields in the future

@paultyng
Copy link
Contributor Author

=== RUN   TestAccDataSourceAzureRMImage_basic
--- PASS: TestAccDataSourceAzureRMImage_basic (492.36s)
=== RUN   TestAccDataSourceAzureRMImage_localFilter
--- PASS: TestAccDataSourceAzureRMImage_localFilter (528.13s)

Copy link
Contributor

@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 @paultyng

Thanks for pushing those changes - I've pushed a commit to update the field name in the documentation - but this now LGTM 👍

Thanks!

@tombuildsstuff tombuildsstuff merged commit ebc6a8f into hashicorp:master Jan 18, 2018
tombuildsstuff added a commit that referenced this pull request Jan 18, 2018
@paultyng paultyng deleted the pt/add-name-regex branch January 18, 2018 15:53
@ghost
Copy link

ghost commented Apr 1, 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 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 1, 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.

Extend azurerm_image to support searching for an image
2 participants