system.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package goscaleio
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. types "github.com/codedellemc/goscaleio/types/v1"
  8. )
  9. type System struct {
  10. System *types.System
  11. client *Client
  12. }
  13. func NewSystem(client *Client) *System {
  14. return &System{
  15. System: new(types.System),
  16. client: client,
  17. }
  18. }
  19. func (client *Client) FindSystem(instanceID, name, href string) (*System, error) {
  20. systems, err := client.GetInstance(href)
  21. if err != nil {
  22. return &System{}, fmt.Errorf("err: problem getting instances: %s", err)
  23. }
  24. for _, system := range systems {
  25. if system.ID == instanceID || system.Name == name || href != "" {
  26. outSystem := NewSystem(client)
  27. outSystem.System = system
  28. return outSystem, nil
  29. }
  30. }
  31. return &System{}, fmt.Errorf("err: systemid or systemname not found")
  32. }
  33. func (system *System) GetStatistics() (statistics *types.Statistics, err error) {
  34. endpoint := system.client.SIOEndpoint
  35. // endpoint.Path = fmt.Sprintf("/api/instances/System::%v/relationships/Statistics", system.System.ID)
  36. link, err := GetLink(system.System.Links, "/api/System/relationship/Statistics")
  37. if err != nil {
  38. return &types.Statistics{}, errors.New("Error: problem finding link")
  39. }
  40. endpoint.Path = link.HREF
  41. req := system.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  42. req.SetBasicAuth("", system.client.Token)
  43. req.Header.Add("Accept", "application/json;version="+system.client.configConnect.Version)
  44. resp, err := system.client.retryCheckResp(&system.client.Http, req)
  45. if err != nil {
  46. return &types.Statistics{}, fmt.Errorf("problem getting response: %v", err)
  47. }
  48. defer resp.Body.Close()
  49. if err = system.client.decodeBody(resp, &statistics); err != nil {
  50. return &types.Statistics{}, fmt.Errorf("error decoding instances response: %s", err)
  51. }
  52. // bs, err := ioutil.ReadAll(resp.Body)
  53. // if err != nil {
  54. // return errors.New("error reading body")
  55. // }
  56. //
  57. // fmt.Println(string(bs))
  58. return statistics, nil
  59. }
  60. func (system *System) CreateSnapshotConsistencyGroup(snapshotVolumesParam *types.SnapshotVolumesParam) (snapshotVolumesResp *types.SnapshotVolumesResp, err error) {
  61. endpoint := system.client.SIOEndpoint
  62. link, err := GetLink(system.System.Links, "self")
  63. if err != nil {
  64. return &types.SnapshotVolumesResp{}, errors.New("Error: problem finding link")
  65. }
  66. endpoint.Path = fmt.Sprintf("%v/action/snapshotVolumes", link.HREF)
  67. jsonOutput, err := json.Marshal(&snapshotVolumesParam)
  68. if err != nil {
  69. return &types.SnapshotVolumesResp{}, fmt.Errorf("error marshaling: %s", err)
  70. }
  71. req := system.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
  72. req.SetBasicAuth("", system.client.Token)
  73. req.Header.Add("Accept", "application/json;version="+system.client.configConnect.Version)
  74. req.Header.Add("Content-Type", "application/json;version="+system.client.configConnect.Version)
  75. resp, err := system.client.retryCheckResp(&system.client.Http, req)
  76. if err != nil {
  77. return &types.SnapshotVolumesResp{}, fmt.Errorf("problem getting response: %v", err)
  78. }
  79. defer resp.Body.Close()
  80. if err = system.client.decodeBody(resp, &snapshotVolumesResp); err != nil {
  81. return &types.SnapshotVolumesResp{}, fmt.Errorf("error decoding snapshotvolumes response: %s", err)
  82. }
  83. return snapshotVolumesResp, nil
  84. }