Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data source for AppHub discovered workload #10107

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8816596
Add data source for Apphub discovered workload
praseedhaPK Mar 4, 2024
37713b3
Add data source for Apphub discovered workload
praseedhaPK Mar 4, 2024
26539d0
Add data source for Apphub Discovered Workload
praseedhaPK Mar 5, 2024
49c9ded
Resolved comments
praseedhaPK Mar 6, 2024
8255dfa
Resolved comments
praseedhaPK Mar 6, 2024
7782050
Resolved comments
praseedhaPK Mar 6, 2024
ab05c4c
Resolved comments
praseedhaPK Mar 6, 2024
b84b99b
Add tests and documentation for data source discovered workload
praseedhaPK Mar 7, 2024
af74301
Resolved comments and added tests
praseedhaPK Mar 7, 2024
4f2b563
Resolved comments
praseedhaPK Mar 7, 2024
395f953
Merge branch 'GoogleCloudPlatform:main' into DataSourceDiscoveredWork…
praseedhaPK Mar 8, 2024
5c7cf1e
Added modifications in tests, data source and documentation
praseedhaPK Mar 8, 2024
32a9f7a
Added modifications in tests, data source and documentation
praseedhaPK Mar 8, 2024
a8bcf05
Added modifications in tests, data source and documentation
praseedhaPK Mar 8, 2024
43b660e
Verified tests
praseedhaPK Mar 8, 2024
cecf207
Tests Verification
praseedhaPK Mar 10, 2024
29214e7
Tests Verification
praseedhaPK Mar 10, 2024
c07bd65
Updated logic to obtain workload_uri
praseedhaPK Mar 11, 2024
fe95787
Updated logic to obtain workload_uri
praseedhaPK Mar 11, 2024
184f5ed
Resolved comments
praseedhaPK Mar 11, 2024
b2ad74e
Resolved comments
praseedhaPK Mar 11, 2024
3b2ffbc
Add billing account and shorten service project name
praseedhaPK Mar 11, 2024
fc5ae10
Add header
praseedhaPK Mar 11, 2024
b9c6ded
Change the project name to start with tf-test
praseedhaPK Mar 11, 2024
ceb46ff
Resolved comments
praseedhaPK Mar 11, 2024
80782ef
Lint changes
praseedhaPK Mar 11, 2024
403a0ce
Removing two sleeps
praseedhaPK Mar 11, 2024
9dd0ac9
Removing two sleeps
praseedhaPK Mar 11, 2024
809a921
Modifying documentation
praseedhaPK Mar 11, 2024
68c0a54
Resolved tests
praseedhaPK Mar 11, 2024
e34a264
Resolved comments
praseedhaPK Mar 11, 2024
e62a150
Resolved comments
praseedhaPK Mar 11, 2024
f831528
logic modification
praseedhaPK Mar 12, 2024
269e7fd
Update Retry logic
praseedhaPK Mar 12, 2024
10e4371
Update Retry logic
praseedhaPK Mar 12, 2024
55b51ab
Resolved tests
praseedhaPK Mar 12, 2024
4cbbe1f
Adding workload_uri description
praseedhaPK Mar 12, 2024
8cfd3d9
Adding workload_uri description
praseedhaPK Mar 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-provider-google/google/services/<%= service -%>"
<% end -%>

"github.com/hashicorp/terraform-provider-google/google/services/apphub"
praseedhaPK marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hashicorp/terraform-provider-google/google/services/composer"
"github.com/hashicorp/terraform-provider-google/google/services/container"
"github.com/hashicorp/terraform-provider-google/google/services/containeraws"
Expand All @@ -27,6 +28,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_alloydb_locations": alloydb.DataSourceAlloydbLocations(),
"google_alloydb_supported_database_flags": alloydb.DataSourceAlloydbSupportedDatabaseFlags(),
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
"google_apphub_discovered_workload": apphub.DataSourceApphubDiscoveredWorkload(),
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
<% unless version == 'ga' -%>
"google_backup_dr_management_server": backupdr.DataSourceGoogleCloudBackupDRService(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package apphub

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceApphubDiscoveredWorkload() *schema.Resource {
return &schema.Resource{
Read: dataSourceApphubDiscoveredWorkloadRead,
Schema: map[string]*schema.Schema{
"location":{
Type: schema.TypeString,
Required: true,
},
"workload_uri": {
Type: schema.TypeString,
Required: true,
},
"discovered_workload": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"workload_reference": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uri":{
Type: schema.TypeString,
Computed: true,
},
},
},
},
"workload_properties": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"gcp_project":{
Type: schema.TypeString,
Computed: true,
},
"location":{
Type: schema.TypeString,
Computed: true,
},
"zone":{
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
}
}

func dataSourceApphubDiscoveredWorkloadRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{ApphubBasePath}}projects/{{project}}/locations/{{location}}/discoveredWorkloads:lookup?uri={{workload_uri}}" )
if err != nil {
return err
}

billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}

var res map[string]interface{}

err = transport_tpg.Retry(transport_tpg.RetryOptions{
RetryFunc: func() (rerr error) {
res, rerr = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
})
return rerr
},
Timeout: d.Timeout(schema.TimeoutRead),
})

if err != nil {
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("ApphubDiscoveredWorkload %q", d.Id()), url)
}

if err := d.Set("discovered_workload", flattenApphubDiscoveredWorkload(res["discoveredWorkload"], d, config)); err != nil {
return fmt.Errorf("Error setting discovered workload: %s", err)
}

id, err := tpgresource.ReplaceVars(d, config, "{{workload_uri}}")
praseedhaPK marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
d.SetId(id)

return nil

}

func flattenApphubDiscoveredWorkload(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}

transformed := make(map[string]interface{})
transformed["name"] = flattenApphubDiscoveredWorkloadDataName(original["name"], d, config)
transformed["workload_reference"] = flattenApphubWorkloadReference(original["workloadReference"], d, config)
transformed["workload_properties"] = flattenApphubWorkloadProperties(original["workloadProperties"], d, config)
return []interface{}{transformed}
}


func flattenApphubWorkloadReference(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["uri"] = flattenApphubDiscoveredWorkloadDataUri(original["uri"], d, config)
return []interface{}{transformed}
}

func flattenApphubWorkloadProperties(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["gcp_project"] = flattenApphubDiscoveredWorkloadDataGcpProject(original["gcpProject"], d, config)
transformed["location"] = flattenApphubDiscoveredWorkloadDataLocation(original["location"], d, config)
transformed["zone"] = flattenApphubDiscoveredWorkloadDataZone(original["zone"], d, config)
return []interface{}{transformed}
}

func flattenApphubDiscoveredWorkloadDataName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenApphubDiscoveredWorkloadDataUri(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenApphubDiscoveredWorkloadDataGcpProject(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenApphubDiscoveredWorkloadDataLocation(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenApphubDiscoveredWorkloadDataZone(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}