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 additional fields to projects data source #2440

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .changelog/3868.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource-manager: added additional fields to `google_projects` datasource
```
64 changes: 61 additions & 3 deletions google-beta/data_source_google_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ func dataSourceGoogleProjects() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `A set of key/value label pairs assigned on a project.`,
},
"parent": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `An optional reference to a parent Resource.`,
},
"number": {
Type: schema.TypeString,
Computed: true,
Description: `The numeric identifier of the project.`,
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: `The numeric identifier of the project.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The optional user-assigned display name of the Project.`,
},
},
},
},
Expand Down Expand Up @@ -79,11 +110,38 @@ func flattenDatasourceGoogleProjectsList(v interface{}) []map[string]interface{}
projects := make([]map[string]interface{}, 0, len(ls))
for _, raw := range ls {
p := raw.(map[string]interface{})

var mId, mNumber, mLabels, mLifecycleState, mName, mCreateTime, mParent interface{}
if pId, ok := p["projectId"]; ok {
projects = append(projects, map[string]interface{}{
"project_id": pId,
})
mId = pId
}
if pNumber, ok := p["projectNumber"]; ok {
mNumber = pNumber
}
if pName, ok := p["name"]; ok {
mName = pName
}
if pLabels, ok := p["labels"]; ok {
mLabels = pLabels
}
if pLifecycleState, ok := p["lifecycleState"]; ok {
mLifecycleState = pLifecycleState
}
if pCreateTime, ok := p["createTime"]; ok {
mCreateTime = pCreateTime
}
if pParent, ok := p["parent"]; ok {
mParent = pParent
}
projects = append(projects, map[string]interface{}{
"project_id": mId,
"number": mNumber,
"name": mName,
"labels": mLabels,
"lifecycle_state": mLifecycleState,
"create_time": mCreateTime,
"parent": mParent,
})
}

return projects
Expand Down
6 changes: 6 additions & 0 deletions google-beta/data_source_google_projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func TestAccDataSourceGoogleProjects_basic(t *testing.T) {
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"),
resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.name"),
resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.number"),
resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.lifecycle_state"),
resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.parent.id"),
resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.parent.type"),
resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.create_time"),
),
},
},
Expand Down