Skip to content

fix(instance): fix datasource name queries #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions scaleway/data_source_instance_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,30 @@ func dataSourceScalewayInstanceImageRead(d *schema.ResourceData, m interface{})
if err != nil {
return err
}
if len(res.Images) == 0 {
var matchingImages []*instance.Image
for _, image := range res.Images {
if image.Name == d.Get("name").(string) {
matchingImages = append(matchingImages, image)
}
}

if len(matchingImages) == 0 {
return fmt.Errorf("no image found with the name %s and architecture %s in zone %s", d.Get("name"), d.Get("architecture"), zone)
}
if len(res.Images) > 1 && !d.Get("latest").(bool) {
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)
if len(matchingImages) > 1 && !d.Get("latest").(bool) {
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)
}
sort.Slice(res.Images, func(i, j int) bool {
return res.Images[i].ModificationDate.After(*res.Images[j].ModificationDate)

sort.Slice(matchingImages, func(i, j int) bool {
return matchingImages[i].ModificationDate.After(*matchingImages[j].ModificationDate)
})
for _, image := range res.Images {
for _, image := range matchingImages {
if image.Name == d.Get("name").(string) {
imageID = image.ID
break
}
}
if imageID == "" {
return fmt.Errorf("no image found with the name %s and architecture %s in zone %s", d.Get("name"), d.Get("architecture"), zone)
}
// imageID will always be set here
}

zonedID := datasourceNewZonedID(imageID, zone)
Expand Down
14 changes: 9 additions & 5 deletions scaleway/data_source_instance_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m inter
if err != nil {
return err
}
if len(res.SecurityGroups) == 0 {
return fmt.Errorf("no security group found with the name %s", d.Get("name"))
for _, sg := range res.SecurityGroups {
if sg.Name == d.Get("name").(string) {
if securityGroupID != "" {
return fmt.Errorf("more than 1 security group found with the same name %s", d.Get("name"))
}
securityGroupID = sg.ID
}
}
if len(res.SecurityGroups) > 1 {
return fmt.Errorf("%d security groups found with the same name %s", len(res.SecurityGroups), d.Get("name"))
if securityGroupID == "" {
return fmt.Errorf("no security group found with the name %s", d.Get("name"))
}
securityGroupID = res.SecurityGroups[0].ID
}

zonedID := datasourceNewZonedID(securityGroupID, zone)
Expand Down
14 changes: 9 additions & 5 deletions scaleway/data_source_instance_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ func dataSourceScalewayInstanceServerRead(d *schema.ResourceData, m interface{})
if err != nil {
return err
}
if len(res.Servers) == 0 {
return fmt.Errorf("no server found with the name %s", d.Get("name"))
for _, instance := range res.Servers {
if instance.Name == d.Get("name").(string) {
if serverID != "" {
return fmt.Errorf("more than 1 server found with the same name %s", d.Get("name"))
}
serverID = instance.ID
}
}
if len(res.Servers) > 1 {
return fmt.Errorf("%d servers found with the same name %s", len(res.Servers), d.Get("name"))
if serverID == "" {
return fmt.Errorf("no server found with the name %s", d.Get("name"))
}
serverID = res.Servers[0].ID
}

zonedID := datasourceNewZonedID(serverID, zone)
Expand Down
14 changes: 9 additions & 5 deletions scaleway/data_source_instance_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ func dataSourceScalewayInstanceVolumeRead(d *schema.ResourceData, m interface{})
if err != nil {
return err
}
if len(res.Volumes) == 0 {
return fmt.Errorf("no volume found with the name %s", d.Get("name"))
for _, volume := range res.Volumes {
if volume.Name == d.Get("name").(string) {
if volumeID != "" {
return fmt.Errorf("more than 1 volume found with the same name %s", d.Get("name"))
}
volumeID = volume.ID
}
}
if len(res.Volumes) > 1 {
return fmt.Errorf("%d volumes found with the same name %s", len(res.Volumes), d.Get("name"))
if volumeID == "" {
return fmt.Errorf("no volume found with the name %s", d.Get("name"))
}
volumeID = res.Volumes[0].ID
}

zonedID := datasourceNewZonedID(volumeID, zone)
Expand Down