Skip to content

Commit

Permalink
Add additional fields to projects data source (#3868) (#2440)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Aug 28, 2020
1 parent d5ec235 commit 7ffa212
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
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

0 comments on commit 7ffa212

Please sign in to comment.