github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/iec/v1/keypairs/requests.go (about) 1 package keypairs 2 3 import ( 4 "net/http" 5 6 "github.com/huaweicloud/golangsdk" 7 ) 8 9 // CreateOptsBuilder allows extensions to add additional parameters to the 10 // create request. 11 type CreateOptsBuilder interface { 12 ToKeyPairCreateMap() (map[string]interface{}, error) 13 } 14 type CreateOpts struct { 15 // Name is a friendly name to refer to this KeyPair in other services. 16 Name string `json:"name" required:"true"` 17 18 // PublicKey [optional] is a pregenerated OpenSSH-formatted public key. 19 // If provided, this key will be imported and no new key will be created. 20 PublicKey string `json:"public_key,omitempty"` 21 } 22 23 // ToSecurityGroupsCreateMap converts CreateOpts structures to map[string]interface{} 24 func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) { 25 b, err := golangsdk.BuildRequestBody(&opts, "") 26 if err != nil { 27 return nil, err 28 } 29 return b, nil 30 } 31 32 // Create create key pair 33 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 34 b, err := opts.ToKeyPairCreateMap() 35 if err != nil { 36 r.Err = err 37 return 38 } 39 createURL := rootURL(client) 40 41 var resp *http.Response 42 resp, r.Err = client.Post(createURL, b, &r.Body, &golangsdk.RequestOpts{ 43 OkCodes: []int{http.StatusOK, http.StatusCreated}, 44 }) 45 if r.Err != nil { 46 return 47 } 48 defer resp.Body.Close() 49 50 return 51 } 52 53 // Get get key pair detail 54 func Get(client *golangsdk.ServiceClient, keyPairID string) (r GetResult) { 55 getURL := GetURL(client, keyPairID) 56 57 var resp *http.Response 58 resp, r.Err = client.Get(getURL, &r.Body, &golangsdk.RequestOpts{ 59 OkCodes: []int{http.StatusOK}, 60 }) 61 if r.Err != nil { 62 return 63 } 64 defer resp.Body.Close() 65 66 return 67 } 68 69 //Delete delete the key pair 70 func Delete(client *golangsdk.ServiceClient, keyPairID string) (r DeleteResult) { 71 deleteURL := DeleteURL(client, keyPairID) 72 73 var resp *http.Response 74 resp, r.Err = client.Delete(deleteURL, &golangsdk.RequestOpts{ 75 OkCodes: []int{http.StatusNoContent}, 76 }) 77 if r.Err != nil { 78 return 79 } 80 defer resp.Body.Close() 81 82 return 83 } 84 85 type ListOpts struct { 86 Limit int `q:"limit"` 87 Offset int `q:"offset"` 88 Name string `q:"name"` 89 } 90 91 type ListKeyPairsOptsBuilder interface { 92 ToListKeyPairsQuery() (string, error) 93 } 94 95 func (opts ListOpts) ToListKeyPairsQuery() (string, error) { 96 b, err := golangsdk.BuildQueryString(&opts) 97 if err != nil { 98 return "", err 99 } 100 return b.String(), nil 101 } 102 103 func List(client *golangsdk.ServiceClient, opts ListKeyPairsOptsBuilder) (r ListResult) { 104 listKeyPairsURL := rootURL(client) 105 if opts != nil { 106 query, err := opts.ToListKeyPairsQuery() 107 if err != nil { 108 r.Err = err 109 return r 110 } 111 listKeyPairsURL += query 112 } 113 114 _, r.Err = client.Get(listKeyPairsURL, &r.Body, &golangsdk.RequestOpts{ 115 OkCodes: []int{http.StatusOK}, 116 }) 117 return 118 }