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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
package profiles
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// Profile represent a detailed profile.
type Profile struct {
CreatedAt time.Time `json:"-"`
Domain string `json:"domain"`
ID string `json:"id"`
Metadata map[string]interface{} `json:"metadata"`
Name string `json:"name"`
Project string `json:"project"`
Spec Spec `json:"spec"`
Type string `json:"type"`
UpdatedAt time.Time `json:"-"`
User string `json:"user"`
}
func (r *Profile) UnmarshalJSON(b []byte) error {
type tmp Profile
var s struct {
tmp
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Profile(s.tmp)
if s.CreatedAt != "" {
r.CreatedAt, err = time.Parse(time.RFC3339, s.CreatedAt)
if err != nil {
return err
}
}
if s.UpdatedAt != "" {
r.UpdatedAt, err = time.Parse(time.RFC3339, s.UpdatedAt)
if err != nil {
return err
}
}
return nil
}
// Spec represents a profile spec.
type Spec struct {
Type string `json:"type"`
Version string `json:"-"`
Properties map[string]interface{} `json:"properties"`
}
func (r *Spec) UnmarshalJSON(b []byte) error {
type tmp Spec
var s struct {
tmp
Version interface{} `json:"version"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Spec(s.tmp)
switch t := s.Version.(type) {
case float64:
if t == 1 {
r.Version = fmt.Sprintf("%.1f", t)
} else {
r.Version = strconv.FormatFloat(t, 'f', -1, 64)
}
case string:
r.Version = t
}
return nil
}
// commonResult is the base result of a Profile operation.
type commonResult struct {
gophercloud.Result
}
// Extract provides access to Profile returned by the Get and Create functions.
func (r commonResult) Extract() (*Profile, error) {
var s struct {
Profile *Profile `json:"profile"`
}
err := r.ExtractInto(&s)
return s.Profile, err
}
// CreateResult is the result of a Create operation. Call its Extract
// method to interpret it as a Profile.
type CreateResult struct {
commonResult
}
// GetResult is the result of a Get operations. Call its Extract
// method to interpret it as a Profile.
type GetResult struct {
commonResult
}
// UpdateResult is the result of a Update operations. Call its Extract
// method to interpret it as a Profile.
type UpdateResult struct {
commonResult
}
// DeleteResult is the result from a Delete operation. Call its ExtractErr
// method to determine if the call succeeded or failed.
type DeleteResult struct {
gophercloud.ErrResult
}
// ProfilePage contains a single page of all profiles from a List operation.
type ProfilePage struct {
pagination.LinkedPageBase
}
// IsEmpty determines if a ProfilePage contains any results.
func (page ProfilePage) IsEmpty() (bool, error) {
profiles, err := ExtractProfiles(page)
return len(profiles) == 0, err
}
// ExtractProfiles returns a slice of Profiles from the List operation.
func ExtractProfiles(r pagination.Page) ([]Profile, error) {
var s struct {
Profiles []Profile `json:"profiles"`
}
err := (r.(ProfilePage)).ExtractInto(&s)
return s.Profiles, err
}
|