123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- // Copyright (c) 2016 VMware, Inc. All Rights Reserved.
- //
- // This product is licensed to you under the Apache License, Version 2.0 (the "License").
- // You may not use this product except in compliance with the License.
- //
- // This product may include a number of subcomponents with separate copyright notices and
- // license terms. Your use of these subcomponents is subject to the terms and conditions
- // of the subcomponent's license, as noted in the LICENSE file.
- package photon
- import (
- "bytes"
- "encoding/json"
- "io"
- )
- // Contains functionality for VMs API.
- type VmAPI struct {
- client *Client
- }
- var vmUrl string = rootUrl + "/vms/"
- func (api *VmAPI) Get(id string) (vm *VM, err error) {
- res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- res, err = getError(res)
- if err != nil {
- return
- }
- vm = &VM{}
- err = json.NewDecoder(res.Body).Decode(vm)
- return
- }
- func (api *VmAPI) Delete(id string) (task *Task, err error) {
- res, err := api.client.restClient.Delete(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) AttachDisk(id string, op *VmDiskOperation) (task *Task, err error) {
- body, err := json.Marshal(op)
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/attach_disk",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) DetachDisk(id string, op *VmDiskOperation) (task *Task, err error) {
- body, err := json.Marshal(op)
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/detach_disk",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) AttachISO(id string, reader io.ReadSeeker, name string) (task *Task, err error) {
- res, err := api.client.restClient.MultipartUpload(
- api.client.Endpoint+vmUrl+id+"/attach_iso", reader, name, nil, api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- result, err := getTask(getError(res))
- return result, err
- }
- func (api *VmAPI) DetachISO(id string) (task *Task, err error) {
- body := []byte{}
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/detach_iso",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) Start(id string) (task *Task, err error) {
- body := []byte{}
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/start",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) Stop(id string) (task *Task, err error) {
- body := []byte{}
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/stop",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) Restart(id string) (task *Task, err error) {
- body := []byte{}
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/restart",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) Resume(id string) (task *Task, err error) {
- body := []byte{}
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/resume",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) Suspend(id string) (task *Task, err error) {
- body := []byte{}
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/suspend",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) SetMetadata(id string, metadata *VmMetadata) (task *Task, err error) {
- body, err := json.Marshal(metadata)
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/set_metadata",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- // Gets all tasks with the specified vm ID, using options to filter the results.
- // If options is nil, no filtering will occur.
- func (api *VmAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
- uri := api.client.Endpoint + vmUrl + id + "/tasks"
- if options != nil {
- uri += getQueryString(options)
- }
- res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
- if err != nil {
- return
- }
- result = &TaskList{}
- err = json.Unmarshal(res, result)
- return
- }
- func (api *VmAPI) GetNetworks(id string) (task *Task, err error) {
- res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/subnets", api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) AcquireFloatingIp(id string, spec *VmFloatingIpSpec) (task *Task, err error) {
- body, err := json.Marshal(spec)
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/acquire_floating_ip",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) ReleaseFloatingIp(id string) (task *Task, err error) {
- res, err := api.client.restClient.Delete(
- api.client.Endpoint+vmUrl+id+"/release_floating_ip",
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) GetMKSTicket(id string) (task *Task, err error) {
- res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/mks_ticket", api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) SetTag(id string, tag *VmTag) (task *Task, err error) {
- body, err := json.Marshal(tag)
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/tags",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
- func (api *VmAPI) CreateImage(id string, options *ImageCreateSpec) (task *Task, err error) {
- body, err := json.Marshal(options)
- if err != nil {
- return
- }
- res, err := api.client.restClient.Post(
- api.client.Endpoint+vmUrl+id+"/create_image",
- "application/json",
- bytes.NewReader(body),
- api.client.options.TokenOptions)
- if err != nil {
- return
- }
- defer res.Body.Close()
- task, err = getTask(getError(res))
- return
- }
|