github.com/gophercloud/gophercloud@v1.11.0/openstack/orchestration/v1/stackresources/results.go (about) 1 package stackresources 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/gophercloud/gophercloud" 8 "github.com/gophercloud/gophercloud/pagination" 9 ) 10 11 // Resource represents a stack resource. 12 type Resource struct { 13 Attributes map[string]interface{} `json:"attributes"` 14 CreationTime time.Time `json:"-"` 15 Description string `json:"description"` 16 Links []gophercloud.Link `json:"links"` 17 LogicalID string `json:"logical_resource_id"` 18 Name string `json:"resource_name"` 19 ParentResource string `json:"parent_resource"` 20 PhysicalID string `json:"physical_resource_id"` 21 RequiredBy []interface{} `json:"required_by"` 22 Status string `json:"resource_status"` 23 StatusReason string `json:"resource_status_reason"` 24 Type string `json:"resource_type"` 25 UpdatedTime time.Time `json:"-"` 26 } 27 28 func (r *Resource) UnmarshalJSON(b []byte) error { 29 type tmp Resource 30 var s struct { 31 tmp 32 CreationTime string `json:"creation_time"` 33 UpdatedTime string `json:"updated_time"` 34 } 35 36 err := json.Unmarshal(b, &s) 37 if err != nil { 38 return err 39 } 40 41 *r = Resource(s.tmp) 42 43 if s.CreationTime != "" { 44 t, err := time.Parse(time.RFC3339, s.CreationTime) 45 if err != nil { 46 t, err = time.Parse(gophercloud.RFC3339NoZ, s.CreationTime) 47 if err != nil { 48 return err 49 } 50 } 51 r.CreationTime = t 52 } 53 54 if s.UpdatedTime != "" { 55 t, err := time.Parse(time.RFC3339, s.UpdatedTime) 56 if err != nil { 57 t, err = time.Parse(gophercloud.RFC3339NoZ, s.UpdatedTime) 58 if err != nil { 59 return err 60 } 61 } 62 r.UpdatedTime = t 63 } 64 65 return nil 66 } 67 68 // FindResult represents the result of a Find operation. 69 type FindResult struct { 70 gophercloud.Result 71 } 72 73 // Extract returns a slice of Resource objects and is called after a 74 // Find operation. 75 func (r FindResult) Extract() ([]Resource, error) { 76 var s struct { 77 Resources []Resource `json:"resources"` 78 } 79 err := r.ExtractInto(&s) 80 return s.Resources, err 81 } 82 83 // ResourcePage abstracts the raw results of making a List() request against the API. 84 // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the 85 // data provided through the ExtractResources call. 86 type ResourcePage struct { 87 pagination.SinglePageBase 88 } 89 90 // IsEmpty returns true if a page contains no Server results. 91 func (r ResourcePage) IsEmpty() (bool, error) { 92 if r.StatusCode == 204 { 93 return true, nil 94 } 95 96 resources, err := ExtractResources(r) 97 return len(resources) == 0, err 98 } 99 100 // ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource entities. 101 func ExtractResources(r pagination.Page) ([]Resource, error) { 102 var s struct { 103 Resources []Resource `json:"resources"` 104 } 105 err := (r.(ResourcePage)).ExtractInto(&s) 106 return s.Resources, err 107 } 108 109 // GetResult represents the result of a Get operation. 110 type GetResult struct { 111 gophercloud.Result 112 } 113 114 // Extract returns a pointer to a Resource object and is called after a 115 // Get operation. 116 func (r GetResult) Extract() (*Resource, error) { 117 var s struct { 118 Resource *Resource `json:"resource"` 119 } 120 err := r.ExtractInto(&s) 121 return s.Resource, err 122 } 123 124 // MetadataResult represents the result of a Metadata operation. 125 type MetadataResult struct { 126 gophercloud.Result 127 } 128 129 // Extract returns a map object and is called after a 130 // Metadata operation. 131 func (r MetadataResult) Extract() (map[string]string, error) { 132 var s struct { 133 Meta map[string]string `json:"metadata"` 134 } 135 err := r.ExtractInto(&s) 136 return s.Meta, err 137 } 138 139 // ResourceTypePage abstracts the raw results of making a ListTypes() request against the API. 140 // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the 141 // data provided through the ExtractResourceTypes call. 142 type ResourceTypePage struct { 143 pagination.SinglePageBase 144 } 145 146 // IsEmpty returns true if a ResourceTypePage contains no resource types. 147 func (r ResourceTypePage) IsEmpty() (bool, error) { 148 if r.StatusCode == 204 { 149 return true, nil 150 } 151 152 rts, err := ExtractResourceTypes(r) 153 return len(rts) == 0, err 154 } 155 156 // ResourceTypes represents the type that holds the result of ExtractResourceTypes. 157 // We define methods on this type to sort it before output 158 type ResourceTypes []string 159 160 func (r ResourceTypes) Len() int { 161 return len(r) 162 } 163 164 func (r ResourceTypes) Swap(i, j int) { 165 r[i], r[j] = r[j], r[i] 166 } 167 168 func (r ResourceTypes) Less(i, j int) bool { 169 return r[i] < r[j] 170 } 171 172 // ExtractResourceTypes extracts and returns resource types. 173 func ExtractResourceTypes(r pagination.Page) (ResourceTypes, error) { 174 var s struct { 175 ResourceTypes ResourceTypes `json:"resource_types"` 176 } 177 err := (r.(ResourceTypePage)).ExtractInto(&s) 178 return s.ResourceTypes, err 179 } 180 181 // TypeSchema represents a stack resource schema. 182 type TypeSchema struct { 183 Attributes map[string]interface{} `json:"attributes"` 184 Properties map[string]interface{} `json:"properties"` 185 ResourceType string `json:"resource_type"` 186 SupportStatus map[string]interface{} `json:"support_status"` 187 } 188 189 // SchemaResult represents the result of a Schema operation. 190 type SchemaResult struct { 191 gophercloud.Result 192 } 193 194 // Extract returns a pointer to a TypeSchema object and is called after a 195 // Schema operation. 196 func (r SchemaResult) Extract() (*TypeSchema, error) { 197 var s *TypeSchema 198 err := r.ExtractInto(&s) 199 return s, err 200 } 201 202 // TemplateResult represents the result of a Template operation. 203 type TemplateResult struct { 204 gophercloud.Result 205 } 206 207 // Extract returns the template and is called after a 208 // Template operation. 209 func (r TemplateResult) Extract() ([]byte, error) { 210 if r.Err != nil { 211 return nil, r.Err 212 } 213 template, err := json.MarshalIndent(r.Body, "", " ") 214 return template, err 215 } 216 217 // MarkUnhealthyResult represents the result of a mark unhealthy operation. 218 type MarkUnhealthyResult struct { 219 gophercloud.ErrResult 220 }