Skip to content

Commit

Permalink
Promote google_cloud_asset_resources_search_all datasource to ga (Goo…
Browse files Browse the repository at this point in the history
  • Loading branch information
averbuks authored and ma-g-22 committed May 21, 2024
1 parent a7b411d commit dc1fc84
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
<% unless version == 'ga' -%>
"google_cloud_asset_resources_search_all": cloudasset.DataSourceGoogleCloudAssetResourcesSearchAll(),
<% end -%>
"google_cloud_asset_search_all_resources": cloudasset.DataSourceGoogleCloudAssetSearchAllResources(),
"google_cloud_identity_groups": cloudidentity.DataSourceGoogleCloudIdentityGroups(),
"google_cloud_identity_group_memberships": cloudidentity.DataSourceGoogleCloudIdentityGroupMemberships(),
"google_cloud_identity_group_lookup": cloudidentity.DataSourceGoogleCloudIdentityGroupLookup(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package cloudasset
<% unless version == 'ga' -%>
import (
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
Expand Down Expand Up @@ -102,7 +101,7 @@ func datasourceGoogleCloudAssetResourcesSearchAllRead(d *schema.ResourceData, me
url := fmt.Sprintf("https://cloudasset.googleapis.com/v1p1beta1/%s/resources:searchAll", scope)
params["query"] = query

url, err = addArrayQueryParam(url, "asset_types", assetTypes)
url, err = transport_tpg.AddArrayQueryParams(url, "asset_types", assetTypes)
if err != nil {
return fmt.Errorf("Error setting asset_types: %s", err)
}
Expand All @@ -129,7 +128,7 @@ func datasourceGoogleCloudAssetResourcesSearchAllRead(d *schema.ResourceData, me
return fmt.Errorf("Error searching resources: %s", err)
}

pageResults := flattenDatasourceGoogleCloudAssetResourcesList(res["results"])
pageResults := vflattenDatasourceGoogleCloudAssetResourcesSearchAllList(res["results"])
results = append(results, pageResults...)

pToken, ok := res["nextPageToken"]
Expand Down Expand Up @@ -157,7 +156,7 @@ func datasourceGoogleCloudAssetResourcesSearchAllRead(d *schema.ResourceData, me
return nil
}

func flattenDatasourceGoogleCloudAssetResourcesList(v interface{}) []map[string]interface{} {
func vflattenDatasourceGoogleCloudAssetResourcesSearchAllList(v interface{}) []map[string]interface{} {
if v == nil {
return make([]map[string]interface{}, 0)
}
Expand Down Expand Up @@ -211,17 +210,4 @@ func flattenDatasourceGoogleCloudAssetResourcesList(v interface{}) []map[string]
return results
}

func addArrayQueryParam(rawurl string, param string, values []interface{}) (string, error) {
u, err := url.Parse(rawurl)
if err != nil {
return "", err
}
q := u.Query()
for _, v := range values {
q.Add(param, v.(string))
}
u.RawQuery = q.Encode()
return u.String(), nil
}

<% end -%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
<% autogen_exception -%>
package cloudasset

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 DataSourceGoogleCloudAssetSearchAllResources() *schema.Resource {
return &schema.Resource{
Read: datasourceGoogleCloudAssetSearchAllResourcesRead,
Schema: map[string]*schema.Schema{
"scope": {
Type: schema.TypeString,
Required: true,
},
"query": {
Type: schema.TypeString,
Optional: true,
},
"asset_types": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
"results": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"asset_type": {
Type: schema.TypeString,
Computed: true,
},
"project": {
Type: schema.TypeString,
Computed: true,
},
"folders": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"organization": {
Type: schema.TypeString,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"network_tags": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"kms_keys": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"parent_full_resource_name": {
Type: schema.TypeString,
Computed: true,
},
"parent_asset_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

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

params := make(map[string]string)
results := make([]map[string]interface{}, 0)

scope := d.Get("scope").(string)
query := d.Get("query").(string)
assetTypes := d.Get("asset_types").([]interface{})

url := fmt.Sprintf("https://cloudasset.googleapis.com/v1/%s:searchAllResources", scope)
params["query"] = query

url, err = transport_tpg.AddArrayQueryParams(url, "asset_types", assetTypes)
if err != nil {
return fmt.Errorf("Error setting asset_types: %s", err)
}

for {
url, err := transport_tpg.AddQueryParams(url, params)
if err != nil {
return err
}

var project string
if config.UserProjectOverride && config.BillingProject != "" {
project = config.BillingProject
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Project: project,
Method: "GET",
RawURL: url,
UserAgent: userAgent,
})
if err != nil {
return fmt.Errorf("Error searching resources: %s", err)
}

pageResults := flattenDatasourceGoogleCloudAssetSearchAllResources(res["results"])
results = append(results, pageResults...)

pToken, ok := res["nextPageToken"]
if ok && pToken != nil && pToken.(string) != "" {
params["pageToken"] = pToken.(string)
} else {
break
}
}

if err := d.Set("results", results); err != nil {
return fmt.Errorf("Error searching resources: %s", err)
}

if err := d.Set("query", query); err != nil {
return fmt.Errorf("Error setting query: %s", err)
}

if err := d.Set("asset_types", assetTypes); err != nil {
return fmt.Errorf("Error setting asset_types: %s", err)
}

d.SetId(scope)

return nil
}

func flattenDatasourceGoogleCloudAssetSearchAllResources(v interface{}) []map[string]interface{} {
if v == nil {
return make([]map[string]interface{}, 0)
}

ls := v.([]interface{})
results := make([]map[string]interface{}, 0, len(ls))
for _, raw := range ls {
p := raw.(map[string]interface{})

var mName, mAssetType, mProject, mFolders, mOrganization, mDisplayName, mDescription, mLocation, mLabels, mNetworkTags, mKmsKeys, mCreateTime, mUpdateTime, mState, mParentFullResourceName, mParentAssetType interface{}
if pName, ok := p["name"]; ok {
mName = pName
}
if pAssetType, ok := p["assetType"]; ok {
mAssetType = pAssetType
}
if pProject, ok := p["project"]; ok {
mProject = pProject
}
if pFolders, ok := p["folders"]; ok {
mFolders = pFolders
}
if pOrganization, ok := p["organization"]; ok {
mOrganization = pOrganization
}
if pDisplayName, ok := p["displayName"]; ok {
mDisplayName = pDisplayName
}
if pDescription, ok := p["description"]; ok {
mDescription = pDescription
}
if pLocation, ok := p["location"]; ok {
mLocation = pLocation
}
if pLabels, ok := p["labels"]; ok {
mLabels = pLabels
}
if pNetworkTags, ok := p["networkTags"]; ok {
mNetworkTags = pNetworkTags
}
if pKmsKeys, ok := p["kmsKeys"]; ok {
mKmsKeys = pKmsKeys
}
if pCreateTime, ok := p["createTime"]; ok {
mCreateTime = pCreateTime
}
if pUpdateTime, ok := p["updateTime"]; ok {
mUpdateTime = pUpdateTime
}
if pState, ok := p["state"]; ok {
mState = pState
}
if pParentFullResourceName, ok := p["parentFullResourceName"]; ok {
mParentFullResourceName = pParentFullResourceName
}
if pParentAssetType, ok := p["parentAssetType"]; ok {
mParentAssetType = pParentAssetType
}
results = append(results, map[string]interface{}{
"name": mName,
"asset_type": mAssetType,
"project": mProject,
"folders": mFolders,
"organization": mOrganization,
"display_name": mDisplayName,
"description": mDescription,
"location": mLocation,
"labels": mLabels,
"network_tags": mNetworkTags,
"kms_keys": mKmsKeys,
"create_time": mCreateTime,
"update_time": mUpdateTime,
"state": mState,
"parent_full_resource_name": mParentFullResourceName,
"parent_asset_type": mParentAssetType,
})
}

return results
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<% autogen_exception -%>
package cloudasset_test

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGoogleCloudAssetSearchAllResources_basic(t *testing.T) {
t.Parallel()

project := envvar.GetTestProjectFromEnv()

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccCheckGoogleCloudAssetProjectResourcesList(project),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("data.google_cloud_asset_search_all_resources.resources",
"results.0.asset_type", regexp.MustCompile("cloudresourcemanager.googleapis.com/Project")),
resource.TestMatchResourceAttr("data.google_cloud_asset_search_all_resources.resources",
"results.0.display_name", regexp.MustCompile(project)),
resource.TestMatchResourceAttr("data.google_cloud_asset_search_all_resources.resources",
"results.0.name", regexp.MustCompile(fmt.Sprintf("//cloudresourcemanager.googleapis.com/projects/%s", project))),
resource.TestCheckResourceAttrSet("data.google_cloud_asset_search_all_resources.resources", "results.0.location"),
resource.TestCheckResourceAttrSet("data.google_cloud_asset_search_all_resources.resources", "results.0.project"),
),
},
},
})
}

func testAccCheckGoogleCloudAssetProjectResourcesList(project string) string {
return fmt.Sprintf(`
data google_cloud_asset_search_all_resources resources {
scope = "projects/%s"
asset_types = [
"cloudresourcemanager.googleapis.com/Project"
]
}
`, project)
}

Loading

0 comments on commit dc1fc84

Please sign in to comment.