Skip to content

Commit

Permalink
move to/fromPb functions to Resource Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Feb 19, 2016
1 parent ff20847 commit 3f07b2c
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 130 deletions.
12 changes: 0 additions & 12 deletions gcloud-java-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@
<site.installationModule>gcloud-java-core</site.installationModule>
</properties>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-cloudresourcemanager</artifactId>
<version>v1beta1-rev10-1.21.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
Expand Down
98 changes: 4 additions & 94 deletions gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.Arrays;
Expand Down Expand Up @@ -176,44 +174,7 @@ public boolean equals(Object obj) {
return false;
}
Identity other = (Identity) obj;
return Objects.equals(toPb(), other.toPb());
}

String toPb() {
switch (type) {
case ALL_USERS:
return "allUsers";
case ALL_AUTHENTICATED_USERS:
return "allAuthenticatedUsers";
case USER:
return "user:" + id;
case SERVICE_ACCOUNT:
return "serviceAccount:" + id;
case GROUP:
return "group:" + id;
default:
return "domain:" + id;
}
}

static Identity fromPb(String identityStr) {
String[] info = identityStr.split(":");
switch (info[0]) {
case "allUsers":
return allUsers();
case "allAuthenticatedUsers":
return allAuthenticatedUsers();
case "user":
return user(info[1]);
case "serviceAccount":
return serviceAccount(info[1]);
case "group":
return group(info[1]);
case "domain":
return domain(info[1]);
default:
throw new IllegalArgumentException("Unexpected identity type: " + info[0]);
}
return Objects.equals(id, other.id()) && Objects.equals(type, other.type());
}
}

Expand Down Expand Up @@ -351,31 +312,7 @@ public boolean equals(Object obj) {
return false;
}
Acl other = (Acl) obj;
return Objects.equals(toPb(), other.toPb());
}

com.google.api.services.cloudresourcemanager.model.Binding toPb() {
com.google.api.services.cloudresourcemanager.model.Binding bindingPb =
new com.google.api.services.cloudresourcemanager.model.Binding();
bindingPb.setMembers(Lists.transform(identities, new Function<Identity, String>() {
@Override
public String apply(Identity identity) {
return identity.toPb();
}
}));
bindingPb.setRole("roles/" + role);
return bindingPb;
}

static Acl fromPb(com.google.api.services.cloudresourcemanager.model.Binding bindingPb) {
return of(
bindingPb.getRole().substring("roles/".length()),
Lists.transform(bindingPb.getMembers(), new Function<String, Identity>() {
@Override
public Identity apply(String memberPb) {
return Identity.fromPb(memberPb);
}
}));
return Objects.equals(identities, other.identities()) && Objects.equals(role, other.role());
}
}

Expand Down Expand Up @@ -489,7 +426,8 @@ public boolean equals(Object obj) {
return false;
}
IamPolicy other = (IamPolicy) obj;
return Objects.equals(toPb(), other.toPb());
return Objects.equals(acls, other.acls()) && Objects.equals(etag, other.etag())
&& Objects.equals(version, other.version());
}

public static Builder builder() {
Expand All @@ -499,32 +437,4 @@ public static Builder builder() {
public Builder toBuilder() {
return new Builder().acls(acls).etag(etag).version(version);
}

com.google.api.services.cloudresourcemanager.model.Policy toPb() {
com.google.api.services.cloudresourcemanager.model.Policy policyPb =
new com.google.api.services.cloudresourcemanager.model.Policy();
policyPb.setBindings(Lists.transform(
acls, new Function<Acl, com.google.api.services.cloudresourcemanager.model.Binding>() {
@Override
public com.google.api.services.cloudresourcemanager.model.Binding apply(Acl acl) {
return acl.toPb();
}
}));
policyPb.setEtag(etag);
policyPb.setVersion(version);
return policyPb;
}

static IamPolicy fromPb(com.google.api.services.cloudresourcemanager.model.Policy policyPb) {
Builder builder = new Builder();
builder.acls(Lists.transform(
policyPb.getBindings(),
new Function<com.google.api.services.cloudresourcemanager.model.Binding, Acl>() {
@Override
public Acl apply(com.google.api.services.cloudresourcemanager.model.Binding binding) {
return Acl.fromPb(binding);
}
}));
return builder.etag(policyPb.getEtag()).version(policyPb.getVersion()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ public void testIdentityOf() {
assertEquals("google.com", DOMAIN.id());
}

@Test
public void testIdentityToAndFromPb() {
assertEquals(ALL_USERS, Identity.fromPb(ALL_USERS.toPb()));
assertEquals(ALL_AUTHENTICATED_USERS, Identity.fromPb(ALL_AUTHENTICATED_USERS.toPb()));
assertEquals(USER, Identity.fromPb(USER.toPb()));
assertEquals(SERVICE_ACCOUNT, Identity.fromPb(SERVICE_ACCOUNT.toPb()));
assertEquals(GROUP, Identity.fromPb(GROUP.toPb()));
assertEquals(DOMAIN, Identity.fromPb(DOMAIN.toPb()));
}

@Test
public void testAclBuilder() {
Acl acl = Acl.builder("owner").addIdentity(USER, GROUP).build();
Expand All @@ -82,21 +72,16 @@ public void testAclBuilder() {
public void testAclOf() {
assertEquals("viewer", ACL1.role());
assertEquals(ImmutableList.of(USER, SERVICE_ACCOUNT, ALL_USERS), ACL1.identities());
Acl aclFromIdentitiesList = Acl.of("editor", ImmutableList.of(USER, SERVICE_ACCOUNT));
assertEquals("editor", aclFromIdentitiesList.role());
assertEquals(ImmutableList.of(USER, SERVICE_ACCOUNT), aclFromIdentitiesList.identities());
Acl aclFromList = Acl.of("editor", ImmutableList.of(USER, SERVICE_ACCOUNT));
assertEquals("editor", aclFromList.role());
assertEquals(ImmutableList.of(USER, SERVICE_ACCOUNT), aclFromList.identities());
}

@Test
public void testAclToBuilder() {
assertEquals(ACL1, ACL1.toBuilder().build());
}

@Test
public void testAclToAndFromPb() {
assertEquals(ACL1, Acl.fromPb(ACL1.toPb()));
}

@Test
public void testIamPolicyBuilder() {
assertEquals(ImmutableList.of(ACL1, ACL2), FULL_POLICY.acls());
Expand All @@ -117,10 +102,4 @@ public void testIamPolicyToBuilder() {
assertEquals(FULL_POLICY, FULL_POLICY.toBuilder().build());
assertEquals(SIMPLE_POLICY, SIMPLE_POLICY.toBuilder().build());
}

@Test
public void testToAndFromPb() {
assertEquals(FULL_POLICY, IamPolicy.fromPb(FULL_POLICY.toPb()));
assertEquals(SIMPLE_POLICY, IamPolicy.fromPb(SIMPLE_POLICY.toPb()));
}
}
12 changes: 12 additions & 0 deletions gcloud-java-resourcemanager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-cloudresourcemanager</artifactId>
<version>v1beta1-rev10-1.21.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gcloud.BaseService;
import com.google.gcloud.IamPolicy;
import com.google.gcloud.IamPolicy.Acl;
import com.google.gcloud.IamPolicy.Identity;
import com.google.gcloud.Page;
import com.google.gcloud.PageImpl;
import com.google.gcloud.PageImpl.NextPageFetcher;
Expand Down Expand Up @@ -189,4 +193,94 @@ public Void call() {
}
return ImmutableMap.copyOf(temp);
}

static String identityToPb(Identity identity) {
switch (identity.type()) {
case ALL_USERS:
return "allUsers";
case ALL_AUTHENTICATED_USERS:
return "allAuthenticatedUsers";
case USER:
return "user:" + identity.id();
case SERVICE_ACCOUNT:
return "serviceAccount:" + identity.id();
case GROUP:
return "group:" + identity.id();
default:
return "domain:" + identity.id();
}
}

static Identity identityFromPb(String identityStr) {
String[] info = identityStr.split(":");
switch (info[0]) {
case "allUsers":
return Identity.allUsers();
case "allAuthenticatedUsers":
return Identity.allAuthenticatedUsers();
case "user":
return Identity.user(info[1]);
case "serviceAccount":
return Identity.serviceAccount(info[1]);
case "group":
return Identity.group(info[1]);
case "domain":
return Identity.domain(info[1]);
default:
throw new IllegalArgumentException("Unexpected identity type: " + info[0]);
}
}

static com.google.api.services.cloudresourcemanager.model.Binding aclToPb(Acl acl) {
com.google.api.services.cloudresourcemanager.model.Binding bindingPb =
new com.google.api.services.cloudresourcemanager.model.Binding();
bindingPb.setMembers(Lists.transform(acl.identities(), new Function<Identity, String>() {
@Override
public String apply(Identity identity) {
return identityToPb(identity);
}
}));
bindingPb.setRole("roles/" + acl.role());
return bindingPb;
}

static Acl aclFromPb(com.google.api.services.cloudresourcemanager.model.Binding bindingPb) {
return Acl.of(
bindingPb.getRole().substring("roles/".length()),
Lists.transform(bindingPb.getMembers(), new Function<String, Identity>() {
@Override
public Identity apply(String memberPb) {
return identityFromPb(memberPb);
}
}));
}

static com.google.api.services.cloudresourcemanager.model.Policy policyToPb(
final IamPolicy policy) {
com.google.api.services.cloudresourcemanager.model.Policy policyPb =
new com.google.api.services.cloudresourcemanager.model.Policy();
policyPb.setBindings(Lists.transform(policy.acls(),
new Function<Acl, com.google.api.services.cloudresourcemanager.model.Binding>() {
@Override
public com.google.api.services.cloudresourcemanager.model.Binding apply(Acl acl) {
return aclToPb(acl);
}
}));
policyPb.setEtag(policy.etag());
policyPb.setVersion(policy.version());
return policyPb;
}

static IamPolicy policyFromPb(
com.google.api.services.cloudresourcemanager.model.Policy policyPb) {
IamPolicy.Builder builder = new IamPolicy.Builder();
builder.acls(Lists.transform(policyPb.getBindings(),
new Function<com.google.api.services.cloudresourcemanager.model.Binding, Acl>() {
@Override
public Acl apply(com.google.api.services.cloudresourcemanager.model.Binding binding) {
return aclFromPb(binding);
}
}));
return builder.etag(policyPb.getEtag()).version(policyPb.getVersion()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableMap;
import com.google.gcloud.IamPolicy;
import com.google.gcloud.IamPolicy.Acl;
import com.google.gcloud.IamPolicy.Identity;
import com.google.gcloud.Page;
import com.google.gcloud.resourcemanager.ProjectInfo.ResourceId;
import com.google.gcloud.resourcemanager.ResourceManager.ProjectField;
Expand Down Expand Up @@ -64,6 +67,18 @@ public class ResourceManagerImplTest {
.parent(PARENT)
.build();
private static final Map<ResourceManagerRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
private static final Identity ALL_USERS = Identity.allUsers();
private static final Identity ALL_AUTH_USERS = Identity.allAuthenticatedUsers();
private static final Identity USER = Identity.user("abc@gmail.com");
private static final Identity SERVICE_ACCOUNT =
Identity.serviceAccount("service-account@gmail.com");
private static final Identity GROUP = Identity.group("group@gmail.com");
private static final Identity DOMAIN = Identity.domain("google.com");
private static final Acl ACL1 = Acl.of("viewer", USER, SERVICE_ACCOUNT, ALL_USERS);
private static final Acl ACL2 = Acl.of("editor", ALL_AUTH_USERS, GROUP, DOMAIN);
private static final IamPolicy FULL_POLICY =
IamPolicy.builder().addAcl(ACL1, ACL2).etag("etag").version(1).build();
private static final IamPolicy SIMPLE_POLICY = IamPolicy.builder().addAcl(ACL1, ACL2).build();

@Rule
public ExpectedException thrown = ExpectedException.none();
Expand Down Expand Up @@ -271,6 +286,35 @@ public void testUndelete() {
}
}

@Test
public void testIdentityToAndFromPb() {
assertEquals(ALL_USERS,
ResourceManagerImpl.identityFromPb(ResourceManagerImpl.identityToPb(ALL_USERS)));
assertEquals(ALL_AUTH_USERS,
ResourceManagerImpl.identityFromPb(
ResourceManagerImpl.identityToPb(ALL_AUTH_USERS)));
assertEquals(USER, ResourceManagerImpl.identityFromPb(ResourceManagerImpl.identityToPb(USER)));
assertEquals(SERVICE_ACCOUNT,
ResourceManagerImpl.identityFromPb(ResourceManagerImpl.identityToPb(SERVICE_ACCOUNT)));
assertEquals(GROUP,
ResourceManagerImpl.identityFromPb(ResourceManagerImpl.identityToPb(GROUP)));
assertEquals(DOMAIN,
ResourceManagerImpl.identityFromPb(ResourceManagerImpl.identityToPb(DOMAIN)));
}

@Test
public void testAclToAndFromPb() {
assertEquals(ACL1, ResourceManagerImpl.aclFromPb(ResourceManagerImpl.aclToPb(ACL1)));
}

@Test
public void testPolicyToAndFromPb() {
assertEquals(FULL_POLICY,
ResourceManagerImpl.policyFromPb(ResourceManagerImpl.policyToPb(FULL_POLICY)));
assertEquals(SIMPLE_POLICY,
ResourceManagerImpl.policyFromPb(ResourceManagerImpl.policyToPb(SIMPLE_POLICY)));
}

@Test
public void testRetryableException() {
ResourceManagerRpcFactory rpcFactoryMock = EasyMock.createMock(ResourceManagerRpcFactory.class);
Expand Down

0 comments on commit 3f07b2c

Please sign in to comment.