disks.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) 2016 VMware, Inc. All Rights Reserved.
  2. //
  3. // This product is licensed to you under the Apache License, Version 2.0 (the "License").
  4. // You may not use this product except in compliance with the License.
  5. //
  6. // This product may include a number of subcomponents with separate copyright notices and
  7. // license terms. Your use of these subcomponents is subject to the terms and conditions
  8. // of the subcomponent's license, as noted in the LICENSE file.
  9. package photon
  10. import (
  11. "encoding/json"
  12. )
  13. // Contains functionality for disks API.
  14. type DisksAPI struct {
  15. client *Client
  16. }
  17. var diskUrl string = rootUrl + "/disks/"
  18. // Gets a PersistentDisk for the disk with specified ID.
  19. func (api *DisksAPI) Get(diskID string) (disk *PersistentDisk, err error) {
  20. res, err := api.client.restClient.Get(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions)
  21. if err != nil {
  22. return
  23. }
  24. defer res.Body.Close()
  25. res, err = getError(res)
  26. if err != nil {
  27. return
  28. }
  29. disk = &PersistentDisk{}
  30. err = json.NewDecoder(res.Body).Decode(disk)
  31. return
  32. }
  33. // Deletes a disk with the specified ID.
  34. func (api *DisksAPI) Delete(diskID string) (task *Task, err error) {
  35. res, err := api.client.restClient.Delete(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions)
  36. if err != nil {
  37. return
  38. }
  39. defer res.Body.Close()
  40. task, err = getTask(getError(res))
  41. return
  42. }
  43. // Gets all tasks with the specified disk ID, using options to filter the results.
  44. // If options is nil, no filtering will occur.
  45. func (api *DisksAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
  46. uri := api.client.Endpoint + diskUrl + id + "/tasks"
  47. if options != nil {
  48. uri += getQueryString(options)
  49. }
  50. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  51. if err != nil {
  52. return
  53. }
  54. result = &TaskList{}
  55. err = json.Unmarshal(res, result)
  56. return
  57. }