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 data source for retrieving a project iam custom role #13302

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_project": resourcemanager.DataSourceGoogleProject(),
"google_projects": resourcemanager.DataSourceGoogleProjects(),
"google_project_ancestry": resourcemanager.DataSourceGoogleProjectAncestry(),
"google_project_iam_custom_role": resourcemanager.DataSourceGoogleProjectIamCustomRole(),
"google_project_organization_policy": resourcemanager.DataSourceGoogleProjectOrganizationPolicy(),
"google_project_service": resourcemanager.DataSourceGoogleProjectService(),
"google_pubsub_subscription": pubsub.DataSourceGooglePubsubSubscription(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package resourcemanager

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceGoogleProjectIamCustomRole() *schema.Resource {
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceGoogleProjectIamCustomRole().Schema)

dsSchema["project"].Computed = false
dsSchema["project"].Optional = true
dsSchema["role_id"].Computed = false
dsSchema["role_id"].Required = true

return &schema.Resource{
Read: dataSourceProjectIamCustomRoleRead,
Schema: dsSchema,
}
}

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

project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for service accounts: %s", err)
}

roleId := d.Get("role_id").(string)
d.SetId(fmt.Sprintf("projects/%s/roles/%s", project, roleId))

id := d.Id()

if err := resourceGoogleProjectIamCustomRoleRead(d, meta); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("Role %s not found!", id)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package resourcemanager_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
)

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

project := envvar.GetTestProjectFromEnv()
roleId := "tfIamCustomRole" + acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccCheckGoogleProjectIamCustomRoleConfig(project, roleId),
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceState(
"data.google_project_iam_custom_role.this",
"google_project_iam_custom_role.this",
),
),
},
},
})
}

func testAccCheckGoogleProjectIamCustomRoleConfig(project string, roleId string) string {
return fmt.Sprintf(`
locals {
project = "%s"
role_id = "%s"
}

resource "google_project_iam_custom_role" "this" {
project = local.project
role_id = local.role_id
title = "Terraform Test"

permissions = [
"iam.roles.create",
"iam.roles.delete",
"iam.roles.list",
]
}

data "google_project_iam_custom_role" "this" {
project = google_project_iam_custom_role.this.project
role_id = google_project_iam_custom_role.this.role_id
}
`, project, roleId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
subcategory: "Cloud Platform"
description: |-
Get information about a Google Cloud IAM Custom Role from a project.
---

# google_project_iam_custom_role

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.

```hcl
data "google_project_iam_custom_role" "example" {
project = "your-project-id"
role_id = "your-role-id"
}

resource "google_project_iam_member" "project" {
project = "your-project-id"
role = data.google_project_iam_custom_role.example.name
member = "user:[email protected]"
}
```

## Argument Reference

The following arguments are supported:

* `role_id` - (Required) The role id that has been used for this role.

* `project` - (Optional) The project were the custom role has been created in. Defaults to the provider project configuration.

## Attributes Reference

In addition to the arguments listed above, the following attributes are exported:

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.

Loading