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

d/image: add most_recent filter to support multiple matches #82

Merged
merged 4 commits into from
Aug 7, 2018

Conversation

nicolai86
Copy link
Contributor

@nicolai86 nicolai86 commented Jul 18, 2018

once merged fixes #81.

Now the data source for images supports most_recent to only return the most recent image if multiple matches exist:

provider "scaleway" {
  region       = "ams1"
}

data "scaleway_image" "ubuntu" {
  architecture = "x86_64"
  name         = "Ubuntu Xenial"
  most_recent  = true
}

output "image_id" {
  value = "${data.scaleway_image.ubuntu.id}" # will be ca9a9340-92e8-4c5f-8ae1-423466f7ef30 for AMS1
}

})
log.Printf("[DEBUG] got %d images: %#v\n", len(images), images)

if len(images) > 1 && !mostRecent {
Copy link

Choose a reason for hiding this comment

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

If we use most_recent + name_filter with Ubuntu, it will return the most recent image among all ( Xenial, Bionic, ...).
Is not it better to check before if all the names are identical ?

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 would not return the most recent ubuntu release, but the most recently build ubuntu release; mighy be different!
The linked issue is different tho: the names are identical, but the IDs are not. The issue of identical names with different ids can not be resolved currently.
Do you see a better option to solve this, @xunleii?

Copy link

@xunleii xunleii Jul 19, 2018

Choose a reason for hiding this comment

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

(yep, sorry, I meant the most recent build and not the most recent release when I've said it will return the most recent image)

For me, it's a great option to solve this problem, but it can sometimes behave unexpectedly; as mentioned above, if we use the most_recent andname_filter = "Ubuntu"options at the same time, the resulting image will be Ubuntu Xenial or Ubuntu Bionic, or whatever, depending on the most recent build date (and doesn't return "The query returned more than one result." as expected).

This can be easily solved by checking if in images, all names are the same. If so, we can select the most recent, otherwise simply return "The query returned more than one result.".

What do you think about this idea, @nicolai86 ?

Copy link
Contributor Author

@nicolai86 nicolai86 Jul 19, 2018

Choose a reason for hiding this comment

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

I agree that name_filter with most_recent is most likely a bad combination, but I'm not sure it deserves special handling at this point. What do you think about disallowing the combination altogether and defering a decision until we have more data about this use case?

Copy link

@xunleii xunleii Jul 19, 2018

Choose a reason for hiding this comment

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

I think it's a good idea.
Thanks for this review :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the good feedback!

this combination can yield inexpected and undesirable results, e.g.

name_filter = "Ubuntu"
most_recent = true

This would return the most recently build image for ubuntu, which could
be very old e.g. because the old images needed to be rebuild due to a
security issue.
@nicolai86
Copy link
Contributor Author

I'll pull in this change optimistically 8/15

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.

otherwise LGTM - but it'd be nice to add a test if possible?

"most_recent": {
Type: schema.TypeBool,
Required: false,
Default: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

minor you don't need to specify either of these, since they should be implied (false is the default value for a bool / required will be false since it's optional)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gotcha.

@@ -34,6 +34,8 @@ resource "scaleway_server" "base" {

* `name` - (Optional) Exact name of desired Image

* `most_recent` - (Optional) Return most recent image if multiple exist
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably worth suffixing with "this cannot be used with the name_prefix field"?

@nicolai86
Copy link
Contributor Author

tests remain green with adjustments:

--- PASS: TestAccScalewayDataSourceImage_Basic (24.55s)
--- PASS: TestAccScalewayDataSourceImage_MostRecent (23.29s)
--- PASS: TestAccScalewayDataSourceImage_Filtered (22.41s)

@nicolai86 nicolai86 merged commit a7c0947 into master Aug 7, 2018
@nicolai86 nicolai86 deleted the d/image-mostRecent branch August 7, 2018 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to handle scaleway_image due to returned more than one result problem
3 participants