-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 DiskInfo, disk configurations and test classes #844
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d238c52
Add DiskInfo, disk configurations and test classes
mziccard 094c3b3
Fix DiskInfo and configurations javadoc and test issues
mziccard 1fb53fa
Remove redundant parameter from configuration Builder constructor
mziccard 520d7b2
Minor fixes to DiskInfo and configuration
mziccard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
* 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 com.google.api.services.compute.model.Disk; | ||
import com.google.common.base.MoreObjects; | ||
import com.google.common.base.MoreObjects.ToStringHelper; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Base class for Google Compute Engine disk configurations. A disk can be used as primary storage | ||
* for your virtual machine instances. Use {@link StandardDiskConfiguration} to create a standard | ||
* disk given a disk type and size. Use {@link ImageDiskConfiguration} to create a disk from a | ||
* Compute Engine disk image. Use {@link SnapshotDiskConfiguration} to create a disk from a Compute | ||
* Engine disk snapshot. | ||
* | ||
* @see <a href="https://cloud.google.com/compute/docs/disks/">Block Storage</a> | ||
*/ | ||
public abstract class DiskConfiguration implements Serializable { | ||
|
||
private static final long serialVersionUID = -1783061701255428417L; | ||
|
||
private final Type type; | ||
private final Long sizeGb; | ||
private final DiskTypeId diskType; | ||
|
||
/** | ||
* Type of a Google Compute Engine disk configuration. | ||
*/ | ||
public enum Type { | ||
/** | ||
* A Google Compute Engine standard disk configuration. | ||
*/ | ||
STANDARD, | ||
|
||
/** | ||
* A Google Compute Engine disk configuration that creates a disk from an image. | ||
*/ | ||
IMAGE, | ||
|
||
/** | ||
* A Google Compute Engine disk configuration that creates a disk from a snapshot. | ||
*/ | ||
SNAPSHOT | ||
} | ||
|
||
/** | ||
* Base builder for disk configurations. | ||
* | ||
* @param <T> the disk configuration type | ||
* @param <B> the disk configuration builder | ||
*/ | ||
public abstract static class Builder<T extends DiskConfiguration, B extends Builder<T, B>> { | ||
|
||
private Type type; | ||
private Long sizeGb; | ||
private DiskTypeId diskType; | ||
|
||
Builder(Type type) { | ||
this.type = type; | ||
} | ||
|
||
Builder(DiskConfiguration diskConfiguration) { | ||
this.type = diskConfiguration.type; | ||
this.sizeGb = diskConfiguration.sizeGb; | ||
this.diskType = diskConfiguration.diskType; | ||
} | ||
|
||
Builder(Type type, Disk diskPb) { | ||
this.type = type; | ||
this.sizeGb = diskPb.getSizeGb(); | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
if (diskPb.getType() != null) { | ||
this.diskType = DiskTypeId.fromUrl(diskPb.getType()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected B self() { | ||
return (B) this; | ||
} | ||
|
||
B type(Type type) { | ||
this.type = type; | ||
return self(); | ||
} | ||
|
||
/** | ||
* Sets the size of the persistent disk, in GB. | ||
*/ | ||
public B sizeGb(Long sizeGb) { | ||
this.sizeGb = sizeGb; | ||
return self(); | ||
} | ||
|
||
/** | ||
* Sets the identity of the disk type. If not set {@code pd-standard} will be used. | ||
*/ | ||
public B diskType(DiskTypeId diskType) { | ||
this.diskType = diskType; | ||
return self(); | ||
} | ||
|
||
/** | ||
* Creates an object. | ||
*/ | ||
public abstract T build(); | ||
} | ||
|
||
DiskConfiguration(Builder builder) { | ||
this.type = builder.type; | ||
this.sizeGb = builder.sizeGb; | ||
this.diskType = builder.diskType; | ||
} | ||
|
||
/** | ||
* Returns the disk configuration's type. This method returns {@link Type#STANDARD} for a standard | ||
* configuration that creates a disk given its type and size. This method returns | ||
* {@link Type#SNAPSHOT} for a configuration that creates a disk from a Google Compute Engine | ||
* snapshot. This method returns {@link Type#IMAGE} for a configuration that creates a disk | ||
* from a Google Compute Engine image. | ||
*/ | ||
public Type type() { | ||
return type; | ||
} | ||
|
||
/** | ||
* Returns the size of the persistent disk, in GB. | ||
*/ | ||
public Long sizeGb() { | ||
return sizeGb; | ||
} | ||
|
||
/** | ||
* Returns the identity of the disk type. | ||
*/ | ||
public DiskTypeId diskType() { | ||
return diskType; | ||
} | ||
|
||
/** | ||
* Returns a builder for the object. | ||
*/ | ||
public abstract Builder toBuilder(); | ||
|
||
ToStringHelper toStringHelper() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("type", type) | ||
.add("sizeGb", sizeGb) | ||
.add("diskType", diskType); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return toStringHelper().toString(); | ||
} | ||
|
||
final int baseHashCode() { | ||
return Objects.hash(type, sizeGb, diskType); | ||
} | ||
|
||
final boolean baseEquals(DiskConfiguration diskConfiguration) { | ||
return diskConfiguration != null | ||
&& getClass().equals(diskConfiguration.getClass()) | ||
&& Objects.equals(toPb(), diskConfiguration.toPb()); | ||
} | ||
|
||
abstract DiskConfiguration setProjectId(String projectId); | ||
|
||
Disk toPb() { | ||
Disk diskPb = new Disk(); | ||
diskPb.setSizeGb(sizeGb); | ||
if (diskType != null) { | ||
diskPb.setType(diskType.selfLink()); | ||
} | ||
return diskPb; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static <T extends DiskConfiguration> T fromPb(Disk diskPb) { | ||
if (diskPb.getSourceImage() != null) { | ||
return (T) ImageDiskConfiguration.fromPb(diskPb); | ||
} else if (diskPb.getSourceSnapshot() != null) { | ||
return (T) SnapshotDiskConfiguration.fromPb(diskPb); | ||
} | ||
return (T) StandardDiskConfiguration.fromPb(diskPb); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.