2016-10-15 17:16:50 +02:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/scaleway/scaleway-cli/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func dataSourceScalewayImage() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Read: dataSourceScalewayImageRead,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"name_filter": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"architecture": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
// Computed values.
|
|
|
|
"organization": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"public": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"creation_date": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 12:37:02 +01:00
|
|
|
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)
|
2016-10-15 17:16:50 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dataSourceScalewayImageRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
scaleway := meta.(*Client).scaleway
|
|
|
|
|
2016-11-05 12:37:02 +01:00
|
|
|
var needle string
|
2016-10-15 17:16:50 +02:00
|
|
|
if name, ok := d.GetOk("name"); ok {
|
2016-11-05 12:37:02 +01:00
|
|
|
needle = name.(string)
|
2016-10-15 17:16:50 +02:00
|
|
|
} else if nameFilter, ok := d.GetOk("name_filter"); ok {
|
2016-11-05 12:37:02 +01:00
|
|
|
needle = nameFilter.(string)
|
2016-10-15 17:16:50 +02:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:37:02 +01:00
|
|
|
images, err := scaleway.ResolveImage(needle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-10-15 17:16:50 +02:00
|
|
|
}
|
2016-11-05 12:37:02 +01:00
|
|
|
images = api.FilterImagesByArch(images, d.Get("architecture").(string))
|
|
|
|
images = api.FilterImagesByRegion(images, scaleway.Region)
|
2016-10-15 17:16:50 +02:00
|
|
|
|
2016-11-05 12:37:02 +01:00
|
|
|
if len(images) > 1 {
|
2016-10-15 17:16:50 +02:00
|
|
|
return fmt.Errorf("The query returned more than one result. Please refine your query.")
|
|
|
|
}
|
2016-11-05 12:37:02 +01:00
|
|
|
if len(images) == 0 {
|
2016-10-15 17:16:50 +02:00
|
|
|
return fmt.Errorf("The query returned no result. Please refine your query.")
|
|
|
|
}
|
|
|
|
|
2016-11-05 12:37:02 +01:00
|
|
|
img, err := scaleway.GetImage(images[0].Identifier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return scalewayImageAttributes(d, img)
|
2016-10-15 17:16:50 +02:00
|
|
|
}
|