2016-07-13 22:03:41 +02:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/scaleway/scaleway-cli/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceScalewayServer() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceScalewayServerCreate,
|
|
|
|
Read: resourceScalewayServerRead,
|
|
|
|
Update: resourceScalewayServerUpdate,
|
|
|
|
Delete: resourceScalewayServerDelete,
|
2016-10-15 23:49:14 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
2016-07-13 22:03:41 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"image": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"bootscript": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"tags": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-07-25 13:49:09 +02:00
|
|
|
"enable_ipv6": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
"dynamic_ip_required": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"security_group": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-29 13:07:35 +02:00
|
|
|
"volume": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"size_in_gb": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateVolumeSize,
|
|
|
|
},
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateVolumeType,
|
|
|
|
},
|
|
|
|
"volume_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-07-25 13:49:09 +02:00
|
|
|
"private_ip": &schema.Schema{
|
2016-07-13 22:03:41 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-07-25 13:49:09 +02:00
|
|
|
"public_ip": &schema.Schema{
|
2016-07-13 22:03:41 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"state": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"state_detail": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
|
|
|
|
scaleway := m.(*Client).scaleway
|
|
|
|
|
|
|
|
image := d.Get("image").(string)
|
|
|
|
var server = api.ScalewayServerDefinition{
|
2016-07-25 13:49:09 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Image: String(image),
|
|
|
|
Organization: scaleway.Organization,
|
|
|
|
EnableIPV6: d.Get("enable_ipv6").(bool),
|
|
|
|
SecurityGroup: d.Get("security_group").(string),
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server.DynamicIPRequired = Bool(d.Get("dynamic_ip_required").(bool))
|
|
|
|
server.CommercialType = d.Get("type").(string)
|
|
|
|
|
|
|
|
if bootscript, ok := d.GetOk("bootscript"); ok {
|
|
|
|
server.Bootscript = String(bootscript.(string))
|
|
|
|
}
|
|
|
|
|
2016-10-29 13:07:35 +02:00
|
|
|
if vs, ok := d.GetOk("volume"); ok {
|
|
|
|
server.Volumes = make(map[string]string)
|
|
|
|
|
|
|
|
volumes := vs.([]interface{})
|
|
|
|
for i, v := range volumes {
|
|
|
|
volume := v.(map[string]interface{})
|
|
|
|
|
|
|
|
volumeID, err := scaleway.PostVolume(api.ScalewayVolumeDefinition{
|
|
|
|
Size: uint64(volume["size_in_gb"].(int)) * gb,
|
|
|
|
Type: volume["type"].(string),
|
|
|
|
Name: fmt.Sprintf("%s-%d", server.Name, volume["size_in_gb"].(int)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
volume["volume_id"] = volumeID
|
|
|
|
volumes[i] = volume
|
|
|
|
server.Volumes[fmt.Sprintf("%d", i+1)] = volumeID
|
|
|
|
}
|
|
|
|
d.Set("volume", volumes)
|
|
|
|
}
|
|
|
|
|
2016-07-18 11:12:15 +02:00
|
|
|
if raw, ok := d.GetOk("tags"); ok {
|
|
|
|
for _, tag := range raw.([]interface{}) {
|
|
|
|
server.Tags = append(server.Tags, tag.(string))
|
|
|
|
}
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
id, err := scaleway.PostServer(server)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(id)
|
|
|
|
if d.Get("state").(string) != "stopped" {
|
|
|
|
err = scaleway.PostServerAction(id, "poweron")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = waitForServerState(scaleway, id, "running")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceScalewayServerRead(d, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceScalewayServerRead(d *schema.ResourceData, m interface{}) error {
|
|
|
|
scaleway := m.(*Client).scaleway
|
|
|
|
server, err := scaleway.GetServer(d.Id())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if serr, ok := err.(api.ScalewayAPIError); ok {
|
|
|
|
log.Printf("[DEBUG] Error reading server: %q\n", serr.APIMessage)
|
|
|
|
|
|
|
|
if serr.StatusCode == 404 {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-15 23:49:14 +02:00
|
|
|
d.Set("name", server.Name)
|
|
|
|
d.Set("image", server.Image.Identifier)
|
|
|
|
d.Set("type", server.CommercialType)
|
|
|
|
d.Set("enable_ipv6", server.EnableIPV6)
|
2016-07-25 13:49:09 +02:00
|
|
|
d.Set("private_ip", server.PrivateIP)
|
|
|
|
d.Set("public_ip", server.PublicAddress.IP)
|
|
|
|
|
2016-07-13 22:03:41 +02:00
|
|
|
d.Set("state", server.State)
|
|
|
|
d.Set("state_detail", server.StateDetail)
|
2016-07-18 11:12:15 +02:00
|
|
|
d.Set("tags", server.Tags)
|
2016-07-13 22:03:41 +02:00
|
|
|
|
|
|
|
d.SetConnInfo(map[string]string{
|
|
|
|
"type": "ssh",
|
|
|
|
"host": server.PublicAddress.IP,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceScalewayServerUpdate(d *schema.ResourceData, m interface{}) error {
|
|
|
|
scaleway := m.(*Client).scaleway
|
|
|
|
|
|
|
|
var req api.ScalewayServerPatchDefinition
|
|
|
|
|
|
|
|
if d.HasChange("name") {
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
req.Name = &name
|
|
|
|
}
|
|
|
|
|
2016-07-18 11:12:15 +02:00
|
|
|
if d.HasChange("tags") {
|
|
|
|
if raw, ok := d.GetOk("tags"); ok {
|
|
|
|
var tags []string
|
|
|
|
for _, tag := range raw.([]interface{}) {
|
|
|
|
tags = append(tags, tag.(string))
|
|
|
|
}
|
|
|
|
req.Tags = &tags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 13:49:09 +02:00
|
|
|
if d.HasChange("enable_ipv6") {
|
|
|
|
req.EnableIPV6 = Bool(d.Get("enable_ipv6").(bool))
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:03:41 +02:00
|
|
|
if d.HasChange("dynamic_ip_required") {
|
|
|
|
req.DynamicIPRequired = Bool(d.Get("dynamic_ip_required").(bool))
|
|
|
|
}
|
|
|
|
|
2016-07-25 13:49:09 +02:00
|
|
|
if d.HasChange("security_group") {
|
|
|
|
req.SecurityGroup = &api.ScalewaySecurityGroup{
|
|
|
|
Identifier: d.Get("security_group").(string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:03:41 +02:00
|
|
|
if err := scaleway.PatchServer(d.Id(), req); err != nil {
|
|
|
|
return fmt.Errorf("Failed patching scaleway server: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceScalewayServerRead(d, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceScalewayServerDelete(d *schema.ResourceData, m interface{}) error {
|
|
|
|
scaleway := m.(*Client).scaleway
|
|
|
|
|
2016-10-27 13:37:40 +02:00
|
|
|
s, err := scaleway.GetServer(d.Id())
|
2016-07-13 22:03:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:37:40 +02:00
|
|
|
if s.State == "stopped" {
|
|
|
|
return deleteStoppedServer(scaleway, s)
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 13:37:40 +02:00
|
|
|
err = deleteRunningServer(scaleway, s)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
d.SetId("")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|