Skip to content

Commit

Permalink
Add SubnetworkInfo, NetworkId, SubnetworkId and test classes (googlea…
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Apr 29, 2016
1 parent a4f9f2d commit 282f1fc
Show file tree
Hide file tree
Showing 7 changed files with 948 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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 network.
*/
public final class NetworkId extends ResourceId {

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

private final String network;

NetworkId(String project, String network) {
super(project);
this.network = checkNotNull(network);
}

private NetworkId(NetworkId networkId) {
super(networkId.project());
this.network = checkNotNull(networkId.network());
}

/**
* Returns the name of the network. The network 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>
*/
public String network() {
return network;
}

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

@Override
ToStringHelper toStringHelper() {
return super.toStringHelper().add("network", network);
}

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

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof NetworkId)) {
return false;
}
NetworkId other = (NetworkId) obj;
return baseEquals(other) && Objects.equals(network, other.network);
}

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

/**
* Returns a new network identity given project and network names. The network 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>
*/
public static NetworkId of(String project, String network) {
return new NetworkId(project, network);
}

/**
* Returns a new network identity given network name. The network 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>
*/
public static NetworkId of(String network) {
return NetworkId.of(null, network);
}

/**
* Returns {@code true} if the provided string matches the expected format of a network URL.
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return PATTERN.matcher(url).matches();
}

static NetworkId fromUrl(String url) {
Matcher matcher = PATTERN.matcher(url);
if (!matcher.matches()) {
throw new IllegalArgumentException(url + " is not a valid network URL");
}
return NetworkId.of(matcher.group(1), matcher.group(2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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;

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

/**
* Identity for a Google Compute Engine subnetwork.
*/
public final class SubnetworkId extends ResourceId {

static final Function<String, SubnetworkId> FROM_URL_FUNCTION =
new Function<String, SubnetworkId>() {
@Override
public SubnetworkId apply(String pb) {
return SubnetworkId.fromUrl(pb);
}
};
static final Function<SubnetworkId, String> TO_URL_FUNCTION =
new Function<SubnetworkId, String>() {
@Override
public String apply(SubnetworkId zoneId) {
return zoneId.selfLink();
}
};

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

private final String region;
private final String subnetwork;

private SubnetworkId(String project, String region, String subnetwork) {
super(project);
this.region = checkNotNull(region);
this.subnetwork = checkNotNull(subnetwork);
}

/**
* Returns the name of the region this subnetwork belongs to.
*/
public String region() {
return region;
}

/**
* Returns the identity of the region this subnetwork belongs to.
*/
public RegionId regionId() {
return RegionId.of(project(), region);
}

/**
* Returns the name of the subnetwork. 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>
*/
public String subnetwork() {
return subnetwork;
}

@Override
public String selfLink() {
return super.selfLink() + "/regions/" + region + "/subnetworks/" + subnetwork;
}

@Override
MoreObjects.ToStringHelper toStringHelper() {
return MoreObjects.toStringHelper(this).add("region", region).add("subnetwork", subnetwork);
}

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

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SubnetworkId)) {
return false;
}
SubnetworkId other = (SubnetworkId) obj;
return baseEquals(other)
&& Objects.equals(region, other.region)
&& Objects.equals(subnetwork, other.subnetwork);
}

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

/**
* Returns a subnetwork identity given the region identity and the subnetwork name. The subnetwork
* 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>
*/
public static SubnetworkId of(RegionId regionId, String subnetwork) {
return new SubnetworkId(regionId.project(), regionId.region(), subnetwork);
}

/**
* Returns a subnetwork identity given the region and subnetwork names. The subnetwork 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>
*/
public static SubnetworkId of(String region, String subnetwork) {
return new SubnetworkId(null, region, subnetwork);
}

/**
* Returns a subnetwork identity given project, region and subnetwork names. The subnetwork 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>
*/
public static SubnetworkId of(String project, String region, String subnetwork) {
return new SubnetworkId(project, region, subnetwork);
}

/**
* Returns {@code true} if the provided string matches the expected format of a subnetwork URL.
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return PATTERN.matcher(url).matches();
}

static SubnetworkId fromUrl(String url) {
Matcher matcher = PATTERN.matcher(url);
if (!matcher.matches()) {
throw new IllegalArgumentException(url + " is not a valid subnetwork URL");
}
return SubnetworkId.of(matcher.group(1), matcher.group(2), matcher.group(3));
}
}
Loading

0 comments on commit 282f1fc

Please sign in to comment.