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

     1  package orders
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Order represents an order in the key manager service.
    12  type Order struct {
    13  	// ContainerRef is the container URL.
    14  	ContainerRef string `json:"container_ref"`
    15  
    16  	// Created is when the order was created.
    17  	Created time.Time `json:"-"`
    18  
    19  	// CreatorID is the creator of the order.
    20  	CreatorID string `json:"creator_id"`
    21  
    22  	// ErrorReason is the reason of the error.
    23  	ErrorReason string `json:"error_reason"`
    24  
    25  	// ErrorStatusCode is the error status code.
    26  	ErrorStatusCode string `json:"error_status_code"`
    27  
    28  	// OrderRef is the order URL.
    29  	OrderRef string `json:"order_ref"`
    30  
    31  	// Meta is secret data about the order.
    32  	Meta Meta `json:"meta"`
    33  
    34  	// SecretRef is the secret URL.
    35  	SecretRef string `json:"secret_ref"`
    36  
    37  	// Status is the status of the order.
    38  	Status string `json:"status"`
    39  
    40  	// SubStatus is the status of the order.
    41  	SubStatus string `json:"sub_status"`
    42  
    43  	// SubStatusMessage is the message of the sub status.
    44  	SubStatusMessage string `json:"sub_status_message"`
    45  
    46  	// Type is the order type.
    47  	Type string `json:"type"`
    48  
    49  	// Updated is when the order was updated.
    50  	Updated time.Time `json:"-"`
    51  }
    52  
    53  func (r *Order) UnmarshalJSON(b []byte) error {
    54  	type tmp Order
    55  	var s struct {
    56  		tmp
    57  		Created gophercloud.JSONRFC3339NoZ `json:"created"`
    58  		Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
    59  	}
    60  	err := json.Unmarshal(b, &s)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	*r = Order(s.tmp)
    65  
    66  	r.Created = time.Time(s.Created)
    67  	r.Updated = time.Time(s.Updated)
    68  
    69  	return nil
    70  }
    71  
    72  type Meta struct {
    73  	// Algorithm is the algorithm of the secret.
    74  	Algorithm string `json:"algorithm"`
    75  
    76  	// BitLength is the bit length of the secret.
    77  	BitLength int `json:"bit_length"`
    78  
    79  	// Expiration is the expiration date of the order.
    80  	Expiration time.Time `json:"-"`
    81  
    82  	// Mode is the mode of the secret.
    83  	Mode string `json:"mode"`
    84  
    85  	// Name is the name of the secret.
    86  	Name string `json:"name"`
    87  
    88  	// PayloadContentType is the content type of the secret payload.
    89  	PayloadContentType string `json:"payload_content_type"`
    90  }
    91  
    92  func (r *Meta) UnmarshalJSON(b []byte) error {
    93  	type tmp Meta
    94  	var s struct {
    95  		tmp
    96  		Expiration gophercloud.JSONRFC3339NoZ `json:"expiration"`
    97  	}
    98  	err := json.Unmarshal(b, &s)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	*r = Meta(s.tmp)
   103  
   104  	r.Expiration = time.Time(s.Expiration)
   105  
   106  	return nil
   107  }
   108  
   109  type commonResult struct {
   110  	gophercloud.Result
   111  }
   112  
   113  // GetResult is the response from a Get operation. Call its Extract method
   114  // to interpret it as a orders.
   115  type GetResult struct {
   116  	commonResult
   117  }
   118  
   119  // CreateResult is the response from a Create operation. Call its Extract method
   120  // to interpret it as a orders.
   121  type CreateResult struct {
   122  	commonResult
   123  }
   124  
   125  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
   126  // determine if the request succeeded or failed.
   127  type DeleteResult struct {
   128  	gophercloud.ErrResult
   129  }
   130  
   131  // OrderPage is a single page of orders results.
   132  type OrderPage struct {
   133  	pagination.LinkedPageBase
   134  }
   135  
   136  // IsEmpty determines whether or not a page of ordersS contains any results.
   137  func (r OrderPage) IsEmpty() (bool, error) {
   138  	if r.StatusCode == 204 {
   139  		return true, nil
   140  	}
   141  
   142  	orders, err := ExtractOrders(r)
   143  	return len(orders) == 0, err
   144  }
   145  
   146  // NextPageURL extracts the "next" link from the links section of the result.
   147  func (r OrderPage) 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  // ExtractOrders returns a slice of Orders contained in a single page of
   160  // results.
   161  func ExtractOrders(r pagination.Page) ([]Order, error) {
   162  	var s struct {
   163  		Orders []Order `json:"orders"`
   164  	}
   165  	err := (r.(OrderPage)).ExtractInto(&s)
   166  	return s.Orders, err
   167  }
   168  
   169  // Extract interprets any commonResult as a Order.
   170  func (r commonResult) Extract() (*Order, error) {
   171  	var s *Order
   172  	err := r.ExtractInto(&s)
   173  	return s, err
   174  }