Skip to content

Commit 7d957c1

Browse files
committed
Support constraint resource
Signed-off-by: Anna Khmelnitsky <[email protected]>
1 parent c2dd8dc commit 7d957c1

10 files changed

+888
-10
lines changed

api/infra/constraint.go

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//nolint:revive
2+
package infra
3+
4+
// The following file has been autogenerated. Please avoid any changes!
5+
import (
6+
"errors"
7+
8+
vapiProtocolClient_ "github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
9+
client0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
10+
model0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
11+
client1 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra"
12+
13+
utl "github.com/vmware/terraform-provider-nsxt/api/utl"
14+
)
15+
16+
type ConstraintClientContext utl.ClientContext
17+
18+
func NewConstraintsClient(sessionContext utl.SessionContext, connector vapiProtocolClient_.Connector) *ConstraintClientContext {
19+
var client interface{}
20+
21+
switch sessionContext.ClientType {
22+
23+
case utl.Local:
24+
client = client0.NewConstraintsClient(connector)
25+
26+
case utl.Multitenancy:
27+
client = client1.NewConstraintsClient(connector)
28+
29+
default:
30+
return nil
31+
}
32+
return &ConstraintClientContext{Client: client, ClientType: sessionContext.ClientType, ProjectID: sessionContext.ProjectID, VPCID: sessionContext.VPCID}
33+
}
34+
35+
func (c ConstraintClientContext) Get(constraintIdParam string) (model0.Constraint, error) {
36+
var obj model0.Constraint
37+
var err error
38+
39+
switch c.ClientType {
40+
41+
case utl.Local:
42+
client := c.Client.(client0.ConstraintsClient)
43+
obj, err = client.Get(constraintIdParam)
44+
if err != nil {
45+
return obj, err
46+
}
47+
48+
case utl.Multitenancy:
49+
client := c.Client.(client1.ConstraintsClient)
50+
obj, err = client.Get(utl.DefaultOrgID, c.ProjectID, constraintIdParam)
51+
if err != nil {
52+
return obj, err
53+
}
54+
55+
default:
56+
return obj, errors.New("invalid infrastructure for model")
57+
}
58+
return obj, err
59+
}
60+
61+
func (c ConstraintClientContext) Patch(constraintIdParam string, constraintParam model0.Constraint) error {
62+
var err error
63+
64+
switch c.ClientType {
65+
66+
case utl.Local:
67+
client := c.Client.(client0.ConstraintsClient)
68+
err = client.Patch(constraintIdParam, constraintParam)
69+
70+
case utl.Multitenancy:
71+
client := c.Client.(client1.ConstraintsClient)
72+
err = client.Patch(utl.DefaultOrgID, c.ProjectID, constraintIdParam, constraintParam)
73+
74+
default:
75+
err = errors.New("invalid infrastructure for model")
76+
}
77+
return err
78+
}
79+
80+
func (c ConstraintClientContext) Update(constraintIdParam string, constraintParam model0.Constraint) (model0.Constraint, error) {
81+
var err error
82+
var obj model0.Constraint
83+
84+
switch c.ClientType {
85+
86+
case utl.Local:
87+
client := c.Client.(client0.ConstraintsClient)
88+
obj, err = client.Update(constraintIdParam, constraintParam)
89+
90+
case utl.Multitenancy:
91+
client := c.Client.(client1.ConstraintsClient)
92+
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, constraintIdParam, constraintParam)
93+
94+
default:
95+
err = errors.New("invalid infrastructure for model")
96+
}
97+
return obj, err
98+
}
99+
100+
func (c ConstraintClientContext) Delete(constraintIdParam string) error {
101+
var err error
102+
103+
switch c.ClientType {
104+
105+
case utl.Local:
106+
client := c.Client.(client0.ConstraintsClient)
107+
err = client.Delete(constraintIdParam)
108+
109+
case utl.Multitenancy:
110+
client := c.Client.(client1.ConstraintsClient)
111+
err = client.Delete(utl.DefaultOrgID, c.ProjectID, constraintIdParam)
112+
113+
default:
114+
err = errors.New("invalid infrastructure for model")
115+
}
116+
return err
117+
}
118+
119+
func (c ConstraintClientContext) List(cursorParam *string, includeMarkForDeleteObjectsParam *bool, includedFieldsParam *string, pageSizeParam *int64, sortAscendingParam *bool, sortByParam *string) (model0.ConstraintListResult, error) {
120+
var err error
121+
var obj model0.ConstraintListResult
122+
123+
switch c.ClientType {
124+
125+
case utl.Local:
126+
client := c.Client.(client0.ConstraintsClient)
127+
obj, err = client.List(cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, pageSizeParam, sortAscendingParam, sortByParam)
128+
129+
case utl.Multitenancy:
130+
client := c.Client.(client1.ConstraintsClient)
131+
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, pageSizeParam, sortAscendingParam, sortByParam)
132+
133+
default:
134+
err = errors.New("invalid infrastructure for model")
135+
}
136+
return obj, err
137+
}

