-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Conversation
azurerm/data_source_image_test.go
Outdated
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)}" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this 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!
azurerm/data_source_image.go
Outdated
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validateNameRegex, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
azurerm/data_source_image.go
Outdated
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Optional: true, |
There was a problem hiding this comment.
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?
website/docs/d/image.html.markdown
Outdated
* `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. |
There was a problem hiding this comment.
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?
azurerm/data_source_image_test.go
Outdated
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)}" |
There was a problem hiding this comment.
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?
azurerm/data_source_image_test.go
Outdated
os_disk { | ||
os_type = "Linux" | ||
os_state = "Generalized" | ||
blob_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" |
There was a problem hiding this comment.
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}
?
azurerm/data_source_image_test.go
Outdated
os_disk { | ||
os_type = "Linux" | ||
os_state = "Generalized" | ||
blob_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
azurerm/data_source_image.go
Outdated
|
||
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
azurerm/data_source_image.go
Outdated
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 { |
There was a problem hiding this comment.
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?
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? |
There was a problem hiding this 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!
azurerm/data_source_image.go
Outdated
ValidateFunc: validation.ValidateRegexp, | ||
ConflictsWith: []string{"name"}, | ||
}, | ||
"name_descending": { |
There was a problem hiding this comment.
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
|
There was a problem hiding this 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!
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! |
Fixes #454