jrperritt | c5c590a | 2016-11-04 14:41:15 -0500 | [diff] [blame] | 1 | package images |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
| 6 | ) |
| 7 | |
| 8 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 9 | // List request. |
| 10 | type ListOptsBuilder interface { |
| 11 | ToImageListQuery() (string, error) |
| 12 | } |
| 13 | |
| 14 | // ListOpts allows the filtering and sorting of paginated collections through |
| 15 | // the API. Filtering is achieved by passing in struct field values that map to |
| 16 | // the server attributes you want to see returned. Marker and Limit are used |
| 17 | // for pagination. |
| 18 | //http://developer.openstack.org/api-ref-image-v2.html |
| 19 | type ListOpts struct { |
| 20 | // Integer value for the limit of values to return. |
| 21 | Limit int `q:"limit"` |
| 22 | |
| 23 | // UUID of the server at which you want to set a marker. |
| 24 | Marker string `q:"marker"` |
| 25 | |
| 26 | Name string `q:"name"` |
| 27 | Visibility ImageVisibility `q:"visibility"` |
| 28 | MemberStatus ImageMemberStatus `q:"member_status"` |
| 29 | Owner string `q:"owner"` |
| 30 | Status ImageStatus `q:"status"` |
| 31 | SizeMin int64 `q:"size_min"` |
| 32 | SizeMax int64 `q:"size_max"` |
| 33 | SortKey string `q:"sort_key"` |
| 34 | SortDir string `q:"sort_dir"` |
| 35 | Tag string `q:"tag"` |
| 36 | } |
| 37 | |
| 38 | // ToImageListQuery formats a ListOpts into a query string. |
| 39 | func (opts ListOpts) ToImageListQuery() (string, error) { |
| 40 | q, err := gophercloud.BuildQueryString(opts) |
| 41 | return q.String(), err |
| 42 | } |
| 43 | |
| 44 | // List implements image list request |
| 45 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 46 | url := listURL(c) |
| 47 | if opts != nil { |
| 48 | query, err := opts.ToImageListQuery() |
| 49 | if err != nil { |
| 50 | return pagination.Pager{Err: err} |
| 51 | } |
| 52 | url += query |
| 53 | } |
| 54 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 55 | return ImagePage{pagination.LinkedPageBase{PageResult: r}} |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. |
| 60 | // The CreateOpts struct in this package does. |
| 61 | type CreateOptsBuilder interface { |
| 62 | // Returns value that can be passed to json.Marshal |
| 63 | ToImageCreateMap() (map[string]interface{}, error) |
| 64 | } |
| 65 | |
| 66 | // CreateOpts implements CreateOptsBuilder |
| 67 | type CreateOpts struct { |
| 68 | // Name is the name of the new image. |
| 69 | Name string `json:"name" required:"true"` |
| 70 | |
| 71 | // Id is the the image ID. |
| 72 | ID string `json:"id,omitempty"` |
| 73 | |
| 74 | // Visibility defines who can see/use the image. |
| 75 | Visibility *ImageVisibility `json:"visibility,omitempty"` |
| 76 | |
| 77 | // Tags is a set of image tags. |
| 78 | Tags []string `json:"tags,omitempty"` |
| 79 | |
| 80 | // ContainerFormat is the format of the |
| 81 | // container. Valid values are ami, ari, aki, bare, and ovf. |
| 82 | ContainerFormat string `json:"container_format,omitempty"` |
| 83 | |
| 84 | // DiskFormat is the format of the disk. If set, |
| 85 | // valid values are ami, ari, aki, vhd, vmdk, raw, qcow2, vdi, |
| 86 | // and iso. |
| 87 | DiskFormat string `json:"disk_format,omitempty"` |
| 88 | |
| 89 | // MinDisk is the amount of disk space in |
| 90 | // GB that is required to boot the image. |
| 91 | MinDisk int `json:"min_disk,omitempty"` |
| 92 | |
| 93 | // MinRAM is the amount of RAM in MB that |
| 94 | // is required to boot the image. |
| 95 | MinRAM int `json:"min_ram,omitempty"` |
| 96 | |
| 97 | // protected is whether the image is not deletable. |
| 98 | Protected *bool `json:"protected,omitempty"` |
| 99 | |
| 100 | // properties is a set of properties, if any, that |
| 101 | // are associated with the image. |
| 102 | Properties map[string]string `json:"-,omitempty"` |
| 103 | } |
| 104 | |
| 105 | // ToImageCreateMap assembles a request body based on the contents of |
| 106 | // a CreateOpts. |
| 107 | func (opts CreateOpts) ToImageCreateMap() (map[string]interface{}, error) { |
| 108 | b, err := gophercloud.BuildRequestBody(opts, "") |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | |
| 113 | if opts.Properties != nil { |
| 114 | for k, v := range opts.Properties { |
| 115 | b[k] = v |
| 116 | } |
| 117 | } |
| 118 | return b, nil |
| 119 | } |
| 120 | |
| 121 | // Create implements create image request |
| 122 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 123 | b, err := opts.ToImageCreateMap() |
| 124 | if err != nil { |
| 125 | r.Err = err |
| 126 | return r |
| 127 | } |
| 128 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{201}}) |
| 129 | return |
| 130 | } |
| 131 | |
| 132 | // Delete implements image delete request |
| 133 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
| 134 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | // Get implements image get request |
| 139 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
| 140 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | // Update implements image updated request |
| 145 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
| 146 | b, err := opts.ToImageUpdateMap() |
| 147 | if err != nil { |
| 148 | r.Err = err |
| 149 | return r |
| 150 | } |
| 151 | _, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
| 152 | OkCodes: []int{200}, |
| 153 | MoreHeaders: map[string]string{"Content-Type": "application/openstack-images-v2.1-json-patch"}, |
| 154 | }) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | // UpdateOptsBuilder implements UpdateOptsBuilder |
| 159 | type UpdateOptsBuilder interface { |
| 160 | // returns value implementing json.Marshaler which when marshaled matches the patch schema: |
| 161 | // http://specs.openstack.org/openstack/glance-specs/specs/api/v2/http-patch-image-api-v2.html |
| 162 | ToImageUpdateMap() ([]interface{}, error) |
| 163 | } |
| 164 | |
| 165 | // UpdateOpts implements UpdateOpts |
| 166 | type UpdateOpts []Patch |
| 167 | |
| 168 | // ToImageUpdateMap builder |
| 169 | func (opts UpdateOpts) ToImageUpdateMap() ([]interface{}, error) { |
| 170 | m := make([]interface{}, len(opts)) |
| 171 | for i, patch := range opts { |
| 172 | patchJSON := patch.ToImagePatchMap() |
| 173 | m[i] = patchJSON |
| 174 | } |
| 175 | return m, nil |
| 176 | } |
| 177 | |
| 178 | // Patch represents a single update to an existing image. Multiple updates to an image can be |
| 179 | // submitted at the same time. |
| 180 | type Patch interface { |
| 181 | ToImagePatchMap() map[string]interface{} |
| 182 | } |
| 183 | |
| 184 | // UpdateVisibility updated visibility |
| 185 | type UpdateVisibility struct { |
| 186 | Visibility ImageVisibility |
| 187 | } |
| 188 | |
| 189 | // ToImagePatchMap builder |
| 190 | func (u UpdateVisibility) ToImagePatchMap() map[string]interface{} { |
| 191 | return map[string]interface{}{ |
| 192 | "op": "replace", |
| 193 | "path": "/visibility", |
| 194 | "value": u.Visibility, |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // ReplaceImageName implements Patch |
| 199 | type ReplaceImageName struct { |
| 200 | NewName string |
| 201 | } |
| 202 | |
| 203 | // ToImagePatchMap builder |
| 204 | func (r ReplaceImageName) ToImagePatchMap() map[string]interface{} { |
| 205 | return map[string]interface{}{ |
| 206 | "op": "replace", |
| 207 | "path": "/name", |
| 208 | "value": r.NewName, |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // ReplaceImageChecksum implements Patch |
| 213 | type ReplaceImageChecksum struct { |
| 214 | Checksum string |
| 215 | } |
| 216 | |
| 217 | // ReplaceImageChecksum builder |
| 218 | func (rc ReplaceImageChecksum) ToImagePatchMap() map[string]interface{} { |
| 219 | return map[string]interface{}{ |
| 220 | "op": "replace", |
| 221 | "path": "/checksum", |
| 222 | "value": rc.Checksum, |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // ReplaceImageTags implements Patch |
| 227 | type ReplaceImageTags struct { |
| 228 | NewTags []string |
| 229 | } |
| 230 | |
| 231 | // ToImagePatchMap builder |
| 232 | func (r ReplaceImageTags) ToImagePatchMap() map[string]interface{} { |
| 233 | return map[string]interface{}{ |
| 234 | "op": "replace", |
| 235 | "path": "/tags", |
| 236 | "value": r.NewTags, |
| 237 | } |
| 238 | } |