Skip to content

Commit 2ccc543

Browse files
Add data source for AppHub discovered workload (#10107) (#17553)
* Add data source for Apphub discovered workload * Add data source for Apphub discovered workload * Add data source for Apphub Discovered Workload * Resolved comments * Resolved comments * Resolved comments * Resolved comments * Add tests and documentation for data source discovered workload * Resolved comments and added tests * Resolved comments * Added modifications in tests, data source and documentation * Added modifications in tests, data source and documentation * Added modifications in tests, data source and documentation * Verified tests * Tests Verification * Tests Verification * Updated logic to obtain workload_uri * Updated logic to obtain workload_uri * Resolved comments * Resolved comments * Add billing account and shorten service project name * Add header * Change the project name to start with tf-test * Resolved comments * Lint changes * Removing two sleeps * Removing two sleeps * Modifying documentation * Resolved tests * Resolved comments * Resolved comments * logic modification * Update Retry logic * Update Retry logic * Resolved tests * Adding workload_uri description * Adding workload_uri description [upstream:a6ceb9e3c35210af4ecfaee801958eb7b79ac377] Signed-off-by: Modular Magician <[email protected]>
1 parent fd2604b commit 2ccc543

File tree

5 files changed

+365
-0
lines changed

5 files changed

+365
-0
lines changed

.changelog/10107.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_apphub_discovered_workload`
3+
```

google/provider/provider_mmv1_resources.go

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
137137
"google_alloydb_locations": alloydb.DataSourceAlloydbLocations(),
138138
"google_alloydb_supported_database_flags": alloydb.DataSourceAlloydbSupportedDatabaseFlags(),
139139
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
140+
"google_apphub_discovered_workload": apphub.DataSourceApphubDiscoveredWorkload(),
140141
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
141142
"google_apphub_discovered_service": apphub.DataSourceApphubDiscoveredService(),
142143
"google_beyondcorp_app_connection": beyondcorp.DataSourceGoogleBeyondcorpAppConnection(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package apphub_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-provider-google/google/acctest"
10+
"github.com/hashicorp/terraform-provider-google/google/envvar"
11+
)
12+
13+
func TestAccDataSourceApphubDiscoveredWorkload_basic(t *testing.T) {
14+
t.Parallel()
15+
16+
context := map[string]interface{}{
17+
"org_id": envvar.GetTestOrgFromEnv(t),
18+
"random_suffix": acctest.RandString(t, 10),
19+
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
20+
}
21+
22+
acctest.VcrTest(t, resource.TestCase{
23+
PreCheck: func() { acctest.AccTestPreCheck(t) },
24+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
25+
ExternalProviders: map[string]resource.ExternalProvider{
26+
"time": {},
27+
},
28+
Steps: []resource.TestStep{
29+
{
30+
Config: testDataSourceApphubDiscoveredWorkload_basic(context),
31+
Check: resource.ComposeTestCheckFunc(
32+
resource.TestCheckResourceAttrSet("data.google_apphub_discovered_workload.catalog-workload", "name"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testDataSourceApphubDiscoveredWorkload_basic(context map[string]interface{}) string {
40+
return acctest.Nprintf(`
41+
resource "google_project" "service_project" {
42+
project_id ="tf-test-ah-%{random_suffix}"
43+
name = "Service Project"
44+
org_id = "%{org_id}"
45+
billing_account = "%{billing_account}"
46+
}
47+
48+
# Enable Compute API
49+
resource "google_project_service" "compute_service_project" {
50+
project = google_project.service_project.project_id
51+
service = "compute.googleapis.com"
52+
}
53+
54+
resource "time_sleep" "wait_120s" {
55+
depends_on = [google_project_service.compute_service_project]
56+
create_duration = "120s"
57+
}
58+
59+
resource "google_apphub_service_project_attachment" "service_project_attachment" {
60+
service_project_attachment_id = google_project.service_project.project_id
61+
depends_on = [time_sleep.wait_120s]
62+
}
63+
64+
data "google_apphub_discovered_workload" "catalog-workload" {
65+
location = "us-central1"
66+
workload_uri = "${replace(google_compute_region_instance_group_manager.mig.instance_group, "https://www.googleapis.com/compute/v1", "//compute.googleapis.com")}"
67+
depends_on = [time_sleep.wait_120s_for_resource_ingestion]
68+
}
69+
70+
# VPC network
71+
resource "google_compute_network" "ilb_network" {
72+
name = "l7-ilb-network-%{random_suffix}"
73+
project = google_project.service_project.project_id
74+
auto_create_subnetworks = false
75+
depends_on = [time_sleep.wait_120s]
76+
}
77+
78+
# backend subnet
79+
resource "google_compute_subnetwork" "ilb_subnet" {
80+
name = "l7-ilb-subnetwork-%{random_suffix}"
81+
project = google_project.service_project.project_id
82+
ip_cidr_range = "10.0.1.0/24"
83+
region = "us-central1"
84+
network = google_compute_network.ilb_network.id
85+
}
86+
87+
resource "time_sleep" "wait_120s_for_resource_ingestion" {
88+
depends_on = [google_compute_region_instance_group_manager.mig]
89+
create_duration = "120s"
90+
}
91+
92+
# instance template
93+
resource "google_compute_instance_template" "instance_template" {
94+
name = "l7-ilb-mig-template-%{random_suffix}"
95+
project = google_project.service_project.project_id
96+
machine_type = "e2-small"
97+
tags = ["http-server"]
98+
network_interface {
99+
network = google_compute_network.ilb_network.id
100+
subnetwork = google_compute_subnetwork.ilb_subnet.id
101+
access_config {
102+
# add external ip to fetch packages
103+
}
104+
}
105+
disk {
106+
source_image = "debian-cloud/debian-10"
107+
auto_delete = true
108+
boot = true
109+
}
110+
# install nginx and serve a simple web page
111+
metadata = {
112+
startup-script = <<-EOF1
113+
#! /bin/bash
114+
set -euo pipefail
115+
export DEBIAN_FRONTEND=noninteractive
116+
apt-get update
117+
apt-get install -y nginx-light jq
118+
NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname")
119+
IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip")
120+
METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])')
121+
cat <<EOF > /var/www/html/index.html
122+
<pre>
123+
Name: $NAME
124+
IP: $IP
125+
Metadata: $METADATA
126+
</pre>
127+
EOF
128+
EOF1
129+
}
130+
lifecycle {
131+
create_before_destroy = true
132+
}
133+
}
134+
135+
resource "google_compute_region_instance_group_manager" "mig" {
136+
name = "l7-ilb-mig1-%{random_suffix}"
137+
project = google_project.service_project.project_id
138+
region = "us-central1"
139+
version {
140+
instance_template = google_compute_instance_template.instance_template.id
141+
name = "primary"
142+
}
143+
base_instance_name = "vm"
144+
target_size = 2
145+
}
146+
`, context)
147+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
subcategory: "Apphub"
3+
description: |-
4+
Get information about a discovered workload.
5+
---
6+
7+
# google\_apphub\_discovered_workload
8+
9+
Get information about a discovered workload from its uri.
10+
11+
12+
## Example Usage
13+
14+
15+
```hcl
16+
data "google_apphub_discovered_workload" "my-workload" {
17+
location = "us-central1"
18+
workload_uri = "my-workload-uri"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `project` - The host project of the discovered workload.
27+
* `workload_uri` - (Required) The uri of the workload (instance group managed by the Instance Group Manager). Example: "//compute.googleapis.com/projects/1/regions/us-east1/instanceGroups/id1"
28+
* `location` - (Required) The location of the discovered workload.
29+
30+
## Attributes Reference
31+
32+
In addition to the arguments listed above, the following computed attributes are exported:
33+
34+
* `name` - Resource name of a Workload. Format: "projects/{host-project-id}/locations/{location}/applications/{application-id}/workloads/{workload-id}".
35+
36+
* `workload_reference` - Reference to an underlying networking resource that can comprise a Workload. Structure is [documented below](#nested_workload_reference)
37+
38+
<a name="nested_workload_reference"></a>The `workload_reference` block supports:
39+
40+
* `uri` - The underlying resource URI.
41+
42+
* `workload_properties` - Properties of an underlying compute resource that can comprise a Workload. Structure is [documented below](#nested_workload_properties)
43+
44+
<a name="nested_workload_properties"></a>The `workload_properties` block supports:
45+
46+
* `gcp_project` - The service project identifier that the underlying cloud resource resides in.
47+
48+
* `location` - The location that the underlying resource resides in.
49+
50+
* `zone` - The location that the underlying resource resides in if it is zonal.

0 commit comments

Comments
 (0)