From 42e2bec633a17a98973ee49713a63187be03d824 Mon Sep 17 00:00:00 2001 From: Olivier CANO Date: Thu, 5 Dec 2019 18:05:41 +0100 Subject: [PATCH 1/6] feat(marketplace): add image data source (beta) --- .../data_source_marketplace_image_beta.go | 50 +++++++++++++++++++ ...data_source_marketplace_image_beta_test.go | 27 ++++++++++ scaleway/provider.go | 1 + website/docs/d/instance_server.html.markdown | 5 ++ .../d/marketplace_image_beta.html.markdown | 34 +++++++++++++ website/docs/r/instance_server.html.markdown | 2 +- website/scaleway.erb | 9 ++++ 7 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 scaleway/data_source_marketplace_image_beta.go create mode 100644 scaleway/data_source_marketplace_image_beta_test.go create mode 100644 website/docs/d/marketplace_image_beta.html.markdown diff --git a/scaleway/data_source_marketplace_image_beta.go b/scaleway/data_source_marketplace_image_beta.go new file mode 100644 index 000000000..81c3eda52 --- /dev/null +++ b/scaleway/data_source_marketplace_image_beta.go @@ -0,0 +1,50 @@ +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, + }) + + 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 +} diff --git a/scaleway/data_source_marketplace_image_beta_test.go b/scaleway/data_source_marketplace_image_beta_test.go new file mode 100644 index 000000000..6ca77b3db --- /dev/null +++ b/scaleway/data_source_marketplace_image_beta_test.go @@ -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"), + ), + }, + }, + }) +} diff --git a/scaleway/provider.go b/scaleway/provider.go index f089cabb5..93deec592 100644 --- a/scaleway/provider.go +++ b/scaleway/provider.go @@ -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(), }, } diff --git a/website/docs/d/instance_server.html.markdown b/website/docs/d/instance_server.html.markdown index 9b64dcb07..bc1fe900e 100644 --- a/website/docs/d/instance_server.html.markdown +++ b/website/docs/d/instance_server.html.markdown @@ -37,6 +37,11 @@ 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. diff --git a/website/docs/d/marketplace_image_beta.html.markdown b/website/docs/d/marketplace_image_beta.html.markdown new file mode 100644 index 000000000..8d14bdd81 --- /dev/null +++ b/website/docs/d/marketplace_image_beta.html.markdown @@ -0,0 +1,34 @@ +--- +layout: "scaleway" +page_title: "Scaleway: scaleway_marketplace_image_beta" +description: |- + Gets image ID of an image from its label name. +--- + +# scaleway_marketplace_image_beta + +Gets information about an instance image. + +## 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 should be created. + +## Attributes Reference + +In addition to all above arguments, the following attributes are exported: + +- `id` - The ID of the image. diff --git a/website/docs/r/instance_server.html.markdown b/website/docs/r/instance_server.html.markdown index 4dfd345a9..04f5b7870 100644 --- a/website/docs/r/instance_server.html.markdown +++ b/website/docs/r/instance_server.html.markdown @@ -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) diff --git a/website/scaleway.erb b/website/scaleway.erb index 073022f5f..dd8ee02b8 100644 --- a/website/scaleway.erb +++ b/website/scaleway.erb @@ -145,6 +145,15 @@ +
  • + Marketplace Data Sources (Beta) + +
  • +
  • Deprecated Resources