Skip to content

Commit 1220b49

Browse files
committed
Add nsxt_policy_security_policy_container_cluster resource
This resource will bind a container cluster to a security policy. Fixes: #1230 Signed-off-by: Kobi Samoray <[email protected]>
1 parent 7d05deb commit 1220b49

File tree

4 files changed

+408
-0
lines changed

4 files changed

+408
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
subcategory: "Beta"
3+
page_title: "NSXT: nsxt_policy_security_policy_container_cluster"
4+
description: A resource to configure a Security Policy Container Cluster.
5+
---
6+
7+
# nsxt_policy_security_policy_container_cluster
8+
9+
This resource provides a method for the management of Container Clusters associated with security policies.
10+
11+
This resource is applicable to NSX Policy Manager.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "nsxt_policy_container_cluster" "cluster" {
17+
display_name = "containercluster1"
18+
}
19+
20+
resource "nsxt_policy_parent_security_policy" "policy1" {
21+
display_name = "policy1"
22+
category = "Application"
23+
}
24+
25+
resource "nsxt_policy_security_policy_container_cluster" "antreacluster" {
26+
display_name = "cluster1"
27+
description = "Terraform provisioned SecurityPolicyContainerCluster"
28+
policy_path = nsxt_policy_parent_security_policy.policy1.path
29+
container_cluster_path = data.nsxt_policy_container_cluster.cluster.path
30+
}
31+
```
32+
33+
## Argument Reference
34+
35+
The following arguments are supported:
36+
37+
* `display_name` - (Required) Display name of the resource.
38+
* `description` - (Optional) Description of the resource.
39+
* `tag` - (Optional) A list of scope + tag pairs to associate with this resource.
40+
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
41+
* `policy_path` - (Required) The path of the Security Policy which the object belongs to
42+
* `container_cluster_path` - (Required) Path to the container cluster entity in NSX
43+
44+
## Attributes Reference
45+
46+
In addition to arguments listed above, the following attributes are exported:
47+
48+
* `id` - ID of the resource.
49+
* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging.
50+
* `path` - The NSX path of the policy resource.
51+
52+
## Importing
53+
54+
An existing object can be [imported][docs-import] into this resource, via the following command:
55+
56+
[docs-import]: https://www.terraform.io/cli/import
57+
58+
```shell
59+
terraform import nsxt_policy_security_policy_container_cluster.antreacluster PATH
60+
```
61+
62+
The above command imports Security Policy Container Cluster named `antreacluster` with the NSX path `PATH`.

nsxt/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ func Provider() *schema.Provider {
537537
"nsxt_policy_gateway_connection": resourceNsxtPolicyGatewayConnection(),
538538
"nsxt_policy_distributed_vlan_connection": resourceNsxtPolicyDistributedVlanConnection(),
539539
"nsxt_policy_parent_gateway_policy": resourceNsxtPolicyParentGatewayPolicy(),
540+
"nsxt_policy_security_policy_container_cluster": resourceNsxtPolicySecurityPolicyContainerCluster(),
540541
"nsxt_vpc": resourceNsxtVpc(),
541542
"nsxt_vpc_attachment": resourceNsxtVpcAttachment(),
542543
"nsxt_vpc_nat_rule": resourceNsxtPolicyVpcNatRule(),
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package nsxt
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
13+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains/security_policies"
14+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
15+
)
16+
17+
func resourceNsxtPolicySecurityPolicyContainerCluster() *schema.Resource {
18+
return &schema.Resource{
19+
Create: resourceNsxtPolicySecurityPolicyContainerClusterCreate,
20+
Read: resourceNsxtPolicySecurityPolicyContainerClusterRead,
21+
Update: resourceNsxtPolicySecurityPolicyContainerClusterUpdate,
22+
Delete: resourceNsxtPolicySecurityPolicyContainerClusterDelete,
23+
Importer: &schema.ResourceImporter{
24+
State: nsxtSecurityPolicyContainerClusterImporter,
25+
},
26+
Schema: map[string]*schema.Schema{
27+
"nsx_id": getNsxIDSchema(),
28+
"path": getPathSchema(),
29+
"display_name": getDataSourceDisplayNameSchema(),
30+
"description": getDataSourceDescriptionSchema(),
31+
"revision": getRevisionSchema(),
32+
"tag": getTagsSchema(),
33+
"policy_path": getPolicyPathSchema(true, true, "Security Policy path"),
34+
"container_cluster_path": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
Description: "Path to the container cluster entity in NSX",
38+
},
39+
},
40+
}
41+
}
42+
43+
func resourceNsxtPolicySecurityPolicyContainerClusterExistsPartial(d *schema.ResourceData, m interface{}, policyPath string) func(string, client.Connector, bool) (bool, error) {
44+
return func(id string, connector client.Connector, isGlobal bool) (bool, error) {
45+
return resourceNsxtPolicySecurityPolicyContainerClusterExists(id, connector, policyPath)
46+
}
47+
}
48+
49+
func resourceNsxtPolicySecurityPolicyContainerClusterExists(id string, connector client.Connector, policyPath string) (bool, error) {
50+
var err error
51+
52+
client := security_policies.NewContainerClusterSpanClient(connector)
53+
domain := getDomainFromResourcePath(policyPath)
54+
policyID := getPolicyIDFromPath(policyPath)
55+
56+
_, err = client.Get(domain, policyID, id)
57+
if err == nil {
58+
return true, nil
59+
}
60+
61+
if isNotFoundError(err) {
62+
return false, nil
63+
}
64+
65+
return false, logAPIError("Error retrieving resource", err)
66+
}
67+
68+
func resourceNsxtPolicySecurityPolicyContainerClusterCreate(d *schema.ResourceData, m interface{}) error {
69+
connector := getPolicyConnector(m)
70+
client := security_policies.NewContainerClusterSpanClient(connector)
71+
72+
policyPath := d.Get("policy_path").(string)
73+
domain := getDomainFromResourcePath(policyPath)
74+
policyID := getPolicyIDFromPath(policyPath)
75+
76+
id, err := getOrGenerateID(d, m, resourceNsxtPolicySecurityPolicyContainerClusterExistsPartial(d, m, policyPath))
77+
if err != nil {
78+
return err
79+
}
80+
81+
displayName := d.Get("display_name").(string)
82+
description := d.Get("description").(string)
83+
containerClusterPath := d.Get("container_cluster_path").(string)
84+
tags := getPolicyTagsFromSchema(d)
85+
86+
obj := model.SecurityPolicyContainerCluster{
87+
DisplayName: &displayName,
88+
Description: &description,
89+
Tags: tags,
90+
ContainerClusterPath: &containerClusterPath,
91+
}
92+
93+
err = client.Patch(domain, policyID, id, obj)
94+
if err != nil {
95+
return handleCreateError("SecurityPolicyContainerCluster", id, err)
96+
}
97+
98+
d.SetId(id)
99+
d.Set("nsx_id", id)
100+
101+
return resourceNsxtPolicySecurityPolicyContainerClusterRead(d, m)
102+
}
103+
104+
func resourceNsxtPolicySecurityPolicyContainerClusterRead(d *schema.ResourceData, m interface{}) error {
105+
connector := getPolicyConnector(m)
106+
107+
id := d.Id()
108+
if id == "" {
109+
return fmt.Errorf("Error obtaining SecurityPolicyContainerCluster ID")
110+
}
111+
policyPath := d.Get("policy_path").(string)
112+
domain := getDomainFromResourcePath(policyPath)
113+
policyID := getPolicyIDFromPath(policyPath)
114+
115+
client := security_policies.NewContainerClusterSpanClient(connector)
116+
117+
obj, err := client.Get(domain, policyID, id)
118+
if err != nil {
119+
return handleReadError(d, "SecurityPolicyContainerCluster", id, err)
120+
}
121+
122+
setPolicyTagsInSchema(d, obj.Tags)
123+
d.Set("nsx_id", id)
124+
d.Set("display_name", obj.DisplayName)
125+
d.Set("description", obj.Description)
126+
d.Set("revision", obj.Revision)
127+
d.Set("path", obj.Path)
128+
d.Set("container_cluster_path", obj.ContainerClusterPath)
129+
130+
return nil
131+
}
132+
133+
func resourceNsxtPolicySecurityPolicyContainerClusterUpdate(d *schema.ResourceData, m interface{}) error {
134+
connector := getPolicyConnector(m)
135+
136+
id := d.Id()
137+
if id == "" {
138+
return fmt.Errorf("Error obtaining SecurityPolicyContainerCluster ID")
139+
}
140+
141+
policyPath := d.Get("policy_path").(string)
142+
domain := getDomainFromResourcePath(policyPath)
143+
policyID := getPolicyIDFromPath(policyPath)
144+
description := d.Get("description").(string)
145+
displayName := d.Get("display_name").(string)
146+
tags := getPolicyTagsFromSchema(d)
147+
containerClusterPath := d.Get("container_cluster_path").(string)
148+
149+
revision := int64(d.Get("revision").(int))
150+
151+
obj := model.SecurityPolicyContainerCluster{
152+
DisplayName: &displayName,
153+
Description: &description,
154+
Tags: tags,
155+
Revision: &revision,
156+
ContainerClusterPath: &containerClusterPath,
157+
}
158+
159+
client := security_policies.NewContainerClusterSpanClient(connector)
160+
_, err := client.Update(domain, policyID, id, obj)
161+
if err != nil {
162+
return handleUpdateError("SecurityPolicyContainerCluster", id, err)
163+
}
164+
165+
return resourceNsxtPolicySecurityPolicyContainerClusterRead(d, m)
166+
}
167+
168+
func resourceNsxtPolicySecurityPolicyContainerClusterDelete(d *schema.ResourceData, m interface{}) error {
169+
id := d.Id()
170+
if id == "" {
171+
return fmt.Errorf("Error obtaining SecurityPolicyContainerCluster ID")
172+
}
173+
policyPath := d.Get("policy_path").(string)
174+
domain := getDomainFromResourcePath(policyPath)
175+
policyID := getPolicyIDFromPath(policyPath)
176+
177+
connector := getPolicyConnector(m)
178+
179+
client := security_policies.NewContainerClusterSpanClient(connector)
180+
err := client.Delete(domain, policyID, id)
181+
182+
if err != nil {
183+
return handleDeleteError("SecurityPolicyContainerCluster", id, err)
184+
}
185+
186+
return nil
187+
}
188+
189+
func nsxtSecurityPolicyContainerClusterImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
190+
importID := d.Id()
191+
rd, err := nsxtPolicyPathResourceImporterHelper(d, m)
192+
if err != nil {
193+
return rd, err
194+
}
195+
ruleIdx := strings.Index(importID, "container-cluster-span")
196+
if ruleIdx <= 0 {
197+
return nil, fmt.Errorf("invalid path of SecurityPolicyContainerCluster to import")
198+
}
199+
d.Set("policy_path", importID[:ruleIdx-1])
200+
return []*schema.ResourceData{d}, nil
201+
}

0 commit comments

Comments
 (0)