github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/keypairs/results.go (about)

     1  package keypairs
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // KeyPair is an SSH key known to the OpenStack Cloud that is available to be
     9  // injected into 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  	// PrivateKey is the private key from this pair, in PEM format.
    24  	// "-----BEGIN RSA PRIVATE KEY-----\nMIICXA..."
    25  	// It is only present if this KeyPair was just returned from a Create call.
    26  	PrivateKey string `json:"private_key"`
    27  
    28  	// UserID is the user who owns this KeyPair.
    29  	UserID string `json:"user_id"`
    30  
    31  	// The type of the keypair
    32  	Type string `json:"type"`
    33  }
    34  
    35  // KeyPairPage stores a single page of all KeyPair results from a List call.
    36  // Use the ExtractKeyPairs function to convert the results to a slice of
    37  // KeyPairs.
    38  type KeyPairPage struct {
    39  	pagination.SinglePageBase
    40  }
    41  
    42  // IsEmpty determines whether or not a KeyPairPage is empty.
    43  func (page KeyPairPage) IsEmpty() (bool, error) {
    44  	if page.StatusCode == 204 {
    45  		return true, nil
    46  	}
    47  
    48  	ks, err := ExtractKeyPairs(page)
    49  	return len(ks) == 0, err
    50  }
    51  
    52  // ExtractKeyPairs interprets a page of results as a slice of KeyPairs.
    53  func ExtractKeyPairs(r pagination.Page) ([]KeyPair, error) {
    54  	type pair struct {
    55  		KeyPair KeyPair `json:"keypair"`
    56  	}
    57  	var s struct {
    58  		KeyPairs []pair `json:"keypairs"`
    59  	}
    60  	err := (r.(KeyPairPage)).ExtractInto(&s)
    61  	results := make([]KeyPair, len(s.KeyPairs))
    62  	for i, pair := range s.KeyPairs {
    63  		results[i] = pair.KeyPair
    64  	}
    65  	return results, err
    66  }
    67  
    68  type keyPairResult struct {
    69  	gophercloud.Result
    70  }
    71  
    72  // Extract is a method that attempts to interpret any KeyPair resource response
    73  // as a KeyPair struct.
    74  func (r keyPairResult) Extract() (*KeyPair, error) {
    75  	var s struct {
    76  		KeyPair *KeyPair `json:"keypair"`
    77  	}
    78  	err := r.ExtractInto(&s)
    79  	return s.KeyPair, err
    80  }
    81  
    82  // CreateResult is the response from a Create operation. Call its Extract method
    83  // to interpret it as a KeyPair.
    84  type CreateResult struct {
    85  	keyPairResult
    86  }
    87  
    88  // GetResult is the response from a Get operation. Call its Extract method to
    89  // interpret it as a KeyPair.
    90  type GetResult struct {
    91  	keyPairResult
    92  }
    93  
    94  // DeleteResult is the response from a Delete operation. Call its ExtractErr
    95  // method to determine if the call succeeded or failed.
    96  type DeleteResult struct {
    97  	gophercloud.ErrResult
    98  }