services.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "bytes"
  12. "encoding/json"
  13. )
  14. // Contains functionality for services API.
  15. type ServicesAPI struct {
  16. client *Client
  17. }
  18. var serviceUrl = rootUrl + "/services/"
  19. // Extended Properties
  20. const (
  21. ExtendedPropertyDNS = "dns"
  22. ExtendedPropertyGateway = "gateway"
  23. ExtendedPropertyNetMask = "netmask"
  24. ExtendedPropertyLoadBalancerIP = "load_balancer_ip"
  25. ExtendedPropertyNumberOfMasters = "number_of_masters"
  26. ExtendedPropertyMasterIPs = "master_ips"
  27. ExtendedPropertyMasterIP = "master_ip"
  28. ExtendedPropertyMasterIP2 = "master_ip2"
  29. ExtendedPropertyContainerNetwork = "container_network"
  30. ExtendedPropertyZookeeperIP1 = "zookeeper_ip1"
  31. ExtendedPropertyZookeeperIP2 = "zookeeper_ip2"
  32. ExtendedPropertyZookeeperIP3 = "zookeeper_ip3"
  33. ExtendedPropertyNumberOfETCDs = "number_of_etcds"
  34. ExtendedPropertyETCDIP1 = "etcd_ip1"
  35. ExtendedPropertyETCDIP2 = "etcd_ip2"
  36. ExtendedPropertyETCDIP3 = "etcd_ip3"
  37. ExtendedPropertySSHKey = "ssh_key"
  38. ExtendedPropertyRegistryCACert = "registry_ca_cert"
  39. ExtendedPropertyAdminPassword = "admin_password"
  40. )
  41. // Deletes a service with specified ID.
  42. func (api *ServicesAPI) Delete(id string) (task *Task, err error) {
  43. res, err := api.client.restClient.Delete(api.client.Endpoint+serviceUrl+id, api.client.options.TokenOptions)
  44. if err != nil {
  45. return
  46. }
  47. defer res.Body.Close()
  48. task, err = getTask(getError(res))
  49. return
  50. }
  51. // Gets a service with the specified ID.
  52. func (api *ServicesAPI) Get(id string) (service *Service, err error) {
  53. res, err := api.client.restClient.Get(api.client.Endpoint+serviceUrl+id, api.client.options.TokenOptions)
  54. if err != nil {
  55. return
  56. }
  57. defer res.Body.Close()
  58. res, err = getError(res)
  59. if err != nil {
  60. return
  61. }
  62. var result Service
  63. err = json.NewDecoder(res.Body).Decode(&result)
  64. return &result, nil
  65. }
  66. // Gets vms for service with the specified ID.
  67. func (api *ServicesAPI) GetVMs(id string) (result *VMs, err error) {
  68. uri := api.client.Endpoint + serviceUrl + id + "/vms"
  69. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  70. if err != nil {
  71. return
  72. }
  73. result = &VMs{}
  74. err = json.Unmarshal(res, result)
  75. return
  76. }
  77. // Resize a service to specified count.
  78. func (api *ServicesAPI) Resize(id string, resize *ServiceResizeOperation) (task *Task, err error) {
  79. body, err := json.Marshal(resize)
  80. if err != nil {
  81. return
  82. }
  83. res, err := api.client.restClient.Post(
  84. api.client.Endpoint+serviceUrl+id+"/resize",
  85. "application/json",
  86. bytes.NewReader(body),
  87. api.client.options.TokenOptions)
  88. if err != nil {
  89. return
  90. }
  91. defer res.Body.Close()
  92. task, err = getTask(getError(res))
  93. return
  94. }
  95. // Start a background process to recreate failed VMs in a service with the specified ID.
  96. func (api *ServicesAPI) TriggerMaintenance(id string) (task *Task, err error) {
  97. body := []byte{}
  98. res, err := api.client.restClient.Post(
  99. api.client.Endpoint+serviceUrl+id+"/trigger_maintenance",
  100. "application/json",
  101. bytes.NewReader(body),
  102. api.client.options.TokenOptions)
  103. if err != nil {
  104. return
  105. }
  106. defer res.Body.Close()
  107. task, err = getTask(getError(res))
  108. return
  109. }
  110. // Change a service version to the specified image by destroying and recreating the VMs.
  111. func (api *ServicesAPI) ChangeVersion(id string, changeVersion *ServiceChangeVersionOperation) (task *Task, err error) {
  112. body, err := json.Marshal(changeVersion)
  113. if err != nil {
  114. return
  115. }
  116. res, err := api.client.restClient.Post(
  117. api.client.Endpoint+serviceUrl+id+"/change_version",
  118. "application/json",
  119. bytes.NewReader(body),
  120. api.client.options.TokenOptions)
  121. if err != nil {
  122. return
  123. }
  124. defer res.Body.Close()
  125. task, err = getTask(getError(res))
  126. return
  127. }