Skip to content
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

feat(instance): data source image #359

Merged
merged 9 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
32 changes: 18 additions & 14 deletions scaleway/data_source_instance_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func dataSourceScalewayInstanceImage() *schema.Resource {
"name": {
Type: schema.TypeString,
Optional: true,
Description: "exact name of the desired image",
Description: "Exact name of the desired image",
ConflictsWith: []string{"image_id"},
},
"image_id": {
Expand All @@ -30,14 +30,14 @@ func dataSourceScalewayInstanceImage() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Default: instance.ArchX86_64.String(),
Description: "architecture of the desired image",
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",
Description: "Select most recent image if multiple match",
ConflictsWith: []string{"image_id"},
},
"zone": zoneSchema(),
Expand All @@ -46,7 +46,7 @@ func dataSourceScalewayInstanceImage() *schema.Resource {
"public": {
Type: schema.TypeBool,
Computed: true,
Description: "indication if the image is public",
Description: "Indication if the image is public",
},
"default_bootscript_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -74,17 +74,17 @@ func dataSourceScalewayInstanceImage() *schema.Resource {
"creation_date": {
Type: schema.TypeString,
Computed: true,
Description: "date when the image was created",
Description: "Date when the image was created",
},
"modification_date": {
Type: schema.TypeString,
Computed: true,
Description: "date when the image was updated",
Description: "Date when the image was updated",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "state of the image",
Description: "State of the image",
},
},
}
Expand All @@ -98,7 +98,7 @@ func dataSourceScalewayInstanceImageRead(d *schema.ResourceData, m interface{})
}

