github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/waf/v1/certificates/results.go (about)

     1  package certificates
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/opentelekomcloud/gophertelekomcloud"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
    10  )
    11  
    12  type Certificate struct {
    13  	// Id of the certificate
    14  	Id string `json:"id"`
    15  	// Name of the certificate
    16  	Name string `json:"name"`
    17  	// ExpireTime - unix timestamp of certificate expiry
    18  	ExpireTime int64 `json:"expireTime"`
    19  
    20  	Timestamp int64 `json:"timestamp"`
    21  }
    22  
    23  type commonResult struct {
    24  	golangsdk.Result
    25  }
    26  
    27  // Extract is a function that accepts a result and extracts a certificate.
    28  func (r commonResult) Extract() (*Certificate, error) {
    29  	var response Certificate
    30  	err := r.ExtractInto(&response)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return &response, err
    35  }
    36  
    37  // CreateResult represents the result of a create operation. Call its Extract
    38  // method to interpret it as a Certificate.
    39  type CreateResult struct {
    40  	commonResult
    41  }
    42  
    43  // UpdateResult represents the result of a update operation. Call its Extract
    44  // method to interpret it as a Certificate.
    45  type UpdateResult struct {
    46  	commonResult
    47  }
    48  
    49  // GetResult represents the result of a get operation. Call its Extract
    50  // method to interpret it as a Certificate.
    51  type GetResult struct {
    52  	commonResult
    53  }
    54  
    55  // DeleteResult represents the result of a delete operation. Call its ExtractErr
    56  // method to determine if the request succeeded or failed.
    57  type DeleteResult struct {
    58  	golangsdk.ErrResult
    59  }
    60  
    61  type CertificatePage struct {
    62  	pagination.OffsetPageBase
    63  }
    64  
    65  // IsEmpty returns true if this Page has no items in it.
    66  func (p CertificatePage) IsEmpty() (bool, error) {
    67  	body, err := p.GetBodyAsMap()
    68  	if err != nil {
    69  		return false, err
    70  	}
    71  
    72  	items, ok := body["items"].([]interface{})
    73  	if !ok {
    74  		return false, fmt.Errorf("map `items` are not a slice: %+v", body)
    75  	}
    76  
    77  	return len(items) == 0, nil
    78  }
    79  
    80  func (p CertificatePage) NextPageURL() (string, error) {
    81  	currentURL := p.URL
    82  	q := currentURL.Query()
    83  	// The default value is 10. If limit is -1, one page with 65535 records is displayed.
    84  	switch q.Get("limit") {
    85  	case "-1":
    86  		return "", nil // in this case is a SinglePageBase
    87  	case "":
    88  		q.Set("limit", "10")
    89  		p.Limit = 10
    90  	}
    91  	// Its value ranges from 0 to 65535. The default value is 0.
    92  	if q.Get("offset") == "" {
    93  		p.Offset = 0
    94  	}
    95  	q.Set("offset", strconv.Itoa(p.LastElement()))
    96  	currentURL.RawQuery = q.Encode()
    97  	return currentURL.String(), nil
    98  }
    99  
   100  func ExtractCertificates(p pagination.Page) ([]Certificate, error) {
   101  	var certs []Certificate
   102  	body := p.(CertificatePage).BodyReader()
   103  	err := extract.IntoSlicePtr(body, &certs, "items")
   104  	return certs, err
   105  }