Skip to content

Commit 7a8afd3

Browse files
committed
feat(security-group):ZSTAC-76758. add data source and resource for security group attachment
- Added `data_source_zstack_networking_secgroup` to support querying ZStack security groups via Terraform - Implemented `resource_zstack_networking_secgroup_attachment` to allow attaching VM NICs to security groups
1 parent e9ed9f2 commit 7a8afd3

19 files changed

+893
-4
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
page_title: "zstack_networking_secgroups Data Source - terraform-provider-zstack"
3+
subcategory: ""
4+
description: |-
5+
Query ZStack Security Groups by name, name pattern, or additional filters.
6+
---
7+
8+
# zstack_networking_secgroups (Data Source)
9+
10+
Query ZStack Security Groups by name, name pattern, or additional filters.
11+
12+
## Example Usage
13+
14+
```terraform
15+
data "zstack_networking_secgroups" "test" {
16+
#name = "p1"
17+
name_pattern = "p%"
18+
filter {
19+
name = "state"
20+
values = ["Enabled"]
21+
}
22+
}
23+
24+
output "zstack_secs" {
25+
value = data.zstack_networking_secgroups.test
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Optional
33+
34+
- `filter` (Block List) Filter results by fields in the security group, such as state or IP version. (see [below for nested schema](#nestedblock--filter))
35+
- `name` (String) Exact name for querying a security group.
36+
- `name_pattern` (String) Pattern for fuzzy matching security group names. Use % or _ like SQL.
37+
38+
### Read-Only
39+
40+
- `networking_secgroups` (Attributes List) List of matched security groups. (see [below for nested schema](#nestedatt--networking_secgroups))
41+
42+
<a id="nestedblock--filter"></a>
43+
### Nested Schema for `filter`
44+
45+
Required:
46+
47+
- `name` (String) Name of the field to filter by.
48+
- `values` (Set of String) List of values to match. Treated as OR conditions.
49+
50+
51+
<a id="nestedatt--networking_secgroups"></a>
52+
### Nested Schema for `networking_secgroups`
53+
54+
Read-Only:
55+
56+
- `attached_l3network_uuids` (Set of String) Set of L3 network UUIDs attached to the security group.
57+
- `description` (String) Description of the security group.
58+
- `name` (String) Name of the security group.
59+
- `rules` (Attributes Set) List of security group rules. (see [below for nested schema](#nestedatt--networking_secgroups--rules))
60+
- `state` (String) State of the security group (Enabled, Disabled).
61+
- `uuid` (String) UUID of the security group.
62+
63+
<a id="nestedatt--networking_secgroups--rules"></a>
64+
### Nested Schema for `networking_secgroups.rules`
65+
66+
Read-Only:
67+
68+
- `allowed_cidr` (String) CIDR allowed by this rule.
69+
- `end_port` (Number) End port for TCP/UDP or ICMP code.
70+
- `ip_version` (String) IP version (IPv4 or IPv6).
71+
- `protocol` (String) Protocol of the rule (TCP, UDP, ICMP, ALL).
72+
- `start_port` (Number) Start port for TCP/UDP or ICMP type.
73+
- `state` (String) State of the rule (Enabled, Disabled).
74+
- `type` (String) Direction of traffic (Ingress or Egress).
75+
- `uuid` (String) UUID of the rule.
76+
77+
78+
79+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
page_title: "zstack_networking_secgroup_attachment Resource - terraform-provider-zstack"
3+
subcategory: ""
4+
description: |-
5+
Attach VM instance NICs to security groups in ZStack.
6+
---
7+
8+
# zstack_networking_secgroup_attachment (Resource)
9+
10+
Attach VM instance NICs to security groups in ZStack.
11+
12+
## Example Usage
13+
14+
```terraform
15+
resource "zstack_networking_secgroup_attachment" "example" {
16+
secgroup_uuid = "f450b20497c34397977091bc1c8f87f9"
17+
nic_uuid = "a8aa88c413704717b138190832864b54"
18+
}
19+
20+
output "secgroup_attachment" {
21+
value = zstack_networking_secgroup_attachment.example
22+
}
23+
```
24+
25+
<!-- schema generated by tfplugindocs -->
26+
## Schema
27+
28+
### Required
29+
30+
- `nic_uuid` (String) The UUID of the vm instance NIC.
31+
- `secgroup_uuid` (String) The UUID of the security group.
32+
33+
### Read-Only
34+
35+
- `id` (String) Terraform resource ID.
36+
37+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
data "zstack_networking_secgroups" "test" {
2+
#name = "p1"
3+
name_pattern = "p%"
4+
filter {
5+
name = "state"
6+
values = ["Enabled"]
7+
}
8+
}
9+
10+
output "zstack_secs" {
11+
value = data.zstack_networking_secgroups.test
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "zstack_networking_secgroup_attachment" "example" {
2+
secgroup_uuid = "f450b20497c34397977091bc1c8f87f9"
3+
nic_uuid = "a8aa88c413704717b138190832864b54"
4+
}
5+
6+
output "secgroup_attachment" {
7+
value = zstack_networking_secgroup_attachment.example
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}"
3+
subcategory: ""
4+
description: |-
5+
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
6+
---
7+
8+
# {{.Name}} ({{.Type}})
9+
10+
{{ .Description }}
11+
12+
## Example Usage
13+
14+
{{tffile "examples/data-sources/networking_secgroups/data-source.tf"}}
15+
16+
{{ .SchemaMarkdown }}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}"
3+
subcategory: ""
4+
description: |-
5+
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
6+
---
7+
8+
# {{.Name}} ({{.Type}})
9+
10+
{{ .Description }}
11+
12+
## Example Usage
13+
14+
{{tffile "examples/resources/networking_secgroup_attachment/resource.tf"}}
15+
16+
{{ .SchemaMarkdown }}

zstack-sdk-go/pkg/client/network_service_actions.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
package client
44

55
import (
6+
"fmt"
7+
"strings"
8+
69
"zstack.io/zstack-sdk-go/pkg/param"
710
"zstack.io/zstack-sdk-go/pkg/view"
811
)
@@ -17,3 +20,50 @@ func (cli *ZSClient) QueryNetworkServiceProvider(params param.QueryParam) ([]vie
1720
func (cli *ZSClient) AttachNetworkServiceToL3Network(l3NetworkUuid string, p param.AttachNetworkServiceToL3NetworkParam) error {
1821
return cli.Post("v1/l3-networks/"+l3NetworkUuid+"/network-services", p, nil)
1922
}
23+
24+
// QurySecurityGroup
25+
func (cli *ZSClient) QuerySecurityGroup(params param.QueryParam) ([]view.SecurityGroupInventoryView, error) {
26+
var resp []view.SecurityGroupInventoryView
27+
return resp, cli.List("v1/security-groups", &params, &resp)
28+
}
29+
30+
// GetSecurityGroup Get security group by UUID
31+
func (cli *ZSClient) GetSecurityGroup(uuid string) ([]view.SecurityGroupInventoryView, error) {
32+
var resp []view.SecurityGroupInventoryView
33+
if err := cli.GetWithSpec("v1/security-groups", uuid, "", responseKeyInventories, nil, &resp); err != nil {
34+
return nil, err
35+
}
36+
if len(resp) == 0 {
37+
return nil, fmt.Errorf("security group with UUID %s not found", uuid)
38+
}
39+
return resp, nil
40+
}
41+
42+
// AddVmNicToSecurityGroup Add VM NIC to security group TODO
43+
func (cli *ZSClient) AddVmNicToSecurityGroup(securityGroupUuid string, p param.AddVmNicToSecurityGroupParam) error {
44+
return cli.Post("v1/security-groups/"+securityGroupUuid+"/vm-instances/nics", p, nil)
45+
}
46+
47+
// GetCandidateVmNicForSecurityGroup Get candidate VM NICs for security group
48+
func (cli *ZSClient) GetCandidateVmNicForSecurityGroup(securityGroupUuid string) ([]view.VmNicInventoryView, error) {
49+
var resp []view.VmNicInventoryView
50+
if err := cli.GetWithSpec("v1/security-groups", securityGroupUuid, "/vm-instances/candidate-nics", responseKeyInventories, nil, &resp); err != nil {
51+
return nil, err
52+
}
53+
return resp, nil
54+
}
55+
56+
// DeleteVmNicFromSecurityGroup Delete VM NIC from security group
57+
func (cli *ZSClient) DeleteVmNicFromSecurityGroup(securityGroupUuid string, vmNicUuids []string) error {
58+
var uuidsStr []string
59+
for _, uuid := range vmNicUuids {
60+
uuidsStr = append(uuidsStr, fmt.Sprintf("vmNicUuids=%s", uuid))
61+
}
62+
uuidsQueryString := strings.Join(uuidsStr, "&")
63+
64+
if err := cli.DeleteWithSpec("v1/security-groups", securityGroupUuid, "vm-instances/nics", uuidsQueryString, nil); err != nil {
65+
return err
66+
}
67+
return nil
68+
69+
}

zstack-sdk-go/pkg/param/network_service.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ type AttachNetworkServiceToL3NetworkParam struct {
1010
type AttachNetworkServiceToL3NetworkDetailParam struct {
1111
NetworkServices map[string][]string `json:"networkServices"`
1212
}
13+
14+
type AddVmNicToSecurityGroupParam struct {
15+
BaseParam
16+
Params AddVmNicToSecurityGroupDetailParam `json:"params"`
17+
}
18+
19+
type AddVmNicToSecurityGroupDetailParam struct {
20+
// SecurityGroupUuid string `json:"securityGroupUuid"`
21+
VmNicUuids []string `json:"vmNicUuids"`
22+
}

zstack-sdk-go/pkg/test1/base_actions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515
// The ZStack Cloud Basic Edition supports login authentication for AccessKey, super admin, and sub-accounts.
1616
// The ZStack Cloud Enterprise Edition supports login authentication for AccessKey, super admin, sub-accounts, and enterprise users.
1717

18-
accountLoginHostname = "172.30.3.3" //ZStack Cloud API endpoint IP address
18+
accountLoginHostname = "172.30.3.2" //ZStack Cloud API endpoint IP address
1919
accountLoginAccountName = "admin"
2020
accountLoginAccountPassword = "password"
2121

zstack-sdk-go/pkg/test1/network_service_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package test
55
import (
66
"testing"
77

8+
"github.com/kataras/golog"
89
"zstack.io/zstack-sdk-go/pkg/param"
10+
"zstack.io/zstack-sdk-go/pkg/util/jsonutils"
911
)
1012

1113
func TestQueryNetworkServiceProvider(t *testing.T) {
@@ -55,3 +57,53 @@ func TestAttachNetworkServiceToL3Network(t *testing.T) {
5557
return
5658
}
5759
}
60+
61+
func TestQuerySecurityGroup(t *testing.T) {
62+
data, err := accountLoginCli.QuerySecurityGroup(param.NewQueryParam())
63+
if err != nil {
64+
t.Error(err)
65+
return
66+
}
67+
68+
golog.Info(jsonutils.Marshal(data))
69+
70+
}
71+
72+
func TestGetSecurityGroup(t *testing.T) {
73+
data, err := accountLoginCli.GetSecurityGroup("f450b20497c34397977091bc1c8f87f9")
74+
if err != nil {
75+
t.Error(err)
76+
return
77+
}
78+
golog.Info(jsonutils.Marshal(data))
79+
}
80+
81+
func TestGetCandidateVmNicForSecurityGroup(t *testing.T) {
82+
data, err := accountLoginCli.GetCandidateVmNicForSecurityGroup("f450b20497c34397977091bc1c8f87f9")
83+
if err != nil {
84+
t.Error(err)
85+
return
86+
}
87+
golog.Info(jsonutils.Marshal(data))
88+
}
89+
90+
func TestAddVmNicToSecurityGroup(t *testing.T) {
91+
err := accountLoginCli.AddVmNicToSecurityGroup("f450b20497c34397977091bc1c8f87f9", param.AddVmNicToSecurityGroupParam{
92+
BaseParam: param.BaseParam{},
93+
Params: param.AddVmNicToSecurityGroupDetailParam{
94+
VmNicUuids: []string{"20ff9a2ba9ca4209a361c1ee52ff1b0f", "a8aa88c413704717b138190832864b54"},
95+
},
96+
})
97+
if err != nil {
98+
t.Error(err)
99+
return
100+
}
101+
}
102+
103+
func TestDeleteVmNicFromSecurityGroup(t *testing.T) {
104+
err := accountLoginCli.DeleteVmNicFromSecurityGroup("f450b20497c34397977091bc1c8f87f9", []string{"20ff9a2ba9ca4209a361c1ee52ff1b0f", "a8aa88c413704717b138190832864b54"})
105+
if err != nil {
106+
t.Error(err)
107+
return
108+
}
109+
}

0 commit comments

Comments
 (0)