Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default spi layer implementation #427

Merged
merged 2 commits into from
Dec 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.google.gcloud.spi;

import static com.google.gcloud.spi.ResourceManagerRpc.Option.FIELDS;
import static com.google.gcloud.spi.ResourceManagerRpc.Option.FILTER;
import static com.google.gcloud.spi.ResourceManagerRpc.Option.PAGE_SIZE;
import static com.google.gcloud.spi.ResourceManagerRpc.Option.PAGE_TOKEN;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.cloudresourcemanager.Cloudresourcemanager;
import com.google.api.services.cloudresourcemanager.model.ListProjectsResponse;
import com.google.api.services.cloudresourcemanager.model.Project;
import com.google.common.collect.ImmutableSet;
import com.google.gcloud.resourcemanager.ResourceManagerException;
Expand All @@ -18,15 +24,13 @@
public class DefaultResourceManagerRpc implements ResourceManagerRpc {

// see https://cloud.google.com/resource-manager/v1/errors/core_errors
private static final Set<Integer> RETRYABLE_CODES = ImmutableSet.of(503, 500, 429, 417);
private static final Set<Integer> RETRYABLE_CODES = ImmutableSet.of(503, 500, 429);

private final ResourceManagerOptions options;
private final Cloudresourcemanager resourceManager;

public DefaultResourceManagerRpc(ResourceManagerOptions options) {
HttpTransport transport = options.httpTransportFactory().create();
HttpRequestInitializer initializer = options.httpRequestInitializer();
this.options = options;
resourceManager =
new Cloudresourcemanager.Builder(transport, new JacksonFactory(), initializer)
.setRootUrl(options.host())
Expand All @@ -52,37 +56,68 @@ private static ResourceManagerException translate(GoogleJsonError exception) {

@Override
public Project create(Project project) throws ResourceManagerException {
// TODO(ajaykannan): fix me!
return null;
try {
return resourceManager.projects().create(project).execute();
} catch (IOException ex) {
throw translate(ex);
}
}

@Override
public void delete(String projectId) throws ResourceManagerException {
// TODO(ajaykannan): fix me!
try {
resourceManager.projects().delete(projectId).execute();
} catch (IOException ex) {
throw translate(ex);
}
}

@Override
public Project get(String projectId, Map<Option, ?> options) throws ResourceManagerException {
// TODO(ajaykannan): fix me!
return null;
try {
return resourceManager.projects()
.get(projectId)
.setFields(FIELDS.getString(options))
.execute();
} catch (IOException ex) {
throw translate(ex);
}
}

@Override
public Tuple<String, Iterable<Project>> list(Map<Option, ?> options)
throws ResourceManagerException {
// TODO(ajaykannan): fix me!
return null;
try {
ListProjectsResponse response = resourceManager.projects()
.list()
.setFields(FIELDS.getString(options))
.setFilter(FILTER.getString(options))
.setPageSize(PAGE_SIZE.getInt(options))
.setPageToken(PAGE_TOKEN.getString(options))
.execute();
return Tuple.<String, Iterable<Project>>of(
response.getNextPageToken(), response.getProjects());
} catch (IOException ex) {
throw translate(ex);
}
}

@Override
public void undelete(String projectId) throws ResourceManagerException {
// TODO(ajaykannan): fix me!
try {
resourceManager.projects().undelete(projectId).execute();
} catch (IOException ex) {
throw translate(ex);
}
}

@Override
public Project replace(Project project) throws ResourceManagerException {
// TODO(ajaykannan): fix me!
return null;
try {
return resourceManager.projects().update(project.getProjectId(), project).execute();
} catch (IOException ex) {
throw translate(ex);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ String getString(Map<Option, ?> options) {
return get(options);
}

Long getInt(Map<Option, ?> options) {
Integer getInt(Map<Option, ?> options) {
return get(options);
}
}
Expand Down