Skip to content

Commit

Permalink
feat(marketplace): add image beta data source (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermoumoute authored Dec 6, 2019
1 parent 601efeb commit a0f39fd
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 8 deletions.
53 changes: 53 additions & 0 deletions scaleway/data_source_marketplace_image_beta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package scaleway

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/marketplace/v1"
)

func dataSourceScalewayMarketplaceImageBeta() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalewayMarketplaceImageReadBeta,

Schema: map[string]*schema.Schema{
"label": {
Type: schema.TypeString,
Required: true,
Description: "Exact label of the desired image",
},
"instance_type": {
Type: schema.TypeString,
Optional: true,
Default: "DEV1-S",
Description: "The instance commercial type of the desired image",
},
"zone": zoneSchema(),
},
}
}

func dataSourceScalewayMarketplaceImageReadBeta(d *schema.ResourceData, m interface{}) error {
meta := m.(*Meta)
marketplaceAPI := marketplace.NewAPI(meta.scwClient)
zone, err := getZone(d, meta)
if err != nil {
return err
}

imageID, err := marketplaceAPI.GetLocalImageIDByLabel(&marketplace.GetLocalImageIDByLabelRequest{
ImageLabel: d.Get("label").(string),
CommercialType: d.Get("instance_type").(string),
Zone: zone,
})
if err != nil {
return err
}

zonedID := datasourceNewZonedID(imageID, zone)
d.SetId(zonedID)
d.Set("zone", zone)
d.Set("label", d.Get("label"))
d.Set("instance_type", d.Get("type"))

return nil
}
27 changes: 27 additions & 0 deletions scaleway/data_source_marketplace_image_beta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scaleway

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccScalewayDataSourceMarketplaceImageBeta_Basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: `
data "scaleway_marketplace_image_beta" "test1" {
label = "ubuntu-bionic"
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceImageExists("data.scaleway_marketplace_image_beta.test1"),
resource.TestCheckResourceAttr("data.scaleway_marketplace_image_beta.test1", "id", "fr-par-1/f974feac-abae-4365-b988-8ec7d1cec10d"),
),
},
},
})
}
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func Provider() terraform.ResourceProvider {
"scaleway_instance_image": dataSourceScalewayInstanceImage(),
"scaleway_instance_volume": dataSourceScalewayInstanceVolume(),
"scaleway_baremetal_offer_beta": dataSourceScalewayBaremetalOfferBeta(),
"scaleway_marketplace_image_beta": dataSourceScalewayMarketplaceImageBeta(),
},
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-
# scaleway_image

**DEPRECATED**: This resource is deprecated and will be removed in `v2.0+`.
Please use `scaleway_instance_image` instead.
Please use `scaleway_instance_image` instead or `scaleway_marketplace_image_beta` 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
4 changes: 2 additions & 2 deletions website/docs/d/instance_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gets information about an instance image.
```hcl
// Get info by image name
data "scaleway_instance_image" "my_image" {
name = "ubuntu bionic"
name = "my-image-name"
}
// Get info by image id
Expand All @@ -33,7 +33,7 @@ data "scaleway_instance_image" "my_image" {

- `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.
- `zone` - (Defaults to [provider](../index.html#zone) `zone`) The [zone](../guides/regions_and_zones.html#zones) in which the image exists.

## Attributes Reference

Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/instance_security_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data "scaleway_instance_security_group" "my_key" {

- `security_group_id` - (Optional) The security group id. Only one of `name` and `security_group_id` should be specified.

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

## Attributes Reference

Expand Down
9 changes: 7 additions & 2 deletions website/docs/d/instance_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ data "scaleway_instance_server" "my_key" {

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

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

## Attributes Reference

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

- `id` - The ID of the server.

- `type` - The commercial type of the server.
You find all the available types on the [pricing page](https://www.scaleway.com/en/pricing/).

- `image` - The UUID and the label of the base image used by the server.

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

- `tags` - The tags associated with the server.
Expand All @@ -58,7 +63,7 @@ attached to the server.

- `state` - The state of the server. Possible values are: `started`, `stopped` or `standby`.

- `cloud_init` - The cloud init script associated with this server. Updates to this field will trigger a stop/start of the server.
- `cloud_init` - The cloud init script associated with this server.

- `user_data` - The user data associated with the server.

Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/instance_volume.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data "scaleway_instance_volume" "my_volume" {
- `volume_id` - (Optional) The volume id.
Only one of `name` and `volume_id` should be specified.

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

- `organization_id` - (Defaults to [provider](../index.html#organization_id) `organization_id`) The ID of the organization the server is associated with.

Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/marketplace_image_beta.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
layout: "scaleway"
page_title: "Scaleway: scaleway_marketplace_image_beta"
description: |-
Gets local image ID of an image from its label name.
---

# scaleway_marketplace_image_beta

Gets local image ID of an image from its label name.

## Example Usage

```hcl
data "scaleway_marketplace_image_beta" "my_image" {
labal = "ubuntu-bionic"
}
```

## Argument Reference

- `label` - (Required) Exact label of the desired image. You can use [this endpoint](https://api-marketplace.scaleway.com/images?page=1&per_page=100)
to find the right `label`.

- `instance_type` - (Optional, default `DEV1-S`) The instance type the image is compatible with.
You find all the available types on the [pricing page](https://www.scaleway.com/en/pricing/).

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

## Attributes Reference

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

- `id` - The ID of the image.
2 changes: 1 addition & 1 deletion website/docs/r/instance_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Updates to this field will recreate a new resource.
[//]: # (TODO: Improve me)

- `image` - (Required) The UUID or the label of the base image used by the server. You can use [this endpoint](https://api-marketplace.scaleway.com/images?page=1&per_page=100)
to find either the right `label` or the right local image `ID` for a given `commercial_type`.
to find either the right `label` or the right local image `ID` for a given `type`.

[//]: # (TODO: Improve me)

Expand Down
9 changes: 9 additions & 0 deletions website/scaleway.erb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@
</ul>
</li>

<li>
<a href="#">Marketplace Data Sources (Beta)</a>
<ul class="nav nav-visible">
<li>
<a href="/docs/providers/scaleway/d/marketplace_image_beta.html">scaleway_marketplace_image_beta</a>
</li>
</ul>
</li>

<li>
<a href="#">Deprecated Resources</a>
<ul class="nav">
Expand Down

0 comments on commit a0f39fd

Please sign in to comment.