|
| 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