github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/keypairs/requests.go (about)

     1  package keypairs
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/servers"
     6  	"github.com/huaweicloud/golangsdk/pagination"
     7  )
     8  
     9  // CreateOptsExt adds a KeyPair option to the base CreateOpts.
    10  type CreateOptsExt struct {
    11  	servers.CreateOptsBuilder
    12  
    13  	// KeyName is the name of the key pair.
    14  	KeyName string `json:"key_name,omitempty"`
    15  }
    16  
    17  // ToServerCreateMap adds the key_name to the base server creation options.
    18  func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
    19  	base, err := opts.CreateOptsBuilder.ToServerCreateMap()
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	if opts.KeyName == "" {
    25  		return base, nil
    26  	}
    27  
    28  	serverMap := base["server"].(map[string]interface{})
    29  	serverMap["key_name"] = opts.KeyName
    30  
    31  	return base, nil
    32  }
    33  
    34  // List returns a Pager that allows you to iterate over a collection of KeyPairs.
    35  func List(client *golangsdk.ServiceClient) pagination.Pager {
    36  	return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
    37  		return KeyPairPage{pagination.SinglePageBase(r)}
    38  	})
    39  }
    40  
    41  // CreateOptsBuilder allows extensions to add additional parameters to the
    42  // Create request.
    43  type CreateOptsBuilder interface {
    44  	ToKeyPairCreateMap() (map[string]interface{}, error)
    45  }
    46  
    47  // CreateOpts specifies KeyPair creation or import parameters.
    48  type CreateOpts struct {
    49  	// Name is a friendly name to refer to this KeyPair in other services.
    50  	Name string `json:"name" required:"true"`
    51  
    52  	// PublicKey [optional] is a pregenerated OpenSSH-formatted public key.
    53  	// If provided, this key will be imported and no new key will be created.
    54  	PublicKey string `json:"public_key,omitempty"`
    55  }
    56  
    57  // ToKeyPairCreateMap constructs a request body from CreateOpts.
    58  func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) {
    59  	return golangsdk.BuildRequestBody(opts, "keypair")
    60  }
    61  
    62  // Create requests the creation of a new KeyPair on the server, or to import a
    63  // pre-existing keypair.
    64  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    65  	b, err := opts.ToKeyPairCreateMap()
    66  	if err != nil {
    67  		r.Err = err
    68  		return
    69  	}
    70  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    71  		OkCodes: []int{200},
    72  	})
    73  	return
    74  }
    75  
    76  // Get returns public data about a previously uploaded KeyPair.
    77  func Get(client *golangsdk.ServiceClient, name string) (r GetResult) {
    78  	_, r.Err = client.Get(getURL(client, name), &r.Body, nil)
    79  	return
    80  }
    81  
    82  // Delete requests the deletion of a previous stored KeyPair from the server.
    83  func Delete(client *golangsdk.ServiceClient, name string) (r DeleteResult) {
    84  	_, r.Err = client.Delete(deleteURL(client, name), nil)
    85  	return
    86  }