github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/keypairs/requests.go (about) 1 package keypairs 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/openstack/compute/v2/servers" 8 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 9 ) 10 11 // CreateOptsExt adds a KeyPair option to the base CreateOpts. 12 type CreateOptsExt struct { 13 servers.CreateOptsBuilder 14 15 // KeyName is the name of the key pair. 16 KeyName string `json:"key_name,omitempty"` 17 } 18 19 // ToServerCreateMap adds the key_name to the base server creation options. 20 func (opts CreateOptsExt) ToServerCreateMap() (map[string]any, error) { 21 base, err := opts.CreateOptsBuilder.ToServerCreateMap() 22 if err != nil { 23 return nil, err 24 } 25 26 if opts.KeyName == "" { 27 return base, nil 28 } 29 30 serverMap := base["server"].(map[string]any) 31 serverMap["key_name"] = opts.KeyName 32 33 return base, nil 34 } 35 36 // ListOptsBuilder allows extensions to add additional parameters to the 37 // List request. 38 type ListOptsBuilder interface { 39 ToKeyPairListQuery() (string, error) 40 } 41 42 // ListOpts enables listing KeyPairs based on specific attributes. 43 type ListOpts struct { 44 // UserID is the user ID that owns the key pair. 45 // This requires microversion 2.10 or higher. 46 UserID string `q:"user_id"` 47 } 48 49 // ToKeyPairListQuery formats a ListOpts into a query string. 50 func (opts ListOpts) ToKeyPairListQuery() (string, error) { 51 q, err := gophercloud.BuildQueryString(opts) 52 return q.String(), err 53 } 54 55 // List returns a Pager that allows you to iterate over a collection of KeyPairs. 56 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 57 url := listURL(client) 58 if opts != nil { 59 query, err := opts.ToKeyPairListQuery() 60 if err != nil { 61 return pagination.Pager{Err: err} 62 } 63 url += query 64 } 65 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 66 return KeyPairPage{pagination.SinglePageBase(r)} 67 }) 68 } 69 70 // CreateOptsBuilder allows extensions to add additional parameters to the 71 // Create request. 72 type CreateOptsBuilder interface { 73 ToKeyPairCreateMap() (map[string]any, error) 74 } 75 76 // CreateOpts specifies KeyPair creation or import parameters. 77 type CreateOpts struct { 78 // Name is a friendly name to refer to this KeyPair in other services. 79 Name string `json:"name" required:"true"` 80 81 // UserID [optional] is the user_id for a keypair. 82 // This allows administrative users to upload keys for other users than themselves. 83 // This requires microversion 2.10 or higher. 84 UserID string `json:"user_id,omitempty"` 85 86 // The type of the keypair. Allowed values are ssh or x509 87 // This requires microversion 2.2 or higher. 88 Type string `json:"type,omitempty"` 89 90 // PublicKey [optional] is a pregenerated OpenSSH-formatted public key. 91 // If provided, this key will be imported and no new key will be created. 92 PublicKey string `json:"public_key,omitempty"` 93 } 94 95 // ToKeyPairCreateMap constructs a request body from CreateOpts. 96 func (opts CreateOpts) ToKeyPairCreateMap() (map[string]any, error) { 97 return gophercloud.BuildRequestBody(opts, "keypair") 98 } 99 100 // Create requests the creation of a new KeyPair on the server, or to import a 101 // pre-existing keypair. 102 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 103 b, err := opts.ToKeyPairCreateMap() 104 if err != nil { 105 r.Err = err 106 return 107 } 108 resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 109 OkCodes: []int{200, 201}, 110 }) 111 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 112 return 113 } 114 115 // GetOptsBuilder allows extensions to add additional parameters to the 116 // Get request. 117 type GetOptsBuilder interface { 118 ToKeyPairGetQuery() (string, error) 119 } 120 121 // GetOpts enables retrieving KeyPairs based on specific attributes. 122 type GetOpts struct { 123 // UserID is the user ID that owns the key pair. 124 // This requires microversion 2.10 or higher. 125 UserID string `q:"user_id"` 126 } 127 128 // ToKeyPairGetQuery formats a GetOpts into a query string. 129 func (opts GetOpts) ToKeyPairGetQuery() (string, error) { 130 q, err := gophercloud.BuildQueryString(opts) 131 return q.String(), err 132 } 133 134 // Get returns public data about a previously uploaded KeyPair. 135 func Get(ctx context.Context, client *gophercloud.ServiceClient, name string, opts GetOptsBuilder) (r GetResult) { 136 url := getURL(client, name) 137 if opts != nil { 138 query, err := opts.ToKeyPairGetQuery() 139 if err != nil { 140 r.Err = err 141 return 142 } 143 url += query 144 } 145 146 resp, err := client.Get(ctx, url, &r.Body, nil) 147 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 148 return 149 } 150 151 // DeleteOptsBuilder allows extensions to add additional parameters to the 152 // Delete request. 153 type DeleteOptsBuilder interface { 154 ToKeyPairDeleteQuery() (string, error) 155 } 156 157 // DeleteOpts enables deleting KeyPairs based on specific attributes. 158 type DeleteOpts struct { 159 // UserID is the user ID of the user that owns the key pair. 160 // This requires microversion 2.10 or higher. 161 UserID string `q:"user_id"` 162 } 163 164 // ToKeyPairDeleteQuery formats a DeleteOpts into a query string. 165 func (opts DeleteOpts) ToKeyPairDeleteQuery() (string, error) { 166 q, err := gophercloud.BuildQueryString(opts) 167 return q.String(), err 168 } 169 170 // Delete requests the deletion of a previous stored KeyPair from the server. 171 func Delete(ctx context.Context, client *gophercloud.ServiceClient, name string, opts DeleteOptsBuilder) (r DeleteResult) { 172 url := deleteURL(client, name) 173 if opts != nil { 174 query, err := opts.ToKeyPairDeleteQuery() 175 if err != nil { 176 r.Err = err 177 return 178 } 179 url += query 180 } 181 182 resp, err := client.Delete(ctx, url, nil) 183 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 184 return 185 }