Skip to content

Commit a80889f

Browse files
authored
feat: implement testPermission api of organizations (googleapis#125)
* feat: implement testPermission api of organization * feat: modified java doc * feat: modified java doc * feat: fix review changes * feat: fix javadoc
1 parent 1d9112a commit a80889f

File tree

8 files changed

+152
-2
lines changed

8 files changed

+152
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- see http://mojo.codehaus.org/clirr-maven-plugin/examples/ignored-differences.html -->
3+
<differences>
4+
<difference>
5+
<className>com/google/cloud/resourcemanager/ResourceManager</className>
6+
<method>java.util.Map testOrgPermissions(java.lang.String, java.util.List)</method>
7+
<differenceType>7012</differenceType>
8+
</difference>
9+
<difference>
10+
<className>com/google/cloud/resourcemanager/spi/v1beta1/ResourceManagerRpc</className>
11+
<method>java.util.Map testOrgPermissions(java.lang.String, java.util.List)</method>
12+
<differenceType>7012</differenceType>
13+
</difference>
14+
</differences>

java-resourcemanager/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<google.core-http.version>1.93.3</google.core-http.version>
7373
<api-client.version>1.30.9</api-client.version>
7474
<easymock.version>3.6</easymock.version>
75+
<mockito.version>1.10.19</mockito.version>
7576
<objenesis.version>2.6</objenesis.version>
7677
<google.auth.version>0.20.0</google.auth.version>
7778
<http-client-bom.version>1.34.2</http-client-bom.version>
@@ -205,6 +206,12 @@
205206
</exclusion>
206207
</exclusions>
207208
</dependency>
209+
<dependency>
210+
<groupId>org.mockito</groupId>
211+
<artifactId>mockito-all</artifactId>
212+
<version>${mockito.version}</version>
213+
<scope>test</scope>
214+
</dependency>
208215
<dependency>
209216
<groupId>org.objenesis</groupId>
210217
<artifactId>objenesis</artifactId>

java-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.resourcemanager.spi.v1beta1.ResourceManagerRpc;
2525
import com.google.common.collect.ImmutableList;
2626
import java.util.List;
27+
import java.util.Map;
2728

2829
/**
2930
* An interface for Google Cloud Resource Manager.
@@ -337,4 +338,20 @@ public static ProjectListOption fields(ProjectField... fields) {
337338
* Platform Services</a>
338339
*/
339340
List<Boolean> testPermissions(String projectId, List<String> permissions);
341+
342+
/**
343+
* Returns the permissions and their results representing whether the caller has the permissions
344+
* on the specified Organization.
345+
*
346+
* @param resource the organization's resource name, e.g. "organizations/123"
347+
* @param permissions the set of permissions to check for the resource. Permissions with wildcards
348+
* (such as '*' or 'storage.*') are not allowed.
349+
* @return the permissions and their results representing whether the caller has the permissions
350+
* on the specified Organization.
351+
* @throws ResourceManagerException upon failure
352+
* @see <a href=
353+
* "https://cloud.google.com/resource-manager/reference/rest/v1/organizations/testIamPermissions">
354+
* Resource Manager testIamPermissions</a>
355+
*/
356+
Map<String, Boolean> testOrgPermissions(String resource, List<String> permissions);
340357
}

