From 2a30359992ecaadbc873fa78a55f3e62adff33e0 Mon Sep 17 00:00:00 2001 From: Olivier CANO Date: Tue, 3 Dec 2019 15:02:22 +0100 Subject: [PATCH 1/5] feat: data source instance server --- scaleway/data_source_helpers.go | 17 ++++- .../data_source_instance_security_group.go | 7 ++- ...ata_source_instance_security_group_test.go | 1 + scaleway/data_source_instance_server.go | 63 +++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 scaleway/data_source_instance_server.go diff --git a/scaleway/data_source_helpers.go b/scaleway/data_source_helpers.go index 644aa8d04..47dc16b37 100644 --- a/scaleway/data_source_helpers.go +++ b/scaleway/data_source_helpers.go @@ -1,9 +1,22 @@ package scaleway -// source: https://github.com/terraform-providers/terraform-provider-google/blob/master/google/datasource_helpers.go - import "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +func datasourceSetAliasAndID(d *schema.ResourceData, alias string, idI interface{}) error { + zone, id, err := parseZonedID(idI.(string)) + if err != nil { + return err + } + + d.SetId(newZonedId(zone, id)) + return d.Set(alias, d.Id()) +} + +//// +// The below methods are imported from Google's terraform provider. +// source: https://github.com/terraform-providers/terraform-provider-google/blob/master/google/datasource_helpers.go +//// + // datasourceSchemaFromResourceSchema is a recursive func that // converts an existing Resource schema to a Datasource schema. // All schema elements are copied, but certain attributes are ignored or changed: diff --git a/scaleway/data_source_instance_security_group.go b/scaleway/data_source_instance_security_group.go index da8ab00d2..ac507d001 100644 --- a/scaleway/data_source_instance_security_group.go +++ b/scaleway/data_source_instance_security_group.go @@ -32,6 +32,7 @@ func dataSourceScalewayInstanceSecurityGroup() *schema.Resource { func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m interface{}) error { meta := m.(*Meta) + getInstanceAPIWithZoneAndID() instanceApi, zone, err := getInstanceAPIWithZone(d, meta) if err != nil { return err @@ -55,7 +56,9 @@ func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m inter securityGroupID = res.SecurityGroups[0].ID } - d.SetId(newZonedId(zone, expandID(securityGroupID))) - d.Set("security_group_id", d.Id()) + err = datasourceSetAliasAndID(d, "security_group_id", securityGroupID) + if err != nil { + return err + } return resourceScalewayInstanceSecurityGroupRead(d, m) } diff --git a/scaleway/data_source_instance_security_group_test.go b/scaleway/data_source_instance_security_group_test.go index d9c995f10..701d751c6 100644 --- a/scaleway/data_source_instance_security_group_test.go +++ b/scaleway/data_source_instance_security_group_test.go @@ -18,6 +18,7 @@ func TestAccScalewayDataSourceInstanceSecurityGroup_Basic(t *testing.T) { Config: ` resource "scaleway_instance_security_group" "main" { name = "` + securityGroupName + `" + } data "scaleway_instance_security_group" "prod" { diff --git a/scaleway/data_source_instance_server.go b/scaleway/data_source_instance_server.go new file mode 100644 index 000000000..eae305f01 --- /dev/null +++ b/scaleway/data_source_instance_server.go @@ -0,0 +1,63 @@ +package scaleway + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" +) + +func dataSourceScalewayInstanceServer() *schema.Resource { + // Generate datasource schema from resource + dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayInstanceServer().Schema) + + // Set 'Optional' schema elements + addOptionalFieldsToSchema(dsSchema, "name", "organization_id", "zone") + + dsSchema["name"].ConflictsWith = []string{"server_id"} + dsSchema["server_id"] = &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "The ID of the server", + ValidateFunc: validationUUIDorUUIDWithLocality(), + ConflictsWith: []string{"name"}, + } + + return &schema.Resource{ + Read: dataSourceScalewayInstanceServerRead, + + Schema: dsSchema, + } +} + +func dataSourceScalewayInstanceServerRead(d *schema.ResourceData, m interface{}) error { + meta := m.(*Meta) + instanceApi, zone, err := getInstanceAPIWithZone(d, meta) + if err != nil { + return err + } + + serverID, ok := d.GetOk("server_id") + if !ok { + res, err := instanceApi.ListServers(&instance.ListServersRequest{ + Zone: zone, + Name: String(d.Get("name").(string)), + }) + if err != nil { + return err + } + if len(res.Servers) == 0 { + return fmt.Errorf("no server found with the name %s", d.Get("name")) + } + if len(res.Servers) > 1 { + return fmt.Errorf("%d servers found with the same name %s", len(res.Servers), d.Get("name")) + } + serverID = res.Servers[0].ID + } + + err = datasourceSetAliasAndID(d, "server_id", serverID) + if err != nil { + return err + } + return resourceScalewayInstanceServerRead(d, m) +} From 94aeab171687cba6413c3e134d75ce647e9644e7 Mon Sep 17 00:00:00 2001 From: Olivier CANO Date: Tue, 3 Dec 2019 15:19:19 +0100 Subject: [PATCH 2/5] add tests --- scaleway/data_source_helpers.go | 10 +++-- .../data_source_instance_security_group.go | 3 +- scaleway/data_source_instance_server.go | 2 +- scaleway/data_source_instance_server_test.go | 42 +++++++++++++++++++ scaleway/provider.go | 1 + 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 scaleway/data_source_instance_server_test.go diff --git a/scaleway/data_source_helpers.go b/scaleway/data_source_helpers.go index 47dc16b37..2e7301fc5 100644 --- a/scaleway/data_source_helpers.go +++ b/scaleway/data_source_helpers.go @@ -1,11 +1,15 @@ package scaleway -import "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/scaleway/scaleway-sdk-go/scw" +) -func datasourceSetAliasAndID(d *schema.ResourceData, alias string, idI interface{}) error { +func datasourceSetAliasAndID(d *schema.ResourceData, alias string, idI interface{}, fallBackZone scw.Zone) error { zone, id, err := parseZonedID(idI.(string)) if err != nil { - return err + id = idI.(string) + zone = fallBackZone } d.SetId(newZonedId(zone, id)) diff --git a/scaleway/data_source_instance_security_group.go b/scaleway/data_source_instance_security_group.go index ac507d001..f885c1230 100644 --- a/scaleway/data_source_instance_security_group.go +++ b/scaleway/data_source_instance_security_group.go @@ -32,7 +32,6 @@ func dataSourceScalewayInstanceSecurityGroup() *schema.Resource { func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m interface{}) error { meta := m.(*Meta) - getInstanceAPIWithZoneAndID() instanceApi, zone, err := getInstanceAPIWithZone(d, meta) if err != nil { return err @@ -56,7 +55,7 @@ func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m inter securityGroupID = res.SecurityGroups[0].ID } - err = datasourceSetAliasAndID(d, "security_group_id", securityGroupID) + err = datasourceSetAliasAndID(d, "security_group_id", securityGroupID, zone) if err != nil { return err } diff --git a/scaleway/data_source_instance_server.go b/scaleway/data_source_instance_server.go index eae305f01..a18703ee3 100644 --- a/scaleway/data_source_instance_server.go +++ b/scaleway/data_source_instance_server.go @@ -55,7 +55,7 @@ func dataSourceScalewayInstanceServerRead(d *schema.ResourceData, m interface{}) serverID = res.Servers[0].ID } - err = datasourceSetAliasAndID(d, "server_id", serverID) + err = datasourceSetAliasAndID(d, "server_id", serverID, zone) if err != nil { return err } diff --git a/scaleway/data_source_instance_server_test.go b/scaleway/data_source_instance_server_test.go new file mode 100644 index 000000000..c48903982 --- /dev/null +++ b/scaleway/data_source_instance_server_test.go @@ -0,0 +1,42 @@ +package scaleway + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccScalewayDataSourceInstanceServer_Basic(t *testing.T) { + serverName := acctest.RandString(10) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckScalewayInstanceServerDestroy, + Steps: []resource.TestStep{ + { + Config: ` +resource "scaleway_instance_server" "main" { + name = "` + serverName + `" + image = "ubuntu-bionic" + type = "DEV1-S" + tags = [ "terraform-test", "data_scaleway_instance_server", "basic" ] +} + +data "scaleway_instance_server" "prod" { + name = "${scaleway_instance_server.main.name}" +} + +data "scaleway_instance_server" "stg" { + server_id = "${scaleway_instance_server.main.id}" +}`, + Check: resource.ComposeTestCheckFunc( + testAccCheckScalewayInstanceServerExists("data.scaleway_instance_server.prod"), + resource.TestCheckResourceAttr("data.scaleway_instance_server.prod", "name", serverName), + testAccCheckScalewayInstanceServerExists("data.scaleway_instance_server.stg"), + resource.TestCheckResourceAttr("data.scaleway_instance_server.stg", "name", serverName), + ), + }, + }, + }) +} diff --git a/scaleway/provider.go b/scaleway/provider.go index 3e71b597f..b7f26f60f 100644 --- a/scaleway/provider.go +++ b/scaleway/provider.go @@ -222,6 +222,7 @@ func Provider() terraform.ResourceProvider { "scaleway_volume": dataSourceScalewayVolume(), "scaleway_account_ssh_key": dataSourceScalewayAccountSSHKey(), "scaleway_instance_security_group": dataSourceScalewayInstanceSecurityGroup(), + "scaleway_instance_server": dataSourceScalewayInstanceServer(), }, } From 113c533b615a63cdd97f8af3824830f3e53053c6 Mon Sep 17 00:00:00 2001 From: Olivier CANO Date: Tue, 3 Dec 2019 15:40:13 +0100 Subject: [PATCH 3/5] fix --- scaleway/data_source_helpers.go | 5 ++--- scaleway/data_source_instance_security_group.go | 7 +++---- scaleway/data_source_instance_server.go | 7 +++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/scaleway/data_source_helpers.go b/scaleway/data_source_helpers.go index 2e7301fc5..c3c4f4a44 100644 --- a/scaleway/data_source_helpers.go +++ b/scaleway/data_source_helpers.go @@ -5,15 +5,14 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" ) -func datasourceSetAliasAndID(d *schema.ResourceData, alias string, idI interface{}, fallBackZone scw.Zone) error { +func datasourceNewZonedID(idI interface{}, fallBackZone scw.Zone) string { zone, id, err := parseZonedID(idI.(string)) if err != nil { id = idI.(string) zone = fallBackZone } - d.SetId(newZonedId(zone, id)) - return d.Set(alias, d.Id()) + return newZonedId(zone, id) } //// diff --git a/scaleway/data_source_instance_security_group.go b/scaleway/data_source_instance_security_group.go index f885c1230..0df278423 100644 --- a/scaleway/data_source_instance_security_group.go +++ b/scaleway/data_source_instance_security_group.go @@ -55,9 +55,8 @@ func dataSourceScalewayInstanceSecurityGroupRead(d *schema.ResourceData, m inter securityGroupID = res.SecurityGroups[0].ID } - err = datasourceSetAliasAndID(d, "security_group_id", securityGroupID, zone) - if err != nil { - return err - } + zonedID := datasourceNewZonedID(securityGroupID, zone) + d.SetId(zonedID) + d.Set("security_group_id", zonedID) return resourceScalewayInstanceSecurityGroupRead(d, m) } diff --git a/scaleway/data_source_instance_server.go b/scaleway/data_source_instance_server.go index a18703ee3..658f956ce 100644 --- a/scaleway/data_source_instance_server.go +++ b/scaleway/data_source_instance_server.go @@ -55,9 +55,8 @@ func dataSourceScalewayInstanceServerRead(d *schema.ResourceData, m interface{}) serverID = res.Servers[0].ID } - err = datasourceSetAliasAndID(d, "server_id", serverID, zone) - if err != nil { - return err - } + zonedID := datasourceNewZonedID(serverID, zone) + d.SetId(zonedID) + d.Set("server_id", zonedID) return resourceScalewayInstanceServerRead(d, m) } From 46dea6cb6fe7c41aa36eaaf42254beb8923c2380 Mon Sep 17 00:00:00 2001 From: Olivier CANO Date: Tue, 3 Dec 2019 15:47:57 +0100 Subject: [PATCH 4/5] add doc --- .../d/instance_security_group.html.markdown | 2 +- website/docs/d/instance_server.html.markdown | 82 +++++++++++++++++++ website/scaleway.erb | 3 + 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 website/docs/d/instance_server.html.markdown diff --git a/website/docs/d/instance_security_group.html.markdown b/website/docs/d/instance_security_group.html.markdown index d321de07f..02990400c 100644 --- a/website/docs/d/instance_security_group.html.markdown +++ b/website/docs/d/instance_security_group.html.markdown @@ -1,6 +1,6 @@ --- layout: "scaleway" -page_title: "Scaleway: scaleway_security_group" +page_title: "Scaleway: scaleway_instance_security_group" description: |- Gets information about a Security Group. --- diff --git a/website/docs/d/instance_server.html.markdown b/website/docs/d/instance_server.html.markdown new file mode 100644 index 000000000..88773ca8e --- /dev/null +++ b/website/docs/d/instance_server.html.markdown @@ -0,0 +1,82 @@ +--- +layout: "scaleway" +page_title: "Scaleway: scaleway_instance_server" +description: |- + Gets information about an Instance Server. +--- + +# scaleway_instance_server + +Gets information about an instance server. + +## Example Usage + +```hcl +// Get info by server name +data "scaleway_instance_server" "my_key" { + name = "my-server-name" +} + +// Get info by server id +data "scaleway_instance_server" "my_key" { + server_id = "11111111-1111-1111-1111-111111111111" +} +``` + +## Argument Reference + +- `name` - (Optional) The server name. Only one of `name` and `server_id` should be specified. + +- `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. + +- `organization_id` - (Defaults to [provider](../index.html#organization_id) `organization_id`) The ID of the organization the server is associated with. + +## Attributes Reference + +In addition to all above arguments, the following attributes are exported: + +- `id` - The ID of the server. + +- `tags` - The tags associated with the server. + +- `security_group_id` - The [security group](https://developers.scaleway.com/en/products/instance/api/#security-groups-8d7f89) the server is attached to. + +- `placement_group_id` - The [placement group](https://developers.scaleway.com/en/products/instance/api/#placement-groups-d8f653) the server is attached to. + +- `root_volume` - Root [volume](https://developers.scaleway.com/en/products/instance/api/#volumes-7e8a39) attached to the server on creation. + - `size_in_gb` - Size of the root volume in gigabytes. + - `delete_on_termination` - Forces deletion of the root volume on instance termination. + +- `additional_volume_ids` - The [additional volumes](https://developers.scaleway.com/en/products/instance/api/#volumes-7e8a39) +attached to the server. + +- `enable_ipv6` - Determines if IPv6 is enabled for the server. + +- `disable_dynamic_ip` - Disable dynamic IP on 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. + +- `user_data` - The user data associated with the server. + + - `key` - The user data key. The `cloud-init` key is reserved, please use `cloud_init` attribute instead. + + - `value` - The user data content. + +- `placement_group_policy_respected` - True when the placement group policy is respected. + +- `root_volume` + - `volume_id` - The volume ID of the root volume of the server. + +- `private_ip` - The Scaleway internal IP address of the server. + +- `public_ip` - The public IPv4 address of the server. + +- `ipv6_address` - The default ipv6 address routed to the server. ( Only set when enable_ipv6 is set to true ) + +- `ipv6_gateway` - The ipv6 gateway address. ( Only set when enable_ipv6 is set to true ) + +- `ipv6_prefix_length` - The prefix length of the ipv6 subnet routed to the server. ( Only set when enable_ipv6 is set to true ) diff --git a/website/scaleway.erb b/website/scaleway.erb index 9dc38c51a..44ec00b0f 100644 --- a/website/scaleway.erb +++ b/website/scaleway.erb @@ -64,6 +64,9 @@
  • Instance Data Sources