Security group
Query the security group list
You can query the security group list with the following code:
1args := &api.ListSecurityGroupArgs{}
2 // Set BCC instance ID for filtering
3args.InstanceId = instanceId
4 // Set VPC instance ID bound to the security group for filtering
5args.VpcId = vpcId
6result, err := client.ListSecurityGroup(args)
7if err != nil {
8 fmt.Println("list all security group failed:", err)
9} else {
10 fmt.Println("list all security group success: ", result)
11}
Create a security group
You can create a security group with the following code:
1args := &api.CreateSecurityGroupArgs{
2 // Set security group name
3 Name: "sdk-create",
4 // Set security group rules
5 Rules: []api.SecurityGroupRuleModel{
6 {
7 // Set security group rule remarks
8 Remark: "Remarks",
9 // Set protocol type
10 Protocol: "tcp",
11 // Set port range, defaulting to 1-65535 when empty. A single port such as 80 can be specified
12 PortRange: "1-65535",
13 // Set ingress/egress, value: ingress/egress
14 Direction: "ingress",
15 // Set source IP address, which cannot be set simultaneously with sourceGroupId
16 SourceIp: "",
17 },
18 },
19}
20result, err := client.CreateSecurityGroup(args)
21if err != nil {
22 fmt.Println("create security group failed:", err)
23} else {
24 fmt.Println("create security group success: ", result)
25}
Rules within the same security group are uniquely indexed by a 6-tuple of remark, protocol, direction, portRange, sourceIp|destIp, and sourceGroupId|destGroupId. Duplicate entries will trigger a 409 error. Value of protocol (tcp|udp|icmp), defaulting to empty, representing all. For detailed descriptions of the API for creating security group rules, refer to the BCC API documentation Create a Security Group.
Delete a security group
You can delete the specified security group with the following code:
1err := client.DeleteSecurityGroup(securityGroupId)
2if err != nil {
3 fmt.Println("delete security group failed:", err)
4} else {
5 fmt.Println("delete security group success")
6}
Authorize security group rules
You can add authorized security group rules to a specified security group with the following code:
1args := &api.AuthorizeSecurityGroupArgs{
2 Rule: &api.SecurityGroupRuleModel{
3 Remark: "Remarks",
4 Protocol: "udp",
5 PortRange: "1-65535",
6 Direction: "ingress",
7 },
8}
9err := client.AuthorizeSecurityGroupRule(securityGroupId, args)
10if err != nil {
11 fmt.Println("authorize security group new rule failed:", err)
12} else {
13 fmt.Println("authorize security group new rule success")
14}
- Rules within the same security group are uniquely identified by a 6-tuple comprising remark, protocol, direction, portRange, sourceIp|destIp, and sourceGroupId|destGroupId. Duplicating entries will result in a 409 error.
- For detailed descriptions of the API, refer to the BCC API documentation Authorize Security Group Rules.
Revoke security group rules
You can revoke authorized security group rules to a specified security group with the following code:
1args := &api.RevokeSecurityGroupArgs{
2 Rule: &api.SecurityGroupRuleModel{
3 Remark: "Remarks",
4 Protocol: "udp",
5 PortRange: "1-65535",
6 Direction: "ingress",
7 SourceIp: "",
8 },
9}
10err := client.RevokeSecurityGroupRule(securityGroupId, args)
11if err != nil {
12 fmt.Println("revoke security group rule failed:", err)
13} else {
14 fmt.Println("revoke security group rule success")
15}
- Rules within the same security group are uniquely identified by a 6-tuple comprising remark, protocol, direction, portRange, sourceIp|destIp, and sourceGroupId|destGroupId. The absence of any rules will result in a 404 error.
- For detailed descriptions of the API, refer to the BCC API documentation Revoke Security Group Rules.
Update regular security group rules
You can update security group rules to a specified regular security group with the following code:
1args := &api.UpdateSecurityGroupRuleArgs{
2 SecurityGroupRuleId: SecurityGroupRuleId,
3 Remark: Remark,
4}
5err := client.UpdateSecurityGroupRule(args)
6if err != nil {
7 fmt.Println("update security group new rule failed:", err)
8} else {
9 fmt.Println("update security group new rule success")
10}
Delete regular security group rules
You can delete security group rules to a specified regular security group with the following code:
1args := &api.DeleteSecurityGroupRuleArgs{
2 SecurityGroupRuleId: SecurityGroupRuleId,
3}
4err := client.DeleteSecurityGroupRule(args)
5if err != nil {
6 fmt.Println("delete security group rule failed:", err)
7} else {
8 fmt.Println("delete security group rule success")
9}
Associate with security group
You can associate with a specified security group with the following code:
1args := &api.BindSgV2Req{
2 InstanceIds: []string{
3 "InstanceIds",
4 },
5 SecurityGroupIds: []string{
6 "SecurityGroupIds",
7 },
8 SecurityGroupType: "SecurityGroupType",
9 }
10res, err := BCC_CLIENT.InstanceBindSecurityGroup(args)
11if err != nil {
12 fmt.Println("bind security group failed:", err)
13} else {
14 fmt.Println("bind security group success")
15}
Unbind security group
You can unbind a specified security group with the following code:
1args := &api.UnbindSgV2Req{
2 InstanceIds: []string{
3 "InstanceIds",
4 },
5 SecurityGroupIds: []string{
6 "SecurityGroupIds",
7 },
8 SecurityGroupType: "SecurityGroupType",
9 }
10 res, err := BCC_CLIENT.InstanceUnbindSecurityGroup(args)
11if err != nil {
12 fmt.Println("unbind security group failed:", err)
13} else {
14 fmt.Println("unbind security group success")
15}
Replace security group
You can replace with a specified security group with the following code:
1args := &api.ReplaceSgV2Req{
2 InstanceIds: []string{
3 "InstanceIds",
4 },
5 SecurityGroupIds: []string{
6 "SecurityGroupIds",
7 },
8 SecurityGroupType: "enterprise",
9 }
10 res, err := BCC_CLIENT.InstanceReplaceSecurityGroup(args)
11if err != nil {
12 fmt.Println("replace security group failed:", err)
13} else {
14 fmt.Println("replace security group success")
15}
