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 service #10105

Merged
merged 24 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ddf5857
Add data source for apphub discovered service
gurankitt Mar 4, 2024
67b35df
Add data source for apphub discovered service'
gurankitt Mar 4, 2024
c1d55e9
Add data source for apphub discovered service
gurankitt Mar 4, 2024
a9a8103
resolved comments
gurankitt Mar 6, 2024
f3f5e02
retry logic added
gurankitt Mar 6, 2024
361919f
add tests and documentation
gurankitt Mar 7, 2024
bcaf67d
Corrected tests and added project field in the data source
gurankitt Mar 8, 2024
a8d409f
changed id field
gurankitt Mar 8, 2024
607e5df
Added random_suffix for the resources created and enabled iam policy …
gurankitt Mar 8, 2024
671b017
modified retry logic
gurankitt Mar 8, 2024
9284d56
Modified schema for the data source, and tests
gurankitt Mar 8, 2024
4239f4f
Removed IAM permission blocks
gurankitt Mar 8, 2024
9c075da
Add dependency for compute api
gurankitt Mar 8, 2024
b61d3e5
Merge branch 'GoogleCloudPlatform:main' into datasource
gurankitt Mar 8, 2024
86b1a6b
Resolve merge conflict
gurankitt Mar 8, 2024
9f2d90e
Add time sleep
gurankitt Mar 11, 2024
6d0ca32
Modified test function name
gurankitt Mar 11, 2024
7102010
Shorten service project name
gurankitt Mar 11, 2024
e4c5f15
Add billing account
gurankitt Mar 11, 2024
1325323
corrected get env variable function call
gurankitt Mar 11, 2024
afbdf9a
Modified project id
gurankitt Mar 11, 2024
a2a9cd1
Combined time delay
gurankitt Mar 11, 2024
9c7c92e
Resolving comments
gurankitt Mar 11, 2024
a5d541b
Remove retry logic and add time sleep for resource ingestion
gurankitt 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 @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-provider-google/google/services/dataflow"
"github.com/hashicorp/terraform-provider-google/google/services/servicenetworking"
"github.com/hashicorp/terraform-provider-google/google/tpgiamresource"
"github.com/hashicorp/terraform-provider-google/google/services/apphub"
)

// Datasources
Expand All @@ -28,6 +29,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_alloydb_supported_database_flags": alloydb.DataSourceAlloydbSupportedDatabaseFlags(),
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
"google_app_engine_default_service_account": appengine.DataSourceGoogleAppEngineDefaultServiceAccount(),
"google_apphub_discovered_service": apphub.DataSourceApphubDiscoveredService(),
<% unless version == 'ga' -%>
"google_backup_dr_management_server": backupdr.DataSourceGoogleCloudBackupDRService(),
<% end -%>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package apphub

import (
"fmt"
"time"

"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 DataSourceApphubDiscoveredService() *schema.Resource {
roaks3 marked this conversation as resolved.
Show resolved Hide resolved
return &schema.Resource{
Read: dataSourceApphubDiscoveredServiceRead,
Schema: map[string]*schema.Schema{
"location":{
roaks3 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
},
"service_uri": {
Type: schema.TypeString,
Required: true,
},
"discovered_service": {
roaks3 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"service_reference": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uri":{
Type: schema.TypeString,
Computed: true,
},
"path":{
Type: schema.TypeString,
Computed: true,
},
},
},
},
"service_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 dataSourceApphubDiscoveredServiceRead(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}}/discoveredServices:lookup?uri={{service_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
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
})
fmt.Println(res, err, "Test print")
roaks3 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ApphubDiscoveredService %q", d.Id()))
roaks3 marked this conversation as resolved.
Show resolved Hide resolved
}

if err := d.Set("discovered_service", flattenApphubDiscoveredService(res["discoveredService"], d, config)); err != nil {
return fmt.Errorf("Error setting discovered service: %s", err)
}

d.SetId(time.Now().UTC().String())
roaks3 marked this conversation as resolved.
Show resolved Hide resolved
return nil

}

func flattenApphubDiscoveredService(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"] = flattenApphubDiscoveredServiceDataName(original["name"], d, config)
transformed["service_reference"] = flattenApphubServiceReference(original["serviceReference"], d, config)
transformed["service_properties"] = flattenApphubServiceProperties(original["serviceProperties"], d, config)
return []interface{}{transformed}
}


func flattenApphubServiceReference(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"] = flattenApphubDiscoveredServiceDataUri(original["uri"], d, config)
transformed["path"] = flattenApphubDiscoveredServiceDataPath(original["path"], d, config)
return []interface{}{transformed}
}

func flattenApphubServiceProperties(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"] = flattenApphubDiscoveredServiceDataGcpProject(original["gcp_project"], d, config)
transformed["location"] = flattenApphubDiscoveredServiceDataLocation(original["location"], d, config)
transformed["zone"] = flattenApphubDiscoveredServiceDataZone(original["zone"], d, config)
return []interface{}{transformed}
}

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

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

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

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

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

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


Loading