Skip to content

Commit c2c6628

Browse files
author
Ajay Kannan
committed
Add get, replace, and test for IAM
1 parent ddd02aa commit c2c6628

File tree

14 files changed

+718
-98
lines changed

14 files changed

+718
-98
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/Identity.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class Identity implements Serializable {
4444
private static final long serialVersionUID = -8181841964597657446L;
4545

4646
private final Type type;
47-
private final String id;
47+
private final String value;
4848

4949
/**
5050
* The types of IAM identities.
@@ -82,17 +82,17 @@ public enum Type {
8282
DOMAIN
8383
}
8484

85-
private Identity(Type type, String id) {
85+
private Identity(Type type, String value) {
8686
this.type = type;
87-
this.id = id;
87+
this.value = value;
8888
}
8989

9090
public Type type() {
9191
return type;
9292
}
9393

9494
/**
95-
* Returns the string identifier for this identity. The id corresponds to:
95+
* Returns the string identifier for this identity. The value corresponds to:
9696
* <ul>
9797
* <li>email address (for identities of type {@code USER}, {@code SERVICE_ACCOUNT}, and
9898
* {@code GROUP})
@@ -101,8 +101,8 @@ public Type type() {
101101
* {@code ALL_AUTHENTICATED_USERS})
102102
* </ul>
103103
*/
104-
public String id() {
105-
return id;
104+
public String value() {
105+
return value;
106106
}
107107

108108
/**
@@ -163,7 +163,7 @@ public static Identity domain(String domain) {
163163

164164
@Override
165165
public int hashCode() {
166-
return Objects.hash(id, type);
166+
return Objects.hash(value, type);
167167
}
168168

169169
@Override
@@ -172,7 +172,7 @@ public boolean equals(Object obj) {
172172
return false;
173173
}
174174
Identity other = (Identity) obj;
175-
return Objects.equals(id, other.id()) && Objects.equals(type, other.type());
175+
return Objects.equals(value, other.value()) && Objects.equals(type, other.type());
176176
}
177177

178178
/**
@@ -186,13 +186,13 @@ public String strValue() {
186186
case ALL_AUTHENTICATED_USERS:
187187
return "allAuthenticatedUsers";
188188
case USER:
189-
return "user:" + id;
189+
return "user:" + value;
190190
case SERVICE_ACCOUNT:
191-
return "serviceAccount:" + id;
191+
return "serviceAccount:" + value;
192192
case GROUP:
193-
return "group:" + id;
193+
return "group:" + value;
194194
case DOMAIN:
195-
return "domain:" + id;
195+
return "domain:" + value;
196196
default:
197197
throw new IllegalStateException("Unexpected identity type: " + type);
198198
}

gcloud-java-core/src/test/java/com/google/gcloud/IdentityTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ public class IdentityTest {
3434
@Test
3535
public void testAllUsers() {
3636
assertEquals(Identity.Type.ALL_USERS, ALL_USERS.type());
37-
assertNull(ALL_USERS.id());
37+
assertNull(ALL_USERS.value());
3838
}
3939

4040
@Test
4141
public void testAllAuthenticatedUsers() {
4242
assertEquals(Identity.Type.ALL_AUTHENTICATED_USERS, ALL_AUTH_USERS.type());
43-
assertNull(ALL_AUTH_USERS.id());
43+
assertNull(ALL_AUTH_USERS.value());
4444
}
4545

4646
@Test
4747
public void testUser() {
4848
assertEquals(Identity.Type.USER, USER.type());
49-
assertEquals("[email protected]", USER.id());
49+
assertEquals("[email protected]", USER.value());
5050
}
5151

5252
@Test(expected = NullPointerException.class)
@@ -57,7 +57,7 @@ public void testUserNullEmail() {
5757
@Test
5858
public void testServiceAccount() {
5959
assertEquals(Identity.Type.SERVICE_ACCOUNT, SERVICE_ACCOUNT.type());
60-
assertEquals("[email protected]", SERVICE_ACCOUNT.id());
60+
assertEquals("[email protected]", SERVICE_ACCOUNT.value());
6161
}
6262

6363
@Test(expected = NullPointerException.class)
@@ -68,7 +68,7 @@ public void testServiceAccountNullEmail() {
6868
@Test
6969
public void testGroup() {
7070
assertEquals(Identity.Type.GROUP, GROUP.type());
71-
assertEquals("[email protected]", GROUP.id());
71+
assertEquals("[email protected]", GROUP.value());
7272
}
7373

7474
@Test(expected = NullPointerException.class)
@@ -79,7 +79,7 @@ public void testGroupNullEmail() {
7979
@Test
8080
public void testDomain() {
8181
assertEquals(Identity.Type.DOMAIN, DOMAIN.type());
82-
assertEquals("google.com", DOMAIN.id());
82+
assertEquals("google.com", DOMAIN.value());
8383
}
8484

8585
@Test(expected = NullPointerException.class)
@@ -100,6 +100,6 @@ public void testIdentityToAndFromPb() {
100100
private void compareIdentities(Identity expected, Identity actual) {
101101
assertEquals(expected, actual);
102102
assertEquals(expected.type(), actual.type());
103-
assertEquals(expected.id(), actual.id());
103+
assertEquals(expected.value(), actual.value());
104104
}
105105
}

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java

+82-20
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@
1717
package com.google.gcloud.resourcemanager;
1818

1919
import com.google.common.annotations.VisibleForTesting;
20-
import com.google.common.base.CaseFormat;
2120
import com.google.common.base.Function;
2221
import com.google.common.collect.ImmutableSet;
2322
import com.google.common.collect.Lists;
2423
import com.google.gcloud.IamPolicy;
2524
import com.google.gcloud.Identity;
2625

26+
import java.io.Serializable;
2727
import java.util.ArrayList;
2828
import java.util.HashMap;
2929
import java.util.LinkedList;
3030
import java.util.List;
3131
import java.util.Map;
32+
import java.util.Objects;
3233
import java.util.Set;
3334

3435
/**
@@ -48,40 +49,101 @@ public class Policy extends IamPolicy<Policy.Role> {
4849
/**
4950
* Represents legacy roles in an IAM Policy.
5051
*/
51-
public enum Role {
52+
public static class Role implements Serializable {
5253

5354
/**
54-
* Permissions for read-only actions that preserve state.
55+
* The recognized roles in a Project's IAM policy.
5556
*/
56-
VIEWER("roles/viewer"),
57+
public enum Type {
58+
59+
/**
60+
* Permissions for read-only actions that preserve state.
61+
*/
62+
VIEWER,
63+
64+
/**
65+
* All viewer permissions and permissions for actions that modify state.
66+
*/
67+
EDITOR,
68+
69+
/**
70+
* All editor permissions and permissions for the following actions:
71+
* <ul>
72+
* <li>Manage access control for a resource.
73+
* <li>Set up billing (for a project).
74+
* </ul>
75+
*/
76+
OWNER
77+
}
78+
79+
private static final long serialVersionUID = 2421978909244287488L;
80+
81+
private final String value;
82+
private final Type type;
83+
84+
private Role(String value, Type type) {
85+
this.value = value;
86+
this.type = type;
87+
}
88+
89+
String value() {
90+
return value;
91+
}
5792

5893
/**
59-
* All viewer permissions and permissions for actions that modify state.
94+
* Returns the type of role (editor, owner, or viewer). Returns {@code null} if the role type
95+
* is unrecognized.
6096
*/
61-
EDITOR("roles/editor"),
97+
public Type type() {
98+
return type;
99+
}
62100

63101
/**
64-
* All editor permissions and permissions for the following actions:
65-
* <ul>
66-
* <li>Manage access control for a resource.
67-
* <li>Set up billing (for a project).
68-
* </ul>
102+
* Returns a {@code Role} of type {@link Type#VIEWER VIEWER}.
69103
*/
70-
OWNER("roles/owner");
104+
public static Role viewer() {
105+
return new Role("roles/viewer", Type.VIEWER);
106+
}
71107

72-
private String strValue;
108+
/**
109+
* Returns a {@code Role} of type {@link Type#EDITOR EDITOR}.
110+
*/
111+
public static Role editor() {
112+
return new Role("roles/editor", Type.EDITOR);
113+
}
73114

74-
private Role(String strValue) {
75-
this.strValue = strValue;
115+
/**
116+
* Returns a {@code Role} of type {@link Type#OWNER OWNER}.
117+
*/
118+
public static Role owner() {
119+
return new Role("roles/owner", Type.OWNER);
76120
}
77121

78-
String strValue() {
79-
return strValue;
122+
static Role rawRole(String roleStr) {
123+
return new Role(roleStr, null);
80124
}
81125

82126
static Role fromStr(String roleStr) {
83-
return Role.valueOf(CaseFormat.LOWER_CAMEL.to(
84-
CaseFormat.UPPER_UNDERSCORE, roleStr.substring("roles/".length())));
127+
try {
128+
Type type = Type.valueOf(roleStr.split("/")[1].toUpperCase());
129+
return new Role(roleStr, type);
130+
} catch (Exception ex) {
131+
return new Role(roleStr, null);
132+
}
133+
}
134+
135+
@Override
136+
public final int hashCode() {
137+
return Objects.hash(value, type);
138+
}
139+
140+
@Override
141+
public final boolean equals(Object obj) {
142+
if (!(obj instanceof Role)) {
143+
return false;
144+
}
145+
Role other = (Role) obj;
146+
return Objects.equals(value, other.value()) && Objects.equals(type, other.type());
85147
}
86148
}
87149

@@ -124,7 +186,7 @@ com.google.api.services.cloudresourcemanager.model.Policy toPb() {
124186
for (Map.Entry<Role, Set<Identity>> binding : bindings().entrySet()) {
125187
com.google.api.services.cloudresourcemanager.model.Binding bindingPb =
126188
new com.google.api.services.cloudresourcemanager.model.Binding();
127-
bindingPb.setRole(binding.getKey().strValue());
189+
bindingPb.setRole(binding.getKey().value());
128190
bindingPb.setMembers(
129191
Lists.transform(
130192
new ArrayList<>(binding.getValue()),

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ public Project reload() {
157157
* completes, the project is not retrievable by the {@link ResourceManager#get} and
158158
* {@link ResourceManager#list} methods. The caller must have modify permissions for this project.
159159
*
160-
* @see <a
161-
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete">Cloud
162-
* Resource Manager delete</a>
163160
* @throws ResourceManagerException upon failure
161+
* @see <a href=
162+
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete">Cloud
163+
* Resource Manager delete</a>
164164
*/
165165
public void delete() {
166166
resourceManager.delete(projectId());
@@ -174,10 +174,10 @@ public void delete() {
174174
* state of {@link ProjectInfo.State#DELETE_IN_PROGRESS}, the project cannot be restored. The
175175
* caller must have modify permissions for this project.
176176
*
177-
* @see <a
178-
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete">Cloud
179-
* Resource Manager undelete</a>
180177
* @throws ResourceManagerException upon failure (including when the project can't be restored)
178+
* @see <a href=
179+
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete">Cloud
180+
* Resource Manager undelete</a>
181181
*/
182182
public void undelete() {
183183
resourceManager.undelete(projectId());
@@ -188,11 +188,11 @@ public void undelete() {
188188
*
189189
* <p>The caller must have modify permissions for this project.
190190
*
191-
* @see <a
192-
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud
193-
* Resource Manager update</a>
194191
* @return the Project representing the new project metadata
195192
* @throws ResourceManagerException upon failure
193+
* @see <a href=
194+
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud
195+
* Resource Manager update</a>
196196
*/
197197
public Project replace() {
198198
return resourceManager.replace(this);

0 commit comments

Comments
 (0)