protectiondomain.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 ProtectionDomain struct {
  11. ProtectionDomain *types.ProtectionDomain
  12. client *Client
  13. }
  14. func NewProtectionDomain(client *Client) *ProtectionDomain {
  15. return &ProtectionDomain{
  16. ProtectionDomain: new(types.ProtectionDomain),
  17. client: client,
  18. }
  19. }
  20. func NewProtectionDomainEx(client *Client, pd *types.ProtectionDomain) *ProtectionDomain {
  21. return &ProtectionDomain{
  22. ProtectionDomain: pd,
  23. client: client,
  24. }
  25. }
  26. func (system *System) CreateProtectionDomain(name string) (string, error) {
  27. endpoint := system.client.SIOEndpoint
  28. protectionDomainParam := &types.ProtectionDomainParam{}
  29. protectionDomainParam.Name = name
  30. jsonOutput, err := json.Marshal(&protectionDomainParam)
  31. if err != nil {
  32. return "", fmt.Errorf("error marshaling: %s", err)
  33. }
  34. endpoint.Path = fmt.Sprintf("/api/types/ProtectionDomain/instances")
  35. req := system.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
  36. req.SetBasicAuth("", system.client.Token)
  37. req.Header.Add("Accept", "application/json;version="+system.client.configConnect.Version)
  38. req.Header.Add("Content-Type", "application/json;version="+system.client.configConnect.Version)
  39. resp, err := system.client.retryCheckResp(&system.client.Http, req)
  40. if err != nil {
  41. return "", err
  42. }
  43. defer resp.Body.Close()
  44. bs, err := ioutil.ReadAll(resp.Body)
  45. if err != nil {
  46. return "", errors.New("error reading body")
  47. }
  48. var pd types.ProtectionDomainResp
  49. err = json.Unmarshal(bs, &pd)
  50. if err != nil {
  51. return "", err
  52. }
  53. return pd.ID, nil
  54. }
  55. func (system *System) GetProtectionDomain(protectiondomainhref string) (protectionDomains []*types.ProtectionDomain, err error) {
  56. endpoint := system.client.SIOEndpoint
  57. if protectiondomainhref == "" {
  58. link, err := GetLink(system.System.Links, "/api/System/relationship/ProtectionDomain")
  59. if err != nil {
  60. return []*types.ProtectionDomain{}, errors.New("Error: problem finding link")
  61. }
  62. endpoint.Path = link.HREF
  63. } else {
  64. endpoint.Path = protectiondomainhref
  65. }
  66. req := system.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  67. req.SetBasicAuth("", system.client.Token)
  68. req.Header.Add("Accept", "application/json;version="+system.client.configConnect.Version)
  69. resp, err := system.client.retryCheckResp(&system.client.Http, req)
  70. if err != nil {
  71. return []*types.ProtectionDomain{}, fmt.Errorf("problem getting response: %v", err)
  72. }
  73. defer resp.Body.Close()
  74. if protectiondomainhref == "" {
  75. if err = system.client.decodeBody(resp, &protectionDomains); err != nil {
  76. return []*types.ProtectionDomain{}, fmt.Errorf("error decoding instances response: %s", err)
  77. }
  78. } else {
  79. protectionDomain := &types.ProtectionDomain{}
  80. if err = system.client.decodeBody(resp, &protectionDomain); err != nil {
  81. return []*types.ProtectionDomain{}, fmt.Errorf("error decoding instances response: %s", err)
  82. }
  83. protectionDomains = append(protectionDomains, protectionDomain)
  84. }
  85. //
  86. // bs, err := ioutil.ReadAll(resp.Body)
  87. // if err != nil {
  88. // return []types.ProtectionDomain{}, errors.New("error reading body")
  89. // }
  90. //
  91. // fmt.Println(string(bs))
  92. // log.Fatalf("here")
  93. // return []types.ProtectionDomain{}, nil
  94. return protectionDomains, nil
  95. }
  96. func (system *System) FindProtectionDomain(id, name, href string) (protectionDomain *types.ProtectionDomain, err error) {
  97. protectionDomains, err := system.GetProtectionDomain(href)
  98. if err != nil {
  99. return &types.ProtectionDomain{}, fmt.Errorf("Error getting protection domains %s", err)
  100. }
  101. for _, protectionDomain = range protectionDomains {
  102. if protectionDomain.ID == id || protectionDomain.Name == name || href != "" {
  103. return protectionDomain, nil
  104. }
  105. }
  106. return &types.ProtectionDomain{}, errors.New("Couldn't find protection domain")
  107. }