diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/binders/BindDriveToPlainTextString.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/binders/BindDriveToPlainTextString.java new file mode 100644 index 0000000000..9db28769f3 --- /dev/null +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/binders/BindDriveToPlainTextString.java @@ -0,0 +1,61 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.binders; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.ws.rs.core.MediaType; + +import org.jclouds.elasticstack.domain.Drive; +import org.jclouds.elasticstack.functions.ListOfMapsToListOfKeyValuesDelimitedByBlankLines; +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.Binder; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class BindDriveToPlainTextString implements Binder { + private final Function> createDriveRequestToMap; + private final ListOfMapsToListOfKeyValuesDelimitedByBlankLines listOfMapsToListOfKeyValuesDelimitedByBlankLines; + + @Inject + public BindDriveToPlainTextString(Function> createDriveRequestToMap, + ListOfMapsToListOfKeyValuesDelimitedByBlankLines listOfMapsToListOfKeyValuesDelimitedByBlankLines) { + this.createDriveRequestToMap = createDriveRequestToMap; + this.listOfMapsToListOfKeyValuesDelimitedByBlankLines = listOfMapsToListOfKeyValuesDelimitedByBlankLines; + } + + public void bindToRequest(HttpRequest request, Object payload) { + checkArgument(payload instanceof Drive, "this binder is only valid for Drive!"); + Drive create = Drive.class.cast(payload); + Map map = createDriveRequestToMap.apply(create); + request.setPayload(listOfMapsToListOfKeyValuesDelimitedByBlankLines.apply(ImmutableSet.of(map))); + request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_PLAIN); + } +} diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/Drive.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/Drive.java new file mode 100644 index 0000000000..da54f37be7 --- /dev/null +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/Drive.java @@ -0,0 +1,219 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; +import java.util.Set; + +import javax.annotation.Nullable; + + +import com.google.common.collect.ImmutableSet; + +/** + * + * @author Adrian Cole + */ +public class Drive extends Item { + public static class Builder extends Item.Builder { + protected long size; + protected ClaimType claimType = ClaimType.EXCLUSIVE; + protected Set readers = ImmutableSet.of(); + + public Builder claimType(ClaimType claimType) { + this.claimType = claimType; + return this; + } + + public Builder readers(Iterable readers) { + this.readers = ImmutableSet.copyOf(checkNotNull(readers, "readers")); + return this; + } + + public Builder size(long size) { + this.size = size; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public Builder uuid(String uuid) { + return Builder.class.cast(super.uuid(uuid)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder name(String name) { + return Builder.class.cast(super.name(name)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder tags(Iterable tags) { + return Builder.class.cast(super.tags(tags)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder userMetadata(Map userMetadata) { + return Builder.class.cast(super.userMetadata(userMetadata)); + } + + public Drive build() { + return new Drive(uuid, name, size, claimType, readers, tags, userMetadata); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((claimType == null) ? 0 : claimType.hashCode()); + result = prime * result + ((readers == null) ? 0 : readers.hashCode()); + result = prime * result + (int) (size ^ (size >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + Builder other = (Builder) obj; + if (claimType != other.claimType) + return false; + if (readers == null) { + if (other.readers != null) + return false; + } else if (!readers.equals(other.readers)) + return false; + if (size != other.size) + return false; + return true; + } + } + + protected final long size; + protected final ClaimType claimType; + protected final Set readers; + + public Drive(@Nullable String uuid, String name, long size, @Nullable ClaimType claimType, + Iterable readers, Iterable tags, Map userMetadata) { + super(uuid, name, tags, userMetadata); + this.size = size; + this.claimType = checkNotNull(claimType, "set claimType to exclusive, not null"); + this.readers = ImmutableSet.copyOf(checkNotNull(readers, "readers")); + } + + /** + * + * @return either 'exclusive' (the default) or 'shared' to allow multiple servers to access a + * drive simultaneously + */ + @Nullable + public ClaimType getClaimType() { + return claimType; + } + + /** + * + * @return list of users allowed to read from a drive or 'ffffffff-ffff-ffff-ffff-ffffffffffff' + * for all users + */ + public Set getReaders() { + return readers; + } + + /** + * + * @return size of drive in bytes + */ + public long getSize() { + return size; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((claimType == null) ? 0 : claimType.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((readers == null) ? 0 : readers.hashCode()); + result = prime * result + (int) (size ^ (size >>> 32)); + result = prime * result + ((tags == null) ? 0 : tags.hashCode()); + result = prime * result + ((userMetadata == null) ? 0 : userMetadata.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Drive other = (Drive) obj; + if (claimType != other.claimType) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (readers == null) { + if (other.readers != null) + return false; + } else if (!readers.equals(other.readers)) + return false; + if (size != other.size) + return false; + if (tags == null) { + if (other.tags != null) + return false; + } else if (!tags.equals(other.tags)) + return false; + if (userMetadata == null) { + if (other.userMetadata != null) + return false; + } else if (!userMetadata.equals(other.userMetadata)) + return false; + return true; + } + + @Override + public String toString() { + return "[uuid=" + uuid + ", name=" + name + ", tags=" + tags + ", userMetadata=" + userMetadata + ", size=" + + size + ", claimType=" + claimType + ", readers=" + readers + "]"; + } + +} \ No newline at end of file diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/DriveMetrics.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/DriveMetrics.java new file mode 100644 index 0000000000..913a547ea6 --- /dev/null +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/DriveMetrics.java @@ -0,0 +1,139 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.domain; + + +/** + * + * @author Adrian Cole + */ +public class DriveMetrics { + public static class Builder { + protected long readBytes; + protected long readRequests; + protected long writeBytes; + protected long writeRequests; + + public Builder readBytes(long readBytes) { + this.readBytes = readBytes; + return this; + } + + public Builder readRequests(long readRequests) { + this.readRequests = readRequests; + return this; + } + + public Builder writeBytes(long writeBytes) { + this.writeBytes = writeBytes; + return this; + } + + public Builder writeRequests(long writeRequests) { + this.writeRequests = writeRequests; + return this; + } + + public DriveMetrics build() { + return new DriveMetrics(readBytes, readRequests, writeBytes, writeRequests); + } + } + + protected final long readBytes; + protected final long readRequests; + protected final long writeBytes; + protected final long writeRequests; + + public DriveMetrics(long readBytes, long readRequests, long writeBytes, long writeRequests) { + this.readBytes = readBytes; + this.readRequests = readRequests; + this.writeBytes = writeBytes; + this.writeRequests = writeRequests; + } + + /** + * + * @return Cumulative i/o byte/request count for each drive + */ + public long getReadBytes() { + return readBytes; + } + + /** + * + * @return Cumulative i/o byte/request count for each drive + */ + public long getReadRequests() { + return readRequests; + } + + /** + * + * @return Cumulative i/o byte/request count for each drive + */ + public long getWriteBytes() { + return writeBytes; + } + + /** + * + * @return Cumulative i/o byte/request count for each drive + */ + public long getWriteRequests() { + return writeRequests; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (int) (readBytes ^ (readBytes >>> 32)); + result = prime * result + (int) (readRequests ^ (readRequests >>> 32)); + result = prime * result + (int) (writeBytes ^ (writeBytes >>> 32)); + result = prime * result + (int) (writeRequests ^ (writeRequests >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DriveMetrics other = (DriveMetrics) obj; + if (readBytes != other.readBytes) + return false; + if (readRequests != other.readRequests) + return false; + if (writeBytes != other.writeBytes) + return false; + if (writeRequests != other.writeRequests) + return false; + return true; + } + + @Override + public String toString() { + return "[readBytes=" + readBytes + ", readRequests=" + readRequests + ", writeBytes=" + writeBytes + + ", writeRequests=" + writeRequests + "]"; + } +} \ No newline at end of file diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/Server.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/Server.java new file mode 100644 index 0000000000..dc50aa7b91 --- /dev/null +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/Server.java @@ -0,0 +1,297 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.Nullable; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; + +/** + * + * @author Adrian Cole + */ +public class Server extends Item { + + public static class Builder extends Item.Builder { + protected int cpu; + protected Integer smp; + protected int mem; + protected boolean persistent; + protected Map devices = ImmutableMap.of(); + protected Set bootDeviceIds = ImmutableSet.of(); + protected List nics = ImmutableList.of(); + protected VNC vnc; + // TODO undocumented + protected String description; + + public Builder cpu(int cpu) { + this.cpu = cpu; + return this; + } + + public Builder smp(Integer smp) { + this.smp = smp; + return this; + } + + public Builder mem(int mem) { + this.mem = mem; + return this; + } + + public Builder persistent(boolean persistent) { + this.persistent = persistent; + return this; + } + + public Builder devices(Map devices) { + this.devices = ImmutableMap.copyOf(checkNotNull(devices, "devices")); + return this; + } + + public Builder bootDeviceIds(Iterable bootDeviceIds) { + this.bootDeviceIds = ImmutableSet.copyOf(checkNotNull(bootDeviceIds, "bootDeviceIds")); + return this; + } + + public Builder nics(Iterable nics) { + this.nics = ImmutableList.copyOf(checkNotNull(nics, "nics")); + return this; + } + + public Builder vnc(VNC vnc) { + this.vnc = vnc; + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public Builder uuid(String uuid) { + return Builder.class.cast(super.uuid(uuid)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder name(String name) { + return Builder.class.cast(super.name(name)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder tags(Iterable tags) { + return Builder.class.cast(super.tags(tags)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder userMetadata(Map userMetadata) { + return Builder.class.cast(super.userMetadata(userMetadata)); + } + + public Server build() { + return new Server(uuid, name, cpu, smp, mem, persistent, devices, tags, bootDeviceIds, userMetadata, nics, + vnc, description); + } + } + + protected final int cpu; + protected final Integer smp; + protected final int mem; + protected final boolean persistent; + @Nullable + protected final Map devices; + protected final Set bootDeviceIds; + protected final List nics; + protected final VNC vnc; + @Nullable + private final String description; + + public Server(@Nullable String uuid, String name, int cpu, @Nullable Integer smp, int mem, boolean persistent, + Map devices, Iterable bootDeviceIds, Iterable tags, + Map userMetadata, Iterable nics, VNC vnc, String description) { + super(uuid, name, tags, userMetadata); + this.cpu = cpu; + this.smp = smp; + this.mem = mem; + this.persistent = persistent; + this.devices = ImmutableMap.copyOf(checkNotNull(devices, "devices")); + this.bootDeviceIds = ImmutableSet.copyOf(checkNotNull(bootDeviceIds, "bootDeviceIds")); + this.nics = ImmutableList.copyOf(checkNotNull(nics, "nics")); + this.vnc = checkNotNull(vnc, "vnc"); + this.description = description; + } + + /** + * + * @return CPU quota in core MHz. + */ + public int getCpu() { + return cpu; + } + + /** + * + * @return number of virtual processors or null if calculated based on cpu. + */ + public Integer getSmp() { + return smp; + } + + /** + * + * @return virtual memory size in MB. + */ + public int getMem() { + return mem; + } + + /** + * + * @return 'true' means that server will revert to a 'stopped' status on server stop or shutdown, + * rather than being destroyed automatically. + */ + public boolean isPersistent() { + return persistent; + } + + /** + * + * @return devices present, mapped by id + */ + public Map getDevices() { + return devices; + } + + /** + * + * @return ids of the devices to boot, e.g. ide:0:0 or ide:1:0 + * @see Device#getId() + */ + public Set getBootDeviceIds() { + return bootDeviceIds; + } + + public List getNics() { + return nics; + } + + public VNC getVnc() { + return vnc; + } + + // TODO undocumented + public String getDescription() { + return description; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((bootDeviceIds == null) ? 0 : bootDeviceIds.hashCode()); + result = prime * result + cpu; + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((devices == null) ? 0 : devices.hashCode()); + result = prime * result + mem; + result = prime * result + ((nics == null) ? 0 : nics.hashCode()); + result = prime * result + (persistent ? 1231 : 1237); + result = prime * result + ((smp == null) ? 0 : smp.hashCode()); + result = prime * result + ((vnc == null) ? 0 : vnc.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + Server other = (Server) obj; + if (bootDeviceIds == null) { + if (other.bootDeviceIds != null) + return false; + } else if (!bootDeviceIds.equals(other.bootDeviceIds)) + return false; + if (cpu != other.cpu) + return false; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (devices == null) { + if (other.devices != null) + return false; + } else if (!devices.equals(other.devices)) + return false; + if (mem != other.mem) + return false; + if (nics == null) { + if (other.nics != null) + return false; + } else if (!nics.equals(other.nics)) + return false; + if (persistent != other.persistent) + return false; + if (smp == null) { + if (other.smp != null) + return false; + } else if (!smp.equals(other.smp)) + return false; + if (vnc == null) { + if (other.vnc != null) + return false; + } else if (!vnc.equals(other.vnc)) + return false; + return true; + } + + @Override + public String toString() { + return "[uuid=" + uuid + ", name=" + name + ", tags=" + tags + ", userMetadata=" + userMetadata + ", cpu=" + cpu + + ", smp=" + smp + ", mem=" + mem + ", persistent=" + persistent + ", devices=" + devices + + ", bootDeviceIds=" + bootDeviceIds + ", nics=" + nics + ", vnc=" + vnc + ", description=" + description + + "]"; + } + +} \ No newline at end of file diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/ServerMetrics.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/ServerMetrics.java new file mode 100644 index 0000000000..48a079bcb3 --- /dev/null +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/domain/ServerMetrics.java @@ -0,0 +1,156 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import com.google.common.collect.ImmutableMap; + +/** + * + * @author Adrian Cole + */ +public class ServerMetrics { + + public static class Builder { + protected long txPackets; + protected long tx; + protected long rxPackets; + protected long rx; + protected Map driveMetrics = ImmutableMap. of(); + + public Builder txPackets(long txPackets) { + this.txPackets = txPackets; + return this; + } + + public Builder tx(long tx) { + this.tx = tx; + return this; + } + + public Builder rxPackets(long rxPackets) { + this.rxPackets = rxPackets; + return this; + } + + public Builder rx(long rx) { + this.rx = rx; + return this; + } + + public Builder driveMetrics(Map driveMetrics) { + this.driveMetrics = ImmutableMap.copyOf(checkNotNull(driveMetrics, "driveMetrics")); + return this; + } + + public ServerMetrics build() { + return new ServerMetrics(tx, txPackets, rx, rxPackets, driveMetrics); + } + } + + protected final long txPackets; + protected final long tx; + protected final long rxPackets; + protected final long rx; + protected final Map driveMetrics; + + public ServerMetrics(long tx, long txPackets, long rx, long rxPackets, Map driveMetrics) { + this.txPackets = txPackets; + this.tx = tx; + this.rxPackets = rxPackets; + this.rx = rx; + this.driveMetrics = ImmutableMap.copyOf(checkNotNull(driveMetrics, "driveMetrics")); + } + + // TODO undocumented + public long getTxPackets() { + return txPackets; + } + + // TODO undocumented + public long getTx() { + return tx; + } + + // TODO undocumented + public long getRxPackets() { + return rxPackets; + } + + // TODO undocumented + public long getRx() { + return rx; + } + + /** + * + * @return metrics keyed on device id ex. {@code ide:0:0} + */ + public Map getDriveMetrics() { + return driveMetrics; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((driveMetrics == null) ? 0 : driveMetrics.hashCode()); + result = prime * result + (int) (rx ^ (rx >>> 32)); + result = prime * result + (int) (rxPackets ^ (rxPackets >>> 32)); + result = prime * result + (int) (tx ^ (tx >>> 32)); + result = prime * result + (int) (txPackets ^ (txPackets >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ServerMetrics other = (ServerMetrics) obj; + if (driveMetrics == null) { + if (other.driveMetrics != null) + return false; + } else if (!driveMetrics.equals(other.driveMetrics)) + return false; + if (rx != other.rx) + return false; + if (rxPackets != other.rxPackets) + return false; + if (tx != other.tx) + return false; + if (txPackets != other.txPackets) + return false; + return true; + } + + @Override + public String toString() { + return "[ txPackets=" + txPackets + ", tx=" + tx + ", rxPackets=" + rxPackets + ", rx=" + rx + ", driveMetrics=" + + driveMetrics + "]"; + } + +} \ No newline at end of file diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/functions/MapToDriveMetrics.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/functions/MapToDriveMetrics.java new file mode 100644 index 0000000000..e7a84950ff --- /dev/null +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/functions/MapToDriveMetrics.java @@ -0,0 +1,87 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.functions; + +import java.util.Map; + +import javax.inject.Singleton; + +import org.jclouds.elasticstack.domain.DriveMetrics; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class MapToDriveMetrics implements Function, Map> { + + public Map apply(Map from) { + Builder builder = ImmutableMap. builder(); + addIDEDevices(from, builder); + addSCSIDevices(from, builder); + addBlockDevices(from, builder); + return builder.build(); + } + + protected void addBlockDevices(Map from, Builder devices) { + BLOCK: for (int index : new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }) { + String key = String.format("block:0:%d", index); + if (!from.containsKey(key)) + break BLOCK; + devices.put(key, buildMetrics(key, from)); + } + } + + protected void addSCSIDevices(Map from, Builder devices) { + SCSI: for (int unit : new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }) { + String key = String.format("scsi:0:%d", unit); + if (!from.containsKey(key)) + break SCSI; + devices.put(key, buildMetrics(key, from)); + } + } + + protected void addIDEDevices(Map from, Builder devices) { + IDE: for (int bus : new int[] { 0, 1 }) + for (int unit : new int[] { 0, 1 }) { + String key = String.format("ide:%d:%d", bus, unit); + if (!from.containsKey(key)) + break IDE; + devices.put(key, buildMetrics(key, from)); + } + } + + protected DriveMetrics buildMetrics(String key, Map from) { + DriveMetrics.Builder builder = new DriveMetrics.Builder(); + if (from.containsKey(key + ":read:bytes")) + builder.readBytes(new Long(from.get(key + ":read:bytes"))); + if (from.containsKey(key + ":read:requests")) + builder.readRequests(new Long(from.get(key + ":read:requests"))); + if (from.containsKey(key + ":write:bytes")) + builder.writeBytes(new Long(from.get(key + ":write:bytes"))); + if (from.containsKey(key + ":write:requests")) + builder.writeRequests(new Long(from.get(key + ":write:requests"))); + return builder.build(); + } +} \ No newline at end of file diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/functions/MapToServerMetrics.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/functions/MapToServerMetrics.java new file mode 100644 index 0000000000..283d56e522 --- /dev/null +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/functions/MapToServerMetrics.java @@ -0,0 +1,60 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.functions; + +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.elasticstack.domain.DriveMetrics; +import org.jclouds.elasticstack.domain.ServerMetrics; + +import com.google.common.base.Function; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class MapToServerMetrics implements Function, ServerMetrics> { + private final Function, Map> mapToDriveMetrics; + + @Inject + public MapToServerMetrics(Function, Map> mapToDriveMetrics) { + this.mapToDriveMetrics = mapToDriveMetrics; + } + + public ServerMetrics apply(Map from) { + ServerMetrics.Builder metricsBuilder = new ServerMetrics.Builder(); + if (from.containsKey("tx:packets")) + metricsBuilder.txPackets(new Long(from.get("tx:packets"))); + if (from.containsKey("tx")) + metricsBuilder.tx(new Long(from.get("tx"))); + if (from.containsKey("rx:packets")) + metricsBuilder.rxPackets(new Long(from.get("rx:packets"))); + if (from.containsKey("rx")) + metricsBuilder.rx(new Long(from.get("rx"))); + metricsBuilder.driveMetrics(mapToDriveMetrics.apply(from)); + + ServerMetrics metrics = metricsBuilder.build(); + return metrics; + } +} \ No newline at end of file diff --git a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/binders/BindDriveToPlainTextStringTest.java b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/binders/BindDriveToPlainTextStringTest.java new file mode 100644 index 0000000000..6482fa87bf --- /dev/null +++ b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/binders/BindDriveToPlainTextStringTest.java @@ -0,0 +1,92 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 org.jclouds.elasticstack.binders; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.net.URI; +import java.util.Map; + +import javax.ws.rs.core.MediaType; + +import org.jclouds.elasticstack.domain.ClaimType; +import org.jclouds.elasticstack.domain.CreateDriveRequest; +import org.jclouds.elasticstack.domain.Drive; +import org.jclouds.elasticstack.domain.DriveData; +import org.jclouds.elasticstack.functions.CreateDriveRequestToMap; +import org.jclouds.elasticstack.functions.DriveDataToMap; +import org.jclouds.http.HttpRequest; +import org.jclouds.util.Utils; +import org.testng.annotations.Test; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.TypeLiteral; + +/** + * + * @author Adrian Cole + */ +@Test(groups = { "unit" }) +public class BindDriveToPlainTextStringTest { + + private static final BindDriveToPlainTextString FN = Guice.createInjector(new AbstractModule() { + + @Override + protected void configure() { + bind(new TypeLiteral>>() { + }).to(CreateDriveRequestToMap.class); + bind(new TypeLiteral>>() { + }).to(DriveDataToMap.class); + } + + }).getInstance(BindDriveToPlainTextString.class); + + public void testSimple() { + HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create")); + FN.bindToRequest(request, new CreateDriveRequest.Builder().name("foo").size(100l).build()); + assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN); + assertEquals(request.getPayload().getRawContent(), "name foo\nsize 100"); + } + + public void testComplete() throws IOException { + CreateDriveRequest input = new CreateDriveRequest.Builder() + .name("Ubuntu 10.10 Server Edition Linux 64bit Preinstalled System") + // + .size(8589934592l)// + .claimType(ClaimType.SHARED)// + .readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"))// + .tags(ImmutableSet.of("tag1", "tag2")).userMetadata(ImmutableMap.of("foo", "bar", "baz", "raz"))// + .encryptionCipher("aes-xts-plain").avoid(ImmutableSet.of("avoid1")).build(); + + HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create")); + FN.bindToRequest(request, input); + assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN); + assertEquals(request.getPayload().getRawContent(), + Utils.toStringAndClose(BindDriveToPlainTextStringTest.class + .getResourceAsStream("/create_drive.txt"))); + + } + +} \ No newline at end of file