java-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ResourceManagerException(IOException exception) {
6666
* @throws ResourceManagerException when {@code ex} was caused by a {@code
6767
* ResourceManagerException}
6868
*/
69-
static ResourceManagerException translateAndThrow(RetryHelperException ex) {
69+
public static ResourceManagerException translateAndThrow(RetryHelperException ex) {
7070
BaseServiceException.translate(ex);
7171
throw new ResourceManagerException(UNKNOWN_CODE, ex.getMessage(), ex.getCause());
7272
}

java-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.common.collect.ImmutableMap;
3333
import com.google.common.collect.Iterables;
3434
import com.google.common.collect.Maps;
35+
import java.io.IOException;
3536
import java.util.List;
3637
import java.util.Map;
3738
import java.util.concurrent.Callable;
@@ -273,6 +274,25 @@ public List<Boolean> call() {
273274
}
274275
}
275276

277+
@Override
278+
public Map<String, Boolean> testOrgPermissions(
279+
final String resource, final List<String> permissions) {
280+
try {
281+
return runWithRetries(
282+
new Callable<Map<String, Boolean>>() {
283+
@Override
284+
public Map<String, Boolean> call() throws IOException {
285+
return resourceManagerRpc.testOrgPermissions(resource, permissions);
286+
}
287+
},
288+
getOptions().getRetrySettings(),
289+
EXCEPTION_HANDLER,
290+
getOptions().getClock());
291+
} catch (RetryHelperException ex) {
292+
throw ResourceManagerException.translateAndThrow(ex);
293+
}
294+
}
295+
276296
private Map<ResourceManagerRpc.Option, ?> optionMap(Option... options) {
277297
Map<ResourceManagerRpc.Option, Object> temp = Maps.newEnumMap(ResourceManagerRpc.Option.class);
278298
for (Option option : options) {

java-resourcemanager/src/main/java/com/google/cloud/resourcemanager/spi/v1beta1/HttpResourceManagerRpc.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsRequest;
4141
import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse;
4242
import com.google.api.services.cloudresourcemanager.model.UndeleteProjectRequest;
43+
import com.google.cloud.RetryHelper;
4344
import com.google.cloud.Tuple;
4445
import com.google.cloud.http.BaseHttpServiceException;
4546
import com.google.cloud.http.HttpTransportOptions;
@@ -301,4 +302,26 @@ projectId, new TestIamPermissionsRequest().setPermissions(permissions))
301302
throw translate(ex);
302303
}
303304
}
305+
306+
@Override
307+
public Map<String, Boolean> testOrgPermissions(String resource, List<String> permissions)
308+
throws IOException {
309+
try {
310+
TestIamPermissionsResponse response =
311+
resourceManager
312+
.organizations()
313+
.testIamPermissions(
314+
resource, new TestIamPermissionsRequest().setPermissions(permissions))
315+
.execute();
316+
Set<String> permissionsOwned =
317+
ImmutableSet.copyOf(firstNonNull(response.getPermissions(), ImmutableList.<String>of()));
318+
ImmutableMap.Builder<String, Boolean> answer = ImmutableMap.builder();
319+
for (String permission : permissions) {
320+
answer.put(permission, permissionsOwned.contains(permission));
321+
}
322+
return answer.build();
323+
} catch (RetryHelper.RetryHelperException ex) {
324+
throw ResourceManagerException.translateAndThrow(ex);
325+
}
326+
}
304327
}

java-resourcemanager/src/main/java/com/google/cloud/resourcemanager/spi/v1beta1/ResourceManagerRpc.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.ServiceRpc;
2222
import com.google.cloud.Tuple;
2323
import com.google.cloud.resourcemanager.ResourceManagerException;
24+
import java.io.IOException;
2425
import java.util.List;
2526
import java.util.Map;
2627

@@ -124,5 +125,12 @@ Integer getInt(Map<Option, ?> options) {
124125
*/
125126
List<Boolean> testPermissions(String projectId, List<String> permissions);
126127

127-
// TODO(ajaykannan): implement "Organization" functionality when available (issue #319)
128+
/**
129+
* Tests whether the caller has the given permissions on the specified Organization. Returns the
130+
* permissions and their results.
131+
*
132+
* @throws ResourceManagerException upon failure
133+
*/
134+
Map<String, Boolean> testOrgPermissions(String resource, List<String> permissions)
135+
throws IOException;
128136
}

java-resourcemanager/src/test/java/com/google/cloud/resourcemanager/ResourceManagerImplTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import static org.junit.Assert.assertSame;
2525
import static org.junit.Assert.assertTrue;
2626
import static org.junit.Assert.fail;
27+
import static org.mockito.Mockito.doThrow;
28+
import static org.mockito.Mockito.verify;
29+
import static org.mockito.Mockito.when;
2730

