File: results.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package diskconfig

import (
	"github.com/mitchellh/mapstructure"
	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
	"github.com/rackspace/gophercloud/pagination"
)

func commonExtract(result gophercloud.Result) (*DiskConfig, error) {
	var resp struct {
		Server struct {
			DiskConfig string `mapstructure:"OS-DCF:diskConfig"`
		} `mapstructure:"server"`
	}

	err := mapstructure.Decode(result.Body, &resp)
	if err != nil {
		return nil, err
	}

	config := DiskConfig(resp.Server.DiskConfig)
	return &config, nil
}

// ExtractGet returns the disk configuration from a servers.Get call.
func ExtractGet(result servers.GetResult) (*DiskConfig, error) {
	return commonExtract(result.Result)
}

// ExtractUpdate returns the disk configuration from a servers.Update call.
func ExtractUpdate(result servers.UpdateResult) (*DiskConfig, error) {
	return commonExtract(result.Result)
}

// ExtractRebuild returns the disk configuration from a servers.Rebuild call.
func ExtractRebuild(result servers.RebuildResult) (*DiskConfig, error) {
	return commonExtract(result.Result)
}

// ExtractDiskConfig returns the DiskConfig setting for a specific server acquired from an
// servers.ExtractServers call, while iterating through a Pager.
func ExtractDiskConfig(page pagination.Page, index int) (*DiskConfig, error) {
	casted := page.(servers.ServerPage).Body

	type server struct {
		DiskConfig string `mapstructure:"OS-DCF:diskConfig"`
	}
	var response struct {
		Servers []server `mapstructure:"servers"`
	}

	err := mapstructure.Decode(casted, &response)
	if err != nil {
		return nil, err
	}

	config := DiskConfig(response.Servers[index].DiskConfig)
	return &config, nil
}