github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/bms/v2/keypairs/results.go (about)

     1  package keypairs
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // KeyPair is an SSH key known to the OpenStack Cloud that is available to be
     9  // injected into bms servers.
    10  type KeyPair struct {
    11  	// Name is used to refer to this keypair from other services within this
    12  	// region.
    13  	Name string `json:"name"`
    14  
    15  	// Fingerprint is a short sequence of bytes that can be used to authenticate
    16  	// or validate a longer public key.
    17  	Fingerprint string `json:"fingerprint"`
    18  
    19  	// PublicKey is the public key from this pair, in OpenSSH format.
    20  	// "ssh-rsa AAAAB3Nz..."
    21  	PublicKey string `json:"public_key"`
    22  }
    23  
    24  // KeyPairPage stores a single page of all KeyPair results from a List call.
    25  // Use the ExtractKeyPairs function to convert the results to a slice of
    26  // KeyPairs.
    27  type KeyPairPage struct {
    28  	pagination.LinkedPageBase
    29  }
    30  
    31  // IsEmpty determines whether or not a KeyPairPage is empty.
    32  func (page KeyPairPage) IsEmpty() (bool, error) {
    33  	ks, err := ExtractKeyPairs(page)
    34  	return len(ks) == 0, err
    35  }
    36  
    37  // ExtractKeyPairs interprets a page of results as a slice of KeyPairs.
    38  func ExtractKeyPairs(r pagination.Page) ([]KeyPair, error) {
    39  	type pair struct {
    40  		KeyPair KeyPair `json:"keypair"`
    41  	}
    42  	var s struct {
    43  		KeyPairs []pair `json:"keypairs"`
    44  	}
    45  	err := (r.(KeyPairPage)).ExtractInto(&s)
    46  	results := make([]KeyPair, len(s.KeyPairs))
    47  	for i, pair := range s.KeyPairs {
    48  		results[i] = pair.KeyPair
    49  	}
    50  	return results, err
    51  }
    52  
    53  type keyPairResult struct {
    54  	golangsdk.Result
    55  }
    56  
    57  // Extract is a method that attempts to interpret any KeyPair resource response
    58  // as a KeyPair struct.
    59  func (r keyPairResult) Extract() (*KeyPair, error) {
    60  	var s struct {
    61  		KeyPair *KeyPair `json:"keypair"`
    62  	}
    63  	err := r.ExtractInto(&s)
    64  	return s.KeyPair, err
    65  }