Skip to content

Commit

Permalink
Add a datasource for retrieving a list of projects (#493)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @rileykarson
  • Loading branch information
modular-magician authored and rileykarson committed Mar 4, 2019
1 parent 54a56b5 commit 065adad
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 8 deletions.
78 changes: 78 additions & 0 deletions google-beta/data_source_google_projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceGoogleProjects() *schema.Resource {
return &schema.Resource{
Read: datasourceGoogleProjectsRead,
Schema: map[string]*schema.Schema{
"filter": {
Type: schema.TypeString,
Required: true,
},
"projects": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func datasourceGoogleProjectsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

params := make(map[string]string)

params["filter"] = d.Get("filter").(string)
url := "https://cloudresourcemanager.googleapis.com/v1/projects"

url, err := addQueryParams(url, params)
if err != nil {
return err
}

res, err := sendRequest(config, "GET", url, nil)
if err != nil {
return fmt.Errorf("Error retrieving projects: %s", err)
}

if err := d.Set("projects", flattenDatasourceGoogleProjectsProjects(res["projects"], d)); err != nil {
return fmt.Errorf("Error retrieving projects: %s", err)
}

d.SetId(d.Get("filter").(string))

return nil
}

func flattenDatasourceGoogleProjectsProjects(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
}

l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"project_id": original["projectId"],
})
}

return transformed
}
36 changes: 36 additions & 0 deletions google-beta/data_source_google_projects_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

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

project := getTestProjectFromEnv()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGoogleProjectsConfig(project),
Check: resource.ComposeTestCheckFunc(
// We can't guarantee no project won't have our project ID as a prefix, so we'll check set-ness rather than correctness
resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.project_id"),
),
},
},
})
}

func testAccCheckGoogleProjectsConfig(project string) string {
return fmt.Sprintf(`
data "google_projects" "my-project" {
filter = "projectId:%s"
}
`, project)
}
1 change: 1 addition & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func Provider() terraform.ResourceProvider {
"google_netblock_ip_ranges": dataSourceGoogleNetblockIpRanges(),
"google_organization": dataSourceGoogleOrganization(),
"google_project": dataSourceGoogleProject(),
"google_projects": dataSourceGoogleProjects(),
"google_project_services": dataSourceGoogleProjectServices(),
"google_service_account": dataSourceGoogleServiceAccount(),
"google_service_account_key": dataSourceGoogleServiceAccountKey(),
Expand Down
16 changes: 8 additions & 8 deletions google-beta/resource_dns_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func flattenDnsPolicyNetworksNetworkUrl(v interface{}, d *schema.ResourceData) i
return v
}

func expandDnsPolicyAlternativeNameServerConfig(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyAlternativeNameServerConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
Expand All @@ -430,7 +430,7 @@ func expandDnsPolicyAlternativeNameServerConfig(v interface{}, d *schema.Resourc
return transformed, nil
}

func expandDnsPolicyAlternativeNameServerConfigTargetNameServers(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyAlternativeNameServerConfigTargetNameServers(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
v = v.(*schema.Set).List()
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
Expand All @@ -453,23 +453,23 @@ func expandDnsPolicyAlternativeNameServerConfigTargetNameServers(v interface{},
return req, nil
}

func expandDnsPolicyAlternativeNameServerConfigTargetNameServersIpv4Address(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyAlternativeNameServerConfigTargetNameServersIpv4Address(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandDnsPolicyDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandDnsPolicyEnableInboundForwarding(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyEnableInboundForwarding(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandDnsPolicyName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandDnsPolicyNetworks(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyNetworks(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
v = v.(*schema.Set).List()
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
Expand All @@ -492,6 +492,6 @@ func expandDnsPolicyNetworks(v interface{}, d *schema.ResourceData, config *Conf
return req, nil
}

func expandDnsPolicyNetworksNetworkUrl(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
func expandDnsPolicyNetworksNetworkUrl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
43 changes: 43 additions & 0 deletions website/docs/d/google_projects.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: "google"
page_title: "Google: google_projects"
sidebar_current: "docs-google-datasource-projects"
description: |-
Retrieve a set of projects based on a filter.
---

# google\_projects

Retrieve information about a set of projects based on a filter. See the
[REST API](https://cloud.google.com/resource-manager/reference/rest/v1/projects/list)
for more details.

## Example Usage - searching for projects about to be deleted in an org

```hcl
data "google_projects" "my-org-projects" {
filter = "parent.id:012345678910 lifecycleState:DELETE_REQUESTED"
}
data "google_project" "deletion-candidate" {
project_id = "${data.google_projects.my-org-projects.projects.0.project_id}"
}
```

## Argument Reference

The following arguments are supported:

* `filter` - (Optional) A string filter as defined in the [REST API](https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#query-parameters).


## Attributes Reference

The following attributes are exported:

* `projects` - A list of projects matching the provided filter. Structure is defined below.

The `projects` block supports:

* `project_id` - The project id of the project.

3 changes: 3 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<li<%= sidebar_current("docs-google-datasource-project") %>>
<a href="/docs/providers/google/d/google_project.html">google_project</a>
</li>
<li<%= sidebar_current("docs-google-datasource-projects") %>>
<a href="/docs/providers/google/d/google_projects.html">google_projects</a>
</li>
<li<%= sidebar_current("docs-google-datasource-service-account") %>>
<a href="/docs/providers/google/d/datasource_google_service_account.html">google_service_account</a>
</li>
Expand Down

0 comments on commit 065adad

Please sign in to comment.