Test which IAM permissions a caller has on an Analytics Hub data exchange.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


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


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

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```java
import com.google.cloud.bigquery.analyticshub.v1.AnalyticsHubServiceClient;
import com.google.cloud.bigquery.analyticshub.v1.DataExchangeName;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class TestIamPermissions {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    String dataExchangeId = "example-data-exchange-id";
    testIamPermissions(projectId, locationId, dataExchangeId);
  }

  public static void testIamPermissions(String projectId, String locationId, String dataExchangeId)
      throws IOException {
    // Initialize the client that sends requests. The client can be reused for
    // multiple requests and relies on Application Default Credentials.
    try (AnalyticsHubServiceClient client = AnalyticsHubServiceClient.create()) {
      String resource = DataExchangeName.of(projectId, locationId, dataExchangeId).toString();
      List<String> permissions =
          Arrays.asList("analyticshub.dataExchanges.get", "analyticshub.dataExchanges.update");

      TestIamPermissionsRequest request =
          TestIamPermissionsRequest.newBuilder()
              .setResource(resource)
              .addAllPermissions(permissions)
              .build();

      TestIamPermissionsResponse response = client.testIamPermissions(request);
      System.out.println("Allowed permissions: " + response.getPermissionsList());
    }
  }
}
```

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


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


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

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```javascript
'use strict';

const {AnalyticsHubServiceClient} = require('@google-cloud/bigquery-analyticshub');
const {status} = require('@grpc/grpc-js');

const client = new AnalyticsHubServiceClient();

/**
 * Tests caller permissions on a data exchange resource.
 *
 * @param {string} projectId - The Google Cloud project ID containing the data exchange.
 * @param {string} locationId - The geographic location where the data exchange resides (for example, 'us' or 'us-central1').
 * @param {string} dataExchangeId - The ID of the data exchange to test permissions on. Must contain only letters, numbers, and underscores.
 */
async function testIamPermissions(projectId, locationId, dataExchangeId) {
  try {
    const resource = `projects/${projectId}/locations/${locationId}/dataExchanges/${dataExchangeId}`;
    const permissions = [
      'analyticshub.dataExchanges.get',
      'analyticshub.dataExchanges.update',
    ];

    const [response] = await client.testIamPermissions({
      resource,
      permissions,
    });

    console.log(`Tested permissions for resource: ${resource}`);
    if (response.permissions && response.permissions.length > 0) {
      console.log('Granted permissions:');
      for (const permission of response.permissions) {
        console.log(`- ${permission}`);
      }
    } else {
      console.log('No permissions granted from the requested set.');
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else {
      console.error(`Error testing permissions: ${err.message}`);
    }
  }
}

module.exports = { testIamPermissions };
```

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples.md.txt?product=bigquery).