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