github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/scaleway/resource_scaleway_server.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/scaleway/scaleway-cli/pkg/api"
     9  )
    10  
    11  func resourceScalewayServer() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceScalewayServerCreate,
    14  		Read:   resourceScalewayServerRead,
    15  		Update: resourceScalewayServerUpdate,
    16  		Delete: resourceScalewayServerDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  			"image": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  			"type": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  			"bootscript": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  			},
    40  			"tags": &schema.Schema{
    41  				Type: schema.TypeList,
    42  				Elem: &schema.Schema{
    43  					Type: schema.TypeString,
    44  				},
    45  				Optional: true,
    46  			},
    47  			"enable_ipv6": &schema.Schema{
    48  				Type:     schema.TypeBool,
    49  				Optional: true,
    50  				Default:  false,
    51  			},
    52  			"dynamic_ip_required": &schema.Schema{
    53  				Type:     schema.TypeBool,
    54  				Optional: true,
    55  			},
    56  			"security_group": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Optional: true,
    59  			},
    60  			"private_ip": &schema.Schema{
    61  				Type:     schema.TypeString,
    62  				Computed: true,
    63  			},
    64  			"public_ip": &schema.Schema{
    65  				Type:     schema.TypeString,
    66  				Computed: true,
    67  			},
    68  			"state": &schema.Schema{
    69  				Type:     schema.TypeString,
    70  				Optional: true,
    71  				Computed: true,
    72  			},
    73  			"state_detail": &schema.Schema{
    74  				Type:     schema.TypeString,
    75  				Computed: true,
    76  			},
    77  		},
    78  	}
    79  }
    80  
    81  func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
    82  	scaleway := m.(*Client).scaleway
    83  
    84  	image := d.Get("image").(string)
    85  	var server = api.ScalewayServerDefinition{
    86  		Name:          d.Get("name").(string),
    87  		Image:         String(image),
    88  		Organization:  scaleway.Organization,
    89  		EnableIPV6:    d.Get("enable_ipv6").(bool),
    90  		SecurityGroup: d.Get("security_group").(string),
    91  	}
    92  
    93  	server.DynamicIPRequired = Bool(d.Get("dynamic_ip_required").(bool))
    94  	server.CommercialType = d.Get("type").(string)
    95  
    96  	if bootscript, ok := d.GetOk("bootscript"); ok {
    97  		server.Bootscript = String(bootscript.(string))
    98  	}
    99  
   100  	if raw, ok := d.GetOk("tags"); ok {
   101  		for _, tag := range raw.([]interface{}) {
   102  			server.Tags = append(server.Tags, tag.(string))
   103  		}
   104  	}
   105  
   106  	id, err := scaleway.PostServer(server)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	d.SetId(id)
   112  	if d.Get("state").(string) != "stopped" {
   113  		err = scaleway.PostServerAction(id, "poweron")
   114  		if err != nil {
   115  			return err
   116  		}
   117  
   118  		err = waitForServerState(scaleway, id, "running")
   119  	}
   120  
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	return resourceScalewayServerRead(d, m)
   126  }
   127  
   128  func resourceScalewayServerRead(d *schema.ResourceData, m interface{}) error {
   129  	scaleway := m.(*Client).scaleway
   130  	server, err := scaleway.GetServer(d.Id())
   131  
   132  	if err != nil {
   133  		if serr, ok := err.(api.ScalewayAPIError); ok {
   134  			log.Printf("[DEBUG] Error reading server: %q\n", serr.APIMessage)
   135  
   136  			if serr.StatusCode == 404 {
   137  				d.SetId("")
   138  				return nil
   139  			}
   140  		}
   141  
   142  		return err
   143  	}
   144  
   145  	d.Set("name", server.Name)
   146  	d.Set("image", server.Image.Identifier)
   147  	d.Set("type", server.CommercialType)
   148  	d.Set("enable_ipv6", server.EnableIPV6)
   149  	d.Set("private_ip", server.PrivateIP)
   150  	d.Set("public_ip", server.PublicAddress.IP)
   151  
   152  	d.Set("state", server.State)
   153  	d.Set("state_detail", server.StateDetail)
   154  	d.Set("tags", server.Tags)
   155  
   156  	d.SetConnInfo(map[string]string{
   157  		"type": "ssh",
   158  		"host": server.PublicAddress.IP,
   159  	})
   160  
   161  	return nil
   162  }
   163  
   164  func resourceScalewayServerUpdate(d *schema.ResourceData, m interface{}) error {
   165  	scaleway := m.(*Client).scaleway
   166  
   167  	var req api.ScalewayServerPatchDefinition
   168  
   169  	if d.HasChange("name") {
   170  		name := d.Get("name").(string)
   171  		req.Name = &name
   172  	}
   173  
   174  	if d.HasChange("tags") {
   175  		if raw, ok := d.GetOk("tags"); ok {
   176  			var tags []string
   177  			for _, tag := range raw.([]interface{}) {
   178  				tags = append(tags, tag.(string))
   179  			}
   180  			req.Tags = &tags
   181  		}
   182  	}
   183  
   184  	if d.HasChange("enable_ipv6") {
   185  		req.EnableIPV6 = Bool(d.Get("enable_ipv6").(bool))
   186  	}
   187  
   188  	if d.HasChange("dynamic_ip_required") {
   189  		req.DynamicIPRequired = Bool(d.Get("dynamic_ip_required").(bool))
   190  	}
   191  
   192  	if d.HasChange("security_group") {
   193  		req.SecurityGroup = &api.ScalewaySecurityGroup{
   194  			Identifier: d.Get("security_group").(string),
   195  		}
   196  	}
   197  
   198  	if err := scaleway.PatchServer(d.Id(), req); err != nil {
   199  		return fmt.Errorf("Failed patching scaleway server: %q", err)
   200  	}
   201  
   202  	return resourceScalewayServerRead(d, m)
   203  }
   204  
   205  func resourceScalewayServerDelete(d *schema.ResourceData, m interface{}) error {
   206  	scaleway := m.(*Client).scaleway
   207  
   208  	s, err := scaleway.GetServer(d.Id())
   209  	if err != nil {
   210  		return err
   211  	}
   212  
   213  	if s.State == "stopped" {
   214  		return deleteStoppedServer(scaleway, s)
   215  	}
   216  
   217  	err = deleteRunningServer(scaleway, s)
   218  
   219  	if err == nil {
   220  		d.SetId("")
   221  	}
   222  
   223  	return err
   224  }