Advertisement
Guest User

SoftlayerFunctionaliteiten

a guest
Mar 4th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.17 KB | None | 0 0
  1. package softlayerFunctions;
  2.  
  3. import java.util.List;
  4.  
  5. import com.softlayer.api.*;
  6. import com.softlayer.api.service.Account;
  7. import com.softlayer.api.service.Account.Service;
  8. import com.softlayer.api.service.Location;
  9. import com.softlayer.api.service.security.ssh.Key;
  10. import com.softlayer.api.service.virtual.Guest;
  11. import com.softlayer.api.service.product.Order;
  12. import com.softlayer.api.service.product.item.Price;
  13. import com.softlayer.api.service.container.product.order.Property;
  14. import com.softlayer.api.service.container.product.order.Receipt;
  15. import com.softlayer.api.service.container.product.order.virtual.guest.Upgrade;
  16. import com.softlayer.api.service.event.Log;
  17.  
  18. public class Application {
  19.  
  20.     public static void main(String[] args) {
  21.         // TODO Auto-generated method stub
  22.  
  23.         ApiClient client = new RestApiClient()
  24.                 .withCredentials("tom.vandenberg@fenego.be",
  25.                         "51fedb188212b59a2074bedde7da4265e9c11153591f96a854fc97f88bcfc364");
  26.  
  27.         Account.Service service = Account.service(client);
  28.         service.withMask().virtualGuests().hostname()
  29.                 .fullyQualifiedDomainName().primaryIpAddress()
  30.                 .primaryBackendIpAddress().startCpus().powerState().name();
  31.  
  32.         Account account = service.getObject();
  33.  
  34.         String hostname = "stagevm";
  35.         int[] priceIds = { 1641 };
  36.         int accountId = 350894;
  37.  
  38.          printVm(client, account);
  39.         // priceCpu(client, account);
  40.         // upgradeVm(client, hostname, accountId, priceIds);
  41.         // createVm(client, account);
  42.         // deleteVm(client);
  43.         // powerOff(client);
  44.         // editVm(client, account);
  45.         // powerOn(client);
  46.         // printVm(client, account);
  47.         // getLog(client);
  48.  
  49.         // String publicKey =
  50.         // "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAtLPWK6nMoR2ItK95zAq1pYy09I4kmNz/P0l01koPNQeqpZ6n26X+9P29UEw3lDimUhlyNNpGGP5vKsFr9UfsKvOHALkHsz6gJMI36etEIw9yT7bnhBpqdtnL7ZL0OOklJA6fCHrvDNrREqgZn8uVJTBnI7WIGZ9EhuQgMV12Z5rqIjcBPB6jWsfGtfookIQHR0F4NLO9H8zT6r3Sb3lentuxFUvYNQh85LXffDv/Es6oxzNg9Vm88CGYR7iONwznkljZE491YEr18so0iZnL6vlVmjddgzDMSOHagDjncSW7tu51ejObrzyaWWASIJuVBEY4axL6VyD6IXAa0iqAcQ== rsa-key-20150226";
  51.         // String label = "newStageKey";
  52.         // String fingerprint =
  53.         // "ce:a8:4a:25:cf:06:9c:c0:24:b2:75:a1:0c:56:c8:83";
  54.         //
  55.         //
  56.         //
  57.         //
  58.         // Key key = new Key();
  59.         // key.setKey(publicKey);
  60.         // key.setLabel(label);
  61.         // key.setNotes(null);
  62.         // key.setFingerprint(fingerprint);
  63.         //
  64.         // createSshKey(client, account, key);
  65.  
  66.     }
  67.  
  68.     // Power on van VM
  69.     public static void powerOn(ApiClient client) {
  70.         for (Guest guest : Account.service(client).getVirtualGuests()) {
  71.             if ("stagevm".equals(guest.getHostname())) {
  72.                 System.out.println("Changing to power on");
  73.                 guest.asService(client).powerOn();
  74.                 System.out.println("power on");
  75.             }
  76.         }
  77.     }
  78.  
  79.     // power off van VM
  80.     public static void powerOff(ApiClient client) {
  81.         for (Guest guest : Account.service(client).getVirtualGuests()) {
  82.             if ("stagevm".equals(guest.getHostname())) {
  83.                 System.out.println("Changing to power off");
  84.                 guest.asService(client).powerOff();
  85.                 System.out.println("power off");
  86.             }
  87.         }
  88.     }
  89.  
  90.     // delete VM
  91.     public static void deleteVm(ApiClient client) {
  92.         for (Guest guest : Account.service(client).getVirtualGuests()) {
  93.             if ("stagevm2".equals(guest.getHostname())) {
  94.                 System.out.println("deleting...");
  95.                 guest.asService(client).deleteObject();
  96.                 System.out.println("deleted");
  97.             }
  98.         }
  99.     }
  100.  
  101.     // ophalen prijs van cpu
  102.     public static void priceCpu(ApiClient client, Account account) {
  103.  
  104.         for (Guest guest : Account.service(client).getVirtualGuests()) {
  105.             if ("stagevm".equals(guest.getHostname())) {
  106.                 for (Price price : guest.asService(client)
  107.                         .getUpgradeItemPrices(true)) {
  108.                     System.out.print(price.getId() + " - ");
  109.                     System.out.print(price.getItem().getKeyName() + " - ");
  110.                     System.out.print(price.getItem().getDescription() + " - ");
  111.                     System.out.println(price.getHourlyRecurringFee());
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     // upgraden van een guest
  118.     public static void upgradeVm(ApiClient client, String hostname,
  119.             int accountId, int[] priceIds) {
  120.  
  121.         Guest guestToUpgrade = null;
  122.         for (Guest guest : Account.service(client, (long) accountId)
  123.                 .getVirtualGuests()) {
  124.             if (guest.getHostname().equals(hostname)) {
  125.                 guestToUpgrade = guest;
  126.             }
  127.         }
  128.  
  129.         Upgrade orderData = new Upgrade();
  130.         List<Price> prices = orderData.getPrices();
  131.  
  132.         int[] pricesId = priceIds;
  133.         for (int i = 0; i < pricesId.length; i++) {
  134.             Price price = new Price();
  135.             price.setId(new Long(pricesId[i]));
  136.             prices.add(price);
  137.         }
  138.  
  139.         orderData.getVirtualGuests().add(guestToUpgrade);
  140.         System.out.println(guestToUpgrade.getId());
  141.  
  142.         Property note = new Property();
  143.         note.setName("CPU Cores");
  144.         note.setValue("Went from 1 to 2");
  145.  
  146.         Property maintenace = new Property();
  147.         note.setName("MAINTENANCE_WINDOW");
  148.         note.setValue("2015-03-2T20:00:00+12:00");
  149.  
  150.         orderData.getProperties().add(note);
  151.         orderData.getProperties().add(maintenace);
  152.  
  153.         Receipt receipt = Order.service(client).placeOrder(orderData, false);
  154.         System.out.println(receipt.getOrderId());
  155.     }
  156.  
  157.     // edit VM
  158.     public static void editVm(ApiClient client, Account account) {
  159.         for (Guest guest : Account.service(client).getVirtualGuests()) {
  160.             if ("stagevm".equals(guest.getHostname())) {
  161.                 System.out.println("editing");
  162.  
  163.                 guest.setHostname("stagevm");
  164.  
  165.                 // guest.setStartCpus((long) 2);
  166.                 guest.asService(client).editObject(guest);
  167.                 System.out.println("edited");
  168.             }
  169.         }
  170.     }
  171.  
  172.     // Rebooten VM
  173.     public void rebootVm(ApiClient client) {
  174.         for (Guest guest : Account.service(client).getVirtualGuests()) {
  175.             if ("stagevm".equals(guest.getHostname())) {
  176.                 System.out.println("rebooting...");
  177.                 guest.asService(client).rebootSoft();
  178.                 System.out.println("rebooted");
  179.             }
  180.         }
  181.     }
  182.  
  183.     // aanmaken VM
  184.     public static void createVm(ApiClient client, Account account) {
  185.         Boolean exists = false;
  186.         for (Guest guest : account.getVirtualGuests()) {
  187.             if (guest.getHostname().equals("stagevm2")) {
  188.                 exists = true;
  189.                 break;
  190.             }
  191.         }
  192.  
  193.         if (exists) {
  194.             System.out.println("bestaat al");
  195.         } else {
  196.             System.out.println("Beschikbaar, wordt aangemaakt");
  197.             Guest guestcreate = new Guest();
  198.             guestcreate.setHostname("stagevm2");
  199.             guestcreate.setDomain("stagefenego.be");
  200.             guestcreate.setStartCpus((long) 1);
  201.             guestcreate.setMaxMemory((long) 1024);
  202.             guestcreate.setHourlyBillingFlag(true);
  203.             guestcreate.setOperatingSystemReferenceCode("UBUNTU_LATEST");
  204.             guestcreate.setLocalDiskFlag(false);
  205.             guestcreate.setDatacenter(new Location());
  206.             guestcreate.getDatacenter().setName("ams01");
  207.             guestcreate = Guest.service(client).createObject(guestcreate);
  208.             System.out.println("Virtuele server aangemaakt met ID: "
  209.                     + guestcreate.getId());
  210.         }
  211.     }
  212.  
  213.     // afdrukken lijst van VMs
  214.     public static void printVm(ApiClient client, Account account) {
  215.         System.out.format("\n%d virtual servers\n", account.getVirtualGuests()
  216.                 .size());
  217.         for (Guest guest : account.getVirtualGuests()) {
  218.             System.out.format("Host: %s, IP: %s (%s),Cores: %s,Status %s\n",
  219.                     guest.getFullyQualifiedDomainName(),
  220.                     guest.getPrimaryIpAddress(),
  221.                     guest.getPrimaryBackendIpAddress(), guest.getStartCpus(),
  222.                     guest.getPowerState().getName());
  223.         }
  224.     }
  225.  
  226.     // SSH Keys
  227.     public static void createSshKey(ApiClient client, Account account,
  228.             String publicKey, String label, String fingerprint) {
  229.         Key key = new Key();
  230.         key.setKey(publicKey);
  231.         key.setLabel(label);
  232.         key.setNotes(null);
  233.         key.setFingerprint(fingerprint);
  234.         key = key.asService(client).createObject(key);
  235.         key.setAccount(account);
  236.     }
  237.  
  238.     public static void getLog(ApiClient client) {
  239.         Log.Service logService = Log.service(client);
  240.  
  241.         logService.withMask().accountId().eventName().ipAddress()
  242.                 .eventCreateDate().label();
  243.  
  244.         List<Log> logs = logService.getAllObjects();
  245.  
  246.         for (Log l : logs) {
  247.             System.out
  248.                     .println(l.getAccountId() + "\t-\t" + l.getEventName()
  249.                             + "\t-\t" + l.getIpAddress() + "\t-\t"
  250.                             + l.getEventCreateDate().getTime() + "\t-\t"
  251.                             + l.getLabel());
  252.  
  253.         }
  254.     }
  255.  
  256.     // SSH Keys
  257.     public static void createSshKey(ApiClient client, Account account, Key key) {
  258.         key = key.asService(client).createObject(key);
  259.         key.setAccount(account);
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement