Skip to content

Commit

Permalink
Merge pull request #794 from mziccard/compute-image
Browse files Browse the repository at this point in the history
Add ImageInfo, ImageId and test classes
  • Loading branch information
mziccard committed Mar 31, 2016
2 parents 32394d2 + f2231c6 commit bc368cc
Show file tree
Hide file tree
Showing 16 changed files with 1,944 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.gcloud.compute;

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

import com.google.common.base.Function;
import com.google.common.base.MoreObjects;

Expand All @@ -28,16 +30,17 @@
/**
* The deprecation status associated to a Google Compute Engine resource.
*
* @param <T> The Google Compute Engine resource to which the deprecation status refers to.
* @param <T> The Google Compute Engine resource identity to which the deprecation status refers
*/
public final class DeprecationStatus<T extends ResourceId> implements Serializable {

private static final long serialVersionUID = -2695077634793679794L;
private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime();
private static final DateTimeFormatter TIMESTAMP_PARSER = ISODateTimeFormat.dateTimeParser();

private final Long deleted;
private final Long deprecated;
private final Long obsolete;
private final String deleted;
private final String deprecated;
private final String obsolete;
private final T replacement;
private final Status status;

Expand All @@ -64,38 +67,203 @@ public enum Status {
DELETED
}

DeprecationStatus(Long deleted, Long deprecated, Long obsolete, T replacement, Status status) {
this.deleted = deleted;
this.deprecated = deprecated;
this.obsolete = obsolete;
this.replacement = replacement;
this.status = status;
/**
* A builder for {@code DeprecationStatus} objects.
*
* @param <T> The Google Compute Engine resource identity to which the deprecation status refers
*/
public static final class Builder<T extends ResourceId> {

private String deleted;
private String deprecated;
private String obsolete;
private T replacement;
private Status status;

Builder() {}

Builder(DeprecationStatus<T> deprecationStatus) {
this.deleted = deprecationStatus.deleted;
this.deprecated = deprecationStatus.deprecated;
this.obsolete = deprecationStatus.obsolete;
this.replacement = deprecationStatus.replacement;
this.status = deprecationStatus.status;
}

/**
* Sets the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#DELETED}. Timestamp should be in RFC3339 format.
*
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
*/
// todo(mziccard): remove this method if #732 is closed
public Builder<T> deleted(String deleted) {
this.deleted = deleted;
return this;
}

/**
* Sets the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#DEPRECATED}. Timestamp should be in RFC3339 format.
*
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
*/
// todo(mziccard): remove this method if #732 is closed
public Builder<T> deprecated(String deprecated) {
this.deprecated = deprecated;
return this;
}

/**
* Sets the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#OBSOLETE}. Timestamp should be in RFC3339 format.
*
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
*/
// todo(mziccard): remove this method if #732 is closed
public Builder<T> obsolete(String obsolete) {
this.obsolete = obsolete;
return this;
}

/**
* Sets the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#DELETED}. In milliseconds since epoch.
*/
public Builder<T> deleted(long deleted) {
this.deleted = TIMESTAMP_FORMATTER.print(deleted);
return this;
}

/**
* Sets the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#DEPRECATED}. In milliseconds since epoch.
*/
public Builder<T> deprecated(long deprecated) {
this.deprecated = TIMESTAMP_FORMATTER.print(deprecated);
return this;
}

/**
* Sets the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#OBSOLETE}. In milliseconds since epoch.
*/
public Builder<T> obsolete(long obsolete) {
this.obsolete = TIMESTAMP_FORMATTER.print(obsolete);
return this;
}

/**
* Sets the identity of the suggested replacement for a deprecated resource. The suggested
* replacement resource must be the same kind of resource as the deprecated resource.
*/
public Builder<T> replacement(T replacement) {
this.replacement = replacement;
return this;
}

/**
* Sets the status of the deprecated resource.
*/
public Builder<T> status(Status status) {
this.status = checkNotNull(status);
return this;
}

/**
* Creates a {@code DeprecationStatus} object.
*/
public DeprecationStatus<T> build() {
return new DeprecationStatus<T>(this);
}
}

DeprecationStatus(Builder<T> builder) {
this.deleted = builder.deleted;
this.deprecated = builder.deprecated;
this.obsolete = builder.obsolete;
this.replacement = builder.replacement;
this.status = checkNotNull(builder.status);
}

/**
* Returns the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#DELETED}. In milliseconds since epoch.
* to {@link Status#DELETED}. Returns {@code null} if not set. This value should be in RFC3339
* format.
*
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
*/
public Long deleted() {
// todo(mziccard): remove this method if #732 is closed
public String deleted() {
return deleted;
}

/**
* Returns the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#DEPRECATED}. In milliseconds since epoch.
* to {@link Status#DEPRECATED}. Returns {@code null} if not set. This value should be in RFC3339
* format.
*
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
*/
public Long deprecated() {
// todo(mziccard): remove this method if #732 is closed
public String deprecated() {
return deprecated;
}

/**
* Returns the timestamp on or after which the deprecation state of this resource will be changed
* to {@link Status#OBSOLETE}. In milliseconds since epoch.
* to {@link Status#OBSOLETE}. Returns {@code null} if not set. This value should be in RFC3339
* format.
*
* @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC3339</a>
*/
public Long obsolete() {
// todo(mziccard): remove this method if #732 is closed
public String obsolete() {
return obsolete;
}

/**
* Returns the timestamp (in milliseconds since epoch) on or after which the deprecation state of
* this resource will be changed to {@link Status#DELETED}. Returns {@code null} if not set.
*
* @throws IllegalStateException if {@link #deleted()} is not a valid date, time or datetime
*/
public Long deletedMillis() {
try {
return deleted != null ? TIMESTAMP_PARSER.parseMillis(deleted) : null;
} catch (IllegalArgumentException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}

/**
* Returns the timestamp (in milliseconds since epoch) on or after which the deprecation state of
* this resource will be changed to {@link Status#DEPRECATED}. Returns {@code null} if not set.
*
* @throws IllegalStateException if {@link #deprecated()} is not a valid date, time or datetime
*/
public Long deprecatedMillis() {
try {
return deprecated != null ? TIMESTAMP_PARSER.parseMillis(deprecated) : null;
} catch (IllegalArgumentException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}

/**
* Returns the timestamp (in milliseconds since epoch) on or after which the deprecation state of
* this resource will be changed to {@link Status#OBSOLETE}. Returns {@code null} if not set.
*
* @throws IllegalStateException if {@link #obsolete()} is not a valid date, time or datetime
*/
public Long obsoleteMillis() {
try {
return obsolete != null ? TIMESTAMP_PARSER.parseMillis(obsolete) : null;
} catch (IllegalArgumentException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}

/**
* Returns the identity of the suggested replacement for a deprecated resource. The suggested
* replacement resource must be the same kind of resource as the deprecated resource.
Expand All @@ -111,6 +279,13 @@ public Status status() {
return status;
}

/**
* Returns a builder for the {@code DeprecationStatus} object.
*/
public Builder<T> toBuilder() {
return new Builder<>(this);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand All @@ -136,37 +311,49 @@ public boolean equals(Object obj) {
com.google.api.services.compute.model.DeprecationStatus toPb() {
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb =
new com.google.api.services.compute.model.DeprecationStatus();
if (deleted != null) {
deprecationStatusPb.setDeleted(TIMESTAMP_FORMATTER.print(deleted));
}
if (deprecated != null) {
deprecationStatusPb.setDeprecated(TIMESTAMP_FORMATTER.print(deprecated));
}
if (obsolete != null) {
deprecationStatusPb.setObsolete(TIMESTAMP_FORMATTER.print(obsolete));
}
if (replacement != null) {
deprecationStatusPb.setReplacement(replacement.selfLink());
}
if (status() != null) {
deprecationStatusPb.setState(status.name());
}
deprecationStatusPb.setDeleted(deleted);
deprecationStatusPb.setDeprecated(deprecated);
deprecationStatusPb.setObsolete(obsolete);
deprecationStatusPb.setReplacement(replacement.selfLink());
deprecationStatusPb.setState(status.name());
return deprecationStatusPb;
}

/**
* Returns the builder for a {@code DeprecationStatus} object given the status.
*/
public static <T extends ResourceId> Builder<T> builder(Status status) {
return new Builder<T>().status(status);
}

/**
* Returns the builder for a {@code DeprecationStatus} object given the status and replacement's
* identity.
*/
public static <T extends ResourceId> Builder<T> builder(Status status, T replacement) {
return new Builder<T>().status(status).replacement(replacement);
}

/**
* Returns a {@code DeprecationStatus} object given the status and replacement's identity.
*/
public static <T extends ResourceId> DeprecationStatus<T> of(Status status, T replacement) {
return builder(status, replacement).build();
}

static <T extends ResourceId> DeprecationStatus<T> fromPb(
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb,
Function<String, T> fromUrl) {
return new DeprecationStatus<>(
deprecationStatusPb.getDeleted() != null
? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getDeleted()) : null,
deprecationStatusPb.getDeprecated() != null
? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getDeprecated()) : null,
deprecationStatusPb.getObsolete() != null
? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getObsolete()) : null,
deprecationStatusPb.getReplacement() != null
? fromUrl.apply(deprecationStatusPb.getReplacement()) : null,
deprecationStatusPb.getState() != null
? Status.valueOf(deprecationStatusPb.getState()) : null);
Builder<T> builder = new Builder<>();
builder.deleted(deprecationStatusPb.getDeleted());
builder.deprecated(deprecationStatusPb.getDeprecated());
builder.obsolete(deprecationStatusPb.getObsolete());
if (deprecationStatusPb.getReplacement() != null) {
builder.replacement(fromUrl.apply(deprecationStatusPb.getReplacement()));
}
if (deprecationStatusPb.getState() != null) {
builder.status(Status.valueOf(deprecationStatusPb.getState()));
}
return builder.build();
}
}
Loading

0 comments on commit bc368cc

Please sign in to comment.