Skip to content

Commit fe0bb3e

Browse files
authored
fix(instance): fix datasource name queries (#585)
Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent 2de6d15 commit fe0bb3e

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

scaleway/data_source_instance_image.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,30 @@ func dataSourceScalewayInstanceImageRead(d *schema.ResourceData, m interface{})
107107
if err != nil {
108108
return err
109109
}
110-
if len(res.Images) == 0 {
110+
var matchingImages []*instance.Image
111+
for _, image := range res.Images {
112+
if image.Name == d.Get("name").(string) {
113+
matchingImages = append(matchingImages, image)
114+
}
115+
}
116+
117+
if len(matchingImages) == 0 {
111118
return fmt.Errorf("no image found with the name %s and architecture %s in zone %s", d.Get("name"), d.Get("architecture"), zone)
112119
}
113-
if len(res.Images) > 1 && !d.Get("latest").(bool) {
114-
return fmt.Errorf("%d images found with the same name %s and architecture %s in zone %s", len(res.Images), d.Get("name"), d.Get("architecture"), zone)
120+
if len(matchingImages) > 1 && !d.Get("latest").(bool) {
121+
return fmt.Errorf("%d images found with the same name %s and architecture %s in zone %s", len(matchingImages), d.Get("name"), d.Get("architecture"), zone)
115122
}
116-
sort.Slice(res.Images, func(i, j int) bool {
117-
return res.Images[i].ModificationDate.After(*res.Images[j].ModificationDate)
123+
124+
sort.Slice(matchingImages, func(i, j int) bool {
125+
return matchingImages[i].ModificationDate.After(*matchingImages[j].ModificationDate)
118126
})
119-
for _, image := range res.Images {
127+
for _, image := range matchingImages {
120128
if image.Name == d.Get("name").(string) {
121129
imageID = image.ID
122130
break
123131
}
124132
}
125-
if imageID == "" {
126-
return fmt.Errorf("no image found with the name %s and architecture %s in zone %s", d.Get("name"), d.Get("architecture"), zone)
127-
}
133+
// imageID will always be set here
128134
}
129135

130136
zonedID := datasourceNewZonedID(imageID, zone)

scaleway/data_source_instance_security_group.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m inter
4747
if err != nil {
4848
return err
4949
}
50-
if len(res.SecurityGroups) == 0 {
51-
return fmt.Errorf("no security group found with the name %s", d.Get("name"))
50+
for _, sg := range res.SecurityGroups {
51+
if sg.Name == d.Get("name").(string) {
52+
if securityGroupID != "" {
53+
return fmt.Errorf("more than 1 security group found with the same name %s", d.Get("name"))
54+
}
55+
securityGroupID = sg.ID
56+
}
5257
}
53-
if len(res.SecurityGroups) > 1 {
54-
return fmt.Errorf("%d security groups found with the same name %s", len(res.SecurityGroups), d.Get("name"))
58+
if securityGroupID == "" {
59+
return fmt.Errorf("no security group found with the name %s", d.Get("name"))
5560
}
56-
securityGroupID = res.SecurityGroups[0].ID
5761
}
5862

5963
zonedID := datasourceNewZonedID(securityGroupID, zone)

scaleway/data_source_instance_server.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ func dataSourceScalewayInstanceServerRead(d *schema.ResourceData, m interface{})
4646
if err != nil {
4747
return err
4848
}
49-
if len(res.Servers) == 0 {
50-
return fmt.Errorf("no server found with the name %s", d.Get("name"))
49+
for _, instance := range res.Servers {
50+
if instance.Name == d.Get("name").(string) {
51+
if serverID != "" {
52+
return fmt.Errorf("more than 1 server found with the same name %s", d.Get("name"))
53+
}
54+
serverID = instance.ID
55+
}
5156
}
52-
if len(res.Servers) > 1 {
53-
return fmt.Errorf("%d servers found with the same name %s", len(res.Servers), d.Get("name"))
57+
if serverID == "" {
58+
return fmt.Errorf("no server found with the name %s", d.Get("name"))
5459
}
55-
serverID = res.Servers[0].ID
5660
}
5761

5862
zonedID := datasourceNewZonedID(serverID, zone)

scaleway/data_source_instance_volume.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ func dataSourceScalewayInstanceVolumeRead(d *schema.ResourceData, m interface{})
4545
if err != nil {
4646
return err
4747
}
48-
if len(res.Volumes) == 0 {
49-
return fmt.Errorf("no volume found with the name %s", d.Get("name"))
48+
for _, volume := range res.Volumes {
49+
if volume.Name == d.Get("name").(string) {
50+
if volumeID != "" {
51+
return fmt.Errorf("more than 1 volume found with the same name %s", d.Get("name"))
52+
}
53+
volumeID = volume.ID
54+
}
5055
}
51-
if len(res.Volumes) > 1 {
52-
return fmt.Errorf("%d volumes found with the same name %s", len(res.Volumes), d.Get("name"))
56+
if volumeID == "" {
57+
return fmt.Errorf("no volume found with the name %s", d.Get("name"))
5358
}
54-
volumeID = res.Volumes[0].ID
5559
}
5660

5761
zonedID := datasourceNewZonedID(volumeID, zone)

0 commit comments

Comments
 (0)