github.com/gophercloud/gophercloud@v1.11.0/openstack/keymanager/v1/secrets/results.go (about)

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