Skip to content

Commit

Permalink
General CRUD for SecurityPolicy
Browse files Browse the repository at this point in the history
Signed-off-by: graysonwu <[email protected]>
  • Loading branch information
GraysonWu committed Dec 16, 2023
1 parent 451bab7 commit bec655f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 90 deletions.
62 changes: 8 additions & 54 deletions nsxt/resource_nsxt_policy_parent_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package nsxt

import (
"fmt"

Check failure on line 7 in nsxt/resource_nsxt_policy_parent_security_policy.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

Expand All @@ -26,31 +24,6 @@ func resourceNsxtPolicyParentSecurityPolicy() *schema.Resource {
}
}

func resourceNsxtPolicyParentSecurityPolicyCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

// Initialize resource Id and verify this ID is not yet used
id, err := getOrGenerateID2(d, m, resourceNsxtPolicySecurityPolicyExistsPartial(d.Get("domain").(string)))
if err != nil {
return err
}

log.Printf("[INFO] Creating Security Policy with ID %s", id)
domain := d.Get("domain").(string)
client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector)

obj := parentSecurityPolicySchemaToModel(d, id)
err = client.Patch(domain, id, obj)
if err != nil {
return handleCreateError("Security Policy", id, err)
}

d.SetId(id)
d.Set("nsx_id", id)

return resourceNsxtPolicyParentSecurityPolicyRead(d, m)
}

func parentSecurityPolicySchemaToModel(d *schema.ResourceData, id string) model.SecurityPolicy {
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
Expand Down Expand Up @@ -80,11 +53,6 @@ func parentSecurityPolicySchemaToModel(d *schema.ResourceData, id string) model.
}
}

func resourceNsxtPolicyParentSecurityPolicyRead(d *schema.ResourceData, m interface{}) error {
_, err := parentSecurityPolicyModelToSchema(d, m)
return err
}

func parentSecurityPolicyModelToSchema(d *schema.ResourceData, m interface{}) (*model.SecurityPolicy, error) {
connector := getPolicyConnector(m)
id := d.Id()
Expand Down Expand Up @@ -118,30 +86,16 @@ func parentSecurityPolicyModelToSchema(d *schema.ResourceData, m interface{}) (*
return &obj, nil
}

func resourceNsxtPolicyParentSecurityPolicyUpdate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining Security Policy id")
}

log.Printf("[INFO] Updating Security Policy with ID %s", id)
domain := d.Get("domain").(string)
client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector)
remoteObj, err := client.Get(domain, id)
if err != nil {
return handleUpdateError("Security Policy", id, err)
}
func resourceNsxtPolicyParentSecurityPolicyCreate(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicySecurityPolicyGeneralCreate(d, m, false)
}

obj := parentSecurityPolicySchemaToModel(d, id)
obj.Rules = remoteObj.Rules
err = client.Patch(domain, id, obj)
if err != nil {
return handleUpdateError("Security Policy", id, err)
}
func resourceNsxtPolicyParentSecurityPolicyRead(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicySecurityPolicyGeneralRead(d, m, false)
}

return resourceNsxtPolicyParentSecurityPolicyRead(d, m)
func resourceNsxtPolicyParentSecurityPolicyUpdate(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicySecurityPolicyGeneralUpdate(d, m, false)
}

func resourceNsxtPolicyParentSecurityPolicyDelete(d *schema.ResourceData, m interface{}) error {
Expand Down
84 changes: 48 additions & 36 deletions nsxt/resource_nsxt_policy_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ func resourceNsxtPolicySecurityPolicyExistsPartial(domainName string) func(sessi
}
}

func policySecurityPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, connector client.Connector, isGlobalManager bool, id string, createFlow bool) error {

func policySecurityPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, id string, createFlow, withRule bool) error {
obj := parentSecurityPolicySchemaToModel(d, id)
domain := d.Get("domain").(string)
revision := int64(d.Get("revision").(int))
log.Printf("[INFO] Creating Security Policy with ID %s", id)

if createFlow {
if createFlow && withRule {
if err := validatePolicyRuleSequence(d); err != nil {
return err
}
Expand All @@ -71,28 +70,58 @@ func policySecurityPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, co
obj.Revision = &revision
}

policyChildren, err := getUpdatedRuleChildren(d)
if err != nil {
return err
}
if len(policyChildren) > 0 {
obj.Children = policyChildren
if withRule {
policyChildren, err := getUpdatedRuleChildren(d)
if err != nil {
return err
}
if len(policyChildren) > 0 {
obj.Children = policyChildren
}
}

log.Printf("[INFO] Using selective H-API for policy with ID %s", id)
return securityPolicyInfraPatch(getSessionContext(d, m), obj, domain, m)
}

func resourceNsxtPolicySecurityPolicyCreate(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicySecurityPolicyGeneralCreate(d, m, true)
}

func resourceNsxtPolicySecurityPolicyRead(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicySecurityPolicyGeneralRead(d, m, true)
}

func resourceNsxtPolicySecurityPolicyUpdate(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicySecurityPolicyGeneralUpdate(d, m, true)
}

func resourceNsxtPolicySecurityPolicyDelete(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining Security Policy id")
}

connector := getPolicyConnector(m)

client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector)
err := client.Delete(d.Get("domain").(string), id)

if err != nil {
return handleDeleteError("Security Policy", id, err)
}

return nil
}

func resourceNsxtPolicySecurityPolicyGeneralCreate(d *schema.ResourceData, m interface{}, withRule bool) error {
// Initialize resource Id and verify this ID is not yet used
id, err := getOrGenerateID2(d, m, resourceNsxtPolicySecurityPolicyExistsPartial(d.Get("domain").(string)))
if err != nil {
return err
}

err = policySecurityPolicyBuildAndPatch(d, m, connector, isPolicyGlobalManager(m), id, true)
err = policySecurityPolicyBuildAndPatch(d, m, id, true, withRule)

if err != nil {
return handleCreateError("Security Policy", id, err)
Expand All @@ -101,46 +130,29 @@ func resourceNsxtPolicySecurityPolicyCreate(d *schema.ResourceData, m interface{
d.SetId(id)
d.Set("nsx_id", id)

return resourceNsxtPolicySecurityPolicyRead(d, m)
return resourceNsxtPolicySecurityPolicyGeneralRead(d, m, withRule)
}

func resourceNsxtPolicySecurityPolicyRead(d *schema.ResourceData, m interface{}) error {
func resourceNsxtPolicySecurityPolicyGeneralRead(d *schema.ResourceData, m interface{}, withRule bool) error {
obj, err := parentSecurityPolicyModelToSchema(d, m)
if err != nil {
return err
}
return setPolicyRulesInSchema(d, obj.Rules)
}

func resourceNsxtPolicySecurityPolicyUpdate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining Security Policy id")
if withRule {
return setPolicyRulesInSchema(d, obj.Rules)
}
err := policySecurityPolicyBuildAndPatch(d, m, connector, isPolicyGlobalManager(m), id, false)
if err != nil {
return handleUpdateError("Security Policy", id, err)
}

return resourceNsxtPolicySecurityPolicyRead(d, m)
return nil
}

func resourceNsxtPolicySecurityPolicyDelete(d *schema.ResourceData, m interface{}) error {
func resourceNsxtPolicySecurityPolicyGeneralUpdate(d *schema.ResourceData, m interface{}, withRule bool) error {
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining Security Policy id")
}

connector := getPolicyConnector(m)

client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector)
err := client.Delete(d.Get("domain").(string), id)

err := policySecurityPolicyBuildAndPatch(d, m, id, false, withRule)
if err != nil {
return handleDeleteError("Security Policy", id, err)
return handleUpdateError("Security Policy", id, err)
}

return nil
return resourceNsxtPolicySecurityPolicyGeneralRead(d, m, withRule)
}

0 comments on commit bec655f

Please sign in to comment.