docs/resources/policy_constraint.md

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
subcategory: "Beta"
3+
page_title: "NSXT: nsxt_policy_constraint"
4+
description: A resource to configure a Constraint (Quota).
5+
---
6+
7+
# nsxt_policy_constraint
8+
9+
This resource provides a method for the management of a Constraint.
10+
11+
This resource is applicable to NSX Policy Manager.
12+
13+
## Example Usage
14+
15+
```hcl
16+
resource "nsxt_policy_constraint" "test" {
17+
display_name = "demo-quota"
18+
description = "Terraform provisioned Constraint"
19+
message = "too many objects mate"
20+
21+
target {
22+
path_prefix = "/orgs/default/projects/demo/"
23+
}
24+
25+
instance_count {
26+
count = 4
27+
target_resource_type = "StaticRoutes"
28+
}
29+
30+
instance_count {
31+
count = 1
32+
target_resource_type = "Infra.Tier1.PolicyDnsForwarder"
33+
}
34+
35+
instance_count {
36+
count = 20
37+
target_resource_type = "Infra.Domain.Group"
38+
}
39+
}
40+
```
41+
42+
## Example Usage - Multi-Tenancy
43+
44+
```hcl
45+
resource "nsxt_policy_constraint" "test" {
46+
context {
47+
project_id = "demo"
48+
}
49+
50+
display_name = "demo1-quota"
51+
52+
target {
53+
path_prefix = "/orgs/default/projects/demo/vpcs/demo1/"
54+
}
55+
56+
instance_count {
57+
count = 4
58+
target_resource_type = "Org.Project.Vpc.PolicyNat.PolicyVpcNatRule"
59+
}
60+
}
61+
```
62+
63+
## Argument Reference
64+
65+
The following arguments are supported:
66+
67+
* `context` - (Optional) The context which the object belongs to
68+
* `display_name` - (Required) Display name of the resource.
69+
* `description` - (Optional) Description of the resource.
70+
* `message` - (Optional) User friendly message to be shown to users upon violation.
71+
* `target` - (Optional) Targets for the constraints to be enforced
72+
* `path_prefix` - (Optional) Prefix match to the path
73+
* `instance_count` - (Optional) Constraint details
74+
* `target_resource_type` - (Required) Type of the resource that should be limited in instance count (refer to the table below)
75+
* `operator` - (Optional) Either `<=` or `<`. Default is `<=`
76+
* `count` - (Required) Limit of instances
77+
* `tag` - (Optional) A list of scope + tag pairs to associate with this resource.
78+
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
79+
80+
81+
## Attributes Reference
82+
83+
In addition to arguments listed above, the following attributes are exported:
84+
85+
* `id` - ID of the resource.
86+
* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging.
87+
* `path` - The NSX path of the policy resource.
88+
89+
90+
## Target resource types
91+
92+
|Object|project + VPC|project only|VPC only|
93+
|------|-------------|------------|--------|
94+
|Group|Group|Infra.Domain.Group|Org.Project.Vpc.Group|
95+
|Service||Infra.Service||
96+
|Service Entry||Infra.Service.ServiceEntry||
97+
|TLS Certificate||Infra.TlsCertificate||
98+
|TLS CRL||Infra.TlsCrl||
99+
|All Firewall Rules|Rule|||
100+
|Security Policy|SecurityPolicy|Infra.Domain.SecurityPolicy|Org.Project.Vpc.SecurityPolicy|
101+
|Security Policy Rule|SecurityPolicy.Rule|Infra.Domain.SecurityPolicy.Rule|Org.Project.Vpc.SecurityPolicy.Rule|
102+
|Gateway Policy|SecurityPolicy|Infra.Domain.SecurityPolicy|Org.Project.Vpc.SecurityPolicy|
103+
|Gateway Policy Rule|GatewayPolicy.Rule|Infra.Domain.GatewayPolicy.Rule|Org.Project.Vpc.GatewayPolicy.Rule|
104+
|IDS Security Policy||Infra.Domain.IdsPolicy||
105+
|IDS Security Policy Rule||Infra.Domain.IdsPolicy.Rule||
106+
|Session Timer Profile||Infra.PolicyFirewallSessionTimerProfile||
107+
|Flood Protection Profile||Infra.FloodProtectionProfile||
108+
|DNS Security Profile||Infra.DnsSecurityProfile||
109+
|Context Profile||Infra.PolicyContextProfile||
110+
|l7 Access Profile||Infra.L7AccessProfile||
111+
|Tier1 Gateway||Infra.Tier1||
112+
|Segment||Infra.Segment||
113+
|Segment Port||Infra.Segment.SegmentPort||
114+
|Subnet|||Org.Project.Vpc.Subnet|
115+
|Subnet Port|||Org.Project.Vpc.Subnet.SubnetPort|
116+
|Segment Security Profile||Infra.SegmentSecurityProfile||
117+
|Segment QoS Profile||Infra.QosProfile||
118+
|Segment IP Discovery Profile||Infra.IpDiscoveryProfile||
119+
|Segment MAC Discovery Profile||Infra.MacDiscoveryProfile||
120+
|Segment Spoof Guard Profile||Infra.SpoofGuardProfile||
121+
|IPv6 NDRA Profile||Infra.Ipv6NdraProfile||
122+
|IPv6 DAD Profile||Infra.Ipv6DadProfile||
123+
|Gateway QoS Profile||Infra.GatewayQosProfile||
124+
|Static Routes|StaticRoutes|Infra.Tier1.StaticRoutes|Org.Project.Vpc.StaticRoutes|
125+
|NAT Rule|NatRule|Infra.Tier1.PolicyNat.PolicyNatRule|Org.Project.Vpc.PolicyNat.PolicyNatRule|
126+
|DNS Forwarder Zone||Infra.PolicyDnsForwarderZone||
127+
|DNS Forwarder||Infra.Tier1.PolicyDnsForwarder||
128+
|IP Address Block||Infra.IpAddressBlock||
129+
|IP Address Pool||Infra.IpAddressPool||
130+
|IP Address Pool Subnet||Infra.IpAddressPool.IpAddressPoolSubnet||
131+
|IP Address Allocation||Infra.IpAddressPool.IpAddressAllocation||
132+
|DHCP Server Config||Infra.DhcpServerConfig||
133+
|IPSec VPN Service||Infra.Tier1.IPSecVpnService||
134+
|IPSec VPN Session||Infra.Tier1.IPSecVpnService.IPSecVpnSession||
135+
|IPSec VPN Local Endpoint||Infra.Tier1.IPSecVpnService.IPSecVpnLocalEndpoint||
136+
|IPSec VPN Tunnel Profile||Infra.IPSecVpnTunnelProfile||
137+
|IPSec VPN IKE Profile||Infra.IPSecVpnIkeProfile||
138+
|IPSec VPN DPD Profile||Infra.IPSecVpnDpdProfile||
139+
|L2 VPN Service||Infra.Tier1.L2VpnService||
140+
|L2 VPN Session||Infra.Tier1.L2VpnService.L2VpnSession||
141+
|VPC||Org.Project.Vpc||
142+
143+
144+
## Importing
145+
146+
An existing object can be [imported][docs-import] into this resource, via the following command:
147+
148+
[docs-import]: https://www.terraform.io/cli/import
149+
150+
```
151+
terraform import nsxt_policy_constraint.test PATH
152+
```
153+
154+
The above command imports Constraint named `test` with the NSX path `PATH`.

