use of com.google.api.services.iam.v1.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method getCryptoKeyPolicy.
// [END kms_restore_cryptokey_version]
// [START kms_get_cryptokey_policy]
/**
* Retrieves the IAM policy for the given crypto key.
*/
public static Policy getCryptoKeyPolicy(String projectId, String locationId, String keyRingId, String cryptoKeyId) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the cryptoKey
String cryptoKey = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
// Get the current IAM policy and add the new account to it.
Policy iamPolicy = kms.projects().locations().keyRings().cryptoKeys().getIamPolicy(cryptoKey).execute();
System.out.println(iamPolicy.getBindings());
return iamPolicy;
}
use of com.google.api.services.iam.v1.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method removeMemberFromCryptoKeyPolicy.
// [END kms_add_member_to_keyring_policy]
// [START kms_remove_member_from_cryptokey_policy]
/**
* Removes the given member from the given policy.
*/
public static Policy removeMemberFromCryptoKeyPolicy(String projectId, String locationId, String keyRingId, String cryptoKeyId, String member, String role) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the cryptoKey
String cryptoKey = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
// Get the current IAM policy and add the new account to it.
Policy iamPolicy = getCryptoKeyPolicy(projectId, locationId, keyRingId, cryptoKeyId);
if (null == iamPolicy.getBindings()) {
// Nothing to remove
return null;
}
// Filter out the given member
for (Binding b : iamPolicy.getBindings()) {
if (role.equals(b.getRole()) && b.getMembers().contains(member)) {
b.getMembers().removeAll(Collections.singletonList(member));
break;
}
}
// Set the new IAM Policy.
Policy newIamPolicy = kms.projects().locations().keyRings().cryptoKeys().setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy)).execute();
System.out.println("Response: " + newIamPolicy);
return newIamPolicy;
}
use of com.google.api.services.iam.v1.model.Policy in project workbench by all-of-us.
the class IamServiceImpl method grantServiceAccountUserRole.
private void grantServiceAccountUserRole(String googleProject, String petServiceAccount) {
Policy policy = cloudIamClient.getServiceAccountIamPolicy(googleProject, petServiceAccount);
List<Binding> bindingList = Optional.ofNullable(policy.getBindings()).orElse(new ArrayList<>());
bindingList.add(new Binding().setRole(SERVICE_ACCOUNT_USER_ROLE).setMembers(Collections.singletonList("serviceAccount:" + petServiceAccount)));
cloudIamClient.setServiceAccountIamPolicy(googleProject, petServiceAccount, policy.setBindings(bindingList));
}
use of com.google.api.services.iam.v1.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class Hl7v2StoreGetIamPolicy method hl7v2StoreGetIamPolicy.
public static void hl7v2StoreGetIamPolicy(String hl7v2StoreName) throws IOException {
// String hl7v2StoreName =
// String.format(
// HL7v2_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-hl7v2-id");
// Initialize the client, which will be used to interact with the service.
CloudHealthcare client = createClient();
// Create request and configure any parameters.
Hl7V2Stores.GetIamPolicy request = client.projects().locations().datasets().hl7V2Stores().getIamPolicy(hl7v2StoreName);
// Execute the request and process the results.
Policy policy = request.execute();
System.out.println("HL7v2 store IAMPolicy retrieved: \n" + policy.toPrettyString());
}
use of com.google.api.services.iam.v1.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class DatasetGetIamPolicy method datasetGetIamPolicy.
public static void datasetGetIamPolicy(String datasetName) throws IOException {
// String datasetName =
// String.format(DATASET_NAME, "your-project-id", "your-region-id", "your-dataset-id");
// Initialize the client, which will be used to interact with the service.
CloudHealthcare client = createClient();
// Create request and configure any parameters.
Datasets.GetIamPolicy request = client.projects().locations().datasets().getIamPolicy(datasetName);
// Execute the request and process the results.
Policy policy = request.execute();
System.out.println("Dataset IAMPolicy retrieved: \n" + policy.toPrettyString());
}
Aggregations