|
| 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 vra |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "log" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | + "github.com/vmware/vra-sdk-go/pkg/client/load_balancer" |
| 14 | +) |
| 15 | + |
| 16 | +func dataSourceLoadBalancer() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + ReadContext: dataSourceLoadBalancerRead, |
| 19 | + |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "name": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + }, |
| 29 | + "nics": nicsSchema(false), |
| 30 | + "project_id": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + }, |
| 34 | + "routes": routesSchema(false), |
| 35 | + "custom_properties": { |
| 36 | + Type: schema.TypeMap, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "description": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "deployment_id": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "internet_facing": { |
| 48 | + Type: schema.TypeBool, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "tags": tagsSchema(), |
| 52 | + "targets": LoadBalancerTargetSchema(), |
| 53 | + "address": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + "created_at": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + }, |
| 61 | + "external_id": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "external_region_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "external_zone_id": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + "links": linksSchema(), |
| 74 | + "organization_id": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "owner": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "updated_at": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func dataSourceLoadBalancerRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 91 | + apiClient := m.(*Client).apiClient |
| 92 | + |
| 93 | + id := d.Get("id").(string) |
| 94 | + resp, err := apiClient.LoadBalancer.GetLoadBalancer(load_balancer.NewGetLoadBalancerParams().WithID(id)) |
| 95 | + if err != nil { |
| 96 | + return diag.FromErr(err) |
| 97 | + } |
| 98 | + |
| 99 | + loadBalancer := *resp.Payload |
| 100 | + d.SetId(id) |
| 101 | + d.Set("address", loadBalancer.Address) |
| 102 | + d.Set("created_at", loadBalancer.CreatedAt) |
| 103 | + d.Set("custom_properties", loadBalancer.CustomProperties) |
| 104 | + d.Set("description", loadBalancer.Description) |
| 105 | + d.Set("deployment_id", loadBalancer.DeploymentID) |
| 106 | + d.Set("external_id", loadBalancer.ExternalID) |
| 107 | + d.Set("external_region_id", loadBalancer.ExternalRegionID) |
| 108 | + d.Set("external_zone_id", loadBalancer.ExternalZoneID) |
| 109 | + d.Set("name", loadBalancer.Name) |
| 110 | + d.Set("organization_id", loadBalancer.OrgID) |
| 111 | + d.Set("owner", loadBalancer.Owner) |
| 112 | + d.Set("project_id", loadBalancer.ProjectID) |
| 113 | + d.Set("updated_at", loadBalancer.UpdatedAt) |
| 114 | + |
| 115 | + if err := d.Set("tags", flattenTags(loadBalancer.Tags)); err != nil { |
| 116 | + return diag.Errorf("error setting load balancer tags - error: %v", err) |
| 117 | + } |
| 118 | + if err := d.Set("routes", flattenRoutes(loadBalancer.Routes)); err != nil { |
| 119 | + return diag.Errorf("error setting load balancer routes - error: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + if err := d.Set("links", flattenLinks(loadBalancer.Links)); err != nil { |
| 123 | + return diag.Errorf("error setting load balancer links - error: %#v", err) |
| 124 | + } |
| 125 | + |
| 126 | + log.Printf("Finished reading the vra_load_balancer data source with id %s", d.Get("id")) |
| 127 | + return nil |
| 128 | +} |
0 commit comments