nsxt/data_source_nsxt_vpc_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func testAccDataSourceNsxtVPCCreate(name string) error {
5656
}
5757

5858
ipBlockID := newUUID()
59-
err = testAccDataSourceNsxtPolicyIPBlockCreate(testAccGetProjectContext(), name, ipBlockID, "192.168.240.0/24", true)
59+
err = testAccDataSourceNsxtPolicyIPBlockCreate(testAccGetMultitenancyContext(), name, ipBlockID, "192.168.240.0/24", true)
6060
if err != nil {
6161
return err
6262
}
@@ -108,7 +108,7 @@ func testAccDataSourceNsxtVPCDeleteByName(name string) error {
108108
if err != nil {
109109
return handleDeleteError("VPC", *objInList.Id, err)
110110
}
111-
return testAccDataSourceNsxtPolicyIPBlockDeleteByName(testAccGetProjectContext(), name)
111+
return testAccDataSourceNsxtPolicyIPBlockDeleteByName(testAccGetMultitenancyContext(), name)
112112
}
113113
}
114114
return fmt.Errorf("error while deleting VPC '%s': resource not found", name)

nsxt/metadata/metadata.go

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ var StandardTypeIdentifier = TypeIdentifier{
2626
APIFieldName: "type",
2727
}
2828

29+
var ResourceTypeTypeIdentifier = TypeIdentifier{
30+
SdkName: "ResourceType",
31+
APIFieldName: "resource_type",
32+
}
33+
2934
const (
3035
PolymorphicTypeFlatten = "flatten"
3136
PolymorphicTypeNested = "nested"

nsxt/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ func Provider() *schema.Provider {
544544
"nsxt_policy_edge_transport_node": resourceNsxtPolicyEdgeTransportNode(),
545545
"nsxt_policy_edge_high_availability_profile": resourceNsxtPolicyEdgeHighAvailabilityProfile(),
546546
"nsxt_policy_edge_cluster": resourceNsxtPolicyEdgeCluster(),
547+
"nsxt_policy_constraint": resourceNsxtPolicyConstraint(),
547548
},
548549

549550
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)