From 451bab7d4f70200fd82ea998c2639651e2b658dd Mon Sep 17 00:00:00 2001 From: graysonwu Date: Wed, 13 Dec 2023 14:19:10 -0800 Subject: [PATCH] remove data source Signed-off-by: graysonwu --- ...source_nsxt_policy_security_policy_rule.go | 86 ------------------- nsxt/policy_common.go | 2 +- nsxt/provider.go | 1 - ...e_nsxt_policy_security_policy_rule_test.go | 14 +-- nsxt/utils.go | 21 +++++ 5 files changed, 23 insertions(+), 101 deletions(-) delete mode 100644 nsxt/data_source_nsxt_policy_security_policy_rule.go diff --git a/nsxt/data_source_nsxt_policy_security_policy_rule.go b/nsxt/data_source_nsxt_policy_security_policy_rule.go deleted file mode 100644 index 55c181c4e..000000000 --- a/nsxt/data_source_nsxt_policy_security_policy_rule.go +++ /dev/null @@ -1,86 +0,0 @@ -/* Copyright © 2023 VMware, Inc. All Rights Reserved. - SPDX-License-Identifier: MPL-2.0 */ - -package nsxt - -import ( - "fmt" - "strings" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - securitypolicies "github.com/vmware/terraform-provider-nsxt/api/infra/domains/security_policies" - "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" -) - -func dataSourceNsxtPolicySecurityPolicyRule() *schema.Resource { - return &schema.Resource{ - Read: dataSourceNsxtPolicySecurityPolicyRuleRead, - - Schema: map[string]*schema.Schema{ - "id": getDataSourceIDSchema(), - "display_name": getDataSourceDisplayNameSchema(), - "description": getDataSourceDescriptionSchema(), - "path": getPathSchema(), - "policy_path": getPolicyPathSchema(true, false, "Security Policy path"), - "context": getContextSchema(), - }, - } -} - -func dataSourceNsxtPolicySecurityPolicyRuleRead(d *schema.ResourceData, m interface{}) error { - connector := getPolicyConnector(m) - - policyPath := d.Get("policy_path").(string) - domain := getDomainFromResourcePath(policyPath) - policyID := getPolicyIDFromPath(policyPath) - - client := securitypolicies.NewRulesClient(getSessionContext(d, m), connector) - objID := d.Get("id").(string) - var obj model.Rule - if objID != "" { - // Get by id - objGet, err := client.Get(domain, policyID, objID) - - if err != nil { - return handleDataSourceReadError(d, "SecurityPolicyRule", objID, err) - } - obj = objGet - } else { - // Get by full name/prefix - displayName := d.Get("display_name").(string) - objList, err := client.List(domain, policyID, nil, nil, nil, nil, nil, nil) - if err != nil { - return handleListError("SecurityPolicyRule", err) - } - // go over the list to find the correct one (prefer a perfect match. If not - prefix match) - var perfectMatch []model.Rule - var prefixMatch []model.Rule - for _, objInList := range objList.Results { - if strings.HasPrefix(*objInList.DisplayName, displayName) { - prefixMatch = append(prefixMatch, objInList) - } - if *objInList.DisplayName == displayName { - perfectMatch = append(perfectMatch, objInList) - } - } - if len(perfectMatch) > 0 { - if len(perfectMatch) > 1 { - return fmt.Errorf("Found multiple SecurityPolicyRule with name '%s'", displayName) - } - obj = perfectMatch[0] - } else if len(prefixMatch) > 0 { - if len(prefixMatch) > 1 { - return fmt.Errorf("Found multiple SecurityPolicyRule with name starting with '%s'", displayName) - } - obj = prefixMatch[0] - } else { - return fmt.Errorf("SecurityPolicyRule with name '%s' was not found", displayName) - } - } - - d.SetId(*obj.Id) - d.Set("display_name", obj.DisplayName) - d.Set("description", obj.Description) - d.Set("path", obj.Path) - return nil -} diff --git a/nsxt/policy_common.go b/nsxt/policy_common.go index cf7a375c0..c0dc850ec 100644 --- a/nsxt/policy_common.go +++ b/nsxt/policy_common.go @@ -305,7 +305,7 @@ func getSecurityPolicyAndGatewayRuleSchema(scopeRequired bool, isIds bool, nsxID Description: "Sequence number of the this rule", Required: true, } - ruleSchema["context"] = getContextSchema() + ruleSchema["context"] = getComputedContextSchema() } else { ruleSchema["sequence_number"] = &schema.Schema{ Type: schema.TypeInt, diff --git a/nsxt/provider.go b/nsxt/provider.go index 6bf9a6c51..55e1ba98d 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -295,7 +295,6 @@ func Provider() *schema.Provider { "nsxt_manager_cluster_node": dataSourceNsxtManagerClusterNode(), "nsxt_policy_host_transport_node_profile": dataSourceNsxtPolicyHostTransportNodeProfile(), "nsxt_transport_node": dataSourceNsxtEdgeTransportNode(), - "nsxt_policy_security_policy_rule": dataSourceNsxtPolicySecurityPolicyRule(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/nsxt/resource_nsxt_policy_security_policy_rule_test.go b/nsxt/resource_nsxt_policy_security_policy_rule_test.go index 825bb891d..adaca01e7 100644 --- a/nsxt/resource_nsxt_policy_security_policy_rule_test.go +++ b/nsxt/resource_nsxt_policy_security_policy_rule_test.go @@ -197,13 +197,8 @@ resource "nsxt_policy_parent_security_policy" "policy1" { } func testAccNsxtPolicySecurityPolicyRuleTemplate(withContext bool, name, action, direction, ipVersion, seqNum string) string { - context := "" - if withContext { - context = testAccNsxtPolicyMultitenancyContext() - } return testAccNsxtPolicySecurityPolicyRuleDeps(withContext) + fmt.Sprintf(` resource "nsxt_policy_security_policy_rule" "test" { -%s display_name = "%s" policy_path = nsxt_policy_parent_security_policy.policy1.path action = "%s" @@ -218,12 +213,5 @@ resource "nsxt_policy_security_policy_rule" "test" { } depends_on = [nsxt_policy_parent_security_policy.policy1, nsxt_policy_group.group2] -} - -data "nsxt_policy_security_policy_rule" "test" { -%s - display_name = "%s" - policy_path = nsxt_policy_parent_security_policy.policy1.path - depends_on = [nsxt_policy_security_policy_rule.test] -}`, context, name, action, direction, ipVersion, seqNum, context, name) +}`, name, action, direction, ipVersion, seqNum) } diff --git a/nsxt/utils.go b/nsxt/utils.go index caedfc5be..2a11896a8 100644 --- a/nsxt/utils.go +++ b/nsxt/utils.go @@ -702,6 +702,27 @@ func getContextSchema() *schema.Schema { } } +func getComputedContextSchema() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeList, + Description: "Resource context", + Optional: true, + Computed: true, + MaxItems: 1, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "project_id": { + Type: schema.TypeString, + Description: "Id of the project which the resource belongs to.", + Required: true, + ForceNew: true, + }, + }, + }, + } +} + func getCustomizedMPTagsFromSchema(d *schema.ResourceData, schemaName string) []mp_model.Tag { tags := d.Get(schemaName).(*schema.Set).List() tagList := make([]mp_model.Tag, 0)