Search in sources :

Example 6 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class SetPolicy method createCloudResourceManagerService.

public static CloudResourceManager createCloudResourceManagerService() throws IOException, GeneralSecurityException {
    // Use the Application Default Credentials strategy for authentication. For more info, see:
    // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
    CloudResourceManager service = new CloudResourceManager.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), new HttpCredentialsAdapter(credential)).setApplicationName("service-accounts").build();
    return service;
}
Also used : HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials)

Example 7 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class TestPermissions method createCloudResourceManagerService.

public static CloudResourceManager createCloudResourceManagerService() throws IOException, GeneralSecurityException {
    // Use the Application Default Credentials strategy for authentication. For more info, see:
    // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
    CloudResourceManager service = new CloudResourceManager.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), new HttpCredentialsAdapter(credential)).setApplicationName("service-accounts").build();
    return service;
}
Also used : HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials)

Example 8 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class GetPolicy method createCloudResourceManagerService.

public static CloudResourceManager createCloudResourceManagerService() throws IOException, GeneralSecurityException {
    // Use the Application Default Credentials strategy for authentication. For more info, see:
    // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
    CloudResourceManager service = new CloudResourceManager.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), new HttpCredentialsAdapter(credential)).setApplicationName("service-accounts").build();
    return service;
}
Also used : HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials)

Example 9 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class GetPolicy method getPolicy.

// Gets a project's policy.
public static Policy getPolicy(String projectId) {
    // projectId = "my-project-id"
    Policy policy = null;
    CloudResourceManager service = null;
    try {
        service = createCloudResourceManagerService();
    } catch (IOException | GeneralSecurityException e) {
        System.out.println("Unable to initialize service: \n" + e.toString());
        return policy;
    }
    try {
        GetIamPolicyRequest request = new GetIamPolicyRequest();
        policy = service.projects().getIamPolicy(projectId, request).execute();
        System.out.println("Policy retrieved: " + policy.toString());
        return policy;
    } catch (IOException e) {
        System.out.println("Unable to get policy: \n" + e.toString());
        return policy;
    }
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)

Example 10 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class Quickstart method addBinding.

public static void addBinding(CloudResourceManager crmService, String projectId, String member, String role) {
    // Gets the project's policy.
    Policy policy = getPolicy(crmService, projectId);
    // Finds binding in policy, if it exists
    Binding binding = null;
    for (Binding b : policy.getBindings()) {
        if (b.getRole().equals(role)) {
            binding = b;
            break;
        }
    }
    if (binding != null) {
        // If binding already exists, adds member to binding.
        binding.getMembers().add(member);
    } else {
        // If binding does not exist, adds binding to policy.
        binding = new Binding();
        binding.setRole(role);
        binding.setMembers(Collections.singletonList(member));
        policy.getBindings().add(binding);
    }
    // Sets the updated policy
    setPolicy(crmService, projectId, policy);
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding)

Aggregations

CloudResourceManager (com.google.api.services.cloudresourcemanager.v3.CloudResourceManager)9 Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)7 IOException (java.io.IOException)6 Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)4 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)4 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)4 GeneralSecurityException (java.security.GeneralSecurityException)4 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)2 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)2 TestIamPermissionsRequest (com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsRequest)1 TestIamPermissionsResponse (com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsResponse)1 Test (org.junit.Test)1