Search in sources :

Example 1 with Role

use of com.google.cloud.Role in project google-cloud-java by GoogleCloudPlatform.

the class BucketIamSnippets method listBucketIamMembers.

/**
   * Example of listing the Bucket-Level IAM Roles and Members
   */
public Policy listBucketIamMembers(String bucketName) {
    // [START view_bucket_iam_members]
    // Initialize a Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // Get IAM Policy for a bucket
    Policy policy = storage.getIamPolicy(bucketName);
    // Print Roles and its identities
    Map<Role, Set<Identity>> policyBindings = policy.getBindings();
    for (Map.Entry<Role, Set<Identity>> entry : policyBindings.entrySet()) {
        System.out.printf("Role: %s Identities: %s\n", entry.getKey(), entry.getValue());
    }
    // [END view_bucket_iam_members]
    return policy;
}
Also used : Policy(com.google.cloud.Policy) Role(com.google.cloud.Role) Storage(com.google.cloud.storage.Storage) Set(java.util.Set) Map(java.util.Map)

Example 2 with Role

use of com.google.cloud.Role in project google-cloud-java by GoogleCloudPlatform.

the class PolicyMarshaller method toPb.

@Override
protected com.google.api.services.cloudresourcemanager.model.Policy toPb(Policy policy) {
    com.google.api.services.cloudresourcemanager.model.Policy policyPb = new com.google.api.services.cloudresourcemanager.model.Policy();
    List<Binding> bindingPbList = new LinkedList<>();
    for (Map.Entry<Role, Set<Identity>> binding : policy.getBindings().entrySet()) {
        Binding bindingPb = new Binding();
        bindingPb.setRole(binding.getKey().getValue());
        bindingPb.setMembers(Lists.transform(new ArrayList<>(binding.getValue()), new Function<Identity, String>() {

            @Override
            public String apply(Identity identity) {
                return IDENTITY_STR_VALUE_FUNCTION.apply(identity);
            }
        }));
        bindingPbList.add(bindingPb);
    }
    policyPb.setBindings(bindingPbList);
    policyPb.setEtag(policy.getEtag());
    policyPb.setVersion(policy.getVersion());
    return policyPb;
}
Also used : Policy(com.google.cloud.Policy) Binding(com.google.api.services.cloudresourcemanager.model.Binding) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Role(com.google.cloud.Role) Function(com.google.common.base.Function) Identity(com.google.cloud.Identity) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Role

use of com.google.cloud.Role in project google-cloud-java by GoogleCloudPlatform.

the class PolicyHelper method convertToApiPolicy.

static com.google.api.services.storage.model.Policy convertToApiPolicy(Policy policy) {
    List<Bindings> bindings = new ArrayList<>(policy.getBindings().size());
    for (Map.Entry<Role, Set<Identity>> entry : policy.getBindings().entrySet()) {
        List<String> members = new ArrayList<>(entry.getValue().size());
        for (Identity identity : entry.getValue()) {
            members.add(identity.strValue());
        }
        bindings.add(new Bindings().setMembers(members).setRole(entry.getKey().getValue()));
    }
    return new com.google.api.services.storage.model.Policy().setBindings(bindings).setEtag(policy.getEtag());
}
Also used : Policy(com.google.cloud.Policy) Set(java.util.Set) ArrayList(java.util.ArrayList) Bindings(com.google.api.services.storage.model.Policy.Bindings) Role(com.google.cloud.Role) Identity(com.google.cloud.Identity) Map(java.util.Map)

Aggregations

Policy (com.google.cloud.Policy)3 Role (com.google.cloud.Role)3 Map (java.util.Map)3 Set (java.util.Set)3 Identity (com.google.cloud.Identity)2 ArrayList (java.util.ArrayList)2 Binding (com.google.api.services.cloudresourcemanager.model.Binding)1 Bindings (com.google.api.services.storage.model.Policy.Bindings)1 Storage (com.google.cloud.storage.Storage)1 Function (com.google.common.base.Function)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1