Skip to content

Commit

Permalink
fix(google): make FULL the default view in instanceTemplate client
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarulg committed Jan 20, 2025
1 parent 46c320d commit d6142a9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class InstanceTemplates {
private final Compute.InstanceTemplates computeApi;
private final GoogleNamedAccountCredentials credentials;
private final GlobalGoogleComputeRequestFactory requestFactory;
private static final String defaultView =
"FULL"; // https://cloud.google.com/sdk/gcloud/reference/beta/compute/instance-templates/list

InstanceTemplates(
GoogleNamedAccountCredentials credentials,
Expand Down Expand Up @@ -61,12 +63,14 @@ public GoogleComputeOperationRequest<Compute.InstanceTemplates.Insert> insert(
return requestFactory.wrapOperationRequest(request, "insert");
}

public PaginatedComputeRequest<Compute.InstanceTemplates.List, InstanceTemplate> list(
String view) {
public PaginatedComputeRequest<Compute.InstanceTemplates.List, InstanceTemplate> list() {
return new PaginatedComputeRequestImpl<>(
pageToken ->
requestFactory.wrapRequest(
computeApi.list(credentials.getProject()).setPageToken(pageToken).setView(view),
computeApi
.list(credentials.getProject())
.setPageToken(pageToken)
.setView(defaultView),
"list"),
InstanceTemplateList::getNextPageToken,
InstanceTemplateList::getItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,7 @@ Collection<Instance> retrieveAllInstancesInRegion() throws IOException {

private Collection<InstanceTemplate> retrieveInstanceTemplates() throws IOException {
InstanceTemplates instanceTemplatesApi = computeApiFactory.createInstanceTemplates(credentials);
return instanceTemplatesApi
.list("FULL") // See view options:
// https://cloud.google.com/sdk/gcloud/reference/beta/compute/instance-templates/list
.execute();
return instanceTemplatesApi.list().execute();
}

Collection<String> getZonesForRegion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void list_success() throws IOException {
InstanceTemplates instanceTemplates = createInstanceTemplates(transport);

PaginatedComputeRequest<Compute.InstanceTemplates.List, InstanceTemplate> request =
instanceTemplates.list("BASIC");
instanceTemplates.list();

ImmutableList<InstanceTemplate> response = request.execute();
assertThat(response).hasSize(2);
Expand All @@ -229,7 +229,7 @@ public void list_noResults() throws IOException {
InstanceTemplates instanceTemplates = createInstanceTemplates(transport);

PaginatedComputeRequest<Compute.InstanceTemplates.List, InstanceTemplate> request =
instanceTemplates.list("BASIC");
instanceTemplates.list();

ImmutableList<InstanceTemplate> response = request.execute();
assertThat(response).isEmpty();
Expand All @@ -249,38 +249,11 @@ public void list_errorResponse() {
InstanceTemplates instanceTemplates = createInstanceTemplates(transport);

PaginatedComputeRequest<Compute.InstanceTemplates.List, InstanceTemplate> request =
instanceTemplates.list("BASIC");
instanceTemplates.list();

assertThatIOException().isThrownBy(() -> request.execute());
}

@Test
public void list_withFullView_success() throws IOException {

MockHttpTransport transport =
new MockHttpTransport.Builder()
.setLowLevelHttpResponse(
new MockLowLevelHttpResponse()
.setStatusCode(200)
.addHeader("Content-Type", "application/json")
.setContent(
"{\"items\": [{\"name\": \"template1\", \"description\": \"Detailed description for template1\"}, {\"name\": \"template2\", \"description\": \"Detailed description for template2\"}], \"nextPageToken\": \"\"}"))
.build();

InstanceTemplates instanceTemplates = createInstanceTemplates(transport);

PaginatedComputeRequest<Compute.InstanceTemplates.List, InstanceTemplate> request =
instanceTemplates.list("FULL");

ImmutableList<InstanceTemplate> response = request.execute();

assertThat(response).hasSize(2);
assertThat(response.get(0).getName()).isEqualTo("template1");
assertThat(response.get(0).getDescription()).isEqualTo("Detailed description for template1");
assertThat(response.get(1).getName()).isEqualTo("template2");
assertThat(response.get(1).getDescription()).isEqualTo("Detailed description for template2");
}

private static InstanceTemplates createInstanceTemplates(HttpTransport transport) {
return createInstanceTemplates(transport, new NoopRegistry());
}
Expand Down

0 comments on commit d6142a9

Please sign in to comment.