This document describes how to grant the Data Catalog
[tagTemplateUser](https://docs.cloud.google.com/iam/docs/roles-permissions/datacatalog) role to
principals. You can do this after you create a Data Catalog tag template in your
[resource project](https://docs.cloud.google.com/data-catalog/docs/concepts/resource-project). This enables them to create metadata, that is, use
your template to tag data resources. For more information, see
[Attaching tags to Google Cloud resources](https://docs.cloud.google.com/data-catalog/docs/concepts/iam#attaching_tags).

The next section shows how to grant the `tagTemplateUser` role.

> [!IMPORTANT]
> If principals use the tag template to write tags on data resources owned by others, they also need to be granted roles that contain the permissions necessary to write metadata to those resources. See [Attaching tags to Google Cloud resources](https://docs.cloud.google.com/data-catalog/docs/concepts/iam#attaching_tags) for examples of the roles needed to write metadata to BigQuery and Pub/Sub data resources.

## Grant the `tagTemplateUser` role

### Console


### Console

To grant the Data Catalog `tagTemplateUser` role to a principal
on a project, do the following:

1. [Go to IAM](https://console.cloud.google.com/iam-admin) in the Google Cloud console and click the edit
   () button at the right of the
   principal's listing.

2. In the **Edit permissions** dialog, click
   **ADD ANOTHER ROLE** , then click the
   **Select a role** drop-down list.

3. In the **Filter** box, insert
   `Data Catalog TagTemplate User` to display this role, then select it and
   click **SAVE**.

<br />

### Java


Before trying this sample, follow the Java setup instructions in the
[Data Catalog quickstart using
client libraries](https://docs.cloud.google.com/data-catalog/docs/reference/libraries).


For more information, see the
[Data Catalog Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-datacatalog/latest/overview).


To authenticate to Data Catalog, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    import com.google.cloud.datacatalog.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-datacatalog/latest/com.google.cloud.datacatalog.v1.DataCatalogClient.html;
    import com.google.cloud.datacatalog.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-datacatalog/latest/com.google.cloud.datacatalog.v1.TagTemplateName.html;
    import com.google.iam.v1.https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html;
    import com.google.iam.v1.https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html;
    import com.google.iam.v1.https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.html;
    import java.io.IOException;

    // Sample to grant tag access on template
    public class GrantTagTemplateUserRole {

      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
        String projectId = "my-project";
        String tagTemplateId = "my_tag_template";
        grantTagTemplateUserRole(projectId, tagTemplateId);
      }

      public static void grantTagTemplateUserRole(String projectId, String templateId)
          throws IOException {
        // Currently, Data Catalog stores metadata in the us-central1 region.
        String location = "us-central1";

        // Format the Template name.
        String templateName =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-datacatalog/latest/com.google.cloud.datacatalog.v1.TagTemplateName.html.newBuilder()
                .setProject(projectId)
                .setLocation(location)
                .setTagTemplate(templateId)
                .build()
                .toString();

        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests. After completing all of your requests, call
        // the "close" method on the client to safely clean up any remaining background resources.
        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-datacatalog/latest/com.google.cloud.datacatalog.v1.DataCatalogClient.html dataCatalogClient = https://docs.cloud.google.com/java/docs/reference/google-cloud-datacatalog/latest/com.google.cloud.datacatalog.v1.DataCatalogClient.html.create()) {

          // Create a Binding to add the Tag Template User role and member to the policy.
          https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html binding =
              https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html.newBuilder()
                  .setRole("roles/datacatalog.tagTemplateUser")
                  .https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.Builder.html#com_google_iam_v1_Binding_Builder_addMembers_java_lang_String_("group:example-analyst-group@google.com")
                  .build();

          // Create a Policy object to update Template's IAM policy by adding the new binding.
          https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html policyUpdate = https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html.newBuilder().https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.Builder.html#com_google_iam_v1_Policy_Builder_addBindings_com_google_iam_v1_Binding_(binding).build();

          https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.html request =
              https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.html.newBuilder()
                  .https://docs.cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.Builder.html#com_google_iam_v1_SetIamPolicyRequest_Builder_setPolicy_com_google_iam_v1_Policy_(policyUpdate)
                  .setResource(templateName)
                  .build();

          // Update Template's policy.
          dataCatalogClient.setIamPolicy(request);
          System.out.println("Role successfully granted");
        }
      }
    }

<br />

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[Data Catalog quickstart using
client libraries](https://docs.cloud.google.com/data-catalog/docs/reference/libraries).


For more information, see the
[Data Catalog Node.js API
reference documentation](https://googleapis.dev/nodejs/datacatalog/latest/index.html).


To authenticate to Data Catalog, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // Import the Google Cloud client library.
    const {DataCatalogClient} = require('https://docs.cloud.google.com/nodejs/docs/reference/datacatalog/latest/overview.html').v1;
    const datacatalog = new https://docs.cloud.google.com/nodejs/docs/reference/datacatalog/latest/overview.html();

    async function grantTagTemplateUserRole() {
      // Grant the tagTemplateUser role to a member of the project.

      /**
       * TODO(developer): Uncomment the following lines before running the sample.
       */
      // const projectId = 'my_project'; // Google Cloud Platform project
      // const templateId = 'my_existing_template';
      // const memberId = 'my_member_id'

      const location = 'us-central1';

      // Format the Template name.
      const templateName = datacatalog.tagTemplatePath(
        projectId,
        location,
        templateId
      );

      // Retrieve Template's current IAM Policy.
      const [getPolicyResponse] = await datacatalog.getIamPolicy({
        resource: templateName,
      });
      const policy = getPolicyResponse;

      // Add Tag Template User role and member to the policy.
      policy.bindings.push({
        role: 'roles/datacatalog.tagTemplateUser',
        members: [memberId],
      });

      const request = {
        resource: templateName,
        policy: policy,
      };

      // Update Template's policy.
      const [updatePolicyResponse] = await datacatalog.setIamPolicy(request);

      updatePolicyResponse.bindings.forEach(binding => {
        console.log(`Role: ${binding.role}, Members: ${binding.members}`);
      });
    }
    grantTagTemplateUserRole();

<br />

### Python


Before trying this sample, follow the Python setup instructions in the
[Data Catalog quickstart using
client libraries](https://docs.cloud.google.com/data-catalog/docs/reference/libraries).


For more information, see the
[Data Catalog Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/datacatalog/latest).


To authenticate to Data Catalog, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/datacatalog/latest
    from google.iam.v1 import iam_policy_pb2 as iam_policy
    from google.iam.v1 import policy_pb2

    datacatalog = https://docs.cloud.google.com/python/docs/reference/datacatalog/latest.https://docs.cloud.google.com/python/docs/reference/datacatalog/latest/google.cloud.datacatalog_v1.services.data_catalog.DataCatalogClient.html()

    # TODO: Set these values before running the sample.
    project_id = "project_id"
    tag_template_id = "existing_tag_template_id"
    # For a full list of values a member can have, see:
    # https://cloud.google.com/iam/docs/reference/rest/v1/Policy?hl=en#binding
    member_id = "user:super-cool.test-user@gmail.com"

    # For all regions available, see:
    # https://cloud.google.com/data-catalog/docs/concepts/regions
    location = "us-central1"

    # Format the Template name.
    template_name = https://docs.cloud.google.com/python/docs/reference/datacatalog/latest.https://docs.cloud.google.com/python/docs/reference/datacatalog/latest/google.cloud.datacatalog_v1.services.data_catalog.DataCatalogClient.html.tag_template_path(
        project_id, location, tag_template_id
    )

    # Retrieve Template's current IAM Policy.
    policy = datacatalog.https://docs.cloud.google.com/python/docs/reference/datacatalog/latest/google.cloud.datacatalog_v1.services.data_catalog.DataCatalogClient.html#google_cloud_datacatalog_v1_services_data_catalog_DataCatalogClient_get_iam_policy(resource=template_name)

    # Add Tag Template User role and member to the policy.
    binding = policy_pb2.Binding()
    binding.role = "roles/datacatalog.tagTemplateUser"
    binding.members.append(member_id)
    policy.bindings.append(binding)

    set_policy_request = iam_policy.SetIamPolicyRequest(
        resource=template_name, policy=policy
    )

    # Update Template's policy.
    policy = datacatalog.https://docs.cloud.google.com/python/docs/reference/datacatalog/latest/google.cloud.datacatalog_v1.services.data_catalog.DataCatalogClient.html#google_cloud_datacatalog_v1_services_data_catalog_DataCatalogClient_set_iam_policy(set_policy_request)

    for binding in policy.bindings:
        for member in binding.members:
            print(f"Member: {member}, Role: {binding.role}")

<br />

### REST \& CMD LINE


### REST

If you do not have access to Cloud Client libraries for your language or
want to test the API using REST requests, see the following examples
and refer to the
[Data Catalog REST API](https://docs.cloud.google.com/data-catalog/docs/reference/rest)
documentation.


Before using any of the request data,
make the following replacements:

- <var translate="no">project-id</var>: Google Cloud project ID
- <var translate="no">template-id</var>: the tag template ID


HTTP method and URL:

```
POST https://datacatalog.googleapis.com/v1/projects/project-id/locations/region/tagTemplates/template-id:setIamPolicy
```


Request JSON body:

```
{
  "policy":{
    "bindings":[
      {
        "role":"roles/datacatalog.tagTemplateUser",
        "members":[
          "user:username@gmail.com"
        ]
      }
    ]
  }
}
```

To send your request, expand one of these options:

#### curl (Linux, macOS, or Cloud Shell)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list).


Save the request body in a file named `request.json`,
and execute the following command:

```
curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "x-goog-user-project: project-id" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d @request.json \
     "https://datacatalog.googleapis.com/v1/projects/project-id/locations/region/tagTemplates/template-id:setIamPolicy"
```

#### PowerShell (Windows)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list).


Save the request body in a file named `request.json`,
and execute the following command:

```
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://datacatalog.googleapis.com/v1/projects/project-id/locations/region/tagTemplates/template-id:setIamPolicy" | Select-Object -Expand Content
```

You should receive a JSON response similar to the following:

```
{
  "version":1,
  "etag":"xxxxx.....",
  "bindings":[
    {
      "role":"roles/datacatalog.tagTemplateUser",
      "members":[
        "user:username@gmail.com"
      ]
    }
  ]
}
```

<br />