Skip to content

Commit

Permalink
address comments: first part
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermoumoute committed Dec 4, 2019
1 parent 31df1f4 commit 1f12844
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
24 changes: 18 additions & 6 deletions scaleway/data_source_instance_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package scaleway

import (
"fmt"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayInstanceImage() *schema.Resource {
Expand All @@ -21,7 +23,7 @@ func dataSourceScalewayInstanceImage() *schema.Resource {
"image_id": {
Type: schema.TypeString,
Optional: true,
Description: "exact name of the desired image",
Description: "ID of the desired image",
ConflictsWith: []string{"name", "architecture"},
},
"architecture": {
Expand All @@ -31,6 +33,13 @@ func dataSourceScalewayInstanceImage() *schema.Resource {
Description: "architecture of the desired image",
ConflictsWith: []string{"image_id"},
},
"latest": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "select most recent image if multiple match",
ConflictsWith: []string{"image_id"},
},
"zone": zoneSchema(),
"organization_id": organizationIDSchema(),

Expand All @@ -39,7 +48,7 @@ func dataSourceScalewayInstanceImage() *schema.Resource {
Computed: true,
Description: "indication if the image is public",
},
"bootscript_id": {
"default_bootscript_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the bootscript associated with this image",
Expand Down Expand Up @@ -94,16 +103,19 @@ func dataSourceScalewayInstanceImageRead(d *schema.ResourceData, m interface{})
Zone: zone,
Name: expandStringPtr(d.Get("name")),
Arch: expandStringPtr(d.Get("architecture")),
})
}, scw.WithAllPages())
if err != nil {
return err
}
if len(res.Images) == 0 {
return fmt.Errorf("no image found with the name %s and architecture %s", d.Get("name"), d.Get("architecture"))
}
if len(res.Images) > 1 {
if len(res.Images) > 1 && !d.Get("latest").(bool) {
return fmt.Errorf("%d images found with the same name %s and architecture %s", len(res.Images), d.Get("name"), d.Get("architecture"))
}
sort.Slice(res.Images, func(i, j int) bool {
return res.Images[i].ModificationDate.After(res.Images[j].ModificationDate)
})
imageID = res.Images[0].ID
}

Expand All @@ -129,9 +141,9 @@ func dataSourceScalewayInstanceImageRead(d *schema.ResourceData, m interface{})
d.Set("state", resp.Image.State.String())

if resp.Image.DefaultBootscript != nil {
d.Set("bootscript_id", resp.Image.DefaultBootscript.ID)
d.Set("default_bootscript_id", resp.Image.DefaultBootscript.ID)
} else {
d.Set("bootscript_id", "")
d.Set("default_bootscript_id", "")
}

if resp.Image.RootVolume != nil {
Expand Down
3 changes: 2 additions & 1 deletion scaleway/data_source_instance_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ data "scaleway_instance_image" "stg" {
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "architecture", "x86_64"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "creation_date", "2018-04-12T10:22:46Z"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "modification_date", "2018-04-12T15:02:26Z"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "latest", "true"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "public", "true"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "from_server_id", ""),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "state", "available"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "bootscript_id", "b1e68c26-a19c-4eac-9222-498b22bd7ad9"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "default_bootscript_id", "b1e68c26-a19c-4eac-9222-498b22bd7ad9"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "root_volume_id", "8fa97c03-ca3b-4267-ba19-2d38190b1c82"),
resource.TestCheckNoResourceAttr("data.scaleway_instance_image.stg", "additional_volume_ids"),
),
Expand Down
3 changes: 2 additions & 1 deletion scaleway/data_source_instance_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayInstanceSecurityGroup() *schema.Resource {
Expand Down Expand Up @@ -42,7 +43,7 @@ func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m inter
res, err := instanceApi.ListSecurityGroups(&instance.ListSecurityGroupsRequest{
Zone: zone,
Name: String(d.Get("name").(string)),
})
}, scw.WithAllPages())
if err != nil {
return err
}
Expand Down

0 comments on commit 1f12844

Please sign in to comment.