Key pair
Create a key pair
You can create a key pair with the following code, which can then be implanted into an instance to enable remote login to BCC instances:
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // Name of key pair to be created
15 name := "bbc"
16 // Description of key pair to be created, optional
17 description := "For creating BCC instances"
18 args := &api.CreateKeypairArgs{
19 Name: name,
20 Description: description,
21 }
22 res, err := bccClient.CreateKeypair(args)
23 fmt.Println(res, err)
24}
Import a key pair
The following code is used for users to import and create key pairs:
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // Name of created key pair
15 name := "keyName"
16 // Description of key pair to be created, optional
17 description := "description"
18 // Public key of key pair to be created
19 publicKey := "ssh-***"
20 args := &api.ImportKeypairArgs{
21 Name: name,
22 Description: description,
23 PublicKey: publicKey,
24 }
25 res, err := bccClient.ImportKeypair(args)
26 fmt.Println(res, err)
27}
Bind a key pair
Use the code provided below to bind a single selected key pair to specific BCC instances (supporting multiple instances). A BCC instance can only be associated with one key pair at a time. If the targeted BCC instance is already bound to a key pair, this action will replace the existing one. This operation is only applicable to Linux-based BCC instances, and the instances must be either running or stopped.
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // Set bound instance ID (supporting multiple)
15 instanceIds := []string{"i-***", "i-***"}
16 // Set bound key pair ID
17 keypairId := "k-***"
18 args := &api.AttackKeypairArgs{
19 InstanceIds: instanceIds,
20 KeypairId: keypairId,
21 }
22 err := bccClient.AttachKeypair(args)
23 fmt.Println(err)
24}
Unbind a key pair
You can unbind selected BCC instances from their respective bound key pairs with the following code. Currently, a BCC instance can only be bound to one key pair. This operation will unbind the selected BCC instances from the key pairs they were bound to through the Baidu AI Cloud’s console. If the virtual machine has no key pair, no action will be taken. This operation is only applicable to BCC instances running on the Linux system, and the selected BCC instances must be in the Running or Stopped status. Notes: 1) The selected BCC instances by the user may not be bound to any key pairs, in which case this operation will have no effect on them. 2) The user may have manually bound a key pair to the selected BCC instances, in which case this operation will have no impact on the manually bound key pairs. 3) If the user has previously bound a key pair to the selected BCC instances through the Baidu AI Cloud’s console and the key pair is in a normal status, this operation will remove the key pair from the BCC instances.
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // List of short IDs of VMs to be unbound
15 instanceIds := []string{"i-***", "i-***"}
16 // ID of key pair to be unbound
17 keypairId := "k-***"
18 args := &api.DetachKeypairArgs{
19 InstanceIds: instanceIds,
20 KeypairId: keypairId,
21 }
22 // Initiate unbinding
23 err := bccClient.DetachKeypair(args)
24 fmt.Println(err)
25}
Delete a key pair
The following code is used to delete key pairs. Key pairs that are already bound to BCC instances cannot be deleted:
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // Set ID of key pair to be deleted
15 keypairId := "k-***"
16 // Construct request body
17 args := &api.DeleteKeypairArgs{
18 KeypairId: keypairId,
19 }
20 err := bccClient.DeleteKeypair(args)
21 fmt.Println(err)
22}
Query key pair details
You can query the detailed information of a single key pair with the following code:
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5)
6func main() {
7 // Set your AK, SK and the endpoint to be accessed
8 ak := "ak"
9 sk := "sk"
10 endpoint := "http://bcc.bj.baidubce.com"
11 // Create a BCC client
12 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
13 // Set queried key pair ID
14 keypairId := "k-***"
15 res, err := bccClient.GetKeypairDetail(keypairId)
16 fmt.Println(res, err)
17}
Query the key pair list
You can query the key pair list with the following code:
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 args := &api.ListKeypairArgs{
15 // Starting position of batch list query, which is a system-generated string
16 Marker: "k-***",
17 // Maximum number of items included per page, maximum 1,000. The default is 1,000
18. MaxKeys: 1,
19 }
20 res, err := bccClient.ListKeypairs(args)
21 fmt.Println(res, err)
22}
Rename a key pair
You can rename a key pair with the following code:
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // Name of new key pair
15 name := "Name3"
16 // ID of key pair to be renamed
17 keypairId := "k-***"
18 args := &api.RenameKeypairArgs{
19 Name: name,
20 KeypairId: keypairId,
21 }
22 err := bccClient.RenameKeypair(args)
23 fmt.Println(err)
24}
Change description of key pair
The following code is used to change the description of a key pair. If the new description provided by the user is empty, the description of the virtual machine being operated will be deleted.
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // Set description of key pair to be updated
15 description := "DESC"
16 // Set ID of key pair whose description is to be updated
17 keypairId := "k-***"
18 args := &api.KeypairUpdateDescArgs{
19 Description: description,
20 KeypairId: keypairId,
21 }
22 // Update description of key pair
23 err := bccClient.UpdateKeypairDescription(args)
24 fmt.Println(err)
25}
