|
| 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/services/nsxt/infra/sites/enforcement_points" |
| 13 | + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" |
| 14 | +) |
| 15 | + |
| 16 | +func dataSourceNsxtPolicyEdgeHighAvailabilityProfile() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Read: dataSourceNsxtPolicyEdgeHighAvailabilityProfileRead, |
| 19 | + |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "id": getDataSourceIDSchema(), |
| 22 | + "display_name": getDataSourceDisplayNameSchema(), |
| 23 | + "description": getDataSourceDescriptionSchema(), |
| 24 | + "path": getPathSchema(), |
| 25 | + "site_path": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Description: "Path of the site this Transport Node Collection belongs to", |
| 28 | + Optional: true, |
| 29 | + ValidateFunc: validatePolicyPath(), |
| 30 | + }, |
| 31 | + "unique_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + Description: "A unique identifier assigned by the system", |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func dataSourceNsxtPolicyEdgeHighAvailabilityProfileRead(d *schema.ResourceData, m interface{}) error { |
| 41 | + connector := getPolicyConnector(m) |
| 42 | + client := enforcement_points.NewEdgeClusterHighAvailabilityProfilesClient(connector) |
| 43 | + |
| 44 | + objID := d.Get("id").(string) |
| 45 | + objName := d.Get("display_name").(string) |
| 46 | + objSitePath := d.Get("site_path").(string) |
| 47 | + objSiteID := defaultSite |
| 48 | + // For local manager, if site path is not provided, use default site |
| 49 | + if objSitePath != "" { |
| 50 | + objSiteID = getPolicyIDFromPath(objSitePath) |
| 51 | + } |
| 52 | + var obj model.PolicyEdgeHighAvailabilityProfile |
| 53 | + if objID != "" { |
| 54 | + // Get by id |
| 55 | + objGet, err := client.Get(objSiteID, getPolicyEnforcementPoint(m), objID) |
| 56 | + if err != nil { |
| 57 | + return handleDataSourceReadError(d, "EdgeHighAvailabilityProfile", objID, err) |
| 58 | + } |
| 59 | + obj = objGet |
| 60 | + } else if objName == "" { |
| 61 | + return fmt.Errorf("Error obtaining EdgeHighAvailabilityProfile ID or name during read") |
| 62 | + } else { |
| 63 | + // Get by full name/prefix |
| 64 | + objList, err := client.List(objSiteID, getPolicyEnforcementPoint(m), nil, nil, nil, nil, nil, nil, nil) |
| 65 | + if err != nil { |
| 66 | + return handleListError("EdgeHighAvailabilityProfile", err) |
| 67 | + } |
| 68 | + // go over the list to find the correct one (prefer a perfect match. If not - prefix match) |
| 69 | + var perfectMatch []model.PolicyEdgeHighAvailabilityProfile |
| 70 | + var prefixMatch []model.PolicyEdgeHighAvailabilityProfile |
| 71 | + for _, objInList := range objList.Results { |
| 72 | + if strings.HasPrefix(*objInList.DisplayName, objName) { |
| 73 | + prefixMatch = append(prefixMatch, objInList) |
| 74 | + } |
| 75 | + if *objInList.DisplayName == objName { |
| 76 | + perfectMatch = append(perfectMatch, objInList) |
| 77 | + } |
| 78 | + } |
| 79 | + if len(perfectMatch) > 0 { |
| 80 | + if len(perfectMatch) > 1 { |
| 81 | + return fmt.Errorf("Found multiple EdgeHighAvailabilityProfile with name '%s'", objName) |
| 82 | + } |
| 83 | + obj = perfectMatch[0] |
| 84 | + } else if len(prefixMatch) > 0 { |
| 85 | + if len(prefixMatch) > 1 { |
| 86 | + return fmt.Errorf("Found multiple EdgeHighAvailabilityProfiles with name starting with '%s'", objName) |
| 87 | + } |
| 88 | + obj = prefixMatch[0] |
| 89 | + } else { |
| 90 | + return fmt.Errorf("EdgeHighAvailabilityProfile with name '%s' was not found", objName) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + d.SetId(*obj.Id) |
| 95 | + d.Set("display_name", obj.DisplayName) |
| 96 | + d.Set("description", obj.Description) |
| 97 | + d.Set("path", obj.Path) |
| 98 | + d.Set("unique_id", obj.UniqueId) |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
0 commit comments