github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/bms/v2/keypairs/results.go (about)

     1  package keypairs
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     6  )
     7  
     8  // KeyPair is an SSH key known to the OpenStack Cloud that is available to be injected into bms servers.
     9  type KeyPair struct {
    10  	// Name is used to refer to this keypair from other services within this region.
    11  	Name string `json:"name"`
    12  	// Fingerprint is a short sequence of bytes that can be used to authenticate or validate a longer public key.
    13  	Fingerprint string `json:"fingerprint"`
    14  	// PublicKey is the public key from this pair, in OpenSSH format. "ssh-rsa AAAAB3Nz..."
    15  	PublicKey string `json:"public_key"`
    16  }
    17  
    18  // KeyPairPage stores a single page of all KeyPair results from a List call.
    19  // Use the ExtractKeyPairs function to convert the results to a slice of KeyPairs.
    20  type KeyPairPage struct {
    21  	pagination.LinkedPageBase
    22  }
    23  
    24  // IsEmpty determines whether a KeyPairPage is empty.
    25  func (page KeyPairPage) IsEmpty() (bool, error) {
    26  	ks, err := ExtractKeyPairs(page)
    27  	return len(ks) == 0, err
    28  }
    29  
    30  // ExtractKeyPairs interprets a page of results as a slice of KeyPairs.
    31  func ExtractKeyPairs(r pagination.Page) ([]KeyPair, error) {
    32  	var res []struct {
    33  		KeyPair KeyPair `json:"keypair"`
    34  	}
    35  
    36  	err := extract.IntoSlicePtr(r.(KeyPairPage).Result.BodyReader(), &res, "keypairs")
    37  	results := make([]KeyPair, len(res))
    38  
    39  	for i, pair := range res {
    40  		results[i] = pair.KeyPair
    41  	}
    42  	return results, err
    43  }