device.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Device struct {
  12. Device *types.Device
  13. client *Client
  14. }
  15. func NewDevice(client *Client) *Device {
  16. return &Device{
  17. Device: new(types.Device),
  18. client: client,
  19. }
  20. }
  21. func NewDeviceEx(client *Client, device *types.Device) *Device {
  22. return &Device{
  23. Device: device,
  24. client: client,
  25. }
  26. }
  27. func (storagePool *StoragePool) AttachDevice(path string, sdsID string) (string, error) {
  28. endpoint := storagePool.client.SIOEndpoint
  29. deviceParam := &types.DeviceParam{}
  30. deviceParam.Name = path
  31. deviceParam.DeviceCurrentPathname = path
  32. deviceParam.StoragePoolID = storagePool.StoragePool.ID
  33. deviceParam.SdsID = sdsID
  34. deviceParam.TestMode = "testAndActivate"
  35. jsonOutput, err := json.Marshal(&deviceParam)
  36. if err != nil {
  37. return "", fmt.Errorf("error marshaling: %s", err)
  38. }
  39. endpoint.Path = fmt.Sprintf("/api/types/Device/instances")
  40. req := storagePool.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
  41. req.SetBasicAuth("", storagePool.client.Token)
  42. req.Header.Add("Accept", "application/json;version="+storagePool.client.configConnect.Version)
  43. req.Header.Add("Content-Type", "application/json;version="+storagePool.client.configConnect.Version)
  44. resp, err := storagePool.client.retryCheckResp(&storagePool.client.Http, req)
  45. if err != nil {
  46. return "", err
  47. }
  48. defer resp.Body.Close()
  49. bs, err := ioutil.ReadAll(resp.Body)
  50. if err != nil {
  51. return "", errors.New("error reading body")
  52. }
  53. var dev types.DeviceResp
  54. err = json.Unmarshal(bs, &dev)
  55. if err != nil {
  56. return "", err
  57. }
  58. return dev.ID, nil
  59. }
  60. func (storagePool *StoragePool) GetDevice() (devices []types.Device, err error) {
  61. endpoint := storagePool.client.SIOEndpoint
  62. endpoint.Path = fmt.Sprintf("/api/instances/StoragePool::%v/relationships/Device", storagePool.StoragePool.ID)
  63. req := storagePool.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
  64. req.SetBasicAuth("", storagePool.client.Token)
  65. req.Header.Add("Accept", "application/json;version="+storagePool.client.configConnect.Version)
  66. resp, err := storagePool.client.retryCheckResp(&storagePool.client.Http, req)
  67. if err != nil {
  68. return []types.Device{}, fmt.Errorf("problem getting response: %v", err)
  69. }
  70. defer resp.Body.Close()
  71. if err = storagePool.client.decodeBody(resp, &devices); err != nil {
  72. return []types.Device{}, fmt.Errorf("error decoding instances response: %s", err)
  73. }
  74. return devices, nil
  75. }
  76. func (storagePool *StoragePool) FindDevice(field, value string) (device *types.Device, err error) {
  77. devices, err := storagePool.GetDevice()
  78. if err != nil {
  79. return &types.Device{}, nil
  80. }
  81. for _, device := range devices {
  82. valueOf := reflect.ValueOf(device)
  83. switch {
  84. case reflect.Indirect(valueOf).FieldByName(field).String() == value:
  85. return &device, nil
  86. }
  87. }
  88. return &types.Device{}, errors.New("Couldn't find DEV")
  89. }