Skip to content

Commit

Permalink
PF-469 Add subnetworks.list and zones.get GCE operations. (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
wchamber authored Apr 27, 2021
1 parent 0b270ab commit 0a8f9b4
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 26 deletions.
2 changes: 1 addition & 1 deletion google-compute/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 0.7.0
version = 0.8.0
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,69 @@ protected JsonObject serialize() {
return result;
}
}

/** See {@link Compute.Subnetworks#list(String, String)}. */
public List list(String project, String region) throws IOException {
return new List(subnetworks.list(project, region));
}

/** See {@link Compute.Subnetworks.List}. */
public class List extends AbstractRequestCow<SubnetworkList> {
private final Compute.Subnetworks.List list;

public List(Compute.Subnetworks.List list) {
super(CloudComputeOperation.GOOGLE_LIST_SUBNETWORK, clientConfig, operationAnnotator, list);
this.list = list;
}

/** See {@link Compute.Subnetworks.List#setProject(String)}. */
public List setProject(String project) {
this.list.setProject(project);
return this;
}

/** See {@link Compute.Subnetworks.List#setRegion(String)}. */
public List setRegion(String region) {
this.list.setRegion(region);
return this;
}

/** See {@link Compute.Subnetworks.List#setFilter(String)}. */
public List setFilter(String filter) {
this.list.setFilter(filter);
return this;
}

/** See {@link Compute.Subnetworks.List#setMaxResults(Long)}. */
public List setMaxResults(Long maxResults) {
this.list.setMaxResults(maxResults);
return this;
}

/** See {@link Compute.Subnetworks.List#setOrderBy(String)}. */
public List setOrderBy(String orderBy) {
this.list.setOrderBy(orderBy);
return this;
}

/** See {@link Compute.Subnetworks.List#setPageToken(String)}. */
public List setPageToken(String pageToken) {
this.list.setPageToken(pageToken);
return this;
}

@Override
protected JsonObject serialize() {
JsonObject result = new JsonObject();
result.addProperty("project_id", list.getProject());
result.addProperty("region", list.getRegion());
result.addProperty("filter", list.getFilter());
result.addProperty("max_results", list.getMaxResults());
result.addProperty("order_by", list.getOrderBy());
result.addProperty("page_token", list.getPageToken());
return result;
}
}
}

public Firewalls firewalls() {
Expand Down Expand Up @@ -356,6 +419,42 @@ protected JsonObject serialize() {
}
}

public Zones zones() {
return new Zones(compute.zones());
}

/** See {@link Compute.Zones}. */
public class Zones {
private final Compute.Zones zones;

private Zones(Compute.Zones zones) {
this.zones = zones;
}

/** See {@link Compute.Zones#get(String, String)}. */
public Get get(String project, String zone) throws IOException {
return new Get(zones.get(project, zone));
}

/** See {@link Compute.Zones.Get}. */
public class Get extends AbstractRequestCow<Zone> {
private final Compute.Zones.Get get;

private Get(Compute.Zones.Get get) {
super(CloudComputeOperation.GOOGLE_GET_ZONE, clientConfig, operationAnnotator, get);
this.get = get;
}

@Override
protected JsonObject serialize() {
JsonObject result = new JsonObject();
result.addProperty("project_id", get.getProject());
result.addProperty("zone", get.getZone());
return result;
}
}
}

