From ddd532dcfd7ebdaac98ddc27cc61aa3f733c3d37 Mon Sep 17 00:00:00 2001 From: Adam Lowe Date: Fri, 2 Dec 2011 15:00:13 +0000 Subject: [PATCH] Adding Cost to ServerDetails --- .../java/org/jclouds/glesys/domain/Cost.java | 102 ++++++++++++++++++ .../jclouds/glesys/domain/ServerDetails.java | 32 ++++-- .../glesys/features/ServerClientLiveTest.java | 16 +-- .../ParseServerDetailsWithoutIPsTest.java | 4 +- 4 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/Cost.java diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/Cost.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/Cost.java new file mode 100644 index 0000000000..df7f7bb895 --- /dev/null +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/Cost.java @@ -0,0 +1,102 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you 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.glesys.domain; + +import com.google.gson.annotations.SerializedName; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * The Cost class contains information about the cost of a server + * + * @author Adam Lowe + * @see ServerDetails + */ +public class Cost { + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private double amount; + private String currency; + private String timePeriod; + + public Builder amount(double amount) { + this.amount = amount; + return this; + } + + public Builder currency(String currency) { + this.currency = currency; + return this; + } + + public Builder timePeriod(String timePeriod) { + this.timePeriod = timePeriod; + return this; + } + + public Cost build() { + return new Cost(amount, currency, timePeriod); + } + + public Builder fromCost(Cost cost) { + return amount(cost.getAmount()).currency(cost.getCurrency()).timePeriod(cost.getTimePeriod()); + } + } + + private final double amount; + private final String currency; + @SerializedName("timeperiod") + private final String timePeriod; + + public Cost(double amount, String currency, String timePeriod) { + this.amount = amount; + this.currency = checkNotNull(currency, "currency"); + this.timePeriod = timePeriod; + } + + /** + * @return the numeric cost in #currency / #timePeriod + */ + public double getAmount() { + return amount; + } + + /** + * @return the currency unit, e.g. "EUR" for Euro + */ + public String getCurrency() { + return currency; + } + + /** + * @return the time period for which this cost charged, e.g. "month" + */ + public String getTimePeriod() { + return timePeriod; + } + + @Override + public String toString() { + return String.format( + "[amount=%f, currency=%s, timePeriod=%s]", amount, currency, timePeriod); + } +} diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerDetails.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerDetails.java index f9138efdf2..022f44ebd5 100644 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerDetails.java +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerDetails.java @@ -23,7 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.gson.annotations.SerializedName; /** - * detailed information about a server such as cpuCores, hardware configuration + * Detailed information about a server such as cpuCores, hardware configuration * (cpu, memory and disk), ip adresses, cost, transfer, os and more. * * @author Adrian Cole @@ -39,6 +39,7 @@ public class ServerDetails extends Server { private int cpuCores; private int memory; private int disk; + private Cost cost; public Builder description(String description) { this.description = description; @@ -60,12 +61,17 @@ public class ServerDetails extends Server { return this; } + public Builder cost(Cost cost) { + this.cost = cost; + return this; + } + public ServerDetails build() { - return new ServerDetails(id, hostname, datacenter, platform, description, cpuCores, memory, disk); + return new ServerDetails(id, hostname, datacenter, platform, description, cpuCores, memory, disk, cost); } public Builder fromServerDetails(ServerDetails in) { - return fromServer(in).memory(in.getMemory()).disk(in.getDisk()).cpuCores(in.getCpuCores()) + return fromServer(in).memory(in.getMemory()).disk(in.getDisk()).cpuCores(in.getCpuCores()).cost(in.getCost()) .description(in.getDescription()); } @@ -93,22 +99,23 @@ public class ServerDetails extends Server { public Builder fromServer(Server in) { return Builder.class.cast(super.fromServer(in)); } - } - + private final String description; @SerializedName("cpucores") private final int cpuCores; private final int memory; private final int disk; - + private final Cost cost; + public ServerDetails(String id, String hostname, String datacenter, String platform, String description, - int cpuCores, int memory, int disk) { + int cpuCores, int memory, int disk, Cost cost) { super(id, hostname, datacenter, platform); this.description = checkNotNull(description, "description"); this.cpuCores = cpuCores; this.memory = memory; this.disk = disk; + this.cost = checkNotNull(cost, "cost"); } /** @@ -139,11 +146,18 @@ public class ServerDetails extends Server { return memory; } + /** + * @return details of the cost of the server + */ + public Cost getCost() { + return cost; + } + @Override public String toString() { return String.format( - "[id=%s, hostname=%s, datacenter=%s, platform=%s, description=%s, cpuCores=%s, memory=%s, disk=%s]", id, - hostname, datacenter, platform, description, cpuCores, memory, disk); + "[id=%s, hostname=%s, datacenter=%s, platform=%s, description=%s, cpuCores=%s, memory=%s, disk=%s, cost=%s]", id, + hostname, datacenter, platform, description, cpuCores, memory, disk, cost); } } diff --git a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerClientLiveTest.java b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerClientLiveTest.java index 6919381c96..0496ad8bca 100644 --- a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerClientLiveTest.java +++ b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerClientLiveTest.java @@ -18,15 +18,15 @@ */ package org.jclouds.glesys.features; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; +import org.jclouds.glesys.domain.Server; +import org.jclouds.glesys.domain.ServerDetails; +import org.jclouds.glesys.domain.ServerStatus; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; import java.util.Set; -import org.jclouds.glesys.domain.Server; -import org.jclouds.glesys.domain.ServerDetails; -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.Test; +import static org.testng.Assert.*; /** * Tests behavior of {@code ServerClient} @@ -58,12 +58,12 @@ public class ServerClientLiveTest extends BaseGleSYSClientLiveTest { checkServer(newDetails); } } - + private void checkServer(ServerDetails server) { // description can be null assert server.getCpuCores() > 0 : server; assert server.getDisk() > 0 : server; assert server.getMemory() > 0 : server; + assert server.getCost() != null; } - } diff --git a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/parse/ParseServerDetailsWithoutIPsTest.java b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/parse/ParseServerDetailsWithoutIPsTest.java index d5bcf9e76f..e3afd05aea 100644 --- a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/parse/ParseServerDetailsWithoutIPsTest.java +++ b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/parse/ParseServerDetailsWithoutIPsTest.java @@ -24,6 +24,7 @@ import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; import org.jclouds.glesys.config.GleSYSParserModule; +import org.jclouds.glesys.domain.Cost; import org.jclouds.glesys.domain.ServerDetails; import org.jclouds.json.BaseItemParserTest; import org.jclouds.json.config.GsonModule; @@ -49,8 +50,9 @@ public class ParseServerDetailsWithoutIPsTest extends BaseItemParserTest