Adding Cost to ServerDetails

This commit is contained in:
Adam Lowe 2011-12-02 15:00:13 +00:00
parent 7b72ef5cfc
commit ddd532dcfd
4 changed files with 136 additions and 18 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<ServerD
@SelectJson("server")
@Consumes(MediaType.APPLICATION_JSON)
public ServerDetails expected() {
Cost cost = Cost.builder().amount(6.38).currency("EUR").timePeriod("month").build();
return ServerDetails.builder().id("vz1541880").hostname("mammamia").datacenter("Falkenberg").platform("OpenVZ")
.description("description").cpuCores(1).memory(128).disk(5).build();
.description("description").cpuCores(1).memory(128).disk(5).cost(cost).build();
}
protected Injector injector() {