github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/keymanager/v1/containers/results.go (about) 1 package containers 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 9 ) 10 11 // Container represents a container in the key manager service. 12 type Container struct { 13 // Consumers are the consumers of the container. 14 Consumers []ConsumerRef `json:"consumers"` 15 16 // ContainerRef is the URL to the container 17 ContainerRef string `json:"container_ref"` 18 19 // Created is the date the container was created. 20 Created time.Time `json:"-"` 21 22 // CreatorID is the creator of the container. 23 CreatorID string `json:"creator_id"` 24 25 // Name is the name of the container. 26 Name string `json:"name"` 27 28 // SecretRefs are the secret references of the container. 29 SecretRefs []SecretRef `json:"secret_refs"` 30 31 // Status is the status of the container. 32 Status string `json:"status"` 33 34 // Type is the type of container. 35 Type string `json:"type"` 36 37 // Updated is the date the container was updated. 38 Updated time.Time `json:"-"` 39 } 40 41 func (r *Container) UnmarshalJSON(b []byte) error { 42 type tmp Container 43 var s struct { 44 tmp 45 Created gophercloud.JSONRFC3339NoZ `json:"created"` 46 Updated gophercloud.JSONRFC3339NoZ `json:"updated"` 47 } 48 err := json.Unmarshal(b, &s) 49 if err != nil { 50 return err 51 } 52 *r = Container(s.tmp) 53 54 r.Created = time.Time(s.Created) 55 r.Updated = time.Time(s.Updated) 56 57 return nil 58 } 59 60 // ConsumerRef represents a consumer reference in a container. 61 type ConsumerRef struct { 62 // Name is the name of the consumer. 63 Name string `json:"name"` 64 65 // URL is the URL to the consumer resource. 66 URL string `json:"url"` 67 } 68 69 // SecretRef is a reference to a secret. 70 type SecretRef struct { 71 SecretRef string `json:"secret_ref"` 72 Name string `json:"name"` 73 } 74 75 type commonResult struct { 76 gophercloud.Result 77 } 78 79 // Extract interprets any commonResult as a Container. 80 func (r commonResult) Extract() (*Container, error) { 81 var s *Container 82 err := r.ExtractInto(&s) 83 return s, err 84 } 85 86 // GetResult is the response from a Get operation. Call its Extract method 87 // to interpret it as a container. 88 type GetResult struct { 89 commonResult 90 } 91 92 // CreateResult is the response from a Create operation. Call its Extract method 93 // to interpret it as a container. 94 type CreateResult struct { 95 commonResult 96 } 97 98 // DeleteResult is the response from a Delete operation. Call its ExtractErr to 99 // determine if the request succeeded or failed. 100 type DeleteResult struct { 101 gophercloud.ErrResult 102 } 103 104 // ContainerPage is a single page of container results. 105 type ContainerPage struct { 106 pagination.LinkedPageBase 107 } 108 109 // IsEmpty determines whether or not a page of Container contains any results. 110 func (r ContainerPage) IsEmpty() (bool, error) { 111 if r.StatusCode == 204 { 112 return true, nil 113 } 114 115 containers, err := ExtractContainers(r) 116 return len(containers) == 0, err 117 } 118 119 // NextPageURL extracts the "next" link from the links section of the result. 120 func (r ContainerPage) NextPageURL() (string, error) { 121 var s struct { 122 Next string `json:"next"` 123 Previous string `json:"previous"` 124 } 125 err := r.ExtractInto(&s) 126 if err != nil { 127 return "", err 128 } 129 return s.Next, err 130 } 131 132 // ExtractContainers returns a slice of Containers contained in a single page of 133 // results. 134 func ExtractContainers(r pagination.Page) ([]Container, error) { 135 var s struct { 136 Containers []Container `json:"containers"` 137 } 138 err := (r.(ContainerPage)).ExtractInto(&s) 139 return s.Containers, err 140 } 141 142 // Consumer represents a consumer in a container. 143 type Consumer struct { 144 // Created is the date the container was created. 145 Created time.Time `json:"-"` 146 147 // Name is the name of the container. 148 Name string `json:"name"` 149 150 // Status is the status of the container. 151 Status string `json:"status"` 152 153 // Updated is the date the container was updated. 154 Updated time.Time `json:"-"` 155 156 // URL is the url to the consumer. 157 URL string `json:"url"` 158 } 159 160 func (r *Consumer) UnmarshalJSON(b []byte) error { 161 type tmp Consumer 162 var s struct { 163 tmp 164 Created gophercloud.JSONRFC3339NoZ `json:"created"` 165 Updated gophercloud.JSONRFC3339NoZ `json:"updated"` 166 } 167 err := json.Unmarshal(b, &s) 168 if err != nil { 169 return err 170 } 171 *r = Consumer(s.tmp) 172 173 r.Created = time.Time(s.Created) 174 r.Updated = time.Time(s.Updated) 175 176 return nil 177 } 178 179 // CreateConsumerResult is the response from a CreateConsumer operation. 180 // Call its Extract method to interpret it as a container. 181 type CreateConsumerResult struct { 182 // This is not a typo: the API returns a Container, not a Consumer 183 commonResult 184 } 185 186 // DeleteConsumerResult is the response from a DeleteConsumer operation. 187 // Call its Extract to interpret it as a container. 188 type DeleteConsumerResult struct { 189 // This is not a typo: the API returns a Container, not a Consumer 190 commonResult 191 } 192 193 // ConsumerPage is a single page of consumer results. 194 type ConsumerPage struct { 195 pagination.LinkedPageBase 196 } 197 198 // IsEmpty determines whether or not a page of consumers contains any results. 199 func (r ConsumerPage) IsEmpty() (bool, error) { 200 if r.StatusCode == 204 { 201 return true, nil 202 } 203 204 consumers, err := ExtractConsumers(r) 205 return len(consumers) == 0, err 206 } 207 208 // NextPageURL extracts the "next" link from the links section of the result. 209 func (r ConsumerPage) NextPageURL() (string, error) { 210 var s struct { 211 Next string `json:"next"` 212 Previous string `json:"previous"` 213 } 214 err := r.ExtractInto(&s) 215 if err != nil { 216 return "", err 217 } 218 return s.Next, err 219 } 220 221 // ExtractConsumers returns a slice of Consumers contained in a single page of 222 // results. 223 func ExtractConsumers(r pagination.Page) ([]Consumer, error) { 224 var s struct { 225 Consumers []Consumer `json:"consumers"` 226 } 227 err := (r.(ConsumerPage)).ExtractInto(&s) 228 return s.Consumers, err 229 } 230 231 // Extract interprets any CreateSecretRefResult as a Container 232 func (r CreateSecretRefResult) Extract() (*Container, error) { 233 var c *Container 234 err := r.ExtractInto(&c) 235 return c, err 236 } 237 238 // CreateSecretRefResult is the response from a CreateSecretRef operation. 239 // Call its Extract method to interpret it as a container. 240 type CreateSecretRefResult struct { 241 // This is not a typo. 242 commonResult 243 } 244 245 // DeleteSecretRefResult is the response from a DeleteSecretRef operation. 246 type DeleteSecretRefResult struct { 247 gophercloud.ErrResult 248 }