|
1 | 1 | package v2
|
2 | 2 |
|
3 |
| -// TODO |
| 3 | +import ( |
| 4 | + "github.com/rackspace/gophercloud" |
| 5 | + //"github.com/rackspace/gophercloud/pagination" |
| 6 | +) |
| 7 | + |
| 8 | +// List : (*gophercloud.ServiceClient) -> pagination.Pager<ImagePage> |
| 9 | +// func List(c *gophercloud.ServiceClient) pagination.Pager { |
| 10 | +// return pagination.NewPager(c, listURL(c), func (r pagination.PageResult) pagination.Page { |
| 11 | +// p := ImagePage{pagination.MarkerPageBase{PageResult: r}} |
| 12 | +// p.MarkerPageBase.Owner = p |
| 13 | +// return p |
| 14 | +// }) |
| 15 | +// } |
| 16 | + |
| 17 | +func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 18 | + var res CreateResult |
| 19 | + body, err := opts.ToImageCreateMap() |
| 20 | + if err != nil { |
| 21 | + res.Err = err |
| 22 | + return res |
| 23 | + } |
| 24 | + response, err := client.Post(createURL(client), body, &res.Body, nil) |
| 25 | + // TODO check response..? |
| 26 | + return res |
| 27 | +} |
| 28 | + |
| 29 | +// CreateOptsBuilder describes struct types that can be accepted by the Create call. |
| 30 | +// The CreateOpts struct in this package does. |
| 31 | +type CreateOptsBuilder interface { |
| 32 | + // Returns value that implements json.Marshaler |
| 33 | + ToImageCreateMap() (map[string]interface{}, error) |
| 34 | +} |
| 35 | + |
| 36 | +// implements CreateOptsBuilder |
| 37 | +type CreateOpts struct { |
| 38 | + // Name [required] is the name of the new image. |
| 39 | + Name string |
| 40 | + |
| 41 | + // Id [optional] is the the image ID. |
| 42 | + Id string |
| 43 | + |
| 44 | + // Visibility [optional] defines who can see/use the image. |
| 45 | + Visibility ImageVisibility |
| 46 | + |
| 47 | + // Tags [optional] is a set of image tags. |
| 48 | + Tags []string |
| 49 | + |
| 50 | + // ContainerFormat [optional] is the format of the |
| 51 | + // container. Valid values are ami, ari, aki, bare, and ovf. |
| 52 | + ContainerFormat string |
| 53 | + |
| 54 | + // DiskFormat [optional] is the format of the disk. If set, |
| 55 | + // valid values are ami, ari, aki, vhd, vmdk, raw, qcow2, vdi, |
| 56 | + // and iso. |
| 57 | + DiskFormat string |
| 58 | + |
| 59 | + // MinDiskGigabytes [optional] is the amount of disk space in |
| 60 | + // GB that is required to boot the image. |
| 61 | + MinDiskGigabytes int |
| 62 | + |
| 63 | + // MinRamMegabytes [optional] is the amount of RAM in MB that |
| 64 | + // is required to boot the image. |
| 65 | + MinRamMegabytes int |
| 66 | + |
| 67 | + // protected [optional] is whether the image is not deletable. |
| 68 | + Protected bool |
| 69 | + |
| 70 | + // properties [optional] is a set of properties, if any, that |
| 71 | + // are associated with the image. |
| 72 | + Properties map[string]string |
| 73 | +} |
| 74 | + |
| 75 | +func setIfNotNil(m map[string]interface{}, k string, v interface{}) { |
| 76 | + if v != nil { |
| 77 | + m[k] = v |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// ToImageCreateMap assembles a request body based on the contents of |
| 82 | +// a CreateOpts. |
| 83 | +func (opts CreateOpts) ToImageCreateMap() (map[string]interface{}, error) { |
| 84 | + body := map[string]interface{}{} |
| 85 | + body["name"] = opts.Name |
| 86 | + setIfNotNil(body, "id", opts.Id) |
| 87 | + setIfNotNil(body, "visibility", opts.Visibility) |
| 88 | + setIfNotNil(body, "tags", opts.Tags) |
| 89 | + setIfNotNil(body, "container_format", opts.ContainerFormat) |
| 90 | + setIfNotNil(body, "disk_format", opts.DiskFormat) |
| 91 | + setIfNotNil(body, "min_disk", opts.MinDiskGigabytes) |
| 92 | + setIfNotNil(body, "min_ram", opts.MinRamMegabytes) |
| 93 | + setIfNotNil(body, "protected", opts.Protected) |
| 94 | + setIfNotNil(body, "properties", opts.Properties) |
| 95 | + return body, nil |
| 96 | +} |
| 97 | + |
| 98 | +func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 99 | + var res DeleteResult |
| 100 | + _, res.Err = client.Delete(deleteURL(client, id), nil) |
| 101 | + return res |
| 102 | +} |
| 103 | + |
| 104 | +func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 105 | + var res GetResult |
| 106 | + client.Get(getURL(client, id), &res.Body, nil) |
| 107 | + return res |
| 108 | +} |
| 109 | + |
| 110 | +func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
| 111 | + var res UpdateResult |
| 112 | + reqBody := opts.ToImageUpdateMap() |
| 113 | + |
| 114 | + _, res.Err = client.Patch(updateURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 115 | + OkCodes: []int{200}, |
| 116 | + }) |
| 117 | + return res |
| 118 | +} |
| 119 | + |
| 120 | +type UpdateOptsBuilder interface { |
| 121 | + // returns value implementing json.Marshaler which when marshaled matches the patch schema: |
| 122 | + // http://specs.openstack.org/openstack/glance-specs/specs/api/v2/http-patch-image-api-v2.html |
| 123 | + ToImageUpdateMap() []interface{} |
| 124 | +} |
| 125 | + |
| 126 | +// implements UpdateOptsBuilder |
| 127 | +type UpdateOpts []Patch |
| 128 | + |
| 129 | +func (opts UpdateOpts) ToImageUpdateMap() []interface{} { |
| 130 | + m := make([]interface{}, len(opts)) |
| 131 | + for i, patch := range opts { |
| 132 | + patchJson := patch.ToImagePatchMap() |
| 133 | + m[i] = patchJson |
| 134 | + } |
| 135 | + return m |
| 136 | +} |
| 137 | + |
| 138 | +// Patch represents a single update to an existing image. Multiple updates to an image can be |
| 139 | +// submitted at the same time. |
| 140 | +type Patch interface { |
| 141 | + ToImagePatchMap() map[string]interface{} |
| 142 | +} |
| 143 | + |
| 144 | +//implements Patch |
| 145 | +type ReplaceImageName struct { |
| 146 | + NewName string |
| 147 | +} |
| 148 | + |
| 149 | +func (r *ReplaceImageName) ToImagePatchMap() map[string]interface{} { |
| 150 | + m := map[string]interface{}{} |
| 151 | + m["op"] = "replace" |
| 152 | + m["path"] = "/name" |
| 153 | + m["value"] = r.NewName |
| 154 | + return m |
| 155 | +} |
| 156 | + |
| 157 | +// Things implementing Patch can also implement UpdateOptsBuilder. |
| 158 | +// Unfortunately we have to specify each of these manually. |
| 159 | + |
| 160 | +func (r *ReplaceImageName) ToImageUpdateMap() map[string]interface{} { |
| 161 | + return r.ToImagePatchMap() |
| 162 | +} |
0 commit comments