provider/scaleway: fix `scaleway_image` datasource returning unknown images (#9899)

* provider/scaleway: expose scaleway_image lookup error

* provider/scaleway: fix image lookup error

fixes #9472
This commit is contained in:
Raphael Randschau 2016-11-05 12:37:02 +01:00 committed by Paul Stack
parent f9d5c7245d
commit fc0455f63a
3 changed files with 50 additions and 75 deletions

View File

@ -54,6 +54,13 @@ func testAccCheckBootscriptID(n string) resource.TestCheckFunc {
if rs.Primary.ID == "" {
return fmt.Errorf("bootscript data source ID not set")
}
scaleway := testAccProvider.Meta().(*Client).scaleway
_, err := scaleway.GetBootscript(rs.Primary.ID)
if err != nil {
return err
}
return nil
}
}

View File

@ -2,8 +2,6 @@ package scaleway
import (
"fmt"
"log"
"regexp"
"github.com/hashicorp/terraform/helper/schema"
"github.com/scaleway/scaleway-cli/pkg/api"
@ -47,87 +45,45 @@ func dataSourceScalewayImage() *schema.Resource {
}
}
func scalewayImageAttributes(d *schema.ResourceData, img imageMatch) error {
d.Set("architecture", img.imageDefinition.Arch)
d.Set("organization", img.marketImage.Organization)
d.Set("public", img.marketImage.Public)
d.Set("creation_date", img.marketImage.CreationDate)
d.Set("name", img.marketImage.Name)
d.SetId(img.imageDefinition.ID)
func scalewayImageAttributes(d *schema.ResourceData, img *api.ScalewayImage) error {
d.Set("architecture", img.Arch)
d.Set("organization", img.Organization)
d.Set("public", img.Public)
d.Set("creation_date", img.CreationDate)
d.Set("name", img.Name)
d.SetId(img.Identifier)
return nil
}
type imageMatch struct {
marketImage api.MarketImage
imageDefinition api.MarketLocalImageDefinition
}
func dataSourceScalewayImageRead(d *schema.ResourceData, meta interface{}) error {
scaleway := meta.(*Client).scaleway
images, err := scaleway.GetImages()
log.Printf("[DEBUG] %#v", images)
var needle string
if name, ok := d.GetOk("name"); ok {
needle = name.(string)
} else if nameFilter, ok := d.GetOk("name_filter"); ok {
needle = nameFilter.(string)
}
images, err := scaleway.ResolveImage(needle)
if err != nil {
return err
}
images = api.FilterImagesByArch(images, d.Get("architecture").(string))
images = api.FilterImagesByRegion(images, scaleway.Region)
if len(images) > 1 {
return fmt.Errorf("The query returned more than one result. Please refine your query.")
}
if len(images) == 0 {
return fmt.Errorf("The query returned no result. Please refine your query.")
}
img, err := scaleway.GetImage(images[0].Identifier)
if err != nil {
return err
}
var isNameMatch = func(api.MarketImage) bool { return true }
var isArchMatch = func(api.MarketLocalImageDefinition) bool { return true }
if name, ok := d.GetOk("name"); ok {
isNameMatch = func(img api.MarketImage) bool {
return img.Name == name.(string)
}
} else if nameFilter, ok := d.GetOk("name_filter"); ok {
exp, err := regexp.Compile(nameFilter.(string))
if err != nil {
return err
}
isNameMatch = func(img api.MarketImage) bool {
return exp.MatchString(img.Name)
}
}
var architecture = d.Get("architecture").(string)
if architecture != "" {
isArchMatch = func(img api.MarketLocalImageDefinition) bool {
return img.Arch == architecture
}
}
var matches []imageMatch
for _, img := range *images {
if !isNameMatch(img) {
continue
}
var imageDefinition *api.MarketLocalImageDefinition
for _, version := range img.Versions {
for _, def := range version.LocalImages {
if isArchMatch(def) {
imageDefinition = &def
break
}
}
}
if imageDefinition == nil {
continue
}
matches = append(matches, imageMatch{
marketImage: img,
imageDefinition: *imageDefinition,
})
}
if len(matches) > 1 {
return fmt.Errorf("The query returned more than one result. Please refine your query.")
}
if len(matches) == 0 {
return fmt.Errorf("The query returned no result. Please refine your query.")
}
return scalewayImageAttributes(d, matches[0])
return scalewayImageAttributes(d, img)
}

View File

@ -19,6 +19,8 @@ func TestAccScalewayDataSourceImage_Basic(t *testing.T) {
testAccCheckImageID("data.scaleway_image.ubuntu"),
resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "architecture", "arm"),
resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "public", "true"),
resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "organization"),
resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "creation_date"),
),
},
},
@ -34,9 +36,11 @@ func TestAccScalewayDataSourceImage_Filtered(t *testing.T) {
Config: testAccCheckScalewayImageFilterConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckImageID("data.scaleway_image.ubuntu"),
resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "name", "Ubuntu Precise"),
resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "name", "Ubuntu Precise (12.04)"),
resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "architecture", "arm"),
resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "public", "true"),
resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "organization"),
resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "creation_date"),
),
},
},
@ -53,6 +57,14 @@ func testAccCheckImageID(n string) resource.TestCheckFunc {
if rs.Primary.ID == "" {
return fmt.Errorf("image data source ID not set")
}
scaleway := testAccProvider.Meta().(*Client).scaleway
_, err := scaleway.GetImage(rs.Primary.ID)
if err != nil {
return err
}
return nil
}
}