Ejemplo n.º 1
0
// CreateDroplet creates a new droplet with a hostname, key and image_id. It
// returns back the dropletInfo.
func (d *DigitalOcean) CreateDroplet(hostname string, keyId, image_id uint) (*DropletInfo, error) {
	params := url.Values{}
	params.Set("name", hostname)

	found_size, err := d.Client.Size(d.Builder.Size)
	if err != nil {
		return nil, fmt.Errorf("Invalid size or lookup failure: '%s': %s", d.Builder.Size, err)
	}

	found_region, err := d.Client.Region(d.Builder.Region)
	if err != nil {
		return nil, fmt.Errorf("Invalid region or lookup failure: '%s': %s", d.Builder.Region, err)
	}

	params.Set("size_slug", found_size.Slug)
	params.Set("image_id", strconv.Itoa(int(image_id)))
	params.Set("region_slug", found_region.Slug)
	params.Set("ssh_key_ids", fmt.Sprintf("%v", keyId))
	params.Set("private_networking", fmt.Sprintf("%v", d.Builder.PrivateNetworking))

	body, err := digitalocean.NewRequest(*d.Client, "droplets/new", params)
	if err != nil {
		return nil, err
	}

	info := &DropletInfo{}
	if err := mapstructure.Decode(body, info); err != nil {
		return nil, err
	}

	return info, nil
}
Ejemplo n.º 2
0
// Droplets returns a slice of all Droplets.
func (d *DigitalOcean) Droplets() (Droplets, error) {
	resp, err := digitalocean.NewRequest(*d.Client, "droplets", url.Values{})
	if err != nil {
		return nil, err
	}

	var result DropletsResp
	if err := mapstructure.Decode(resp, &result); err != nil {
		return nil, err
	}

	return result.Droplets, nil
}
Ejemplo n.º 3
0
Archivo: key.go Proyecto: koding/koding
// Keys returns all available public SSH Keys
func (d *DigitalOcean) Keys() (SSHKeys, error) {
	resp, err := digitalocean.NewRequest(*d.Client, "ssh_keys", url.Values{})
	if err != nil {
		return nil, err
	}

	var result SSHKeys
	if err := mapstructure.Decode(resp["ssh_keys"], &result); err != nil {
		return nil, err
	}

	return result, nil
}
Ejemplo n.º 4
0
// Reboot restart the machine for the given dropletID and returns the eventId
// back to track the event.
func (d *DigitalOcean) Reboot(dropletId uint) (int, error) {
	path := fmt.Sprintf("droplets/%v/reboot", dropletId)
	body, err := digitalocean.NewRequest(*d.Client, path, url.Values{})
	if err != nil {
		return 0, err
	}

	eventId, ok := body["event_id"].(float64)
	if !ok {
		return 0, fmt.Errorf("restart malformed data %v", body)
	}

	return int(eventId), nil
}
Ejemplo n.º 5
0
// CheckEvent checks the given eventID and returns back the result. It's useful
// for checking the status of an event. Usually it's called in a for/select
// statement and get polled.
func (d *DigitalOcean) CheckEvent(eventId int) (*Event, error) {
	path := fmt.Sprintf("events/%d", eventId)

	body, err := digitalocean.NewRequest(*d.Client, path, url.Values{})
	if err != nil {
		return nil, err
	}

	event := &Event{}
	if err := mapstructure.Decode(body, event); err != nil {
		return nil, err
	}

	return event, nil
}
Ejemplo n.º 6
0
func (d *DigitalOcean) Rename(dropletId uint, newName string) (int, error) {
	params := url.Values{}
	params.Set("name", newName)

	path := fmt.Sprintf("droplets/%v/rename", dropletId)
	body, err := digitalocean.NewRequest(*d.Client, path, params)
	if err != nil {
		return 0, err
	}

	eventId, ok := body["event_id"].(float64)
	if !ok {
		return 0, fmt.Errorf("restart malformed data %v", body)
	}

	return int(eventId), nil
}
Ejemplo n.º 7
0
// Image returns a slice of all images.
func (d *DigitalOcean) Images(myImages bool) (Images, error) {
	v := url.Values{}

	if myImages {
		v.Set("filter", "my_images")
	}

	resp, err := digitalocean.NewRequest(*d.Client, "images", v)
	if err != nil {
		return nil, err
	}

	var result digitalocean.ImagesResp
	if err := mapstructure.Decode(resp, &result); err != nil {
		return nil, err
	}

	return result.Images, nil
}
Ejemplo n.º 8
0
func (d *DigitalOcean) ShowDroplet(dropletId uint) (*Droplet, error) {
	path := fmt.Sprintf("droplets/%v", dropletId)
	resp, err := digitalocean.NewRequest(*d.Client, path, url.Values{})
	if err != nil {
		return nil, err
	}

	droplet, ok := resp["droplet"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("show droplet malformed data received %v", resp)
	}

	var result Droplet
	if err := mapstructure.Decode(droplet, &result); err != nil {
		return nil, err
	}

	return &result, err
}
Ejemplo n.º 9
0
Archivo: key.go Proyecto: koding/koding
// ShowKey shows the key content for the given key id
func (d *DigitalOcean) ShowKey(keyId uint) (*SSHKey, error) {
	path := fmt.Sprintf("ssh_keys/%v", keyId)
	resp, err := digitalocean.NewRequest(*d.Client, path, url.Values{})
	if err != nil {
		return nil, err
	}

	ssh_key, ok := resp["ssh_key"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("showKey: malformed data received %v", resp)
	}

	var result SSHKey
	if err := mapstructure.Decode(ssh_key, &result); err != nil {
		return nil, err
	}

	return &result, nil
}