-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a datasource for retrieving a list of projects (#3178)
<!-- This change is generated by MagicModules. --> /cc @rileykarson
- Loading branch information
1 parent
4f7decf
commit 08a2b15
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters