1 /* 2 * Copyright 2016 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.resourcemanager; 18 19 import com.google.api.services.cloudresourcemanager.model.Binding; 20 import com.google.cloud.Identity; 21 import com.google.cloud.Policy; 22 import com.google.cloud.Policy.Marshaller; 23 import com.google.cloud.Role; 24 import com.google.common.base.Function; 25 import com.google.common.collect.ImmutableSet; 26 import com.google.common.collect.Lists; 27 import java.util.ArrayList; 28 import java.util.HashMap; 29 import java.util.LinkedList; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.Set; 33 34 /** @deprecated v3 GAPIC client of ResourceManager is now available */ 35 @Deprecated 36 final class PolicyMarshaller 37 extends Marshaller<com.google.api.services.cloudresourcemanager.model.Policy> { 38 39 static final PolicyMarshaller INSTANCE = new PolicyMarshaller(); 40 PolicyMarshaller()41 private PolicyMarshaller() {} 42 43 private static class Builder extends Policy.Builder { 44 Builder(Map<Role, Set<Identity>> bindings, String etag, Integer version)45 private Builder(Map<Role, Set<Identity>> bindings, String etag, Integer version) { 46 setBindings(bindings); 47 setEtag(etag); 48 if (version != null) { 49 setVersion(version); 50 } 51 } 52 } 53 54 @Override fromPb(com.google.api.services.cloudresourcemanager.model.Policy policyPb)55 protected Policy fromPb(com.google.api.services.cloudresourcemanager.model.Policy policyPb) { 56 Map<Role, Set<Identity>> bindings = new HashMap<>(); 57 if (policyPb.getBindings() != null) { 58 for (Binding bindingPb : policyPb.getBindings()) { 59 bindings.put( 60 Role.of(bindingPb.getRole()), 61 ImmutableSet.copyOf( 62 Lists.transform( 63 bindingPb.getMembers(), 64 new Function<String, Identity>() { 65 @Override 66 public Identity apply(String s) { 67 return IDENTITY_VALUE_OF_FUNCTION.apply(s); 68 } 69 }))); 70 } 71 } 72 return new Builder(bindings, policyPb.getEtag(), policyPb.getVersion()).build(); 73 } 74 75 @Override toPb(Policy policy)76 protected com.google.api.services.cloudresourcemanager.model.Policy toPb(Policy policy) { 77 com.google.api.services.cloudresourcemanager.model.Policy policyPb = 78 new com.google.api.services.cloudresourcemanager.model.Policy(); 79 List<Binding> bindingPbList = new LinkedList<>(); 80 for (Map.Entry<Role, Set<Identity>> binding : policy.getBindings().entrySet()) { 81 Binding bindingPb = new Binding(); 82 bindingPb.setRole(binding.getKey().getValue()); 83 bindingPb.setMembers( 84 Lists.transform( 85 new ArrayList<>(binding.getValue()), 86 new Function<Identity, String>() { 87 @Override 88 public String apply(Identity identity) { 89 return IDENTITY_STR_VALUE_FUNCTION.apply(identity); 90 } 91 })); 92 bindingPbList.add(bindingPb); 93 } 94 policyPb.setBindings(bindingPbList); 95 policyPb.setEtag(policy.getEtag()); 96 policyPb.setVersion(policy.getVersion()); 97 return policyPb; 98 } 99 } 100