github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/blockstorages.go (about) 1 package oneandone 2 3 import ( 4 "net/http" 5 "time" 6 ) 7 8 type BlockStorageRequest struct { 9 Name string `json:"name"` 10 Description string `json:"description,omitempty"` 11 Size *int `json:"size"` 12 ServerId string `json:"server,omitempty"` 13 DatacenterId string `json:"datacenter_id,omitempty"` 14 ExecutionGroup string `json:"execution_group,omitempty"` 15 } 16 17 type BlockStorage struct { 18 Identity 19 descField 20 Size int `json:"size"` 21 State string `json:"state,omitempty"` 22 // Name string `json:"name,omitempty"` 23 CreationDate time.Time `json:"creation_date,omitempty"` 24 Datacenter *Datacenter `json:"datacenter,omitempty"` 25 Server *BlockStorageServer `json:"server,omitempty"` 26 DiskID string `json:"disk_id,omitemtpy"` 27 UUID string `json:"uuid,omitemtpy"` 28 ApiPtr 29 } 30 31 type BlockStorageServer struct { 32 Id string `json:"id,omitempty"` 33 ServerId string `json:"server,omitempty"` 34 Name string `json:"name,omitempty"` 35 } 36 37 type UpdateBlockStorageRequest struct { 38 Name string `json:"name,omitempty"` 39 Description string `json:"description,omitempty"` 40 } 41 42 func (api *API) ListBlockStorages(args ...interface{}) ([]BlockStorage, error) { 43 url, err := processQueryParams(createUrl(api, blockStoragePathSegment), args...) 44 if err != nil { 45 return nil, err 46 } 47 result := []BlockStorage{} 48 err = api.Client.Get(url, &result, http.StatusOK) 49 if err != nil { 50 return nil, err 51 } 52 for index := range result { 53 result[index].api = api 54 } 55 return result, nil 56 } 57 58 func (api *API) GetBlockStorage(id string) (*BlockStorage, error) { 59 result := new(BlockStorage) 60 url := createUrl(api, blockStoragePathSegment, id) 61 err := api.Client.Get(url, &result, http.StatusOK) 62 if err != nil { 63 return nil, err 64 } 65 result.api = api 66 return result, nil 67 } 68 69 func (api *API) GetBlockStorageServer(id string) (*BlockStorageServer, error) { 70 result := new(BlockStorageServer) 71 url := createUrl(api, blockStoragePathSegment, id, "server") 72 err := api.Client.Get(url, &result, http.StatusCreated) 73 if err != nil { 74 return nil, err 75 } 76 return result, nil 77 } 78 79 func (api *API) AddBlockStorageServer(blockStorageId string, serverId string) (*BlockStorage, error) { 80 result := new(BlockStorage) 81 req := BlockStorageServer{ServerId: serverId} 82 url := createUrl(api, blockStoragePathSegment, blockStorageId, "server") 83 err := api.Client.Post(url, &req, &result, http.StatusCreated) 84 if err != nil { 85 return nil, err 86 } 87 result.api = api 88 return result, nil 89 } 90 91 func (api *API) RemoveBlockStorageServer(blockStorageId string, serverId string) (*BlockStorage, error) { 92 result := new(BlockStorage) 93 blockStorage, err := api.GetBlockStorage(blockStorageId) 94 95 if err != nil { 96 return nil, err 97 } 98 99 req := BlockStorageServer{ServerId: serverId} 100 url := createUrl(api, blockStoragePathSegment, blockStorage.Id, "server") 101 102 err = api.Client.Delete(url, &req, &result, http.StatusOK) 103 if err != nil { 104 return nil, err 105 } 106 result.api = api 107 108 return result, nil 109 } 110 111 func (api *API) CreateBlockStorage(request *BlockStorageRequest) (string, *BlockStorage, error) { 112 result := new(BlockStorage) 113 url := createUrl(api, blockStoragePathSegment) 114 err := api.Client.Post(url, request, &result, http.StatusCreated) 115 116 if err != nil { 117 return "", nil, err 118 } 119 result.api = api 120 return result.Id, result, nil 121 } 122 123 func (bs *BlockStorage) GetState() (string, error) { 124 in, err := bs.api.GetBlockStorage(bs.Id) 125 if in == nil { 126 return "", err 127 } 128 return in.State, err 129 } 130 131 func (api *API) DeleteBlockStorage(id string) (*BlockStorage, error) { 132 result := new(BlockStorage) 133 url := createUrl(api, blockStoragePathSegment, id) 134 err := api.Client.Delete(url, nil, &result, http.StatusOK) 135 if err != nil { 136 return nil, err 137 } 138 result.api = api 139 return result, nil 140 } 141 142 func (api *API) UpdateBlockStorage(id string, request *UpdateBlockStorageRequest) (*BlockStorage, error) { 143 result := new(BlockStorage) 144 url := createUrl(api, blockStoragePathSegment, id) 145 err := api.Client.Put(url, &request, &result, http.StatusOK) 146 if err != nil { 147 return nil, err 148 } 149 result.api = api 150 return result, nil 151 }