This project requires JVM version of at least 1.7
Table of Contents top
- Table of Contents
- scaleway-java-api
- Overview
- Building the Package
- Running the Tests
- Logging - slf4j
- Artefact Publishing - Github
- Artefact Publishing - Bintray
- License
scaleway-java-api top
A java api for the scaleway service
Overview top
This is a Java implementation of the Scaleway API which allows you to build and deploy servers on the Scaleway cloud provider.
a simple example is below:
package synapticloop.scaleway.api;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import synapticloop.scaleway.api.exception.ScalewayApiException;
import synapticloop.scaleway.api.model.Image;
import synapticloop.scaleway.api.model.Server;
import synapticloop.scaleway.api.model.ServerAction;
import synapticloop.scaleway.api.model.ServerTask;
import synapticloop.scaleway.api.model.ServerTaskStatus;
import synapticloop.scaleway.api.model.ServerType;
import synapticloop.scaleway.api.model.Volume;
public class Main {
private static String getUbuntuImage(ScalewayApiClient scalewayApiClient) throws ScalewayApiException {
for (int i = 1; i < Integer.MAX_VALUE; i++) {
List<Image> images = scalewayApiClient.getAllImages(i, 100).getImages();
for (Image image : images) {
if("Ubuntu Xenial (16.04 latest)".equals(image.getName())) {
return(image.getId());
}
}
}
return(null);
}
private static String getOrganizationId(ScalewayApiClient scalewayApiClient) throws ScalewayApiException {
return(scalewayApiClient.getAllOrganizations().get(0).getId());
}
public static void main(String[] args) throws ScalewayApiException {
// you can spin up a VM in either Amsterdam or Paris
ScalewayApiClient scalewayApiClient = new ScalewayApiClient(System.getenv("YOUR_SCALEWAY_API_TOKEN_GOES_HERE"), Region.PARIS1);
// a simple creation of a server
Server server = scalewayApiClient.createServer("This is a test server",
getUbuntuImage(scalewayApiClient),
getOrganizationId(scalewayApiClient),
ServerType.VC1S,
new String[] {"lots", "of", "tags"});
// now that we have created the server (and a volume is also created for it)
// we need to power it on this may take some time - so we need to wait until
// it is finished
ServerTask powerOnServerTask = scalewayApiClient.executeServerAction(server.getId(), ServerAction.POWERON);
boolean isStarted = false;
while(!isStarted) {
ServerTask taskStatus = scalewayApiClient.getTaskStatus(powerOnServerTask.getId());
System.out.println(String.format("Server task with id '%s' is in current state '%s' (progress '%s')", taskStatus.getId(), taskStatus.getStatus(), taskStatus.getProgress()));
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
System.err.println("The sleeping thread was interrupted, continuing...");
}
if(taskStatus.getStatus() == ServerTaskStatus.success) {
isStarted = true;
}
}
// now we can power down the server
ServerTask powerOffServerTask = scalewayApiClient.executeServerAction(server.getId(), ServerAction.POWEROFF);
boolean isEnded = false;
while(!isEnded) {
ServerTask taskStatus = scalewayApiClient.getTaskStatus(powerOffServerTask.getId());
System.out.println(String.format("Server task with id '%s' is in current state '%s' (progress '%s')", taskStatus.getId(), taskStatus.getStatus(), taskStatus.getProgress()));
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
System.err.println("The sleeping thread was interrupted, continuing...");
}
if(taskStatus.getStatus() == ServerTaskStatus.success) {
isEnded = true;
}
}
// now delete the server
scalewayApiClient.deleteServer(server.getId());
// don't forget to delete any attached volumes
Map<String, Volume> volumes = server.getVolumes();
Iterator<String> iterator = volumes.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Volume volume = volumes.get(key);
scalewayApiClient.deleteVolume(volume.getId());
}
}
}
Building the Package top
*NIX/Mac OS X top
From the root of the project, simply run
./gradlew build
Windows top
./gradlew.bat build
This will compile and assemble the artefacts into the build/libs/
directory.
Note that this may also run tests (if applicable see the Testing notes)
Running the Tests top
*NIX/Mac OS X top
From the root of the project, simply run
gradle --info test
if you do not have gradle installed, try:
gradlew --info test
Windows top
From the root of the project, simply run
gradle --info test
if you do not have gradle installed, try:
./gradlew.bat --info test
The --info
switch will also output logging for the tests
WARNING: These tests make calls against resources (either API calls to a service provider, or consumption of resources from a service provider) which may contribute to your limits, which may lead to a cost.
Logging - slf4j top
slf4j is the logging framework used for this project. In order to set up a logging framework with this project, sample configurations are below:
Log4j top
You will need to include dependencies for this - note that the versions may need to be updated.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
<scope>runtime</scope>
</dependency>
dependencies {
...
runtime(group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.5', ext: 'jar')
runtime(group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5', ext: 'jar')
...
}
dependencies {
...
runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
runtime 'org.apache.logging.log4j:log4j-core:2.5'
...
}
A sample log4j2.xml
is below:
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Artefact Publishing - Github top
This project publishes artefacts to GitHub
Note that the latest version can be found https://github.com/synapticloopltd/scaleway-java-api/releases
As such, this is not a repository, but a location to download files from.
Artefact Publishing - Bintray top
This project publishes artefacts to bintray
Note that the latest version can be found https://bintray.com/synapticloop/maven/scaleway-java-api/view
maven setup top
this comes from the jcenter bintray, to set up your repository:
<?xml version="1.0" encoding="UTF-8" ?>
<settings xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd' xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray-plugins</name>
<url>http://jcenter.bintray.com</url>
</pluginRepository>
</pluginRepositories>
<id>bintray</id>
</profile>
</profiles>
<activeProfiles>
<activeProfile>bintray</activeProfile>
</activeProfiles>
</settings>
gradle setup top
Repository
repositories {
maven {
url "http://jcenter.bintray.com"
}
}
or just
repositories {
jcenter()
}
Dependencies - Gradle top
dependencies {
runtime(group: 'synapticloopltd', name: 'scaleway-java-api', version: '0.11.0', ext: 'jar')
compile(group: 'synapticloopltd', name: 'scaleway-java-api', version: '0.11.0', ext: 'jar')
}
or, more simply for versions of gradle greater than 2.1
dependencies {
runtime 'synapticloopltd:scaleway-java-api:0.11.0'
compile 'synapticloopltd:scaleway-java-api:0.11.0'
}
Dependencies - Maven top
<dependency>
<groupId>synapticloopltd</groupId>
<artifactId>scaleway-java-api</artifactId>
<version>0.11.0</version>
<type>jar</type>
</dependency>
Dependencies - Downloads top
You will also need to download the following dependencies:
net.sourceforge.cobertura:cobertura:2.1.1
: (It may be available on one of: bintray mvn central)
org.apache.httpcomponents:httpclient:4.5.1
: (It may be available on one of: bintray mvn central)commons-io:commons-io:2.4
: (It may be available on one of: bintray mvn central)com.fasterxml.jackson.core:jackson-databind:2.8.5
: (It may be available on one of: bintray mvn central)org.slf4j:slf4j-api:1.7.13
: (It may be available on one of: bintray mvn central)
org.apache.httpcomponents:httpclient:4.5.1
: (It may be available on one of: bintray mvn central)commons-io:commons-io:2.4
: (It may be available on one of: bintray mvn central)com.fasterxml.jackson.core:jackson-databind:2.8.5
: (It may be available on one of: bintray mvn central)org.slf4j:slf4j-api:1.7.13
: (It may be available on one of: bintray mvn central)
junit:junit:4.12
: (It may be available on one of: bintray mvn central)org.apache.logging.log4j:log4j-slf4j-impl:2.5
: (It may be available on one of: bintray mvn central)org.apache.logging.log4j:log4j-core:2.5
: (It may be available on one of: bintray mvn central)
junit:junit:4.12
: (It may be available on one of: bintray mvn central)org.apache.logging.log4j:log4j-slf4j-impl:2.5
: (It may be available on one of: bintray mvn central)org.apache.logging.log4j:log4j-core:2.5
: (It may be available on one of: bintray mvn central)
NOTE: You may need to download any dependencies of the above dependencies in turn (i.e. the transitive dependencies)
License top
The MIT License (MIT)
Copyright (c) 2017 synapticloopltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--
This README.md file was hand-crafted with care utilising synapticloop
templar
->
documentr
--