/**
* See {@link Compute#globalOperations()}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public enum CloudComputeOperation implements CloudOperation {
GOOGLE_GET_NETWORK,
GOOGLE_GET_ROUTE,
GOOGLE_GET_SUBNETWORK,
GOOGLE_GET_ZONE,
GOOGLE_LIST_SUBNETWORK,
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void createAndGetAndDeleteNetwork() throws Exception {
}

@Test
public void createAndGetSubnetwork() throws Exception {
public void createAndGetAndListSubnetwork() throws Exception {
String projectId = reusableProject.getProjectId();
String region = "us-west1";
String ipCidrRange = "10.130.0.0/20";
Expand Down Expand Up @@ -105,6 +105,9 @@ public void createAndGetSubnetwork() throws Exception {
assertEquals(network.getSelfLink(), createdSubnet.getNetwork());
assertEquals(ipCidrRange, createdSubnet.getIpCidrRange());
assertEquals(regionName(projectId, region), createdSubnet.getRegion());

SubnetworkList subnetworkList = cloudComputeCow.subnetworks().list(projectId, region).execute();
assertThat(subnetworkList.getItems().size(), Matchers.greaterThan(0));
}

@Test
Expand Down Expand Up @@ -166,6 +169,13 @@ public void createAndGetRoute() throws Exception {
createdRoute.getNextHopGateway());
}

@Test
public void getZone() throws Exception {
Zone zone =
defaultCompute().zones().get(reusableProject.getProjectId(), "us-east1-b").execute();
assertThat(zone.getRegion(), Matchers.containsString("us-east1"));
}

@Test
public void networkInsertSerialize() throws Exception {
Network network = new Network().setName("network-name");
Expand Down Expand Up @@ -218,6 +228,24 @@ public void subnetworkGetSerialize() throws Exception {
get.serialize().toString());
}

@Test
public void subnetworkListSerialize() throws Exception {
CloudComputeCow.Subnetworks.List list =
defaultCompute()
.subnetworks()
.list("project-id", "us-west1")
.setFilter("my-filter")
.setMaxResults(42L)
.setOrderBy("order-by")
.setPageToken("page-token");

assertEquals(
"{\"project_id\":\"project-id\",\"region\":\"us-west1\","
+ "\"filter\":\"my-filter\",\"max_results\":42,\"order_by\":\"order-by\","
+ "\"page_token\":\"page-token\"}",
list.serialize().toString());
}

@Test
public void firewallInsertSerialize() throws Exception {
Firewall firewall = new Firewall().setName("firewall-name");
Expand Down Expand Up @@ -268,6 +296,13 @@ public void routeGetSerialize() throws Exception {
get.serialize().toString());
}

@Test
public void zoneGetSerialize() throws Exception {
CloudComputeCow.Zones.Get get = defaultCompute().zones().get("project-id", "us-east1-b");
assertEquals(
"{\"project_id\":\"project-id\",\"zone\":\"us-east1-b\"}", get.serialize().toString());
}

/** Create Project then set billing account, enable compute compute service */
private static Project createPreparedProject() throws Exception {
Project project = ProjectUtils.executeCreateProject();
Expand Down
2 changes: 1 addition & 1 deletion google-iam/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 0.6.0
version = 0.6.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BooleanSupplier;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
Expand Down Expand Up @@ -79,7 +76,6 @@ public void createAndGetAndListAndDeleteServiceAccount() throws Exception {

serviceAccounts.delete(serviceAccountName).execute();


GoogleJsonResponseException getAfterDelete = null;
for (int retryNum = 0; retryNum < 20; retryNum++) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package bio.terra.cloudres.google.iam;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("unit")
public class ServiceAccountNameTest {
@Test
public void name() {
String name = "projects/my-project/serviceAccounts/[email protected]";
ServiceAccountName serviceAccountName = ServiceAccountName.fromNameFormat(name);
@Test
public void name() {
String name = "projects/my-project/serviceAccounts/[email protected]";
ServiceAccountName serviceAccountName = ServiceAccountName.fromNameFormat(name);

assertEquals("my-project", serviceAccountName.projectId());
assertEquals("[email protected]", serviceAccountName.email());
assertEquals(name, serviceAccountName.formatName());
}
assertEquals("my-project", serviceAccountName.projectId());
assertEquals("[email protected]", serviceAccountName.email());
assertEquals(name, serviceAccountName.formatName());
}

@Test
public void invalidName() {
assertThrows(IllegalArgumentException.class, () -> ServiceAccountName.fromNameFormat("foo"));
}
@Test
public void invalidName() {
assertThrows(IllegalArgumentException.class, () -> ServiceAccountName.fromNameFormat("foo"));
}

@Test
public void emailAccountId() {
assertEquals("[email protected]", ServiceAccountName.emailFromAccountId("foo", "my-project"));
}
@Test
public void emailAccountId() {
assertEquals(
"[email protected]",
ServiceAccountName.emailFromAccountId("foo", "my-project"));
}
}

0 comments on commit 0a8f9b4

Please sign in to comment.