sds.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package goscaleio
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "reflect"
  9. types "github.com/codedellemc/goscaleio/types/v1"
  10. )
  11. type Sds struct {
  12. Sds *types.Sds
  13. client *Client
  14. }
  15. func NewSds(client *Client) *Sds {
  16. return &Sds{
  17. Sds: new(types.Sds),
  18. client: client,
  19. }
  20. }
  21. func NewSdsEx(client *Client, sds *types.Sds) *Sds {
  22. return &Sds{
  23. Sds: sds,
  24. client: client,
  25. }
  26. }
  27. func (protectionDomain *ProtectionDomain) CreateSds(name string, ipList []string) (string, error) {
  28. endpoint := protectionDomain.client.SIOEndpoint
  29. sdsParam := &types.SdsParam{}
  30. sdsParam.Name = name
  31. sdsParam.ProtectionDomainID = protectionDomain.ProtectionDomain.ID
  32. if len(ipList) == 0 {
  33. return "", fmt.Errorf("Must provide at least 1 SDS IP")
  34. } else if len(ipList) == 1 {
  35. sdsIP := types.SdsIp{IP: ipList[0], Role: "all"}
  36. sdsIPList := &types.SdsIpList{sdsIP}
  37. sdsParam.IPList = append(sdsParam.IPList, sdsIPList)
  38. } else if len(ipList) >= 2 {
  39. sdsIP1 := types.SdsIp{IP: ipList[0], Role: "sdcOnly"}
  40. sdsIP2 := types.SdsIp{IP: ipList[1], Role: "sdsOnly"}
  41. sdsIPList1 := &types.SdsIpList{sdsIP1}
  42. sdsIPList2 := &types.SdsIpList{sdsIP2}
  43. sdsParam.IPList = append(sdsParam.IPList, sdsIPList1)
  44. sdsParam.IPList = append(sdsParam.IPList, sdsIPList2)
  45. }
  46. jsonOutput, err := json.Marshal(&sdsParam)
  47. if err != nil {
  48. return "", fmt.Errorf("error marshaling: %s", err)
  49. }
  50. endpoint.Path = fmt.Sprintf("/api/types/Sds/instances")
  51. req := protectionDomain.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
  52. req.SetBasicAuth("", protectionDomain.client.Token)
  53. req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
  54. req.Header.Add("Content-Type", "application/json;version="+protectionDomain.client.configConnect.Version)
  55. resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
  56. if err != nil {
  57. return "", err
  58. }
  59. defer resp.Body.Close()
  60. bs, err := ioutil.ReadAll(resp.Body)
  61. if err != nil {
  62. return "", errors.New("error reading body")
  63. }
  64. var sds types.SdsResp
  65. err = json.Unmarshal(bs, &sds)
  66. if err != nil {
  67. return "", err
  68. }
  69. return sds.ID, nil
  70. }
  71. func (protectionDomain *ProtectionDomain) GetSds() (sdss []types.Sds, err error) {
  72. endpoint := protectionDomain.client.SIOEndpoint
  73. endpoint.Path = fmt.Sprintf("/api/instances/ProtectionDomain::%v/relationships/Sds", protectionDomain.ProtectionDomain.ID)
  74. req := protectionDomain.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  75. req.SetBasicAuth("", protectionDomain.client.Token)
  76. req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
  77. resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
  78. if err != nil {
  79. return []types.Sds{}, fmt.Errorf("problem getting response: %v", err)
  80. }
  81. defer resp.Body.Close()
  82. if err = protectionDomain.client.decodeBody(resp, &sdss); err != nil {
  83. return []types.Sds{}, fmt.Errorf("error decoding instances response: %s", err)
  84. }
  85. return sdss, nil
  86. }
  87. func (protectionDomain *ProtectionDomain) FindSds(field, value string) (sds *types.Sds, err error) {
  88. sdss, err := protectionDomain.GetSds()
  89. if err != nil {
  90. return &types.Sds{}, nil
  91. }
  92. for _, sds := range sdss {
  93. valueOf := reflect.ValueOf(sds)
  94. switch {
  95. case reflect.Indirect(valueOf).FieldByName(field).String() == value:
  96. return &sds, nil
  97. }
  98. }
  99. return &types.Sds{}, errors.New("Couldn't find SDS")
  100. }