class RavelloPowerAdapter(): def __init__(self, user, password, application_name, vm_name): self.client = RavelloClient() self.client.login(user, password) self.application_name = application_name self.vm_name = vm_name def _get_application_id(self): for app in self.client.get_applications(): if app['name'] == self.application_name: return app['id'] return None def _get_vm_id(self): app_id = self._get_application_id() for vm in self.client.get_vms(app_id): if vm['name'] == self.vm_name: return vm['id'] return None def get_vm_state(self): return self.client.get_vm_state(self._get_application_id(), self._get_vm_id()) def power_on_vm(self): vm_state = self.get_vm_state() if vm_state in ['STARTED', 'STARTING']: return elif vm_state == 'STOPPED': self.client.start_vm(self._get_application_id(), self._get_vm_id()) else: raise Exception("Error when powering on the VM. Cannot handle " "VM state: '%s'" % vm_state) def power_off_vm(self): vm_state = self.get_vm_state() if vm_state in ['STOPPED', 'STOPPING']: return if vm_state == 'STARTED': self.client.poweroff_vm(self._get_application_id(), self._get_vm_id()) else: raise Exception("Error when powering off the VM. Cannot handle " "VM state: '%s'" % vm_state)
#!/usr/bin/python #will probably need these later #import os #import re #import sys #import json from ravello_sdk import RavelloClient client = RavelloClient() client.login('*****@*****.**', 'Redhat1234') for app in client.get_applications(): print('Found Application: {0}'.format(app['name']))