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

Add support for Compute's addresses #755

Merged
merged 11 commits into from
Mar 22, 2016
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@

package com.google.gcloud.compute;

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

import com.google.common.base.MoreObjects;

import java.util.Objects;

/**
* Interface for Google Compute Engine address identities.
* Base class for Google Compute Engine address identities.
*/
public interface AddressId {
public abstract class AddressId extends ResourceId {

private static final long serialVersionUID = 147328216049936438L;

private final String address;

/**
* Possible types for a Google Compute Engine address identity.
Expand All @@ -29,35 +39,53 @@ enum Type {
* Global static external IP addresses can be assigned to global forwarding rules.
*/
GLOBAL,

/**
* Region static external IP addresses can be assigned to instances and region forwarding rules.
*/
REGION
}

/**
* Returns the type of this address identity.
*/
Type type();
AddressId(String project, String address) {
super(project);
this.address = checkNotNull(address);
}

/**
* Returns the name of the project.
* Returns the type of this address identity.
*/
String project();
public abstract Type type();

/**
* Returns the name of the address resource. The name must be 1-63 characters long, and comply
* with RFC1035. Specifically, the name must be 1-63 characters long and match the regular
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
* except the last character, which cannot be a dash.
* Returns the name of the address resource. The name must be 1-63 characters long and comply with
* RFC1035. Specifically, the name must match the regular expression
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
* and all following characters must be a dash, lowercase letter, or digit, except the last
* character, which cannot be a dash.
*
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>

This comment was marked as spam.

*/
String address();
public String address() {
return address;
}

/**
* Returns a fully qualified URL to the entity.
*/
String selfLink();
@Override
MoreObjects.ToStringHelper toStringHelper() {
return super.toStringHelper().add("address", address);
}

@Override
final int baseHashCode() {
return Objects.hash(super.baseHashCode(), address);
}

@Override
final boolean baseEquals(ResourceId resourceId) {
return resourceId instanceof AddressId
&& super.baseEquals(resourceId)
&& Objects.equals(address, ((AddressId) resourceId).address);
}

@Override
abstract AddressId setProjectId(String projectId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Identity for a Google Compute Engine disk type.
*/
public final class DiskTypeId extends ZoneResourceId {
public final class DiskTypeId extends ResourceId {

static final Function<String, DiskTypeId> FROM_URL_FUNCTION = new Function<String, DiskTypeId>() {
@Override
Expand All @@ -43,14 +43,16 @@ public String apply(DiskTypeId diskTypeId) {
}
};

private static final String REGEX = ZoneResourceId.REGEX + "diskTypes/([^/]+)";
private static final String REGEX = ResourceId.REGEX + "zones/([^/]+)/diskTypes/([^/]+)";
private static final Pattern PATTERN = Pattern.compile(REGEX);
private static final long serialVersionUID = 7337881474103686219L;

private final String zone;
private final String diskType;

private DiskTypeId(String project, String zone, String diskType) {
super(project, zone);
super(project);
this.zone = checkNotNull(zone);
this.diskType = checkNotNull(diskType);
}

Expand All @@ -61,25 +63,40 @@ public String diskType() {
return diskType;
}

/**
* Returns the name of the zone this disk type belongs to.
*/
public String zone() {
return zone;
}

/**
* Returns the identity of the zone this disk type belongs to.
*/
public ZoneId zoneId() {
return ZoneId.of(project(), zone);
}

@Override
public String selfLink() {
return super.selfLink() + "/diskTypes/" + diskType;
return super.selfLink() + "/zones/" + zone + "/diskTypes/" + diskType;
}

@Override
MoreObjects.ToStringHelper toStringHelper() {
return super.toStringHelper().add("diskType", diskType);
return super.toStringHelper().add("zone", zone).add("diskType", diskType);
}

@Override
public int hashCode() {
return Objects.hash(super.baseHashCode(), diskType);
return Objects.hash(super.baseHashCode(), zone, diskType);
}

@Override
public boolean equals(Object obj) {
return obj instanceof DiskTypeId
&& baseEquals((DiskTypeId) obj)
&& Objects.equals(zone, ((DiskTypeId) obj).zone)

This comment was marked as spam.

&& Objects.equals(diskType, ((DiskTypeId) obj).diskType);
}

Expand All @@ -88,7 +105,7 @@ DiskTypeId setProjectId(String projectId) {
if (project() != null) {
return this;
}
return DiskTypeId.of(projectId, zone(), diskType);
return DiskTypeId.of(projectId, zone, diskType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,25 @@

package com.google.gcloud.compute;

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

import com.google.common.base.MoreObjects;

import java.util.Objects;

/**
* Interface for Google Compute Engine forwarding rule identities.

This comment was marked as spam.

*/
public interface ForwardingRuleId {
public abstract class ForwardingRuleId extends ResourceId {

private static final long serialVersionUID = -4352410760458355391L;

private final String rule;

ForwardingRuleId(String project, String rule) {
super(project);
this.rule = checkNotNull(rule);
}

/**
* Possible types for a Google Compute Engine forwarding rule identity.
Expand All @@ -30,6 +45,7 @@ enum Type {
* load balancing.
*/
GLOBAL,

/**

This comment was marked as spam.

* Region forwarding rules are used to forward traffic to the correct pool of target virtual
* machines.
Expand All @@ -40,26 +56,38 @@ enum Type {
/**
* Returns the type of this forwarding rule identity.
*/
Type type();

/**
* Returns the name of the project.
*/
String project();
public abstract Type type();

/**
* Returns the name of the forwarding rule. The name must be 1-63 characters long, and comply with
* RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression
* Returns the name of the forwarding rule. The forwarding rule name must be 1-63 characters long
* and comply with RFC1035. Specifically, the name must match the regular expression
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
* and all following characters must be a dash, lowercase letter, or digit, except the last
* character, which cannot be a dash.
*
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

*/
String rule();
public String rule() {
return rule;
}

/**
* Returns a fully qualified URL to the entity.
*/
String selfLink();
@Override
MoreObjects.ToStringHelper toStringHelper() {
return super.toStringHelper().add("rule", rule);
}

@Override
final int baseHashCode() {
return Objects.hash(super.baseHashCode(), rule);
}

@Override
final boolean baseEquals(ResourceId resourceId) {
return resourceId instanceof ForwardingRuleId
&& super.baseEquals(resourceId)
&& Objects.equals(rule, ((ForwardingRuleId) resourceId).rule);
}

@Override
abstract ForwardingRuleId setProjectId(String projectId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,56 @@

package com.google.gcloud.compute;

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

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

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Identity for a Google Compute Engine global address.
*/
public final class GlobalAddressId extends ResourceId implements AddressId {
public final class GlobalAddressId extends AddressId {

private static final String REGEX = ResourceId.REGEX + "global/addresses/([^/]+)";
private static final Pattern PATTERN = Pattern.compile(REGEX);
private static final long serialVersionUID = -2950815290049218593L;

private final String address;

private GlobalAddressId(String project, String address) {
super(project);
this.address = checkNotNull(address);
super(project, address);
}

@Override
public Type type() {
return Type.GLOBAL;
}

@Override
public String address() {
return address;
}

@Override
public String selfLink() {
return super.selfLink() + "/global/addresses/" + address;
}

@Override
public ToStringHelper toStringHelper() {
return super.toStringHelper().add("address", address);
return super.selfLink() + "/global/addresses/" + address();
}

@Override
public int hashCode() {
return Objects.hash(baseHashCode(), address);
return baseHashCode();
}

@Override
public boolean equals(Object obj) {
return obj instanceof GlobalAddressId
&& baseEquals((GlobalAddressId) obj)
&& Objects.equals(address, ((GlobalAddressId) obj).address);
return obj instanceof GlobalAddressId && baseEquals((GlobalAddressId) obj);
}

@Override
GlobalAddressId setProjectId(String projectId) {
if (project() != null) {
return this;
}
return GlobalAddressId.of(projectId, address);
return GlobalAddressId.of(projectId, address());
}

/**
* Returns an address identity given the address name. The address name must be 1-63 characters
* long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match
* the regular expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must
* be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
* except the last character, which cannot be a dash.
* long and comply with RFC1035. Specifically, the name must match the regular expression
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
* and all following characters must be a dash, lowercase letter, or digit, except the last
* character, which cannot be a dash.
*
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>

This comment was marked as spam.

*/
Expand All @@ -95,10 +75,10 @@ public static GlobalAddressId of(String address) {

/**
* Returns an address identity given project and address names. The address name must be 1-63
* characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long
* and match the regular expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first
* character must be a lowercase letter, and all following characters must be a dash, lowercase
* letter, or digit, except the last character, which cannot be a dash.
* characters long and comply with RFC1035. Specifically, the name must match the regular
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
* except the last character, which cannot be a dash.
*
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
*/
Expand Down
Loading