mirror of https://github.com/apache/jclouds.git
Adding ServerStatus domain objects for making status calls to api
This commit is contained in:
parent
ddd532dcfd
commit
1268c8f344
|
@ -18,20 +18,20 @@
|
|||
*/
|
||||
package org.jclouds.glesys.config;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.json.config.GsonModule.DateAdapter;
|
||||
import org.jclouds.json.config.GsonModule.Iso8601DateAdapter;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import org.jclouds.glesys.domain.ServerState;
|
||||
import org.jclouds.glesys.domain.ServerUptime;
|
||||
import org.jclouds.glesys.functions.internal.CustomDeserializers;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
import org.jclouds.json.config.GsonModule.DateAdapter;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class GleSYSParserModule extends AbstractModule {
|
||||
|
@ -39,12 +39,15 @@ public class GleSYSParserModule extends AbstractModule {
|
|||
@Provides
|
||||
@Singleton
|
||||
public Map<Type, Object> provideCustomAdapterBindings() {
|
||||
return ImmutableMap.<Type, Object> of();
|
||||
return ImmutableMap.<Type, Object>of(
|
||||
ServerState.class, new CustomDeserializers.ServerStateAdapter(),
|
||||
ServerUptime.class, new CustomDeserializers.ServerUptimeAdaptor()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
|
||||
bind(DateAdapter.class).to(GsonModule.Iso8601DateAdapter.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Detailed information on Server bandwidth
|
||||
*
|
||||
* @author Adam Lowe
|
||||
* @see ServerStatus
|
||||
*/
|
||||
public class Bandwidth {
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private long today;
|
||||
private long last30Days;
|
||||
private long max;
|
||||
|
||||
public Builder today(long today) {
|
||||
this.today = today;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder last30Days(long last30Days) {
|
||||
this.last30Days = last30Days;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder max(long max) {
|
||||
this.max = max;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Bandwidth build() {
|
||||
return new Bandwidth(today, last30Days, max);
|
||||
}
|
||||
|
||||
public Builder fromBandwidth(Bandwidth in) {
|
||||
return today(in.getToday()).last30Days(in.getLast30Days()).max(in.getMax());
|
||||
}
|
||||
}
|
||||
|
||||
private final long today;
|
||||
private final long last30Days;
|
||||
private final long max;
|
||||
|
||||
public Bandwidth(long today, long last30Days, long max) {
|
||||
this.today = today;
|
||||
this.last30Days = last30Days;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the bandwidth used today in MB
|
||||
*/
|
||||
public long getToday() {
|
||||
return today;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the bandwidth used in the past 30 days in GB
|
||||
*/
|
||||
public long getLast30Days() {
|
||||
return last30Days;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the max bandwidth allowed over a 30 day period in GB
|
||||
*/
|
||||
public long getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Bandwidth[today=%d, last30Days=%d, max=%d]", today, last30Days, max);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Detailed information on Server cpu usage
|
||||
*
|
||||
* @author Adam Lowe
|
||||
* @see ServerStatus
|
||||
*/
|
||||
|
||||
public class Cpu {
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private double system;
|
||||
private double user;
|
||||
private double nice;
|
||||
private double idle;
|
||||
private String unit;
|
||||
|
||||
public Builder system(double system) {
|
||||
this.system = system;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder user(double user) {
|
||||
this.user = user;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder nice(double nice) {
|
||||
this.nice = nice;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder idle(double idle) {
|
||||
this.idle = idle;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder unit(String unit) {
|
||||
this.unit = unit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Cpu build() {
|
||||
return new Cpu(system, user, nice, idle, unit);
|
||||
}
|
||||
|
||||
public Builder fromCpu(Cpu in) {
|
||||
return system(in.getSystem()).user(in.getUser()).nice(in.getNice()).idle(in.getIdle()).unit(in.getUnit());
|
||||
}
|
||||
}
|
||||
|
||||
private final double system;
|
||||
private final double user;
|
||||
private final double nice;
|
||||
private final double idle;
|
||||
private final String unit;
|
||||
|
||||
public Cpu(double system, double user, double nice, double idle, String unit) {
|
||||
this.system = system;
|
||||
this.user = user;
|
||||
this.nice = nice;
|
||||
this.idle = idle;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the system time in use in #unit
|
||||
*/
|
||||
public double getSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the user time in use in #unit
|
||||
*/
|
||||
public Double getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the nice setting
|
||||
*/
|
||||
public Double getNice() {
|
||||
return nice;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the idle time in #unit
|
||||
*/
|
||||
public Double getIdle() {
|
||||
return idle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the unit used
|
||||
*/
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Cpu[system=%f, user=%f, nice=%f, idle=%f, unit=%s]",
|
||||
system, user, nice, idle, unit);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Detailed information on Server disk usage
|
||||
*
|
||||
* @author Adam Lowe
|
||||
* @see ServerStatus
|
||||
*/
|
||||
public class Disk {
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private long used;
|
||||
private long size;
|
||||
private String unit;
|
||||
|
||||
public Builder used(long used) {
|
||||
this.used = used;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder size(long size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder unit(String unit) {
|
||||
this.unit = unit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Disk build() {
|
||||
return new Disk(used, size, unit);
|
||||
}
|
||||
|
||||
public Builder fromDisk(Disk in) {
|
||||
return used(in.getUsed()).size(in.getSize()).unit(in.getUnit());
|
||||
}
|
||||
}
|
||||
|
||||
private final long used;
|
||||
private final long size;
|
||||
private final String unit;
|
||||
|
||||
public Disk(long used, long size, String unit) {
|
||||
this.used = used;
|
||||
this.size = size;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the disk used in #unit
|
||||
*/
|
||||
public long getUsed() {
|
||||
return used;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the disk size allocated in #unit
|
||||
*/
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the unit used
|
||||
*/
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Disk[used=%d, size=%d, unit=%s]", used, size, unit);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Detailed information on Server memory usage
|
||||
*
|
||||
* @author Adam Lowe
|
||||
* @see ServerStatus
|
||||
*/
|
||||
public class Memory {
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private long usage;
|
||||
private long size;
|
||||
private String unit;
|
||||
|
||||
public Builder usage(long usage) {
|
||||
this.usage = usage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder size(long size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder unit(String unit) {
|
||||
this.unit = unit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Memory build() {
|
||||
return new Memory(usage, size, unit);
|
||||
}
|
||||
|
||||
public Builder fromMemory(Memory in) {
|
||||
return usage(in.getUsage()).size(in.getSize()).unit(in.getUnit());
|
||||
}
|
||||
}
|
||||
|
||||
@SerializedName("memusage")
|
||||
private final long usage;
|
||||
@SerializedName("memsize")
|
||||
private final long size;
|
||||
private final String unit;
|
||||
|
||||
public Memory(long usage, long size, String unit) {
|
||||
this.usage = usage;
|
||||
this.size = size;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the memory usage in #unit
|
||||
*/
|
||||
public long getUsage() {
|
||||
return usage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the memory size allocated in #unit
|
||||
*/
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the unit used
|
||||
*/
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Memory[usage=%d, size=%d, unit=%s]", usage, size, unit);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* 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.common.base.CaseFormat;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* @author Adam Lowe
|
||||
* @see ServerStatus
|
||||
*/
|
||||
public enum ServerState {
|
||||
|
||||
RUNNING, UNRECOGNIZED;
|
||||
|
||||
public String value() {
|
||||
return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value();
|
||||
}
|
||||
|
||||
public static ServerState fromValue(String state) {
|
||||
try {
|
||||
return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(state, "state")));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return UNRECOGNIZED;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Detailed information server status including hardware usage (cpu, memory and disk), bandwidth and up-time.
|
||||
*
|
||||
* @author Adam Lowe
|
||||
* @see <a href= "https://customer.glesys.com/api.php?a=doc#server_status" />
|
||||
*/
|
||||
|
||||
public class ServerStatus {
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private ServerState state;
|
||||
private Cpu cpu;
|
||||
private Memory memory;
|
||||
private Disk disk;
|
||||
private Bandwidth bandwidth;
|
||||
private long uptime;
|
||||
|
||||
public Builder state(ServerState state) {
|
||||
this.state = state;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder cpu(Cpu cpu) {
|
||||
this.cpu = cpu;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder memory(Memory memory) {
|
||||
this.memory = memory;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder disk(Disk disk) {
|
||||
this.disk = disk;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder bandwidth(Bandwidth bandwidth) {
|
||||
this.bandwidth = bandwidth;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder uptime(long uptime) {
|
||||
this.uptime = uptime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerStatus build() {
|
||||
return new ServerStatus(state, cpu, memory, disk, bandwidth, uptime);
|
||||
}
|
||||
|
||||
public Builder fromServerStatus(ServerStatus in) {
|
||||
return state(in.getState()).cpu(in.getCpu()).memory(in.getMemory()).disk(in.getDisk()).bandwidth(in.getBandwidth()).uptime(in.getUptime());
|
||||
}
|
||||
}
|
||||
|
||||
private final ServerState state;
|
||||
private final Cpu cpu;
|
||||
private final Memory memory;
|
||||
private final Disk disk;
|
||||
private final Bandwidth bandwidth;
|
||||
private final ServerUptime uptime;
|
||||
|
||||
public ServerStatus(ServerState state, Cpu cpu, Memory memory, Disk disk, Bandwidth bandwidth, long uptime) {
|
||||
this.state = state;
|
||||
this.cpu = cpu;
|
||||
this.memory = memory;
|
||||
this.disk = disk;
|
||||
this.bandwidth = bandwidth;
|
||||
this.uptime = ServerUptime.fromValue(uptime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the state of the server (e.g. "running")
|
||||
*/
|
||||
public ServerState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CPU usage information
|
||||
*/
|
||||
public Cpu getCpu() {
|
||||
return cpu;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return details of memory usage and limits
|
||||
*/
|
||||
public Memory getMemory() {
|
||||
return memory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return details of disk usage and limits
|
||||
*/
|
||||
public Disk getDisk() {
|
||||
return disk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return details of bandwidth usage and limits
|
||||
*/
|
||||
public Bandwidth getBandwidth() {
|
||||
return bandwidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the uptime of the server
|
||||
*/
|
||||
public long getUptime() {
|
||||
return uptime.getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Status[state=%s, cpu=%s, memory=%s, disk=%s, bandwidth=%s, uptime=%s]",
|
||||
state, cpu, memory, disk, bandwidth, uptime);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* 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.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Adam Lowe
|
||||
*/
|
||||
public class ServerUptime {
|
||||
private final long time;
|
||||
private final String timeString;
|
||||
|
||||
public ServerUptime(long time) {
|
||||
this.time = time;
|
||||
long days = TimeUnit.SECONDS.toDays(time);
|
||||
long hours = TimeUnit.SECONDS.toHours(time - TimeUnit.DAYS.toSeconds(days));
|
||||
Long[] bits = new Long[]{
|
||||
0L,
|
||||
(days / 365),
|
||||
((days % 365) / 30),
|
||||
((days % 365) % 30),
|
||||
hours,
|
||||
TimeUnit.SECONDS.toMinutes(time - TimeUnit.HOURS.toSeconds(hours) - TimeUnit.DAYS.toSeconds(days)),
|
||||
time % 60
|
||||
};
|
||||
this.timeString = Joiner.on(' ').join(bits);
|
||||
}
|
||||
|
||||
private ServerUptime(String timeString) {
|
||||
Splitter splitter = Splitter.on(' ').omitEmptyStrings().trimResults();
|
||||
List<String> data = new ArrayList<String>();
|
||||
Iterables.addAll(data, splitter.split(timeString));
|
||||
long result = Integer.parseInt(data.get(6));
|
||||
result += TimeUnit.SECONDS.convert(Integer.parseInt(data.get(5)), TimeUnit.MINUTES);
|
||||
result += TimeUnit.SECONDS.convert(Integer.parseInt(data.get(4)), TimeUnit.HOURS);
|
||||
result += TimeUnit.SECONDS.convert(Integer.parseInt(data.get(3)), TimeUnit.DAYS);
|
||||
result += TimeUnit.SECONDS.convert(Integer.parseInt(data.get(2)) * 30, TimeUnit.DAYS);
|
||||
result += TimeUnit.SECONDS.convert(Integer.parseInt(data.get(1)) * 365, TimeUnit.DAYS);
|
||||
this.time = result;
|
||||
this.timeString = timeString;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uptimeString a Glesys uptime string
|
||||
*/
|
||||
public static ServerUptime fromValue(String uptimeString) {
|
||||
return new ServerUptime(uptimeString);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param time number of seconds
|
||||
*/
|
||||
public static ServerUptime fromValue(long time) {
|
||||
return new ServerUptime(time);
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return timeString;
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@ import javax.ws.rs.core.MediaType;
|
|||
|
||||
import org.jclouds.glesys.domain.Server;
|
||||
import org.jclouds.glesys.domain.ServerDetails;
|
||||
import org.jclouds.glesys.domain.ServerStatus;
|
||||
import org.jclouds.http.filters.BasicAuthentication;
|
||||
import org.jclouds.rest.annotations.ExceptionParser;
|
||||
import org.jclouds.rest.annotations.RequestFilters;
|
||||
|
@ -68,4 +69,15 @@ public interface ServerAsyncClient {
|
|||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
ListenableFuture<ServerDetails> getServerDetails(@PathParam("id") String id);
|
||||
|
||||
|
||||
/**
|
||||
* @see ServerClient#getServerDetails
|
||||
*/
|
||||
@GET
|
||||
@Path("/server/status/serverid/{id}/format/json")
|
||||
@SelectJson("server")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
ListenableFuture<ServerStatus> getServerStatus(@PathParam("id") String id);
|
||||
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
|
|||
import org.jclouds.concurrent.Timeout;
|
||||
import org.jclouds.glesys.domain.Server;
|
||||
import org.jclouds.glesys.domain.ServerDetails;
|
||||
import org.jclouds.glesys.domain.ServerStatus;
|
||||
|
||||
/**
|
||||
* Provides synchronous access to Server.
|
||||
|
@ -54,4 +55,14 @@ public interface ServerClient {
|
|||
*/
|
||||
ServerDetails getServerDetails(String id);
|
||||
|
||||
/**
|
||||
* Get detailed information about a server status including up-time and hardware usage
|
||||
* (cpu, disk, memory and bandwidth)
|
||||
*
|
||||
* @param id
|
||||
* id of the server
|
||||
* @return server or null if not found
|
||||
*/
|
||||
ServerStatus getServerStatus(String id);
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package org.jclouds.glesys.functions.internal;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.jclouds.glesys.domain.ServerState;
|
||||
import org.jclouds.glesys.domain.ServerStatus;
|
||||
import org.jclouds.glesys.domain.ServerUptime;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @Author Adam Lowe
|
||||
*/
|
||||
public class CustomDeserializers {
|
||||
|
||||
public static class ServerStateAdapter implements JsonDeserializer<ServerState> {
|
||||
@Override
|
||||
public ServerState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
String toParse = jsonElement.getAsJsonPrimitive().getAsString();
|
||||
return ServerState.fromValue(toParse);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ServerUptimeAdaptor implements JsonDeserializer<ServerUptime> {
|
||||
@Override
|
||||
public ServerUptime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
String toParse = jsonElement.getAsJsonPrimitive().getAsString();
|
||||
return ServerUptime.fromValue(toParse);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -18,16 +18,18 @@
|
|||
*/
|
||||
package org.jclouds.glesys.features;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
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 static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests behavior of {@code ServerClient}
|
||||
*
|
||||
|
@ -56,6 +58,8 @@ public class ServerClientLiveTest extends BaseGleSYSClientLiveTest {
|
|||
assertEquals(newDetails.getPlatform(), server.getPlatform());
|
||||
assertEquals(newDetails.getDatacenter(), server.getDatacenter());
|
||||
checkServer(newDetails);
|
||||
ServerStatus newStatus = client.getServerStatus(server.getId());
|
||||
checkStatus(newStatus);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,4 +70,31 @@ public class ServerClientLiveTest extends BaseGleSYSClientLiveTest {
|
|||
assert server.getMemory() > 0 : server;
|
||||
assert server.getCost() != null;
|
||||
}
|
||||
|
||||
private void checkStatus(ServerStatus status) {
|
||||
assertNotNull(status.getState());
|
||||
assertNotNull(status.getUptime());
|
||||
|
||||
assertNotNull(status.getBandwidth());
|
||||
assert status.getBandwidth().getToday() >= 0 : status;
|
||||
assert status.getBandwidth().getLast30Days() >= 0 : status;
|
||||
assert status.getBandwidth().getMax() >= 0 : status;
|
||||
|
||||
assertNotNull(status.getCpu());
|
||||
assert status.getCpu().getSystem() >= 0.0 : status;
|
||||
assert status.getCpu().getUser() >= 0.0 : status;
|
||||
assert status.getCpu().getNice() >= 0.0 : status;
|
||||
assert status.getCpu().getIdle() >= 0.0 : status;
|
||||
assertNotNull(status.getCpu().getUnit());
|
||||
|
||||
assertNotNull(status.getDisk());
|
||||
assert status.getDisk().getSize() >= 0 : status;
|
||||
assert status.getDisk().getUsed() >= 0 : status;
|
||||
assertNotNull(status.getDisk().getUnit());
|
||||
|
||||
assertNotNull(status.getMemory());
|
||||
assert status.getMemory().getSize() > 0 : status;
|
||||
assert status.getMemory().getUsage() >= 0 : status;
|
||||
assertNotNull(status.getMemory().getUnit());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
/**
|
||||
* 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.parse;
|
||||
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import org.jclouds.glesys.config.GleSYSParserModule;
|
||||
import org.jclouds.glesys.domain.Bandwidth;
|
||||
import org.jclouds.glesys.domain.Cost;
|
||||
import org.jclouds.glesys.domain.Cpu;
|
||||
import org.jclouds.glesys.domain.Disk;
|
||||
import org.jclouds.glesys.domain.Memory;
|
||||
import org.jclouds.glesys.domain.ServerDetails;
|
||||
import org.jclouds.glesys.domain.ServerState;
|
||||
import org.jclouds.glesys.domain.ServerStatus;
|
||||
import org.jclouds.json.BaseItemParserTest;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
import org.jclouds.rest.annotations.SelectJson;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
/**
|
||||
* @author Adam Lowe
|
||||
*/
|
||||
@Test(groups = "unit", testName = "ParseServerStatusTest")
|
||||
public class ParseServerStatusTest extends BaseItemParserTest<ServerStatus> {
|
||||
|
||||
@Override
|
||||
public String resource() {
|
||||
return "/server_status.json";
|
||||
}
|
||||
|
||||
@Override
|
||||
@SelectJson("server")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public ServerStatus expected() {
|
||||
Bandwidth bandwidth = Bandwidth.builder().today(0).last30Days(0).max(50).build();
|
||||
Cpu cpu = Cpu.builder().unit("%").idle(100.0).system(0.0).user(0.0).nice(0.0).build();
|
||||
Disk disk = Disk.builder().unit("MB").size(0).used(0).build();
|
||||
Memory memory = Memory.builder().unit("MB").usage(3).size(128).build();
|
||||
return ServerStatus.builder().state(ServerState.RUNNING).uptime(38 * 60 + 6).bandwidth(bandwidth).
|
||||
cpu(cpu).disk(disk).memory(memory).build();
|
||||
}
|
||||
|
||||
protected Injector injector() {
|
||||
return Guice.createInjector(new GleSYSParserModule(), new GsonModule());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"response":{"status":{"code":"200","text":"OK"},"server":{"state":"running","cpu":{"system":"0.00","user":"0.00","nice":"0.00","idle":"100.00","unit":"%"},"memory":{"memusage":"3","memsize":"128","unit":"MB"},"disk":{"diskused":372,"disksize":5120,"unit":"MB"},"bandwidth":{"today":0,"last30days":0,"max":"50"},"uptime":"0 0 0 0 0 38 6"},"debug":{"input":{"serverid":"vz1188183"}}}}
|
Loading…
Reference in New Issue