github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/scaleway/data_source_scaleway_image.go (about) 1 package scaleway 2 3 import ( 4 "fmt" 5 "log" 6 "regexp" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/scaleway/scaleway-cli/pkg/api" 10 ) 11 12 func dataSourceScalewayImage() *schema.Resource { 13 return &schema.Resource{ 14 Read: dataSourceScalewayImageRead, 15 16 Schema: map[string]*schema.Schema{ 17 "name": &schema.Schema{ 18 Type: schema.TypeString, 19 Optional: true, 20 ForceNew: true, 21 Computed: true, 22 }, 23 "name_filter": &schema.Schema{ 24 Type: schema.TypeString, 25 Optional: true, 26 ForceNew: true, 27 }, 28 "architecture": &schema.Schema{ 29 Type: schema.TypeString, 30 Required: true, 31 ForceNew: true, 32 }, 33 // Computed values. 34 "organization": &schema.Schema{ 35 Type: schema.TypeString, 36 Computed: true, 37 }, 38 "public": &schema.Schema{ 39 Type: schema.TypeBool, 40 Computed: true, 41 }, 42 "creation_date": &schema.Schema{ 43 Type: schema.TypeString, 44 Computed: true, 45 }, 46 }, 47 } 48 } 49 50 func scalewayImageAttributes(d *schema.ResourceData, img imageMatch) error { 51 d.Set("architecture", img.imageDefinition.Arch) 52 d.Set("organization", img.marketImage.Organization) 53 d.Set("public", img.marketImage.Public) 54 d.Set("creation_date", img.marketImage.CreationDate) 55 d.Set("name", img.marketImage.Name) 56 d.SetId(img.imageDefinition.ID) 57 58 return nil 59 } 60 61 type imageMatch struct { 62 marketImage api.MarketImage 63 imageDefinition api.MarketLocalImageDefinition 64 } 65 66 func dataSourceScalewayImageRead(d *schema.ResourceData, meta interface{}) error { 67 scaleway := meta.(*Client).scaleway 68 69 images, err := scaleway.GetImages() 70 log.Printf("[DEBUG] %#v", images) 71 if err != nil { 72 return err 73 } 74 75 var isNameMatch = func(api.MarketImage) bool { return true } 76 var isArchMatch = func(api.MarketLocalImageDefinition) bool { return true } 77 78 if name, ok := d.GetOk("name"); ok { 79 isNameMatch = func(img api.MarketImage) bool { 80 return img.Name == name.(string) 81 } 82 } else if nameFilter, ok := d.GetOk("name_filter"); ok { 83 exp, err := regexp.Compile(nameFilter.(string)) 84 if err != nil { 85 return err 86 } 87 88 isNameMatch = func(img api.MarketImage) bool { 89 return exp.MatchString(img.Name) 90 } 91 } 92 93 var architecture = d.Get("architecture").(string) 94 if architecture != "" { 95 isArchMatch = func(img api.MarketLocalImageDefinition) bool { 96 return img.Arch == architecture 97 } 98 } 99 100 var matches []imageMatch 101 for _, img := range *images { 102 if !isNameMatch(img) { 103 continue 104 } 105 106 var imageDefinition *api.MarketLocalImageDefinition 107 for _, version := range img.Versions { 108 for _, def := range version.LocalImages { 109 if isArchMatch(def) { 110 imageDefinition = &def 111 break 112 } 113 } 114 } 115 116 if imageDefinition == nil { 117 continue 118 } 119 matches = append(matches, imageMatch{ 120 marketImage: img, 121 imageDefinition: *imageDefinition, 122 }) 123 } 124 125 if len(matches) > 1 { 126 return fmt.Errorf("The query returned more than one result. Please refine your query.") 127 } 128 if len(matches) == 0 { 129 return fmt.Errorf("The query returned no result. Please refine your query.") 130 } 131 132 return scalewayImageAttributes(d, matches[0]) 133 }