imageID, ok := d.GetOk("image_id")
if !ok {
if !ok { // Get instance by name, zone, and arch.
res, err := instanceApi.ListImages(&instance.ListImagesRequest{
QuentinBrosse marked this conversation as resolved.
Show resolved Hide resolved
Zone: zone,
Name: expandStringPtr(d.Get("name")),
Expand All @@ -108,28 +108,32 @@ func dataSourceScalewayInstanceImageRead(d *schema.ResourceData, m interface{})
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"))
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", len(res.Images), d.Get("name"), d.Get("architecture"))
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)
}
sort.Slice(res.Images, func(i, j int) bool {
return res.Images[i].ModificationDate.After(res.Images[j].ModificationDate)
})
imageID = res.Images[0].ID
}

zonedID := datasourceNewZonedID(imageID, zone)
zone, imageID, _ = parseZonedID(zonedID)

d.SetId(zonedID)
d.Set("image_id", zonedID)
d.Set("zone", zone)

resp, err := instanceApi.GetImage(&instance.GetImageRequest{
Zone: zone,
kindermoumoute marked this conversation as resolved.
Show resolved Hide resolved
ImageID: imageID.(string),
})
if err != nil {
return err
}
zonedID := datasourceNewZonedID(imageID, zone)
d.SetId(zonedID)
d.Set("security_group_id", zonedID)
d.Set("zone", zone)

d.Set("organization_id", resp.Image.Organization)
d.Set("architecture", resp.Image.Arch)
d.Set("name", resp.Image.Name)
Expand Down
39 changes: 23 additions & 16 deletions scaleway/data_source_instance_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,35 @@ func TestAccScalewayDataSourceInstanceImage_Basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: `
data "scaleway_instance_image" "prod" {
data "scaleway_instance_image" "test1" {
name = "golang 1.10"
}

data "scaleway_instance_image" "stg" {
data "scaleway_instance_image" "test2" {
image_id = "43213956-c7a3-44b8-9d96-d51fa7457969"
}

data "scaleway_instance_image" "test3" {
image_id = "fr-par-1/43213956-c7a3-44b8-9d96-d51fa7457969"
}`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceImageExists("data.scaleway_instance_image.prod"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.prod", "name", "Golang 1.10"),
testAccCheckScalewayInstanceImageExists("data.scaleway_instance_image.stg"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.stg", "name", "Golang 1.10"),
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", "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"),
testAccCheckScalewayInstanceImageExists("data.scaleway_instance_image.test1"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test1", "name", "Golang 1.10"),
testAccCheckScalewayInstanceImageExists("data.scaleway_instance_image.test2"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "image_id", "fr-par-1/43213956-c7a3-44b8-9d96-d51fa7457969"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "name", "Golang 1.10"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "architecture", "x86_64"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "creation_date", "2018-04-12T10:22:46Z"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "modification_date", "2018-04-12T15:02:26Z"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "latest", "true"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "public", "true"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "from_server_id", ""),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "state", "available"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "default_bootscript_id", "b1e68c26-a19c-4eac-9222-498b22bd7ad9"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test2", "root_volume_id", "8fa97c03-ca3b-4267-ba19-2d38190b1c82"),
resource.TestCheckNoResourceAttr("data.scaleway_instance_image.test2", "additional_volume_ids"),
testAccCheckScalewayInstanceImageExists("data.scaleway_instance_image.test3"),
resource.TestCheckResourceAttr("data.scaleway_instance_image.test3", "name", "Golang 1.10"),
),
},
},
Expand Down
3 changes: 3 additions & 0 deletions website/docs/d/image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ description: |-

# scaleway_image

**DEPRECATED**: This resource is deprecated and will be removed in `v2.0+`.
Please use `scaleway_instance_image` instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or scaleway_marketplace_image depending on your usage


Use this data source to get the ID of a registered Image for use with the
`scaleway_server` resource.

Expand Down
60 changes: 60 additions & 0 deletions website/docs/d/instance_image.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
layout: "scaleway"
page_title: "Scaleway: scaleway_instance_image"
description: |-
Gets information about an Instance Image.
---

# scaleway_instance_image

Gets information about an instance image.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are looking for public image like oses, and ready-to-use application please have a look at scaleway_marketplace_image datasource instead


## Example Usage

```hcl
// Get info by image name
data "scaleway_instance_image" "my_image" {
name = "ubuntu bionic"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't feature a use-case like ubuntu. These use-case should be cover by scaleway_marketplace_image
Use a name like my-image-name

}

// Get info by image id
data "scaleway_instance_image" "my_image" {
image_id = "11111111-1111-1111-1111-111111111111"
}
```

## Argument Reference

- `name` - (Optional) The image name. Only one of `name` and `image_id` should be specified.

- `image_id` - (Optional) The image id. Only one of `name` and `image_id` should be specified.

- `architecture` - (Optional, default `x86_64`) The architecture the image is compatible with. Possible values are: `x86_64` or `arm`.

- `latest` - (Optional, default `true`) Use the latest image ID.

- `zone` - (Defaults to [provider](../index.html#zone) `zone`) The [zone](../guides/regions_and_zones.html#zones) in which the image should be created.

## Attributes Reference

In addition to all above arguments, the following attributes are exported:

- `id` - The ID of the image.

- `organization_id` - The ID of the organization the image is associated with.

- `creation_date` - Date of the image creation.

- `modification_date` - Date of image latest update.

- `public` - Set to `true` if the image is public.

- `from_server_id` - ID of the server the image if based from.

- `state` - State of the image. Possible values are: `available`, `creating` or `error`.

- `default_bootscript_id` - ID of the default bootscript for this image.

- `root_volume_id` - ID of the root volume in this image.

- `additional_volume_ids` - IDs of the additional volumes in this image.
3 changes: 3 additions & 0 deletions website/docs/d/security_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ description: |-

# scaleway_security_group

**DEPRECATED**: This resource is deprecated and will be removed in `v2.0+`.
Please use `scaleway_instance_security_group` instead.

Gets information about a Security Group.

## Example Usage
Expand Down
7 changes: 5 additions & 2 deletions website/scaleway.erb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
<a href="/docs/providers/scaleway/d/instance_server.html">scaleway_instance_server</a>
</li>
<li>
<a href="/docs/providers/scaleway/d/bootscript.html">scaleway_bootscript</a>
<a href="/docs/providers/scaleway/d/instance_image.html">scaleway_instance_image</a>
</li>
<li>
<a href="/docs/providers/scaleway/d/image.html">scaleway_image</a>
<a href="/docs/providers/scaleway/d/bootscript.html">scaleway_bootscript</a>
</li>
<li>
<a href="/docs/providers/scaleway/d/instance_security_group.html">scaleway_instance_security_group</a>
Expand Down Expand Up @@ -178,6 +178,9 @@
<li>
<a href="/docs/providers/scaleway/d/security_group.html">scaleway_security_group</a>
</li>
<li>
<a href="/docs/providers/scaleway/d/image.html">scaleway_image</a>
kindermoumoute marked this conversation as resolved.
Show resolved Hide resolved
</li>
</ul>
</li>

Expand Down