def test_get_vapp_name(self): """ check get vapp name """ self.assertEqual( vcloud_network_plugin.get_vapp_name({ vcloud_network_plugin.VCLOUD_VAPP_NAME: "name" }), "name" ) with self.assertRaises(cfy_exc.NonRecoverableError): vcloud_network_plugin.get_vapp_name({ "aa": "aaa" })
def _get_state(vca_client): """ check network connection availability for host """ vapp_name = get_vapp_name(ctx.instance.runtime_properties) config = get_vcloud_config() vdc = vca_client.get_vdc(config['vdc']) vapp = vca_client.get_vapp(vdc, vapp_name) nw_connections = _get_vm_network_connections(vapp) if len(nw_connections) == 0: ctx.logger.info("No networks connected") ctx.instance.runtime_properties['ip'] = None ctx.instance.runtime_properties['networks'] = {} return True if not all([connection['ip'] for connection in nw_connections]): ctx.logger.info("Network configuration is not finished yet.") return False ctx.instance.runtime_properties['networks'] = { connection['network_name']: connection['ip'] for connection in nw_connections} for connection in nw_connections: if connection['is_primary']: ctx.logger.info("Primary network ip address '{0}' for" " network '{1}'." .format(connection['ip'], connection['network_name'])) ctx.instance.runtime_properties['ip'] = connection['ip'] return True return False
def stop(vca_client, **kwargs): """ poweroff server, if external resource - server stay poweroned """ if ctx.node.properties.get('use_external_resource'): ctx.logger.info('not stopping server since an external server is ' 'being used') else: vapp_name = get_vapp_name(ctx.instance.runtime_properties) config = get_vcloud_config() vdc = vca_client.get_vdc(config['vdc']) vapp = vca_client.get_vapp(vdc, vapp_name) ctx.logger.info("Power-off and undeploy VApp {0}".format(vapp_name)) task = vapp.undeploy() if not task: raise cfy_exc.NonRecoverableError("Could not undeploy vApp {0}". format(error_response(vapp))) wait_for_task(vca_client, task)
def start(vca_client, **kwargs): """ power on server and wait network connection availability for host """ if ctx.node.properties.get('use_external_resource'): ctx.logger.info('not starting server since an external server is ' 'being used') else: vapp_name = get_vapp_name(ctx.instance.runtime_properties) config = get_vcloud_config() vdc = vca_client.get_vdc(config['vdc']) vapp = vca_client.get_vapp(vdc, vapp_name) _power_on_vm(vca_client, vapp, vapp_name) if not _get_state(vca_client): return ctx.operation.retry( message="Waiting for VM's configuration to complete", retry_after=5)
def _volume_operation(vca_client, operation): """ attach/detach volume """ vdc_name = get_vcloud_config()['vdc'] vdc = vca_client.get_vdc(vdc_name) vmName = get_vapp_name(ctx.target.instance.runtime_properties) if ctx.source.node.properties.get('use_external_resource'): volumeName = ctx.source.node.properties['resource_id'] else: volumeName = ctx.source.node.properties['volume']['name'] vapp = vca_client.get_vapp(vdc, vmName) for ref in vca_client.get_diskRefs(vdc): if ref.name == volumeName: if operation == 'ATTACH': ctx.logger.info("Attach volume node '{0}'.".format(volumeName)) task = vapp.attach_disk_to_vm(vmName, ref) if task: wait_for_task(vca_client, task) ctx.logger.info( "Volume node '{0}' has been attached".format( volumeName)) else: raise cfy_exc.NonRecoverableError( "Can't attach disk: '{0}' with error: {1}".format( volumeName, error_response(vapp))) elif operation == 'DETACH': ctx.logger.info("Detach volume node '{0}'.".format(volumeName)) task = vapp.detach_disk_from_vm(vmName, ref) if task: wait_for_task(vca_client, task) ctx.logger.info( "Volume node '{0}' has been detached.".format( volumeName)) else: raise cfy_exc.NonRecoverableError( "Can't detach disk: '{0}'. With error: {1}".format( volumeName, error_response(vapp))) else: raise cfy_exc.NonRecoverableError( "Unknown operation '{0}'".format(operation))
def delete(vca_client, **kwargs): """ delete server """ if ctx.node.properties.get('use_external_resource'): ctx.logger.info('not deleting server since an external server is ' 'being used') else: vapp_name = get_vapp_name(ctx.instance.runtime_properties) config = get_vcloud_config() vdc = vca_client.get_vdc(config['vdc']) vapp = vca_client.get_vapp(vdc, vapp_name) ctx.logger.info("Deleting VApp {0}".format(vapp_name)) task = vapp.delete() if not task: raise cfy_exc.NonRecoverableError("Could not delete vApp {0}". format(error_response(vapp))) wait_for_task(vca_client, task) del ctx.instance.runtime_properties[VCLOUD_VAPP_NAME]
def _volume_operation(vca_client, operation): """ attach/detach volume """ vdc_name = get_vcloud_config()['vdc'] vdc = vca_client.get_vdc(vdc_name) vmName = get_vapp_name(ctx.target.instance.runtime_properties) if ctx.source.node.properties.get('use_external_resource'): volumeName = ctx.source.node.properties['resource_id'] else: volumeName = ctx.source.node.properties['volume']['name'] vapp = vca_client.get_vapp(vdc, vmName) for ref in vca_client.get_diskRefs(vdc): if ref.name == volumeName: if operation == 'ATTACH': ctx.logger.info("Attach volume node '{0}'.".format(volumeName)) task = vapp.attach_disk_to_vm(vmName, ref) if task: wait_for_task(vca_client, task) ctx.logger.info( "Volume node '{0}' has been attached".format(volumeName)) else: raise cfy_exc.NonRecoverableError( "Can't attach disk: '{0}' with error: {1}". format(volumeName, error_response(vapp))) elif operation == 'DETACH': ctx.logger.info("Detach volume node '{0}'.".format(volumeName)) task = vapp.detach_disk_from_vm(vmName, ref) if task: wait_for_task(vca_client, task) ctx.logger.info( "Volume node '{0}' has been detached.". format(volumeName)) else: raise cfy_exc.NonRecoverableError( "Can't detach disk: '{0}'. With error: {1}". format(volumeName, error_response(vapp))) else: raise cfy_exc.NonRecoverableError( "Unknown operation '{0}'".format(operation))