|
1 | 1 | package images
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
5 | 6 |
|
6 | 7 | "github.com/rackspace/gophercloud"
|
@@ -59,7 +60,7 @@ func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) paginat
|
59 | 60 | }
|
60 | 61 |
|
61 | 62 | // Get acquires additional detail about a specific image by ID.
|
62 |
| -// Use ExtractImage() to interpret the result as an openstack Image. |
| 63 | +// Use GetResult.Extract() to interpret the result as an openstack Image. |
63 | 64 | func Get(client *gophercloud.ServiceClient, id string) GetResult {
|
64 | 65 | var result GetResult
|
65 | 66 | _, result.Err = client.Get(getURL(client, id), &result.Body, nil)
|
@@ -107,3 +108,121 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error)
|
107 | 108 | return "", fmt.Errorf("Found %d images matching %s", imageCount, name)
|
108 | 109 | }
|
109 | 110 | }
|
| 111 | + |
| 112 | +// Metadata requests all the metadata for the given image ID. |
| 113 | +func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult { |
| 114 | + var res GetMetadataResult |
| 115 | + _, res.Err = client.Get(metadataURL(client, id), &res.Body, nil) |
| 116 | + return res |
| 117 | +} |
| 118 | + |
| 119 | +// MetadataOpts is a map that contains key-value pairs. |
| 120 | +type MetadataOpts map[string]string |
| 121 | + |
| 122 | +// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts. |
| 123 | +func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) { |
| 124 | + return map[string]interface{}{"metadata": opts}, nil |
| 125 | +} |
| 126 | + |
| 127 | +// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts. |
| 128 | +func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) { |
| 129 | + return map[string]interface{}{"metadata": opts}, nil |
| 130 | +} |
| 131 | + |
| 132 | +// ResetMetadataOptsBuilder allows extensions to add additional parameters to the |
| 133 | +// Reset request. |
| 134 | +type ResetMetadataOptsBuilder interface { |
| 135 | + ToMetadataResetMap() (map[string]interface{}, error) |
| 136 | +} |
| 137 | + |
| 138 | +// ResetMetadata will create multiple new key-value pairs for the given image ID. |
| 139 | +// Note: Using this operation will erase any already-existing metadata and create |
| 140 | +// the new metadata provided. To keep any already-existing metadata, use the |
| 141 | +// UpdateMetadatas or UpdateMetadata function. |
| 142 | +func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult { |
| 143 | + var res ResetMetadataResult |
| 144 | + metadata, err := opts.ToMetadataResetMap() |
| 145 | + if err != nil { |
| 146 | + res.Err = err |
| 147 | + return res |
| 148 | + } |
| 149 | + _, res.Err = client.Put(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{ |
| 150 | + OkCodes: []int{200}, |
| 151 | + }) |
| 152 | + return res |
| 153 | +} |
| 154 | + |
| 155 | +// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the |
| 156 | +// Create request. |
| 157 | +type UpdateMetadataOptsBuilder interface { |
| 158 | + ToMetadataUpdateMap() (map[string]interface{}, error) |
| 159 | +} |
| 160 | + |
| 161 | +// UpdateMetadata updates (or creates) all the metadata specified by opts for the given image ID. |
| 162 | +// This operation does not affect already-existing metadata that is not specified |
| 163 | +// by opts. |
| 164 | +func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult { |
| 165 | + var res UpdateMetadataResult |
| 166 | + metadata, err := opts.ToMetadataUpdateMap() |
| 167 | + if err != nil { |
| 168 | + res.Err = err |
| 169 | + return res |
| 170 | + } |
| 171 | + _, res.Err = client.Post(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{ |
| 172 | + OkCodes: []int{200}, |
| 173 | + }) |
| 174 | + return res |
| 175 | +} |
| 176 | + |
| 177 | +// Metadatum requests the key-value pair with the given key for the given image ID. |
| 178 | +func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult { |
| 179 | + var res GetMetadatumResult |
| 180 | + _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{ |
| 181 | + JSONResponse: &res.Body, |
| 182 | + }) |
| 183 | + return res |
| 184 | +} |
| 185 | + |
| 186 | +// MetadatumOpts is a map of length one that contains a key-value pair. |
| 187 | +type MetadatumOpts map[string]interface{} |
| 188 | + |
| 189 | +// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts. |
| 190 | +func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) { |
| 191 | + if len(opts) != 1 { |
| 192 | + return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.") |
| 193 | + } |
| 194 | + metadatum := map[string]interface{}{"meta": opts} |
| 195 | + var key string |
| 196 | + for k := range metadatum["meta"].(MetadatumOpts) { |
| 197 | + key = k |
| 198 | + } |
| 199 | + return metadatum, key, nil |
| 200 | +} |
| 201 | + |
| 202 | +// MetadatumOptsBuilder allows extensions to add additional parameters to the |
| 203 | +// Create request. |
| 204 | +type MetadatumOptsBuilder interface { |
| 205 | + ToMetadatumCreateMap() (map[string]interface{}, string, error) |
| 206 | +} |
| 207 | + |
| 208 | +// CreateMetadatum will create or update the key-value pair with the given key for the given image ID. |
| 209 | +func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult { |
| 210 | + var res CreateMetadatumResult |
| 211 | + metadatum, key, err := opts.ToMetadatumCreateMap() |
| 212 | + if err != nil { |
| 213 | + res.Err = err |
| 214 | + return res |
| 215 | + } |
| 216 | + |
| 217 | + _, res.Err = client.Put(metadatumURL(client, id, key), metadatum, &res.Body, &gophercloud.RequestOpts{ |
| 218 | + OkCodes: []int{200}, |
| 219 | + }) |
| 220 | + return res |
| 221 | +} |
| 222 | + |
| 223 | +// DeleteMetadatum will delete the key-value pair with the given key for the given image ID. |
| 224 | +func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult { |
| 225 | + var res DeleteMetadatumResult |
| 226 | + _, res.Err = client.Delete(metadatumURL(client, id, key), nil) |
| 227 | + return res |
| 228 | +} |
0 commit comments