Ash Wilson | 0e5b92a | 2014-10-23 11:42:04 -0400 | [diff] [blame^] | 1 | package diskconfig |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | func commonExtract(result gophercloud.Result) (*DiskConfig, error) { |
| 11 | var resp struct { |
| 12 | Server struct { |
| 13 | DiskConfig string `mapstructure:"OS-DCF:diskConfig"` |
| 14 | } `mapstructure:"server"` |
| 15 | } |
| 16 | |
| 17 | err := mapstructure.Decode(result.Body, &resp) |
| 18 | if err != nil { |
| 19 | return nil, err |
| 20 | } |
| 21 | |
| 22 | config := DiskConfig(resp.Server.DiskConfig) |
| 23 | return &config, nil |
| 24 | } |
| 25 | |
| 26 | // ExtractGet returns the disk configuration from a servers.Get call. |
| 27 | func ExtractGet(result servers.GetResult) (*DiskConfig, error) { |
| 28 | return commonExtract(result.Result) |
| 29 | } |
| 30 | |
| 31 | // ExtractUpdate returns the disk configuration from a servers.Update call. |
| 32 | func ExtractUpdate(result servers.UpdateResult) (*DiskConfig, error) { |
| 33 | return commonExtract(result.Result) |
| 34 | } |
| 35 | |
| 36 | // ExtractRebuild returns the disk configuration from a servers.Rebuild call. |
| 37 | func ExtractRebuild(result servers.RebuildResult) (*DiskConfig, error) { |
| 38 | return commonExtract(result.Result) |
| 39 | } |
| 40 | |
| 41 | // ExtractDiskConfig returns the DiskConfig setting for a specific server acquired from an |
| 42 | // servers.ExtractServers call, while iterating through a Pager. |
| 43 | func ExtractDiskConfig(page pagination.Page, index int) (*DiskConfig, error) { |
| 44 | casted := page.(servers.ServerPage).Body |
| 45 | |
| 46 | type server struct { |
| 47 | DiskConfig string `mapstructure:"OS-CDF:diskConfig"` |
| 48 | } |
| 49 | var response struct { |
| 50 | Servers []server `mapstructure:"servers"` |
| 51 | } |
| 52 | |
| 53 | err := mapstructure.Decode(casted, &response) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | config := DiskConfig(response.Servers[index].DiskConfig) |
| 59 | return &config, nil |
| 60 | } |