@@ -2,8 +2,6 @@ package scaleway
22
33import (
44 "fmt"
5- "log"
6- "regexp"
75
86 "github.com/hashicorp/terraform/helper/schema"
97 "github.com/scaleway/scaleway-cli/pkg/api"
@@ -47,87 +45,45 @@ func dataSourceScalewayImage() *schema.Resource {
4745 }
4846}
4947
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 )
48+ func scalewayImageAttributes (d * schema.ResourceData , img * api. ScalewayImage ) error {
49+ d .Set ("architecture" , img .Arch )
50+ d .Set ("organization" , img .Organization )
51+ d .Set ("public" , img .Public )
52+ d .Set ("creation_date" , img .CreationDate )
53+ d .Set ("name" , img .Name )
54+ d .SetId (img .Identifier )
5755
5856 return nil
5957}
6058
61- type imageMatch struct {
62- marketImage api.MarketImage
63- imageDefinition api.MarketLocalImageDefinition
64- }
65-
6659func dataSourceScalewayImageRead (d * schema.ResourceData , meta interface {}) error {
6760 scaleway := meta .(* Client ).scaleway
6861
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-
62+ var needle string
7863 if name , ok := d .GetOk ("name" ); ok {
79- isNameMatch = func (img api.MarketImage ) bool {
80- return img .Name == name .(string )
81- }
64+ needle = name .(string )
8265 } 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- }
66+ needle = nameFilter .(string )
9867 }
9968
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- })
69+ images , err := scaleway .ResolveImage (needle )
70+ if err != nil {
71+ return err
12372 }
73+ images = api .FilterImagesByArch (images , d .Get ("architecture" ).(string ))
74+ images = api .FilterImagesByRegion (images , scaleway .Region )
12475
125- if len (matches ) > 1 {
76+ if len (images ) > 1 {
12677 return fmt .Errorf ("The query returned more than one result. Please refine your query." )
12778 }
128- if len (matches ) == 0 {
79+ if len (images ) == 0 {
12980 return fmt .Errorf ("The query returned no result. Please refine your query." )
13081 }
13182
132- return scalewayImageAttributes (d , matches [0 ])
83+ img , err := scaleway .GetImage (images [0 ].Identifier )
84+ if err != nil {
85+ return err
86+ }
87+
88+ return scalewayImageAttributes (d , img )
13389}
0 commit comments