Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/data-sources/policy_container_cluster.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
subcategory: "Beta"
page_title: "NSXT: policy_container_cluster"
description: Policy Container Cluster data source.
---

# nsxt_policy_container_cluster

This data source provides information about Policy Container Cluster configured on NSX.
This data source is applicable to NSX Policy Manager.

## Example Usage

```hcl
data "nsxt_policy_container_cluster" "cluster" {
display_name = "containercluster1"
}
```

## Argument Reference

* `id` - (Optional) The ID of Container Cluster to retrieve. If ID is specified, no additional argument should be configured.
* `display_name` - (Optional) The Display Name prefix of the Container Cluster to retrieve.

## Attributes Reference

In addition to arguments listed above, the following attributes are exported:

* `description` - The description of the resource.
* `path` - The NSX path of the policy resource.
118 changes: 118 additions & 0 deletions nsxt/data_source_nsxt_policy_container_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// © Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0

package nsxt

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

func dataSourceNsxtPolicyContainerCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtPolicyContainerClusterRead,

Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"display_name": getDataSourceDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
"path": getPathSchema(),
},
}
}

func listClusterControlPlanes(siteID, epID string, connector client.Connector) ([]model.ClusterControlPlane, error) {
client := enforcement_points.NewClusterControlPlanesClient(connector)
if client == nil {
return nil, policyResourceNotSupportedError()
}

var results []model.ClusterControlPlane
boolFalse := false
var cursor *string
total := 0

for {
policies, err := client.List(siteID, epID, cursor, nil, nil, nil, &boolFalse, nil)
if err != nil {
return results, err
}
results = append(results, policies.Results...)
if total == 0 && policies.ResultCount != nil {
// first response
total = int(*policies.ResultCount)
}

cursor = policies.Cursor
if len(results) >= total {
return results, nil
}
}
}

func dataSourceNsxtPolicyContainerClusterRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := enforcement_points.NewClusterControlPlanesClient(connector)

// As Project resource type paths reside under project and not under /infra or /global_infra or such, and since
// this data source fetches extra attributes, e.g site_info and tier0_gateway_paths, it's simpler to implement using .List()
// instead of using search API.

objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj model.ClusterControlPlane

if objID != "" {
// Get by id
objGet, err := client.Get(defaultSite, defaultEnforcementPoint, objID)
if err != nil {
return handleDataSourceReadError(d, "ClusterControlPlane", objID, err)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("Error obtaining ClusterControlPlane ID or name during read")
} else {
// Get by full name/prefix
objList, err := listClusterControlPlanes(defaultSite, defaultEnforcementPoint, connector)
if err != nil {
return handleListError("ClusterControlPlane", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.ClusterControlPlane
var prefixMatch []model.ClusterControlPlane
for _, objInList := range objList {
if strings.HasPrefix(*objInList.DisplayName, objName) {
prefixMatch = append(prefixMatch, objInList)
}
if *objInList.DisplayName == objName {
perfectMatch = append(perfectMatch, objInList)
}
}
if len(perfectMatch) > 0 {
if len(perfectMatch) > 1 {
return fmt.Errorf("Found multiple ClusterControlPlanes with name '%s'", objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("Found multiple ClusterControlPlanes with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("ClusterControlPlane with name '%s' was not found", objName)
}
}

d.SetId(*obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
d.Set("path", obj.Path)

return nil
}
45 changes: 45 additions & 0 deletions nsxt/data_source_nsxt_policy_container_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// © Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0

package nsxt

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceNsxtPolicyContainerCluster_basic(t *testing.T) {
containerClusterName := os.Getenv("NSXT_TEST_CONTAINER_CLUSTER")
testResourceName := "data.nsxt_policy_container_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccOnlyLocalManager(t)
testAccNSXVersion(t, "9.0.0")
testAccEnvDefined(t, "NSXT_TEST_CONTAINER_CLUSTER")
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNSXPolicyContainerClusterReadTemplate(containerClusterName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", containerClusterName),
resource.TestCheckResourceAttrSet(testResourceName, "id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
),
},
},
})
}

func testAccNSXPolicyContainerClusterReadTemplate(name string) string {
return fmt.Sprintf(`
data "nsxt_policy_container_cluster" "test" {
display_name = "%s"
}`, name)
}
1 change: 1 addition & 0 deletions nsxt/policy_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

var defaultDomain = "default"
var defaultSite = "default"
var defaultEnforcementPoint = "default"
var securityPolicyCategoryValues = []string{"Ethernet", "Emergency", "Infrastructure", "Environment", "Application"}
var securityPolicyDirectionValues = []string{model.Rule_DIRECTION_IN, model.Rule_DIRECTION_OUT, model.Rule_DIRECTION_IN_OUT}
var securityPolicyIPProtocolValues = []string{"NONE", model.Rule_IP_PROTOCOL_IPV4, model.Rule_IP_PROTOCOL_IPV6, model.Rule_IP_PROTOCOL_IPV4_IPV6}
Expand Down
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func Provider() *schema.Provider {
"nsxt_policy_edge_high_availability_profile": dataSourceNsxtPolicyEdgeHighAvailabilityProfile(),
"nsxt_policy_edge_transport_node_realization": dataSourceNsxtPolicyEdgeTransportNodeRealization(),
"nsxt_policy_segment_port": dataSourceNsxtPolicySegmentPort(),
"nsxt_policy_container_cluster": dataSourceNsxtPolicyContainerCluster(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
1 change: 0 additions & 1 deletion nsxt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const edgeClusterDefaultName string = "EDGECLUSTER1"
const vlanTransportZoneName string = "transportzone2"
const overlayTransportZoneNamePrefix string = "1-transportzone"
const macPoolDefaultName string = "DefaultMacPool"
const defaultEnforcementPoint string = "default"

const realizationResourceName string = "data.nsxt_policy_realization_info.realization_info"
const defaultTestResourceName string = "terraform-acctest"
Expand Down
Loading