github.com/gophercloud/gophercloud@v1.11.0/openstack/sharedfilesystems/v2/sharetypes/requests.go (about) 1 package sharetypes 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // CreateOptsBuilder allows extensions to add additional parameters to the 9 // Create request. 10 type CreateOptsBuilder interface { 11 ToShareTypeCreateMap() (map[string]interface{}, error) 12 } 13 14 // CreateOpts contains options for creating a ShareType. This object is 15 // passed to the sharetypes.Create function. For more information about 16 // these parameters, see the ShareType object. 17 type CreateOpts struct { 18 // The share type name 19 Name string `json:"name" required:"true"` 20 // Indicates whether a share type is publicly accessible 21 IsPublic bool `json:"os-share-type-access:is_public"` 22 // The extra specifications for the share type 23 ExtraSpecs ExtraSpecsOpts `json:"extra_specs" required:"true"` 24 } 25 26 // ExtraSpecsOpts represent the extra specifications that can be selected for a share type 27 type ExtraSpecsOpts struct { 28 // An extra specification that defines the driver mode for share server, or storage, life cycle management 29 DriverHandlesShareServers bool `json:"driver_handles_share_servers" required:"true"` 30 // An extra specification that filters back ends by whether they do or do not support share snapshots 31 SnapshotSupport *bool `json:"snapshot_support,omitempty"` 32 } 33 34 // ToShareTypeCreateMap assembles a request body based on the contents of a 35 // CreateOpts. 36 func (opts CreateOpts) ToShareTypeCreateMap() (map[string]interface{}, error) { 37 return gophercloud.BuildRequestBody(opts, "share_type") 38 } 39 40 // Create will create a new ShareType based on the values in CreateOpts. To 41 // extract the ShareType object from the response, call the Extract method 42 // on the CreateResult. 43 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 44 b, err := opts.ToShareTypeCreateMap() 45 if err != nil { 46 r.Err = err 47 return 48 } 49 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 50 OkCodes: []int{200, 202}, 51 }) 52 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 53 return 54 } 55 56 // Delete will delete the existing ShareType with the provided ID. 57 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 58 resp, err := client.Delete(deleteURL(client, id), nil) 59 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 60 return 61 } 62 63 // ListOptsBuilder allows extensions to add additional parameters to the List 64 // request. 65 type ListOptsBuilder interface { 66 ToShareTypeListQuery() (string, error) 67 } 68 69 // ListOpts holds options for listing ShareTypes. It is passed to the 70 // sharetypes.List function. 71 type ListOpts struct { 72 // Select if public types, private types, or both should be listed 73 IsPublic string `q:"is_public"` 74 } 75 76 // ToShareTypeListQuery formats a ListOpts into a query string. 77 func (opts ListOpts) ToShareTypeListQuery() (string, error) { 78 q, err := gophercloud.BuildQueryString(opts) 79 return q.String(), err 80 } 81 82 // List returns ShareTypes optionally limited by the conditions provided in ListOpts. 83 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 84 url := listURL(client) 85 if opts != nil { 86 query, err := opts.ToShareTypeListQuery() 87 if err != nil { 88 return pagination.Pager{Err: err} 89 } 90 url += query 91 } 92 93 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 94 return ShareTypePage{pagination.SinglePageBase(r)} 95 }) 96 } 97 98 // GetDefault will retrieve the default ShareType. 99 func GetDefault(client *gophercloud.ServiceClient) (r GetDefaultResult) { 100 resp, err := client.Get(getDefaultURL(client), &r.Body, nil) 101 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 102 return 103 } 104 105 // GetExtraSpecs will retrieve the extra specifications for a given ShareType. 106 func GetExtraSpecs(client *gophercloud.ServiceClient, id string) (r GetExtraSpecsResult) { 107 resp, err := client.Get(getExtraSpecsURL(client, id), &r.Body, nil) 108 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 109 return 110 } 111 112 // SetExtraSpecsOptsBuilder allows extensions to add additional parameters to the 113 // SetExtraSpecs request. 114 type SetExtraSpecsOptsBuilder interface { 115 ToShareTypeSetExtraSpecsMap() (map[string]interface{}, error) 116 } 117 118 type SetExtraSpecsOpts struct { 119 // A list of all extra specifications to be added to a ShareType 120 ExtraSpecs map[string]interface{} `json:"extra_specs" required:"true"` 121 } 122 123 // ToShareTypeSetExtraSpecsMap assembles a request body based on the contents of a 124 // SetExtraSpecsOpts. 125 func (opts SetExtraSpecsOpts) ToShareTypeSetExtraSpecsMap() (map[string]interface{}, error) { 126 return gophercloud.BuildRequestBody(opts, "") 127 } 128 129 // SetExtraSpecs will set new specifications for a ShareType based on the values 130 // in SetExtraSpecsOpts. To extract the extra specifications object from the response, 131 // call the Extract method on the SetExtraSpecsResult. 132 func SetExtraSpecs(client *gophercloud.ServiceClient, id string, opts SetExtraSpecsOptsBuilder) (r SetExtraSpecsResult) { 133 b, err := opts.ToShareTypeSetExtraSpecsMap() 134 if err != nil { 135 r.Err = err 136 return 137 } 138 139 resp, err := client.Post(setExtraSpecsURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 140 OkCodes: []int{200, 202}, 141 }) 142 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 143 return 144 } 145 146 // UnsetExtraSpecs will unset an extra specification for an existing ShareType. 147 func UnsetExtraSpecs(client *gophercloud.ServiceClient, id string, key string) (r UnsetExtraSpecsResult) { 148 resp, err := client.Delete(unsetExtraSpecsURL(client, id, key), nil) 149 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 150 return 151 } 152 153 // ShowAccess will show access details for an existing ShareType. 154 func ShowAccess(client *gophercloud.ServiceClient, id string) (r ShowAccessResult) { 155 resp, err := client.Get(showAccessURL(client, id), &r.Body, nil) 156 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 157 return 158 } 159 160 // AddAccessOptsBuilder allows extensions to add additional parameters to the 161 // AddAccess 162 type AddAccessOptsBuilder interface { 163 ToAddAccessMap() (map[string]interface{}, error) 164 } 165 166 type AccessOpts struct { 167 // The UUID of the project to which access to the share type is granted. 168 Project string `json:"project"` 169 } 170 171 // ToAddAccessMap assembles a request body based on the contents of a 172 // AccessOpts. 173 func (opts AccessOpts) ToAddAccessMap() (map[string]interface{}, error) { 174 return gophercloud.BuildRequestBody(opts, "addProjectAccess") 175 } 176 177 // AddAccess will add access to a ShareType based on the values 178 // in AccessOpts. 179 func AddAccess(client *gophercloud.ServiceClient, id string, opts AddAccessOptsBuilder) (r AddAccessResult) { 180 b, err := opts.ToAddAccessMap() 181 if err != nil { 182 r.Err = err 183 return 184 } 185 186 resp, err := client.Post(addAccessURL(client, id), b, nil, &gophercloud.RequestOpts{ 187 OkCodes: []int{200, 202}, 188 }) 189 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 190 return 191 } 192 193 // RemoveAccessOptsBuilder allows extensions to add additional parameters to the 194 // RemoveAccess 195 type RemoveAccessOptsBuilder interface { 196 ToRemoveAccessMap() (map[string]interface{}, error) 197 } 198 199 // ToRemoveAccessMap assembles a request body based on the contents of a 200 // AccessOpts. 201 func (opts AccessOpts) ToRemoveAccessMap() (map[string]interface{}, error) { 202 return gophercloud.BuildRequestBody(opts, "removeProjectAccess") 203 } 204 205 // RemoveAccess will remove access to a ShareType based on the values 206 // in AccessOpts. 207 func RemoveAccess(client *gophercloud.ServiceClient, id string, opts RemoveAccessOptsBuilder) (r RemoveAccessResult) { 208 b, err := opts.ToRemoveAccessMap() 209 if err != nil { 210 r.Err = err 211 return 212 } 213 214 resp, err := client.Post(removeAccessURL(client, id), b, nil, &gophercloud.RequestOpts{ 215 OkCodes: []int{200, 202}, 216 }) 217 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 218 return 219 }