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): add data source instance server #350

Merged
merged 5 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 18 additions & 2 deletions scaleway/data_source_helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
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"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func datasourceNewZonedID(idI interface{}, fallBackZone scw.Zone) string {
zone, id, err := parseZonedID(idI.(string))
if err != nil {
id = idI.(string)
zone = fallBackZone
}

import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
return newZonedId(zone, 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.
Expand Down
7 changes: 4 additions & 3 deletions scaleway/data_source_instance_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func dataSourceScalewayInstanceSecurityGroup() *schema.Resource {
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayInstanceSecurityGroup().Schema)

// Set 'Optional' schema elements
addOptionalFieldsToSchema(dsSchema, "name", "organization_id", "zone")
addOptionalFieldsToSchema(dsSchema, "name", "zone")

dsSchema["name"].ConflictsWith = []string{"security_group_id"}
dsSchema["security_group_id"] = &schema.Schema{
Expand Down Expand Up @@ -55,7 +55,8 @@ 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())
zonedID := datasourceNewZonedID(securityGroupID, zone)
d.SetId(zonedID)
d.Set("security_group_id", zonedID)
return resourceScalewayInstanceSecurityGroupRead(d, m)
}
1 change: 1 addition & 0 deletions scaleway/data_source_instance_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
62 changes: 62 additions & 0 deletions scaleway/data_source_instance_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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", "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
}

zonedID := datasourceNewZonedID(serverID, zone)
d.SetId(zonedID)
d.Set("server_id", zonedID)
return resourceScalewayInstanceServerRead(d, m)
}
42 changes: 42 additions & 0 deletions scaleway/data_source_instance_server_test.go
Original file line number Diff line number Diff line change
@@ -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),
),
},
},
})
}
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}

Expand Down
6 changes: 3 additions & 3 deletions website/docs/d/instance_security_group.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
---
Expand Down Expand Up @@ -31,14 +31,14 @@ data "scaleway_instance_security_group" "my_key" {

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

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

## Attributes Reference

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

- `id` - The ID of the security group.

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

- `inbound_default_policy` - The default policy on incoming traffic. Possible values are: `accept` or `drop`.

- `outbound_default_policy` - The default policy on outgoing traffic. Possible values are: `accept` or `drop`.
Expand Down
82 changes: 82 additions & 0 deletions website/docs/d/instance_server.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

## Attributes Reference

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

- `id` - The ID of the server.

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

- `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 )
3 changes: 3 additions & 0 deletions website/scaleway.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
<li>
<a href="#">Instance Data Sources</a>
<ul class="nav nav-visible">
<li>
<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>
</li>
Expand Down