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 (123 lines) | stat: -rw-r--r-- 3,182 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package snapshots

import (
	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/pagination"

	"github.com/mitchellh/mapstructure"
)

// Snapshot contains all the information associated with an OpenStack Snapshot.
type Snapshot struct {
	// Currect status of the Snapshot.
	Status string `mapstructure:"status"`

	// Display name.
	Name string `mapstructure:"display_name"`

	// Instances onto which the Snapshot is attached.
	Attachments []string `mapstructure:"attachments"`

	// Logical group.
	AvailabilityZone string `mapstructure:"availability_zone"`

	// Is the Snapshot bootable?
	Bootable string `mapstructure:"bootable"`

	// Date created.
	CreatedAt string `mapstructure:"created_at"`

	// Display description.
	Description string `mapstructure:"display_discription"`

	// See VolumeType object for more information.
	VolumeType string `mapstructure:"volume_type"`

	// ID of the Snapshot from which this Snapshot was created.
	SnapshotID string `mapstructure:"snapshot_id"`

	// ID of the Volume from which this Snapshot was created.
	VolumeID string `mapstructure:"volume_id"`

	// User-defined key-value pairs.
	Metadata map[string]string `mapstructure:"metadata"`

	// Unique identifier.
	ID string `mapstructure:"id"`

	// Size of the Snapshot, in GB.
	Size int `mapstructure:"size"`
}

// CreateResult contains the response body and error from a Create request.
type CreateResult struct {
	commonResult
}

// GetResult contains the response body and error from a Get request.
type GetResult struct {
	commonResult
}

// DeleteResult contains the response body and error from a Delete request.
type DeleteResult struct {
	gophercloud.ErrResult
}

// ListResult is a pagination.Pager that is returned from a call to the List function.
type ListResult struct {
	pagination.SinglePageBase
}

// IsEmpty returns true if a ListResult contains no Snapshots.
func (r ListResult) IsEmpty() (bool, error) {
	volumes, err := ExtractSnapshots(r)
	if err != nil {
		return true, err
	}
	return len(volumes) == 0, nil
}

// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
	var response struct {
		Snapshots []Snapshot `json:"snapshots"`
	}

	err := mapstructure.Decode(page.(ListResult).Body, &response)
	return response.Snapshots, err
}

// UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
type UpdateMetadataResult struct {
	commonResult
}

// ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	m := r.Body.(map[string]interface{})["metadata"]
	return m.(map[string]interface{}), nil
}

type commonResult struct {
	gophercloud.Result
}

// Extract will get the Snapshot object out of the commonResult object.
func (r commonResult) Extract() (*Snapshot, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	var res struct {
		Snapshot *Snapshot `json:"snapshot"`
	}

	err := mapstructure.Decode(r.Body, &res)

	return res.Snapshot, err
}