Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package softlayerFunctions;
- import java.util.List;
- import com.softlayer.api.*;
- import com.softlayer.api.service.Account;
- import com.softlayer.api.service.Account.Service;
- import com.softlayer.api.service.Location;
- import com.softlayer.api.service.security.ssh.Key;
- import com.softlayer.api.service.virtual.Guest;
- import com.softlayer.api.service.product.Order;
- import com.softlayer.api.service.product.item.Price;
- import com.softlayer.api.service.container.product.order.Property;
- import com.softlayer.api.service.container.product.order.Receipt;
- import com.softlayer.api.service.container.product.order.virtual.guest.Upgrade;
- import com.softlayer.api.service.event.Log;
- public class Application {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApiClient client = new RestApiClient()
- .withCredentials("tom.vandenberg@fenego.be",
- "51fedb188212b59a2074bedde7da4265e9c11153591f96a854fc97f88bcfc364");
- Account.Service service = Account.service(client);
- service.withMask().virtualGuests().hostname()
- .fullyQualifiedDomainName().primaryIpAddress()
- .primaryBackendIpAddress().startCpus().powerState().name();
- Account account = service.getObject();
- String hostname = "stagevm";
- int[] priceIds = { 1641 };
- int accountId = 350894;
- printVm(client, account);
- // priceCpu(client, account);
- // upgradeVm(client, hostname, accountId, priceIds);
- // createVm(client, account);
- // deleteVm(client);
- // powerOff(client);
- // editVm(client, account);
- // powerOn(client);
- // printVm(client, account);
- // getLog(client);
- // String publicKey =
- // "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAtLPWK6nMoR2ItK95zAq1pYy09I4kmNz/P0l01koPNQeqpZ6n26X+9P29UEw3lDimUhlyNNpGGP5vKsFr9UfsKvOHALkHsz6gJMI36etEIw9yT7bnhBpqdtnL7ZL0OOklJA6fCHrvDNrREqgZn8uVJTBnI7WIGZ9EhuQgMV12Z5rqIjcBPB6jWsfGtfookIQHR0F4NLO9H8zT6r3Sb3lentuxFUvYNQh85LXffDv/Es6oxzNg9Vm88CGYR7iONwznkljZE491YEr18so0iZnL6vlVmjddgzDMSOHagDjncSW7tu51ejObrzyaWWASIJuVBEY4axL6VyD6IXAa0iqAcQ== rsa-key-20150226";
- // String label = "newStageKey";
- // String fingerprint =
- // "ce:a8:4a:25:cf:06:9c:c0:24:b2:75:a1:0c:56:c8:83";
- //
- //
- //
- //
- // Key key = new Key();
- // key.setKey(publicKey);
- // key.setLabel(label);
- // key.setNotes(null);
- // key.setFingerprint(fingerprint);
- //
- // createSshKey(client, account, key);
- }
- // Power on van VM
- public static void powerOn(ApiClient client) {
- for (Guest guest : Account.service(client).getVirtualGuests()) {
- if ("stagevm".equals(guest.getHostname())) {
- System.out.println("Changing to power on");
- guest.asService(client).powerOn();
- System.out.println("power on");
- }
- }
- }
- // power off van VM
- public static void powerOff(ApiClient client) {
- for (Guest guest : Account.service(client).getVirtualGuests()) {
- if ("stagevm".equals(guest.getHostname())) {
- System.out.println("Changing to power off");
- guest.asService(client).powerOff();
- System.out.println("power off");
- }
- }
- }
- // delete VM
- public static void deleteVm(ApiClient client) {
- for (Guest guest : Account.service(client).getVirtualGuests()) {
- if ("stagevm2".equals(guest.getHostname())) {
- System.out.println("deleting...");
- guest.asService(client).deleteObject();
- System.out.println("deleted");
- }
- }
- }
- // ophalen prijs van cpu
- public static void priceCpu(ApiClient client, Account account) {
- for (Guest guest : Account.service(client).getVirtualGuests()) {
- if ("stagevm".equals(guest.getHostname())) {
- for (Price price : guest.asService(client)
- .getUpgradeItemPrices(true)) {
- System.out.print(price.getId() + " - ");
- System.out.print(price.getItem().getKeyName() + " - ");
- System.out.print(price.getItem().getDescription() + " - ");
- System.out.println(price.getHourlyRecurringFee());
- }
- }
- }
- }
- // upgraden van een guest
- public static void upgradeVm(ApiClient client, String hostname,
- int accountId, int[] priceIds) {
- Guest guestToUpgrade = null;
- for (Guest guest : Account.service(client, (long) accountId)
- .getVirtualGuests()) {
- if (guest.getHostname().equals(hostname)) {
- guestToUpgrade = guest;
- }
- }
- Upgrade orderData = new Upgrade();
- List<Price> prices = orderData.getPrices();
- int[] pricesId = priceIds;
- for (int i = 0; i < pricesId.length; i++) {
- Price price = new Price();
- price.setId(new Long(pricesId[i]));
- prices.add(price);
- }
- orderData.getVirtualGuests().add(guestToUpgrade);
- System.out.println(guestToUpgrade.getId());
- Property note = new Property();
- note.setName("CPU Cores");
- note.setValue("Went from 1 to 2");
- Property maintenace = new Property();
- note.setName("MAINTENANCE_WINDOW");
- note.setValue("2015-03-2T20:00:00+12:00");
- orderData.getProperties().add(note);
- orderData.getProperties().add(maintenace);
- Receipt receipt = Order.service(client).placeOrder(orderData, false);
- System.out.println(receipt.getOrderId());
- }
- // edit VM
- public static void editVm(ApiClient client, Account account) {
- for (Guest guest : Account.service(client).getVirtualGuests()) {
- if ("stagevm".equals(guest.getHostname())) {
- System.out.println("editing");
- guest.setHostname("stagevm");
- // guest.setStartCpus((long) 2);
- guest.asService(client).editObject(guest);
- System.out.println("edited");
- }
- }
- }
- // Rebooten VM
- public void rebootVm(ApiClient client) {
- for (Guest guest : Account.service(client).getVirtualGuests()) {
- if ("stagevm".equals(guest.getHostname())) {
- System.out.println("rebooting...");
- guest.asService(client).rebootSoft();
- System.out.println("rebooted");
- }
- }
- }
- // aanmaken VM
- public static void createVm(ApiClient client, Account account) {
- Boolean exists = false;
- for (Guest guest : account.getVirtualGuests()) {
- if (guest.getHostname().equals("stagevm2")) {
- exists = true;
- break;
- }
- }
- if (exists) {
- System.out.println("bestaat al");
- } else {
- System.out.println("Beschikbaar, wordt aangemaakt");
- Guest guestcreate = new Guest();
- guestcreate.setHostname("stagevm2");
- guestcreate.setDomain("stagefenego.be");
- guestcreate.setStartCpus((long) 1);
- guestcreate.setMaxMemory((long) 1024);
- guestcreate.setHourlyBillingFlag(true);
- guestcreate.setOperatingSystemReferenceCode("UBUNTU_LATEST");
- guestcreate.setLocalDiskFlag(false);
- guestcreate.setDatacenter(new Location());
- guestcreate.getDatacenter().setName("ams01");
- guestcreate = Guest.service(client).createObject(guestcreate);
- System.out.println("Virtuele server aangemaakt met ID: "
- + guestcreate.getId());
- }
- }
- // afdrukken lijst van VMs
- public static void printVm(ApiClient client, Account account) {
- System.out.format("\n%d virtual servers\n", account.getVirtualGuests()
- .size());
- for (Guest guest : account.getVirtualGuests()) {
- System.out.format("Host: %s, IP: %s (%s),Cores: %s,Status %s\n",
- guest.getFullyQualifiedDomainName(),
- guest.getPrimaryIpAddress(),
- guest.getPrimaryBackendIpAddress(), guest.getStartCpus(),
- guest.getPowerState().getName());
- }
- }
- // SSH Keys
- public static void createSshKey(ApiClient client, Account account,
- String publicKey, String label, String fingerprint) {
- Key key = new Key();
- key.setKey(publicKey);
- key.setLabel(label);
- key.setNotes(null);
- key.setFingerprint(fingerprint);
- key = key.asService(client).createObject(key);
- key.setAccount(account);
- }
- public static void getLog(ApiClient client) {
- Log.Service logService = Log.service(client);
- logService.withMask().accountId().eventName().ipAddress()
- .eventCreateDate().label();
- List<Log> logs = logService.getAllObjects();
- for (Log l : logs) {
- System.out
- .println(l.getAccountId() + "\t-\t" + l.getEventName()
- + "\t-\t" + l.getIpAddress() + "\t-\t"
- + l.getEventCreateDate().getTime() + "\t-\t"
- + l.getLabel());
- }
- }
- // SSH Keys
- public static void createSshKey(ApiClient client, Account account, Key key) {
- key = key.asService(client).createObject(key);
- key.setAccount(account);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement