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 (113 lines) | stat: -rw-r--r-- 2,819 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
package cloudnetworks

import (
	"fmt"
	"reflect"
	"time"

	"github.com/mitchellh/mapstructure"
	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/pagination"
)

// CloudNetwork represents a network associated with a RackConnect configuration.
type CloudNetwork struct {
	// Specifies the ID of the newtork.
	ID string `mapstructure:"id"`
	// Specifies the user-provided name of the network.
	Name string `mapstructure:"name"`
	// Specifies the IP range for this network.
	CIDR string `mapstructure:"cidr"`
	// Specifies the time the network was created.
	CreatedAt time.Time `mapstructure:"-"`
	// Specifies the time the network was last updated.
	UpdatedAt time.Time `mapstructure:"-"`
}

// CloudNetworkPage is the page returned by a pager when traversing over a
// collection of CloudNetworks.
type CloudNetworkPage struct {
	pagination.SinglePageBase
}

// IsEmpty returns true if a CloudNetworkPage contains no CloudNetworks.
func (r CloudNetworkPage) IsEmpty() (bool, error) {
	cns, err := ExtractCloudNetworks(r)
	if err != nil {
		return true, err
	}
	return len(cns) == 0, nil
}

// ExtractCloudNetworks extracts and returns CloudNetworks. It is used while iterating over
// a cloudnetworks.List call.
func ExtractCloudNetworks(page pagination.Page) ([]CloudNetwork, error) {
	var res []CloudNetwork
	casted := page.(CloudNetworkPage).Body
	err := mapstructure.Decode(casted, &res)

	var rawNets []interface{}
	switch casted.(type) {
	case interface{}:
		rawNets = casted.([]interface{})
	default:
		return res, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
	}

	for i := range rawNets {
		thisNet := (rawNets[i]).(map[string]interface{})

		if t, ok := thisNet["created"].(string); ok && t != "" {
			creationTime, err := time.Parse(time.RFC3339, t)
			if err != nil {
				return res, err
			}
			res[i].CreatedAt = creationTime
		}

		if t, ok := thisNet["updated"].(string); ok && t != "" {
			updatedTime, err := time.Parse(time.RFC3339, t)
			if err != nil {
				return res, err
			}
			res[i].UpdatedAt = updatedTime
		}
	}

	return res, err
}

// GetResult represents the result of a Get operation.
type GetResult struct {
	gophercloud.Result
}

// Extract is a function that extracts a CloudNetwork from a GetResult.
func (r GetResult) Extract() (*CloudNetwork, error) {
	if r.Err != nil {
		return nil, r.Err
	}
	var res CloudNetwork

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

	b := r.Body.(map[string]interface{})

	if date, ok := b["created"]; ok && date != nil {
		t, err := time.Parse(time.RFC3339, date.(string))
		if err != nil {
			return nil, err
		}
		res.CreatedAt = t
	}

	if date, ok := b["updated"]; ok && date != nil {
		t, err := time.Parse(time.RFC3339, date.(string))
		if err != nil {
			return nil, err
		}
		res.UpdatedAt = t
	}

	return &res, err
}