|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package apphub |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 10 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 11 | +) |
| 12 | + |
| 13 | +func DataSourceApphubDiscoveredWorkload() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceApphubDiscoveredWorkloadRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "project": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Optional: true, |
| 20 | + }, |
| 21 | + "location": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "workload_uri": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + "name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "workload_reference": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Computed: true, |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "uri": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + "workload_properties": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Computed: true, |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "gcp_project": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "location": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "zone": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func dataSourceApphubDiscoveredWorkloadRead(d *schema.ResourceData, meta interface{}) error { |
| 70 | + config := meta.(*transport_tpg.Config) |
| 71 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + url, err := tpgresource.ReplaceVars(d, config, fmt.Sprintf("{{ApphubBasePath}}projects/{{project}}/locations/{{location}}/discoveredWorkloads:lookup?uri={{workload_uri}}")) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + billingProject := "" |
| 82 | + |
| 83 | + // err == nil indicates that the billing_project value was found |
| 84 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 85 | + billingProject = bp |
| 86 | + } |
| 87 | + |
| 88 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 89 | + Config: config, |
| 90 | + Method: "GET", |
| 91 | + Project: billingProject, |
| 92 | + RawURL: url, |
| 93 | + UserAgent: userAgent, |
| 94 | + }) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("ApphubDiscoveredWorkload %q", d.Id()), url) |
| 98 | + } |
| 99 | + |
| 100 | + if err := d.Set("name", flattenApphubDiscoveredWorkloadName(res["discoveredWorkload"].(map[string]interface{})["name"], d, config)); err != nil { |
| 101 | + return fmt.Errorf("Error setting workload name: %s", err) |
| 102 | + } |
| 103 | + |
| 104 | + if err := d.Set("workload_reference", flattenApphubDiscoveredWorkloadReference(res["discoveredWorkload"].(map[string]interface{})["workloadReference"], d, config)); err != nil { |
| 105 | + return fmt.Errorf("Error setting service reference: %s", err) |
| 106 | + } |
| 107 | + |
| 108 | + if err := d.Set("workload_properties", flattenApphubDiscoveredWorkloadProperties(res["discoveredWorkload"].(map[string]interface{})["workloadProperties"], d, config)); err != nil { |
| 109 | + return fmt.Errorf("Error setting workload properties: %s", err) |
| 110 | + } |
| 111 | + |
| 112 | + d.SetId(res["discoveredWorkload"].(map[string]interface{})["name"].(string)) |
| 113 | + |
| 114 | + return nil |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +func flattenApphubDiscoveredWorkloadReference(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 119 | + if v == nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + original := v.(map[string]interface{}) |
| 123 | + if len(original) == 0 { |
| 124 | + return nil |
| 125 | + } |
| 126 | + transformed := make(map[string]interface{}) |
| 127 | + transformed["uri"] = flattenApphubDiscoveredWorkloadDataUri(original["uri"], d, config) |
| 128 | + return []interface{}{transformed} |
| 129 | +} |
| 130 | + |
| 131 | +func flattenApphubDiscoveredWorkloadProperties(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 132 | + if v == nil { |
| 133 | + return nil |
| 134 | + } |
| 135 | + original := v.(map[string]interface{}) |
| 136 | + if len(original) == 0 { |
| 137 | + return nil |
| 138 | + } |
| 139 | + transformed := make(map[string]interface{}) |
| 140 | + transformed["gcp_project"] = flattenApphubDiscoveredWorkloadDataGcpProject(original["gcpProject"], d, config) |
| 141 | + transformed["location"] = flattenApphubDiscoveredWorkloadDataLocation(original["location"], d, config) |
| 142 | + transformed["zone"] = flattenApphubDiscoveredWorkloadDataZone(original["zone"], d, config) |
| 143 | + return []interface{}{transformed} |
| 144 | +} |
| 145 | + |
| 146 | +func flattenApphubDiscoveredWorkloadName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 147 | + return v |
| 148 | +} |
| 149 | + |
| 150 | +func flattenApphubDiscoveredWorkloadDataUri(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 151 | + return v |
| 152 | +} |
| 153 | + |
| 154 | +func flattenApphubDiscoveredWorkloadDataGcpProject(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 155 | + return v |
| 156 | +} |
| 157 | + |
| 158 | +func flattenApphubDiscoveredWorkloadDataLocation(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 159 | + return v |
| 160 | +} |
| 161 | + |
| 162 | +func flattenApphubDiscoveredWorkloadDataZone(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 163 | + return v |
| 164 | +} |
0 commit comments