| 1 | package cs |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | ) |
| 6 | |
| 7 | type VolumeDriverType string |
| 8 | |
| 9 | const ( |
| 10 | OSSFSDriver VolumeDriverType = "ossfs" |
| 11 | NASDriver VolumeDriverType = "nas" |
| 12 | ) |
| 13 | |
| 14 | type DriverOptions interface { |
| 15 | driverOptions() |
| 16 | } |
| 17 | |
| 18 | type OSSOpts struct { |
| 19 | Bucket string `json:"bucket"` |
| 20 | AccessKeyId string `json:"ak_id"` |
| 21 | AccessKeySecret string `json:"ak_secret"` |
| 22 | URL string `json:"url"` |
| 23 | NoStatCache string `json:"no_stat_cache"` |
| 24 | OtherOpts string `json:"other_opts"` |
| 25 | } |
| 26 | |
| 27 | func (*OSSOpts) driverOptions() {} |
| 28 | |
| 29 | type NASOpts struct { |
| 30 | DiskId string `json:"disk_id"` |
| 31 | Host string `json:"host"` |
| 32 | Path string `json:"path"` |
| 33 | Mode string `json:"mode"` |
| 34 | } |
| 35 | |
| 36 | func (*NASOpts) driverOptions() {} |
| 37 | |
| 38 | type VolumeRef struct { |
| 39 | Name string `json:"Name"` |
| 40 | ID string `json:"ID"` |
| 41 | } |
| 42 | |
| 43 | type VolumeCreationArgs struct { |
| 44 | Name string `json:"name"` |
| 45 | Driver VolumeDriverType `json:"driver"` |
| 46 | DriverOpts DriverOptions `json:"driverOpts"` |
| 47 | } |
| 48 | |
| 49 | type VolumeCreationResponse struct { |
| 50 | Name string `json:"Name"` |
| 51 | Driver string `json:"Driver"` |
| 52 | Mountpoint string `json:"Mountpoint"` |
| 53 | Labels map[string]string `json:"Labels"` |
| 54 | Scope string `json:"Scope"` |
| 55 | } |
| 56 | |
| 57 | type GetVolumeResponse struct { |
| 58 | Name string `json:"Name"` |
| 59 | Driver string `json:"Driver"` |
| 60 | Mountpoint string `json:"Mountpoint"` |
| 61 | Labels map[string]string `json:"Labels"` |
| 62 | Scope string `json:"Scope"` |
| 63 | Node string `json:"Node"` |
| 64 | Refs []VolumeRef `json:"Refs"` |
| 65 | } |
| 66 | |
| 67 | type GetVolumesResponse struct { |
| 68 | Volumes []GetVolumeResponse `json:"Volumes"` |
| 69 | } |
| 70 | |
| 71 | func (client *ProjectClient) CreateVolume(args *VolumeCreationArgs) (err error) { |
| 72 | err = client.Invoke(http.MethodPost, "/volumes/create", nil, args, nil) |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | func (client *ProjectClient) GetVolume(name string) (volume GetVolumeResponse, err error) { |
| 77 | err = client.Invoke(http.MethodGet, "/volumes/"+name, nil, nil, &volume) |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | func (client *ProjectClient) GetVolumes() (volumes GetVolumesResponse, err error) { |
| 82 | err = client.Invoke(http.MethodGet, "/volumes", nil, nil, &volumes) |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | func (client *ProjectClient) DeleteVolume(name string) (err error) { |
| 87 | err = client.Invoke(http.MethodDelete, "/volumes/"+name, nil, nil, nil) |
| 88 | return |
| 89 | } |