github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/keymanager/v1/secrets/results.go (about) 1 package secrets 2 3 import ( 4 "encoding/json" 5 "io" 6 "time" 7 8 "github.com/vnpaycloud-console/gophercloud/v2" 9 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 10 ) 11 12 // Secret represents a secret stored in the key manager service. 13 type Secret struct { 14 // BitLength is the bit length of the secret. 15 BitLength int `json:"bit_length"` 16 17 // Algorithm is the algorithm type of the secret. 18 Algorithm string `json:"algorithm"` 19 20 // Expiration is the expiration date of the secret. 21 Expiration time.Time `json:"-"` 22 23 // ContentTypes are the content types of the secret. 24 ContentTypes map[string]string `json:"content_types"` 25 26 // Created is the created date of the secret. 27 Created time.Time `json:"-"` 28 29 // CreatorID is the creator of the secret. 30 CreatorID string `json:"creator_id"` 31 32 // Mode is the mode of the secret. 33 Mode string `json:"mode"` 34 35 // Name is the name of the secret. 36 Name string `json:"name"` 37 38 // SecretRef is the URL to the secret. 39 SecretRef string `json:"secret_ref"` 40 41 // SecretType represents the type of secret. 42 SecretType string `json:"secret_type"` 43 44 // Status represents the status of the secret. 45 Status string `json:"status"` 46 47 // Updated is the updated date of the secret. 48 Updated time.Time `json:"-"` 49 } 50 51 func (r *Secret) UnmarshalJSON(b []byte) error { 52 type tmp Secret 53 var s struct { 54 tmp 55 Created gophercloud.JSONRFC3339NoZ `json:"created"` 56 Updated gophercloud.JSONRFC3339NoZ `json:"updated"` 57 Expiration gophercloud.JSONRFC3339NoZ `json:"expiration"` 58 } 59 err := json.Unmarshal(b, &s) 60 if err != nil { 61 return err 62 } 63 *r = Secret(s.tmp) 64 65 r.Created = time.Time(s.Created) 66 r.Updated = time.Time(s.Updated) 67 r.Expiration = time.Time(s.Expiration) 68 69 return nil 70 } 71 72 type commonResult struct { 73 gophercloud.Result 74 } 75 76 // Extract interprets any commonResult as a Secret. 77 func (r commonResult) Extract() (*Secret, error) { 78 var s *Secret 79 err := r.ExtractInto(&s) 80 return s, err 81 } 82 83 // GetResult is the response from a Get operation. Call its Extract method 84 // to interpret it as a secrets. 85 type GetResult struct { 86 commonResult 87 } 88 89 // CreateResult is the response from a Create operation. Call its Extract method 90 // to interpret it as a secrets. 91 type CreateResult struct { 92 commonResult 93 } 94 95 // UpdateResult is the response from an Update operation. Call its ExtractErr 96 // method to determine if the request succeeded or failed. 97 type UpdateResult struct { 98 gophercloud.ErrResult 99 } 100 101 // DeleteResult is the response from a Delete operation. Call its ExtractErr 102 // method to determine if the request succeeded or failed. 103 type DeleteResult struct { 104 gophercloud.ErrResult 105 } 106 107 // PayloadResult is the response from a GetPayload operation. Call its Extract 108 // method to extract the payload as a string. 109 type PayloadResult struct { 110 gophercloud.Result 111 Body io.ReadCloser 112 } 113 114 // Extract is a method that takes a PayloadResult's io.Reader body and reads 115 // all available data into a slice of bytes. Please be aware that its io.Reader 116 // is forward-only - meaning that it can only be read once and not rewound. You 117 // can recreate a reader from the output of this function by using 118 // bytes.NewReader(downloadBytes) 119 func (r PayloadResult) Extract() ([]byte, error) { 120 if r.Err != nil { 121 return nil, r.Err 122 } 123 defer r.Body.Close() 124 body, err := io.ReadAll(r.Body) 125 if err != nil { 126 return nil, err 127 } 128 return body, nil 129 } 130 131 // SecretPage is a single page of secrets results. 132 type SecretPage struct { 133 pagination.LinkedPageBase 134 } 135 136 // IsEmpty determines whether or not a page of secrets contains any results. 137 func (r SecretPage) IsEmpty() (bool, error) { 138 if r.StatusCode == 204 { 139 return true, nil 140 } 141 142 secrets, err := ExtractSecrets(r) 143 return len(secrets) == 0, err 144 } 145 146 // NextPageURL extracts the "next" link from the links section of the result. 147 func (r SecretPage) NextPageURL() (string, error) { 148 var s struct { 149 Next string `json:"next"` 150 Previous string `json:"previous"` 151 } 152 err := r.ExtractInto(&s) 153 if err != nil { 154 return "", err 155 } 156 return s.Next, err 157 } 158 159 // ExtractSecrets returns a slice of Secrets contained in a single page of 160 // results. 161 func ExtractSecrets(r pagination.Page) ([]Secret, error) { 162 var s struct { 163 Secrets []Secret `json:"secrets"` 164 } 165 err := (r.(SecretPage)).ExtractInto(&s) 166 return s.Secrets, err 167 } 168 169 // MetadataResult is the result of a metadata request. Call its Extract method 170 // to interpret it as a map[string]string. 171 type MetadataResult struct { 172 gophercloud.Result 173 } 174 175 // Extract interprets any MetadataResult as map[string]string. 176 func (r MetadataResult) Extract() (map[string]string, error) { 177 var s struct { 178 Metadata map[string]string `json:"metadata"` 179 } 180 err := r.ExtractInto(&s) 181 return s.Metadata, err 182 } 183 184 // MetadataCreateResult is the result of a metadata create request. Call its 185 // Extract method to interpret it as a map[string]string. 186 type MetadataCreateResult struct { 187 gophercloud.Result 188 } 189 190 // Extract interprets any MetadataCreateResult as a map[string]string. 191 func (r MetadataCreateResult) Extract() (map[string]string, error) { 192 var s map[string]string 193 err := r.ExtractInto(&s) 194 return s, err 195 } 196 197 // Metadatum represents an individual metadata. 198 type Metadatum struct { 199 Key string `json:"key"` 200 Value string `json:"value"` 201 } 202 203 // MetadatumResult is the result of a metadatum request. Call its 204 // Extract method to interpret it as a map[string]string. 205 type MetadatumResult struct { 206 gophercloud.Result 207 } 208 209 // Extract interprets any MetadatumResult as a map[string]string. 210 func (r MetadatumResult) Extract() (*Metadatum, error) { 211 var s *Metadatum 212 err := r.ExtractInto(&s) 213 return s, err 214 } 215 216 // MetadatumCreateResult is the response from a metadata Create operation. Call 217 // its ExtractErr method to determine if the request succeeded or failed. 218 // 219 // NOTE: This could be a MetadatumResponse but, at the time of testing, it looks 220 // like Barbican was returning errneous JSON in the response. 221 type MetadatumCreateResult struct { 222 gophercloud.ErrResult 223 } 224 225 // MetadatumDeleteResult is the response from a metadatum Delete operation. Call 226 // its ExtractErr method to determine if the request succeeded or failed. 227 type MetadatumDeleteResult struct { 228 gophercloud.ErrResult 229 }