instance.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package goscaleio
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "strings"
  9. types "github.com/codedellemc/goscaleio/types/v1"
  10. )
  11. func (client *Client) GetInstance(systemhref string) (systems []*types.System, err error) {
  12. endpoint := client.SIOEndpoint
  13. if systemhref == "" {
  14. endpoint.Path += "/types/System/instances"
  15. } else {
  16. endpoint.Path = systemhref
  17. }
  18. req := client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  19. req.SetBasicAuth("", client.Token)
  20. req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
  21. resp, err := client.retryCheckResp(&client.Http, req)
  22. if err != nil {
  23. return []*types.System{}, fmt.Errorf("problem getting response: %v", err)
  24. }
  25. defer resp.Body.Close()
  26. if systemhref == "" {
  27. if err = client.decodeBody(resp, &systems); err != nil {
  28. return []*types.System{}, fmt.Errorf("error decoding instances response: %s", err)
  29. }
  30. } else {
  31. system := &types.System{}
  32. if err = client.decodeBody(resp, &system); err != nil {
  33. return []*types.System{}, fmt.Errorf("error decoding instances response: %s", err)
  34. }
  35. systems = append(systems, system)
  36. }
  37. // bs, err := ioutil.ReadAll(resp.Body)
  38. // if err != nil {
  39. // return types.Systems{}, errors.New("error reading body")
  40. // }
  41. return systems, nil
  42. }
  43. func (client *Client) GetVolume(volumehref, volumeid, ancestorvolumeid, volumename string, getSnapshots bool) (volumes []*types.Volume, err error) {
  44. endpoint := client.SIOEndpoint
  45. if volumename != "" {
  46. volumeid, err = client.FindVolumeID(volumename)
  47. if err != nil && err.Error() == "Not found" {
  48. return nil, nil
  49. }
  50. if err != nil {
  51. return []*types.Volume{}, fmt.Errorf("Error: problem finding volume: %s", err)
  52. }
  53. }
  54. if volumeid != "" {
  55. endpoint.Path = fmt.Sprintf("/api/instances/Volume::%s", volumeid)
  56. } else if volumehref == "" {
  57. endpoint.Path = "/api/types/Volume/instances"
  58. } else {
  59. endpoint.Path = volumehref
  60. }
  61. req := client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  62. req.SetBasicAuth("", client.Token)
  63. req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
  64. resp, err := client.retryCheckResp(&client.Http, req)
  65. if err != nil {
  66. return []*types.Volume{}, fmt.Errorf("problem getting response: %v", err)
  67. }
  68. defer resp.Body.Close()
  69. if volumehref == "" && volumeid == "" {
  70. if err = client.decodeBody(resp, &volumes); err != nil {
  71. return []*types.Volume{}, fmt.Errorf("error decoding storage pool response: %s", err)
  72. }
  73. var volumesNew []*types.Volume
  74. for _, volume := range volumes {
  75. if (!getSnapshots && volume.AncestorVolumeID == ancestorvolumeid) || (getSnapshots && volume.AncestorVolumeID != "") {
  76. volumesNew = append(volumesNew, volume)
  77. }
  78. }
  79. volumes = volumesNew
  80. } else {
  81. volume := &types.Volume{}
  82. if err = client.decodeBody(resp, &volume); err != nil {
  83. return []*types.Volume{}, fmt.Errorf("error decoding instances response: %s", err)
  84. }
  85. volumes = append(volumes, volume)
  86. }
  87. return volumes, nil
  88. }
  89. func (client *Client) FindVolumeID(volumename string) (volumeID string, err error) {
  90. endpoint := client.SIOEndpoint
  91. volumeQeryIdByKeyParam := &types.VolumeQeryIdByKeyParam{}
  92. volumeQeryIdByKeyParam.Name = volumename
  93. jsonOutput, err := json.Marshal(&volumeQeryIdByKeyParam)
  94. if err != nil {
  95. return "", fmt.Errorf("error marshaling: %s", err)
  96. }
  97. endpoint.Path = fmt.Sprintf("/api/types/Volume/instances/action/queryIdByKey")
  98. req := client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
  99. req.SetBasicAuth("", client.Token)
  100. req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
  101. req.Header.Add("Content-Type", "application/json;version="+client.configConnect.Version)
  102. resp, err := client.retryCheckResp(&client.Http, req)
  103. if err != nil {
  104. return "", err
  105. }
  106. defer resp.Body.Close()
  107. bs, err := ioutil.ReadAll(resp.Body)
  108. if err != nil {
  109. return "", errors.New("error reading body")
  110. }
  111. volumeID = string(bs)
  112. volumeID = strings.TrimRight(volumeID, `"`)
  113. volumeID = strings.TrimLeft(volumeID, `"`)
  114. return volumeID, nil
  115. }
  116. func (client *Client) CreateVolume(volume *types.VolumeParam, storagePoolName string) (volumeResp *types.VolumeResp, err error) {
  117. endpoint := client.SIOEndpoint
  118. endpoint.Path = "/api/types/Volume/instances"
  119. storagePool, err := client.FindStoragePool("", storagePoolName, "")
  120. if err != nil {
  121. return nil, err
  122. }
  123. volume.StoragePoolID = storagePool.ID
  124. volume.ProtectionDomainID = storagePool.ProtectionDomainID
  125. jsonOutput, err := json.Marshal(&volume)
  126. if err != nil {
  127. return &types.VolumeResp{}, fmt.Errorf("error marshaling: %s", err)
  128. }
  129. req := client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
  130. req.SetBasicAuth("", client.Token)
  131. req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
  132. req.Header.Add("Content-Type", "application/json;version="+client.configConnect.Version)
  133. resp, err := client.retryCheckResp(&client.Http, req)
  134. if err != nil {
  135. return &types.VolumeResp{}, fmt.Errorf("problem getting response: %v", err)
  136. }
  137. defer resp.Body.Close()
  138. if err = client.decodeBody(resp, &volumeResp); err != nil {
  139. return &types.VolumeResp{}, fmt.Errorf("error decoding volume creation response: %s", err)
  140. }
  141. return volumeResp, nil
  142. }
  143. func (client *Client) GetStoragePool(storagepoolhref string) (storagePools []*types.StoragePool, err error) {
  144. endpoint := client.SIOEndpoint
  145. if storagepoolhref == "" {
  146. endpoint.Path = "/api/types/StoragePool/instances"
  147. } else {
  148. endpoint.Path = storagepoolhref
  149. }
  150. req := client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  151. req.SetBasicAuth("", client.Token)
  152. req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
  153. resp, err := client.retryCheckResp(&client.Http, req)
  154. if err != nil {
  155. return []*types.StoragePool{}, fmt.Errorf("problem getting response: %v", err)
  156. }
  157. defer resp.Body.Close()
  158. if storagepoolhref == "" {
  159. if err = client.decodeBody(resp, &storagePools); err != nil {
  160. return []*types.StoragePool{}, fmt.Errorf("error decoding storage pool response: %s", err)
  161. }
  162. } else {
  163. storagePool := &types.StoragePool{}
  164. if err = client.decodeBody(resp, &storagePool); err != nil {
  165. return []*types.StoragePool{}, fmt.Errorf("error decoding instances response: %s", err)
  166. }
  167. storagePools = append(storagePools, storagePool)
  168. }
  169. return storagePools, nil
  170. }
  171. func (client *Client) FindStoragePool(id, name, href string) (storagePool *types.StoragePool, err error) {
  172. storagePools, err := client.GetStoragePool(href)
  173. if err != nil {
  174. return &types.StoragePool{}, fmt.Errorf("Error getting storage pool %s", err)
  175. }
  176. for _, storagePool = range storagePools {
  177. if storagePool.ID == id || storagePool.Name == name || href != "" {
  178. return storagePool, nil
  179. }
  180. }
  181. return &types.StoragePool{}, errors.New("Couldn't find storage pool")
  182. }