github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/kms/v3/keypairs/requests.go (about) 1 package keypairs 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 ) 6 7 // AssociateOpts is the request body of binding an SSH keypair 8 type AssociateOpts struct { 9 // the SSH keypair name 10 Name string `json:"keypair_name" required:"true"` 11 // Information about the VM to which the key pair is to be bound 12 Server EcsServerOpts `json:"server" required:"true"` 13 } 14 15 // DisassociateOpts is the request body of unbinding an SSH keypair 16 type DisassociateOpts struct { 17 // the ID of the VM to which the SSH key pair is to be unbound 18 ServerID string `json:"id" required:"true"` 19 // server authentication object, this parameter is required when the server is poweron 20 Auth *AuthOpts `json:"auth,omitempty"` 21 } 22 23 // EcsServerOpts is object about the VM to which the key pair is to be bound 24 type EcsServerOpts struct { 25 // the ID of the VM to which the SSH key pair is to be bound 26 ID string `json:"id" required:"true"` 27 // server authentication object, this parameter is required when the server is poweron 28 Auth *AuthOpts `json:"auth,omitempty"` 29 // whether to disable SSH login on the VM 30 DisablePassword *bool `json:"disable_password,omitempty"` 31 } 32 33 // AuthOpts is the object about server authentication 34 type AuthOpts struct { 35 // the Authentication type, the value can be password or keypair 36 Type string `json:"type,omitempty"` 37 // If type is set to password, this parameter indicates the password. 38 // If type is set to keypair, this parameter indicates the private key. 39 Key string `json:"key,omitempty"` 40 } 41 42 // Associate is used to bind an SSH key pair to a specified VM 43 func Associate(c *golangsdk.ServiceClient, opts AssociateOpts) (string, error) { 44 b, err := golangsdk.BuildRequestBody(opts, "") 45 if err != nil { 46 return "", err 47 } 48 49 var r golangsdk.Result 50 _, err = c.Post(associateURL(c), b, &r.Body, nil) 51 if err != nil { 52 return "", err 53 } 54 55 var resp TaskResp 56 r.ExtractInto(&resp) 57 return resp.ID, nil 58 } 59 60 // Disassociate is used to unbind an SSH key pair to a specified VM 61 func Disassociate(c *golangsdk.ServiceClient, opts DisassociateOpts) (string, error) { 62 b, err := golangsdk.BuildRequestBody(opts, "server") 63 if err != nil { 64 return "", err 65 } 66 67 var r golangsdk.Result 68 _, err = c.Post(disassociateURL(c), b, &r.Body, nil) 69 if err != nil { 70 return "", err 71 } 72 73 var resp TaskResp 74 r.ExtractInto(&resp) 75 return resp.ID, nil 76 } 77 78 // GetTask retrieves the keypair task with the provided ID. To extract the task object 79 // from the response, call the Extract method on the GetResult. 80 func GetTask(client *golangsdk.ServiceClient, taskID string) (r GetResult) { 81 _, r.Err = client.Get(getTaskURL(client, taskID), &r.Body, nil) 82 return 83 }