Skip to content

Commit 07bb066

Browse files
Add data source for retrieving a project iam custom role (#13302) (#21866)
[upstream:8e77cbb4e60799af9dce31291408dc1cb40fbe9b] Signed-off-by: Modular Magician <[email protected]>
1 parent 4846fa0 commit 07bb066

File tree

6 files changed

+155
-4
lines changed

6 files changed

+155
-4
lines changed

.changelog/13302.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_project_iam_custom_role`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
308308
"google_project": resourcemanager.DataSourceGoogleProject(),
309309
"google_projects": resourcemanager.DataSourceGoogleProjects(),
310310
"google_project_ancestry": resourcemanager.DataSourceGoogleProjectAncestry(),
311+
"google_project_iam_custom_role": resourcemanager.DataSourceGoogleProjectIamCustomRole(),
311312
"google_project_iam_custom_roles": resourcemanager.DataSourceGoogleProjectIamCustomRoles(),
312313
"google_project_organization_policy": resourcemanager.DataSourceGoogleProjectOrganizationPolicy(),
313314
"google_project_service": resourcemanager.DataSourceGoogleProjectService(),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package resourcemanager
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
10+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
11+
)
12+
13+
func DataSourceGoogleProjectIamCustomRole() *schema.Resource {
14+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceGoogleProjectIamCustomRole().Schema)
15+
16+
dsSchema["project"].Computed = false
17+
dsSchema["project"].Optional = true
18+
dsSchema["role_id"].Computed = false
19+
dsSchema["role_id"].Required = true
20+
21+
return &schema.Resource{
22+
Read: dataSourceProjectIamCustomRoleRead,
23+
Schema: dsSchema,
24+
}
25+
}
26+
27+
func dataSourceProjectIamCustomRoleRead(d *schema.ResourceData, meta interface{}) error {
28+
config := meta.(*transport_tpg.Config)
29+
30+
project, err := tpgresource.GetProject(d, config)
31+
if err != nil {
32+
return fmt.Errorf("Error fetching project for service accounts: %s", err)
33+
}
34+
35+
roleId := d.Get("role_id").(string)
36+
d.SetId(fmt.Sprintf("projects/%s/roles/%s", project, roleId))
37+
38+
id := d.Id()
39+
40+
if err := resourceGoogleProjectIamCustomRoleRead(d, meta); err != nil {
41+
return err
42+
}
43+
44+
if d.Id() == "" {
45+
return fmt.Errorf("Role %s not found!", id)
46+
}
47+
48+
return nil
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package resourcemanager_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-provider-google/google/acctest"
11+
"github.com/hashicorp/terraform-provider-google/google/envvar"
12+
)
13+
14+
func TestAccDataSourceGoogleProjectIamCustomRole_basic(t *testing.T) {
15+
t.Parallel()
16+
17+
project := envvar.GetTestProjectFromEnv()
18+
roleId := "tfIamCustomRole" + acctest.RandString(t, 10)
19+
20+
acctest.VcrTest(t, resource.TestCase{
21+
PreCheck: func() { acctest.AccTestPreCheck(t) },
22+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccCheckGoogleProjectIamCustomRoleConfig(project, roleId),
26+
Check: resource.ComposeTestCheckFunc(
27+
acctest.CheckDataSourceStateMatchesResourceState(
28+
"data.google_project_iam_custom_role.this",
29+
"google_project_iam_custom_role.this",
30+
),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccCheckGoogleProjectIamCustomRoleConfig(project string, roleId string) string {
38+
return fmt.Sprintf(`
39+
locals {
40+
project = "%s"
41+
role_id = "%s"
42+
}
43+
44+
resource "google_project_iam_custom_role" "this" {
45+
project = local.project
46+
role_id = local.role_id
47+
title = "Terraform Test"
48+
49+
permissions = [
50+
"iam.roles.create",
51+
"iam.roles.delete",
52+
"iam.roles.list",
53+
]
54+
}
55+
56+
data "google_project_iam_custom_role" "this" {
57+
project = google_project_iam_custom_role.this.project
58+
role_id = google_project_iam_custom_role.this.role_id
59+
}
60+
`, project, roleId)
61+
}

google/services/resourcemanager/data_source_google_project_iam_custom_roles.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
func DataSourceGoogleProjectIamCustomRoles() *schema.Resource {
1717
return &schema.Resource{
18-
Read: dataSourceProjectIamCustomRoleRead,
18+
Read: dataSourceProjectIamCustomRolesRead,
1919
Schema: map[string]*schema.Schema{
2020
"project": {
2121
Type: schema.TypeString,
@@ -30,7 +30,7 @@ func DataSourceGoogleProjectIamCustomRoles() *schema.Resource {
3030
Type: schema.TypeString,
3131
Optional: true,
3232
Default: "BASIC",
33-
ValidateFunc: validateView,
33+
ValidateFunc: validateViewProjectIamCustomRoles,
3434
},
3535
"roles": {
3636
Type: schema.TypeList,
@@ -77,7 +77,7 @@ func DataSourceGoogleProjectIamCustomRoles() *schema.Resource {
7777
}
7878
}
7979

80-
func validateView(val interface{}, key string) ([]string, []error) {
80+
func validateViewProjectIamCustomRoles(val interface{}, key string) ([]string, []error) {
8181
v := val.(string)
8282
var errs []error
8383

@@ -88,7 +88,7 @@ func validateView(val interface{}, key string) ([]string, []error) {
8888
return nil, errs
8989
}
9090

91-
func dataSourceProjectIamCustomRoleRead(d *schema.ResourceData, meta interface{}) error {
91+
func dataSourceProjectIamCustomRolesRead(d *schema.ResourceData, meta interface{}) error {
9292
config := meta.(*transport_tpg.Config)
9393
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
9494
if err != nil {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
subcategory: "Cloud Platform"
3+
description: |-
4+
Get information about a Google Cloud IAM Custom Role from a project.
5+
---
6+
7+
# google_project_iam_custom_role
8+
9+
Get information about a Google Cloud Project IAM Custom Role. Note that you must have the `roles/iam.roleViewer` role (or equivalent permissions) at the project level to use this datasource.
10+
11+
```hcl
12+
data "google_project_iam_custom_role" "example" {
13+
project = "your-project-id"
14+
role_id = "your-role-id"
15+
}
16+
17+
resource "google_project_iam_member" "project" {
18+
project = "your-project-id"
19+
role = data.google_project_iam_custom_role.example.name
20+
member = "user:[email protected]"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
* `role_id` - (Required) The role id that has been used for this role.
29+
30+
* `project` - (Optional) The project were the custom role has been created in. Defaults to the provider project configuration.
31+
32+
## Attributes Reference
33+
34+
In addition to the arguments listed above, the following attributes are exported:
35+
36+
See [google_project_iam_custom_role](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_project_iam_custom_role) resource for details of the available attributes.
37+

0 commit comments

Comments
 (0)