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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions scaleway/data_source_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package scaleway

import (
"fmt"
"log"
"regexp"
"sort"
"time"

"github.com/hashicorp/terraform/helper/schema"
api "github.com/nicolai86/scaleway-sdk"
Expand Down Expand Up @@ -32,6 +35,13 @@ func dataSourceScalewayImage() *schema.Resource {
ForceNew: true,
Description: "architecture of the desired image",
},
"most_recent": {
Type: schema.TypeBool,
Required: false,
Default: false,
Optional: true,
Description: "select most recent image if multiple match",
},
// Computed values.
"organization": {
Type: schema.TypeString,
Expand Down Expand Up @@ -63,6 +73,11 @@ func scalewayImageAttributes(d *schema.ResourceData, img *api.Image) error {
return nil
}

type localImage struct {
api.MarketLocalImageDefinition
ModificationDate *time.Time
}

func dataSourceScalewayImageRead(d *schema.ResourceData, meta interface{}) error {
scaleway := meta.(*Client).scaleway

Expand All @@ -85,7 +100,7 @@ func dataSourceScalewayImageRead(d *schema.ResourceData, meta interface{}) error
if err != nil {
return err
}
images := []api.MarketLocalImageDefinition{}
images := []localImage{}
for _, image := range *imgs {
if !nameMatch(image) {
continue
Expand All @@ -94,13 +109,26 @@ func dataSourceScalewayImageRead(d *schema.ResourceData, meta interface{}) error
for _, v := range image.Versions {
for _, l := range v.LocalImages {
if l.Arch == d.Get("architecture").(string) && l.Zone == scaleway.Region {
images = append(images, l)
t, err := time.Parse(time.RFC3339, v.ModificationDate)
if err != nil {
log.Printf("[WARNING] could not parse modification date: %v", err.Error())
}
images = append(images, localImage{
MarketLocalImageDefinition: l,
ModificationDate: &t,
})
}
}
}
}

if len(images) > 1 {
mostRecent := d.Get("most_recent").(bool)
sort.Slice(images, func(i, j int) bool {
return images[i].ModificationDate.Before(*images[j].ModificationDate)
})
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!

return fmt.Errorf("The query returned more than one result. Please refine your query.")
}
if len(images) == 0 {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"?


## Attributes Reference

`id` is set to the ID of the found Image. In addition, the following attributes
Expand Down