storagepool.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package goscaleio
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. types "github.com/codedellemc/goscaleio/types/v1"
  9. )
  10. type StoragePool struct {
  11. StoragePool *types.StoragePool
  12. client *Client
  13. }
  14. func NewStoragePool(client *Client) *StoragePool {
  15. return &StoragePool{
  16. StoragePool: new(types.StoragePool),
  17. client: client,
  18. }
  19. }
  20. func NewStoragePoolEx(client *Client, pool *types.StoragePool) *StoragePool {
  21. return &StoragePool{
  22. StoragePool: pool,
  23. client: client,
  24. }
  25. }
  26. func (protectionDomain *ProtectionDomain) CreateStoragePool(name string) (string, error) {
  27. endpoint := protectionDomain.client.SIOEndpoint
  28. storagePoolParam := &types.StoragePoolParam{}
  29. storagePoolParam.Name = name
  30. storagePoolParam.ProtectionDomainID = protectionDomain.ProtectionDomain.ID
  31. jsonOutput, err := json.Marshal(&storagePoolParam)
  32. if err != nil {
  33. return "", fmt.Errorf("error marshaling: %s", err)
  34. }
  35. endpoint.Path = fmt.Sprintf("/api/types/StoragePool/instances")
  36. req := protectionDomain.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
  37. req.SetBasicAuth("", protectionDomain.client.Token)
  38. req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
  39. req.Header.Add("Content-Type", "application/json;version="+protectionDomain.client.configConnect.Version)
  40. resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
  41. if err != nil {
  42. return "", err
  43. }
  44. defer resp.Body.Close()
  45. bs, err := ioutil.ReadAll(resp.Body)
  46. if err != nil {
  47. return "", errors.New("error reading body")
  48. }
  49. var sp types.StoragePoolResp
  50. err = json.Unmarshal(bs, &sp)
  51. if err != nil {
  52. return "", err
  53. }
  54. return sp.ID, nil
  55. }
  56. func (protectionDomain *ProtectionDomain) GetStoragePool(storagepoolhref string) (storagePools []*types.StoragePool, err error) {
  57. endpoint := protectionDomain.client.SIOEndpoint
  58. if storagepoolhref == "" {
  59. link, err := GetLink(protectionDomain.ProtectionDomain.Links, "/api/ProtectionDomain/relationship/StoragePool")
  60. if err != nil {
  61. return []*types.StoragePool{}, errors.New("Error: problem finding link")
  62. }
  63. endpoint.Path = link.HREF
  64. } else {
  65. endpoint.Path = storagepoolhref
  66. }
  67. req := protectionDomain.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  68. req.SetBasicAuth("", protectionDomain.client.Token)
  69. req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
  70. resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
  71. if err != nil {
  72. return []*types.StoragePool{}, fmt.Errorf("problem getting response: %v", err)
  73. }
  74. defer resp.Body.Close()
  75. if storagepoolhref == "" {
  76. if err = protectionDomain.client.decodeBody(resp, &storagePools); err != nil {
  77. return []*types.StoragePool{}, fmt.Errorf("error decoding storage pool response: %s", err)
  78. }
  79. } else {
  80. storagePool := &types.StoragePool{}
  81. if err = protectionDomain.client.decodeBody(resp, &storagePool); err != nil {
  82. return []*types.StoragePool{}, fmt.Errorf("error decoding instances response: %s", err)
  83. }
  84. storagePools = append(storagePools, storagePool)
  85. }
  86. return storagePools, nil
  87. }
  88. func (protectionDomain *ProtectionDomain) FindStoragePool(id, name, href string) (storagePool *types.StoragePool, err error) {
  89. storagePools, err := protectionDomain.GetStoragePool(href)
  90. if err != nil {
  91. return &types.StoragePool{}, fmt.Errorf("Error getting protection domains %s", err)
  92. }
  93. for _, storagePool = range storagePools {
  94. if storagePool.ID == id || storagePool.Name == name || href != "" {
  95. return storagePool, nil
  96. }
  97. }
  98. return &types.StoragePool{}, errors.New("Couldn't find protection domain")
  99. }
  100. func (storagePool *StoragePool) GetStatistics() (statistics *types.Statistics, err error) {
  101. link, err := GetLink(storagePool.StoragePool.Links, "/api/StoragePool/relationship/Statistics")
  102. if err != nil {
  103. return &types.Statistics{}, errors.New("Error: problem finding link")
  104. }
  105. endpoint := storagePool.client.SIOEndpoint
  106. endpoint.Path = link.HREF
  107. req := storagePool.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  108. req.SetBasicAuth("", storagePool.client.Token)
  109. req.Header.Add("Accept", "application/json;version="+storagePool.client.configConnect.Version)
  110. resp, err := storagePool.client.retryCheckResp(&storagePool.client.Http, req)
  111. if err != nil {
  112. return &types.Statistics{}, fmt.Errorf("problem getting response: %v", err)
  113. }
  114. defer resp.Body.Close()
  115. if err = storagePool.client.decodeBody(resp, &statistics); err != nil {
  116. return &types.Statistics{}, fmt.Errorf("error decoding instances response: %s", err)
  117. }
  118. return statistics, nil
  119. }