2831
import com.google.api.gax.paging.Page;
2932
import com.google.cloud.Identity;
@@ -38,6 +41,7 @@
3841
import com.google.cloud.resourcemanager.testing.LocalResourceManagerHelper;
3942
import com.google.common.collect.ImmutableList;
4043
import com.google.common.collect.ImmutableMap;
44+
import java.io.IOException;
4145
import java.util.Iterator;
4246
import java.util.List;
4347
import java.util.Map;
@@ -46,7 +50,11 @@
4650
import org.junit.Before;
4751
import org.junit.BeforeClass;
4852
import org.junit.Test;
53+
import org.junit.runner.RunWith;
54+
import org.mockito.Mockito;
55+
import org.mockito.runners.MockitoJUnitRunner;
4956

57+
@RunWith(MockitoJUnitRunner.class)
5058
public class ResourceManagerImplTest {
5159

5260
private static final LocalResourceManagerHelper RESOURCE_MANAGER_HELPER =
@@ -75,6 +83,9 @@ public class ResourceManagerImplTest {
7583
.addIdentity(Role.editor(), Identity.serviceAccount("[email protected]"))
7684
.build();
7785

86+
private ResourceManagerRpcFactory rpcFactoryMock = Mockito.mock(ResourceManagerRpcFactory.class);
87+
private ResourceManagerRpc resourceManagerRpcMock = Mockito.mock(ResourceManagerRpc.class);
88+
7889
@BeforeClass
7990
public static void beforeClass() {
8091
RESOURCE_MANAGER_HELPER.start();
@@ -456,4 +467,54 @@ public void testRuntimeException() {
456467
assertEquals(exceptionMessage, exception.getCause().getMessage());
457468
}
458469
}
470+
471+
@Test
472+
public void testTestOrgPermissions() throws IOException {
473+
String organization = "organization/12345";
474+
List<String> permissions =
475+
ImmutableList.of(
476+
"resourcemanager.organizations.get", "resourcemanager.organizations.getIamPolicy");
477+
Map<String, Boolean> expected =
478+
ImmutableMap.of(
479+
"resourcemanager.organizations.get",
480+
true,
481+
"resourcemanager.organizations.getIamPolicy",
482+
false);
483+
when(rpcFactoryMock.create(Mockito.any(ResourceManagerOptions.class)))
484+
.thenReturn(resourceManagerRpcMock);
485+
ResourceManager resourceManager =
486+
ResourceManagerOptions.newBuilder()
487+
.setServiceRpcFactory(rpcFactoryMock)
488+
.build()
489+
.getService();
490+
when(resourceManagerRpcMock.testOrgPermissions(organization, permissions)).thenReturn(expected);
491+
Map<String, Boolean> actual = resourceManager.testOrgPermissions(organization, permissions);
492+
assertEquals(expected, actual);
493+
verify(resourceManagerRpcMock).testOrgPermissions(organization, permissions);
494+
}
495+
496+
@Test
497+
public void testTestOrgPermissionsWithResourceManagerException() throws IOException {
498+
String organization = "organizations/12345";
499+
String exceptionMessage = "Not Found";
500+
List<String> permissions =
501+
ImmutableList.of(
502+
"resourcemanager.organizations.get", "resourcemanager.organizations.getIamPolicy");
503+
when(rpcFactoryMock.create(Mockito.any(ResourceManagerOptions.class)))
504+
.thenReturn(resourceManagerRpcMock);
505+
ResourceManager resourceManager =
506+
ResourceManagerOptions.newBuilder()
507+
.setServiceRpcFactory(rpcFactoryMock)
508+
.build()
509+
.getService();
510+
doThrow(new ResourceManagerException(404, exceptionMessage))
511+
.when(resourceManagerRpcMock)
512+
.testOrgPermissions(organization, permissions);
513+
try {
514+
resourceManager.testOrgPermissions(organization, permissions);
515+
} catch (ResourceManagerException expected) {
516+
assertEquals(404, expected.getCode());
517+
assertEquals(exceptionMessage, expected.getMessage());
518+
}
519+
}
459520
}

0 commit comments

Comments
 (0)