コード例 #1
0
func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
	scaleway := m.(*Client).scaleway

	image := d.Get("image").(string)
	var server = api.ScalewayServerDefinition{
		Name:          d.Get("name").(string),
		Image:         String(image),
		Organization:  scaleway.Organization,
		EnableIPV6:    d.Get("enable_ipv6").(bool),
		SecurityGroup: d.Get("security_group").(string),
	}

	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))
	}

	if raw, ok := d.GetOk("tags"); ok {
		for _, tag := range raw.([]interface{}) {
			server.Tags = append(server.Tags, tag.(string))
		}
	}

	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)
}
コード例 #2
0
func resourceServerCreate(d *schema.ResourceData, m interface{}) error {
	scaleway := m.(*api.ScalewayAPI)

	image := d.Get("image").(string)

	volumes := make(map[string]string)

	/*for k, v := range d.Get("volumes").(map[string]interface{}) {
		volumes[k] = v.(string)
	}*/

	var def api.ScalewayServerDefinition

	def.Name = d.Get("name").(string)
	def.Image = &image
	def.Volumes = volumes

	ipRequired := false
	def.DynamicIPRequired = &ipRequired
	def.CommercialType = d.Get("type").(string)

	if bootscriptI, ok := d.GetOk("bootscript"); ok {
		bootscript := bootscriptI.(string)
		def.Bootscript = &bootscript
	}

	if tags, ok := d.GetOk("tags"); ok {
		def.Tags = tags.([]string)
	}

	id, err := scaleway.PostServer(def)
	if err != nil {
		serr := err.(api.ScalewayAPIError)
		// _, _ := json.Marshal(def)
		return fmt.Errorf("Error Posting server with image %s. Reason: %s", image, serr.APIMessage)
	}

	err = scaleway.PostServerAction(id, "poweron")

	if err != nil {
		return err
	}

	d.SetId(id)

	_, err = api.WaitForServerState(scaleway, id, "running")

	if err != nil {
		return err
	}

	return resourceServerRead(d, m)
}
コード例 #3
0
func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
	scaleway := m.(*Client).scaleway

	image := d.Get("image").(string)
	var server = api.ScalewayServerDefinition{
		Name:          d.Get("name").(string),
		Image:         String(image),
		Organization:  scaleway.Organization,
		EnableIPV6:    d.Get("enable_ipv6").(bool),
		SecurityGroup: d.Get("security_group").(string),
	}

	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))
	}

	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)
	}

	if raw, ok := d.GetOk("tags"); ok {
		for _, tag := range raw.([]interface{}) {
			server.Tags = append(server.Tags, tag.(string))
		}
	}

	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)
}