Skip to content

Commit

Permalink
Add exception handling, add back resource ID, and other cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Nov 27, 2015
1 parent 0ec9f50 commit b1da3ad
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Base class for Resource Manager operation options
*/
public class Option implements Serializable {
class Option implements Serializable {

private static final long serialVersionUID = 2655177550880762967L;

Expand Down Expand Up @@ -70,4 +70,3 @@ public String toString() {
.toString();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
/**
* A Google Cloud Resource Manager project object.
*
* This class' member variables are immutable. Methods that change or update the underlying Project
* information return a new Project instance.
* A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
* AppEngine Apps, VMs, and other Google Cloud Platform resources. This class' member variables are
* immutable. Methods that change or update the underlying Project information return a new Project
* instance.
*/
public class Project {

Expand Down Expand Up @@ -69,7 +71,7 @@ public ResourceManager resourceManager() {
* @throws ResourceManagerException upon failure
*/
public Project reload() {
return new Project(resourceManager, resourceManager.get(info.id()));
return Project.load(resourceManager, info.projectId());
}

/**
Expand All @@ -89,13 +91,13 @@ public Project reload() {
* completes, the project is not retrievable by the {@link ResourceManager#get} and
* {@link ResourceManager#list} methods. The caller must have modify permissions for this project.
*
* @see
* <a href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete">
* Cloud Resource Manager delete</a>
* @see <a
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete">Cloud
* Resource Manager delete</a>
* @throws ResourceManagerException upon failure
*/
public void delete() {
resourceManager.delete(info.id());
resourceManager.delete(info.projectId());
}

/**
Expand All @@ -106,23 +108,23 @@ public void delete() {
* state of {@link ProjectInfo.State#DELETE_IN_PROGRESS}, the project cannot be restored. The
* caller must have modify permissions for this project.
*
* @see <a href=
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete">
* Cloud Resource Manager undelete</a>
* @see <a
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete">Cloud
* Resource Manager undelete</a>
* @throws ResourceManagerException upon failure (including when the project can't be restored)
*/
public void undelete() {
resourceManager.undelete(info.id());
resourceManager.undelete(info.projectId());
}

/**
* Replaces the attributes of the project.
*
* The caller must have modify permissions for this project.
*
* @see
* <a href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">
* Cloud Resource Manager update</a>
* @see <a
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud
* Resource Manager update</a>
* @return the ProjectInfo representing the new project metadata
* @throws ResourceManagerException upon failure
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

package com.google.gcloud.resourcemanager;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.client.util.Data;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;

import java.io.Serializable;
Expand All @@ -28,26 +31,23 @@

/**
* A Google Cloud Resource Manager project metadata object.
*
* A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
* AppEngine Apps, VMs, and other Google Cloud Platform resources.
*/
public class ProjectInfo implements Serializable {

private static final long serialVersionUID = 9148970963697734236L;
private final String name;
private final String id;
private final String projectId;
private final Map<String, String> labels;
private final Long number;
private final Long projectNumber;
private final State state;
private final Long createTimeMillis;
private final ResourceId parent;

/**
* The project lifecycle state.
*
* <ul>
* <li>LIFECYCLE_STATE_UNSPECIFIED:
* <li>ACTIVE:
* <li>DELETE_REQUESTED:
* <li>DELETE_IN_PROGRESS:
* <ul>
* The project lifecycle states.
*/
public enum State {
/**
Expand All @@ -72,16 +72,71 @@ public enum State {
DELETE_IN_PROGRESS
}

static class ResourceId implements Serializable {

private static final long serialVersionUID = -325199985993344726L;

private final String id;
private final String type;

ResourceId(String id, String type) {
this.id = checkNotNull(id);
this.type = checkNotNull(type);
}

String id() {
return id;
}

String type() {
return type;
}

@Override
public boolean equals(Object obj) {
return obj instanceof ResourceId && Objects.equals(toPb(), ((ResourceId) obj).toPb());
}

@Override
public int hashCode() {
return Objects.hash(id, type);
}

com.google.api.services.cloudresourcemanager.model.ResourceId toPb() {
com.google.api.services.cloudresourcemanager.model.ResourceId resourceIdPb =
new com.google.api.services.cloudresourcemanager.model.ResourceId();
resourceIdPb.setId(id);
resourceIdPb.setType(type.toString().toLowerCase());
return resourceIdPb;
}

static ResourceId fromPb(
com.google.api.services.cloudresourcemanager.model.ResourceId resourceIdPb) {
return new ResourceId(resourceIdPb.getId(), resourceIdPb.getType());
}
}

public static class Builder {

private String name;
private String id;
private Map<String, String> labels;
private Long number;
private String projectId;
private Map<String, String> labels = new HashMap<>();
private Long projectNumber;
private State state;
private Long createTimeMillis;
private ResourceId parent;

Builder() {
labels = new HashMap<>();
private Builder() {
}

Builder(ProjectInfo info) {
this.name = info.name;
this.projectId = info.projectId;
this.labels = Maps.newHashMap(checkNotNull(info.labels));
this.projectNumber = info.projectNumber;
this.state = info.state;
this.createTimeMillis = info.createTimeMillis;
this.parent = info.parent;
}

/**
Expand All @@ -92,7 +147,7 @@ public static class Builder {
* field can be changed after project creation.
*/
public Builder name(String name) {
this.name = name;
this.name = firstNonNull(name, Data.<String>nullOf(String.class));
return this;
}

Expand All @@ -103,8 +158,8 @@ public Builder name(String name) {
* Trailing hyphens are prohibited. This field cannot be changed after the server creates the
* project.
*/
public Builder id(String id) {
this.id = id;
public Builder projectId(String projectId) {
this.projectId = projectId;
return this;
}

Expand Down Expand Up @@ -148,8 +203,8 @@ public Builder labels(Map<String, String> labels) {
return this;
}

Builder number(Long number) {
this.number = number;
Builder projectNumber(Long projectNumber) {
this.projectNumber = projectNumber;
return this;
}

Expand All @@ -163,27 +218,33 @@ Builder createTimeMillis(Long createTimeMillis) {
return this;
}

Builder parent(ResourceId parent) {
this.parent = parent;
return this;
}

public ProjectInfo build() {
return new ProjectInfo(this);
}
}

ProjectInfo(Builder builder) {
this.name = builder.name;
this.id = checkNotNull(builder.id);
this.projectId = checkNotNull(builder.projectId);
this.labels = ImmutableMap.copyOf(builder.labels);
this.number = builder.number;
this.projectNumber = builder.projectNumber;
this.state = builder.state;
this.createTimeMillis = builder.createTimeMillis;
this.parent = builder.parent;
}

/**
* Get the unique, user-assigned ID of the project.
*
* This field cannot be changed after the server creates the project.
*/
public String id() {
return id;
public String projectId() {
return projectId;
}

/**
Expand All @@ -192,16 +253,16 @@ public String id() {
* This field is optional, can remain unset, and can be changed after project creation.
*/
public String name() {
return name;
return Data.isNull(name) ? null : name;
}

/**
* Get number uniquely identifying the project.
*
* This field is set by the server and is read-only.
*/
public Long number() {
return number;
public Long projectNumber() {
return projectNumber;
}

/**
Expand All @@ -221,6 +282,10 @@ public State state() {
return state;
}

ResourceId parent() {
return parent;
}

/**
* Get the project's creation time (in milliseconds).
*
Expand All @@ -237,45 +302,54 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
return Objects.hash(name, id, labels, number, state, createTimeMillis);
return Objects.hash(name, projectId, labels, projectNumber, state, createTimeMillis, parent);
}

public static Builder builder(String id) {
return new Builder().id(id);
return new Builder().projectId(id);
}

public Builder toBuilder() {
return new Builder()
.name(name)
.id(id)
.labels(labels)
.number(number)
.state(state)
.createTimeMillis(createTimeMillis);
return new Builder(this);
}

com.google.api.services.cloudresourcemanager.model.Project toPb() {
com.google.api.services.cloudresourcemanager.model.Project projectPb =
new com.google.api.services.cloudresourcemanager.model.Project();
projectPb.setName(name);
projectPb.setProjectId(id);
projectPb.setProjectId(projectId);
projectPb.setLabels(labels);
projectPb.setProjectNumber(number);
projectPb.setProjectNumber(projectNumber);
if (state != null) {
projectPb.setLifecycleState(state.toString());
}
if (createTimeMillis != null) {
projectPb.setCreateTime(ISODateTimeFormat.dateTime().print(createTimeMillis));
}
if (parent != null) {
projectPb.setParent(parent.toPb());
}
return projectPb;
}

static ProjectInfo fromPb(com.google.api.services.cloudresourcemanager.model.Project projectPb) {
ProjectInfo.Builder builder =
ProjectInfo.builder(projectPb.getProjectId()).name(projectPb.getName());
ProjectInfo.builder(projectPb.getProjectId()).projectNumber(projectPb.getProjectNumber());
if (projectPb.getName() != null) {
builder.name(projectPb.getName());
}
if (projectPb.getLabels() != null) {
builder.labels(projectPb.getLabels());
}
if (projectPb.getLifecycleState() != null) {
builder.state(State.valueOf(projectPb.getLifecycleState()));
}
if (projectPb.getCreateTime() != null) {
builder.createTimeMillis(DateTime.parse(projectPb.getCreateTime()).getMillis());
}
if (projectPb.getParent() != null) {
builder.parent(ResourceId.fromPb(projectPb.getParent()));
}
return builder.build();
}
}
Loading

0 comments on commit b1da3ad

Please sign in to comment.