/
datasource_vcd_nsxt_dynamic_security_group.go
119 lines (106 loc) · 3.78 KB
/
datasource_vcd_nsxt_dynamic_security_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package vcd
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
func datasourceVcdDynamicSecurityGroup() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdDynamicSecurityGroupRead,
Schema: map[string]*schema.Schema{
"org": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of organization to use, optional if defined at provider " +
"level. Useful when connected as sysadmin working across different organizations",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Dynamic Security Group name",
},
"vdc_group_id": {
Type: schema.TypeString,
Required: true,
Description: "VDC Group ID in which Dynamic Security Group is located",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Dynamic Security Group description",
},
"criteria": {
Type: schema.TypeSet,
Computed: true,
Description: "Criteria to be used to define the Dynamic Security Group",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rule": {
Type: schema.TypeSet,
Description: "Up to 4 rules can be used to define single criteria",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
Description: "Type of object matching 'VM_TAG' or 'VM_NAME'",
},
"operator": {
Type: schema.TypeString,
Required: true,
Description: "Operator can be one of 'EQUALS', 'CONTAINS', 'STARTS_WITH', 'ENDS_WITH'",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Filter value",
},
},
},
},
},
},
},
"member_vms": {
Type: schema.TypeSet,
Computed: true,
Description: "Set of VM IDs",
Elem: nsxtFirewallGroupMemberVms,
},
},
}
}
func datasourceVcdDynamicSecurityGroupRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
vdcGroupId := d.Get("vdc_group_id").(string)
org, err := vcdClient.GetOrgFromResource(d)
if err != nil {
return diag.Errorf("[nsxt dynamic security group data source read] error retrieving Org: %s", err)
}
vdcGroup, err := org.GetVdcGroupById(vdcGroupId)
if err != nil {
return diag.Errorf("error retrieving VDC Group: %s", err)
}
securityGroup, err := vdcGroup.GetNsxtFirewallGroupByName(d.Get("name").(string), types.FirewallGroupTypeVmCriteria)
if err != nil {
return diag.Errorf("[nsxt dynamic security group data source read] error getting NSX-T dynamic security group: %s", err)
}
err = setNsxtDynamicSecurityGroupData(d, securityGroup.NsxtFirewallGroup)
if err != nil {
return diag.Errorf("[nsxt dynamic security group data source read] error setting NSX-T Dynamic Security Group: %s", err)
}
// A separate GET call is required to get all associated VMs
associatedVms, err := securityGroup.GetAssociatedVms()
if err != nil {
return diag.Errorf("[nsxt dynamic security group data source read] error getting associated VMs for Security Group '%s': %s", securityGroup.NsxtFirewallGroup.Name, err)
}
err = setNsxtSecurityGroupAssociatedVmsData(d, associatedVms)
if err != nil {
return diag.Errorf("[nsxt dynamic security group data source read] error getting associated VMs for Security Group '%s': %s", securityGroup.NsxtFirewallGroup.Name, err)
}
d.SetId(securityGroup.NsxtFirewallGroup.ID)
return nil
}