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

     1  package containers
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/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  type consumerResult struct {
   180  	gophercloud.Result
   181  }
   182  
   183  // Extract interprets any consumerResult as a Consumer.
   184  func (r consumerResult) Extract() (*Consumer, error) {
   185  	var s *Consumer
   186  	err := r.ExtractInto(&s)
   187  	return s, err
   188  }
   189  
   190  // CreateConsumerResult is the response from a CreateConsumer operation.
   191  // Call its Extract method to interpret it as a container.
   192  type CreateConsumerResult struct {
   193  	// This is not a typo.
   194  	commonResult
   195  }
   196  
   197  // DeleteConsumerResult is the response from a DeleteConsumer operation.
   198  // Call its Extract to interpret it as a container.
   199  type DeleteConsumerResult struct {
   200  	// This is not a typo.
   201  	commonResult
   202  }
   203  
   204  // ConsumerPage is a single page of consumer results.
   205  type ConsumerPage struct {
   206  	pagination.LinkedPageBase
   207  }
   208  
   209  // IsEmpty determines whether or not a page of consumers contains any results.
   210  func (r ConsumerPage) IsEmpty() (bool, error) {
   211  	if r.StatusCode == 204 {
   212  		return true, nil
   213  	}
   214  
   215  	consumers, err := ExtractConsumers(r)
   216  	return len(consumers) == 0, err
   217  }
   218  
   219  // NextPageURL extracts the "next" link from the links section of the result.
   220  func (r ConsumerPage) NextPageURL() (string, error) {
   221  	var s struct {
   222  		Next     string `json:"next"`
   223  		Previous string `json:"previous"`
   224  	}
   225  	err := r.ExtractInto(&s)
   226  	if err != nil {
   227  		return "", err
   228  	}
   229  	return s.Next, err
   230  }
   231  
   232  // ExtractConsumers returns a slice of Consumers contained in a single page of
   233  // results.
   234  func ExtractConsumers(r pagination.Page) ([]Consumer, error) {
   235  	var s struct {
   236  		Consumers []Consumer `json:"consumers"`
   237  	}
   238  	err := (r.(ConsumerPage)).ExtractInto(&s)
   239  	return s.Consumers, err
   240  }
   241  
   242  // Extract interprets any CreateSecretRefResult as a Container
   243  func (r CreateSecretRefResult) Extract() (*Container, error) {
   244  	var c *Container
   245  	err := r.ExtractInto(&c)
   246  	return c, err
   247  }
   248  
   249  // CreateSecretRefResult is the response from a CreateSecretRef operation.
   250  // Call its Extract method to interpret it as a container.
   251  type CreateSecretRefResult struct {
   252  	// This is not a typo.
   253  	commonResult
   254  }
   255  
   256  // DeleteSecretRefResult is the response from a DeleteSecretRef operation.
   257  type DeleteSecretRefResult struct {
   258  	gophercloud.ErrResult
   259  }