def test_is_ondemand(self): # subscription self.assertFalse(vcloud_plugin_common.is_ondemand(vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE)) # ondemand self.assertTrue(vcloud_plugin_common.is_ondemand(vcloud_plugin_common.ONDEMAND_SERVICE_TYPE)) # None, by default used subscription service, so False self.assertFalse(vcloud_plugin_common.is_ondemand(None))
def test_is_ondemand(self): # subscription self.assertFalse( vcloud_plugin_common.is_ondemand( vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE)) # ondemand self.assertTrue( vcloud_plugin_common.is_ondemand( vcloud_plugin_common.ONDEMAND_SERVICE_TYPE)) # None, by default used subscription service, so False self.assertFalse(vcloud_plugin_common.is_ondemand(None))
def _save_configuration(gateway, vca_client, operation, public_ip): """ save/refresh nat rules on gateway """ ctx.logger.info("Save NAT configuration.") success = save_gateway_configuration(gateway, vca_client, ctx) if not success: return False ctx.logger.info("NAT configuration has been saved.") if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip else: service_type = get_vcloud_config().get('service_type') if is_ondemand(service_type): if not ctx.target.node.properties['nat'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx ) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if PORT_REPLACEMENT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PORT_REPLACEMENT] if SSH_PORT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PORT] if SSH_PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PUBLIC_IP] return True
def _save_configuration(gateway, vca_client, operation, public_ip): """ save/refresh nat rules on gateway """ ctx.logger.info("Save NAT configuration.") success = save_gateway_configuration(gateway, vca_client, ctx) if not success: return False ctx.logger.info("NAT configuration has been saved.") if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip else: service_type = get_vcloud_config().get('service_type') if is_ondemand(service_type): if not ctx.target.node.properties['nat'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if PORT_REPLACEMENT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PORT_REPLACEMENT] if SSH_PORT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PORT] if SSH_PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PUBLIC_IP] return True
def _floatingip_operation(operation, vca_client, ctx): """ create/release floating ip by nat rules for this ip with relation to internal ip for current node, save selected public_ip in runtime properties """ service_type = get_vcloud_config().get('service_type') gateway = get_gateway( vca_client, ctx.target.node.properties['floatingip']['edge_gateway']) internal_ip = get_vm_ip(vca_client, ctx, gateway) nat_operation = None public_ip = (ctx.target.instance.runtime_properties.get(PUBLIC_IP) or ctx.target.node.properties['floatingip'].get(PUBLIC_IP)) if operation == CREATE: CheckAssignedInternalIp(internal_ip, gateway) if public_ip: CheckAssignedExternalIp(public_ip, gateway) else: public_ip = get_public_ip(vca_client, gateway, service_type, ctx) nat_operation = _add_nat_rule elif operation == DELETE: if not public_ip: ctx.logger.info("Can't get external IP".format(public_ip)) return True nat_operation = _del_nat_rule else: raise cfy_exc.NonRecoverableError( "Unknown operation {0}".format(operation) ) external_ip = check_ip(public_ip) nat_operation(gateway, "SNAT", internal_ip, external_ip) nat_operation(gateway, "DNAT", external_ip, internal_ip) success = save_gateway_configuration(gateway, vca_client, ctx) if not success: return False if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip save_ssh_parameters(ctx, '22', external_ip) else: if is_ondemand(service_type): if not ctx.target.node.properties['floatingip'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if SSH_PUBLIC_IP in ctx.source.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PUBLIC_IP] if SSH_PORT in ctx.target.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PORT] return True
def get_public_ip(vca_client, gateway, service_type, ctx): """ return new public ip """ if not is_ondemand(service_type): public_ip = getFreeIP(gateway) ctx.logger.info("Assign external IP {0}".format(public_ip)) else: public_ip = get_ondemand_public_ip(vca_client, gateway, ctx) return public_ip
def _floatingip_operation(operation, vca_client, ctx): """ create/release floating ip by nat rules for this ip with relation to internal ip for current node, save selected public_ip in runtime properties """ service_type = get_vcloud_config().get('service_type') gateway = get_gateway( vca_client, ctx.target.node.properties['floatingip']['edge_gateway']) internal_ip = get_vm_ip(vca_client, ctx, gateway) nat_operation = None public_ip = (ctx.target.instance.runtime_properties.get(PUBLIC_IP) or ctx.target.node.properties['floatingip'].get(PUBLIC_IP)) if operation == CREATE: CheckAssignedInternalIp(internal_ip, gateway) if public_ip: CheckAssignedExternalIp(public_ip, gateway) else: public_ip = get_public_ip(vca_client, gateway, service_type, ctx) nat_operation = _add_nat_rule elif operation == DELETE: if not public_ip: ctx.logger.info("Can't get external IP".format(public_ip)) return True nat_operation = _del_nat_rule else: raise cfy_exc.NonRecoverableError( "Unknown operation {0}".format(operation)) external_ip = check_ip(public_ip) nat_operation(gateway, "SNAT", internal_ip, external_ip) nat_operation(gateway, "DNAT", external_ip, internal_ip) success = save_gateway_configuration(gateway, vca_client, ctx) if not success: return False if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip save_ssh_parameters(ctx, '22', external_ip) else: if is_ondemand(service_type): if not ctx.target.node.properties['floatingip'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if SSH_PUBLIC_IP in ctx.source.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PUBLIC_IP] if SSH_PORT in ctx.source.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PORT] return True
def _save_configuration(gateway, vca_client, operation, public_ip): if not save_gateway_configuration(gateway, vca_client): return ctx.operation.retry(message='Waiting for gateway.', retry_after=10) ctx.logger.info("NAT configuration has been saved") if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip else: service_type = get_vcloud_config().get('service_type') if is_ondemand(service_type): if not ctx.target.node.properties['nat'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) del ctx.target.instance.runtime_properties[PUBLIC_IP]
def _floatingip_operation(operation, vca_client, ctx): service_type = get_vcloud_config().get('service_type') gateway = get_gateway( vca_client, ctx.target.node.properties['floatingip']['edge_gateway']) internal_ip = get_vm_ip(vca_client, ctx, gateway) nat_operation = None public_ip = (ctx.target.instance.runtime_properties.get(PUBLIC_IP) or ctx.target.node.properties['floatingip'].get(PUBLIC_IP)) if operation == CREATE: CheckAssignedInternalIp(internal_ip, gateway) if public_ip: CheckAssignedExternalIp(public_ip, gateway) else: public_ip = get_public_ip(vca_client, gateway, service_type, ctx) nat_operation = _add_nat_rule elif operation == DELETE: if not public_ip: ctx.logger.info("Can't get external IP".format(public_ip)) return nat_operation = _del_nat_rule else: raise cfy_exc.NonRecoverableError( "Unknown operation {0}").format(operation) external_ip = check_ip(public_ip) nat_operation(gateway, "SNAT", internal_ip, external_ip) nat_operation(gateway, "DNAT", external_ip, internal_ip) if not save_gateway_configuration(gateway, vca_client): return ctx.operation.retry(message='Waiting for gateway.', retry_after=10) if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip else: if is_ondemand(service_type): if not ctx.target.node.properties['floatingip'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) del ctx.target.instance.runtime_properties[PUBLIC_IP]
def _save_configuration(gateway, vca_client, operation, public_ip): """ save/refresh nat rules on gateway """ if not save_gateway_configuration(gateway, vca_client): return ctx.operation.retry(message='Waiting for gateway.', retry_after=10) ctx.logger.info("NAT configuration has been saved") if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip else: service_type = get_vcloud_config().get('service_type') if is_ondemand(service_type): if not ctx.target.node.properties['nat'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) del ctx.target.instance.runtime_properties[PUBLIC_IP]