<s> from __future__ import absolute_import <EOL> import functools <EOL> from celery import shared_task <EOL> from nodeconductor . iaas . models import Instance , SecurityGroup <EOL> from nodeconductor . iaas . backend import OpenStackBackend <EOL> from nodeconductor . core . tasks import throttle , retry_if_false <EOL> def track_openstack_session ( task_fn ) : <EOL> @ functools . wraps ( task_fn ) <EOL> def wrapped ( tracked_session , * args , ** kwargs ) : <EOL> session = OpenStackBackend . recover_session ( tracked_session ) <EOL> session . validate ( ) <EOL> task_fn ( session , * args , ** kwargs ) <EOL> return session <EOL> return wrapped <EOL> @ shared_task <EOL> def openstack_create_session ( ** kwargs ) : <EOL> return OpenStackBackend . create_session ( ** kwargs ) <EOL> @ shared_task <EOL> @ track_openstack_session <EOL> def nova_server_resize ( session , server_id , flavor_id ) : <EOL> OpenStackBackend . create_nova_client ( session ) . servers . resize ( server_id , flavor_id , '<STR_LIT>' ) <EOL> @ shared_task <EOL> @ track_openstack_session <EOL> def nova_server_resize_confirm ( session , server_id ) : <EOL> OpenStackBackend . create_nova_client ( session ) . servers . confirm_resize ( server_id ) <EOL> @ shared_task ( max_retries = <NUM_LIT> , default_retry_delay = <NUM_LIT:3> ) <EOL> @ track_openstack_session <EOL> @ retry_if_false <EOL> def nova_wait_for_server_status ( session , server_id , status ) : <EOL> server = OpenStackBackend . create_nova_client ( session ) . servers . get ( server_id ) <EOL> return server . status == status <EOL> @ shared_task ( is_heavy_task = True ) <EOL> def openstack_provision_instance ( instance_uuid , backend_flavor_id , <EOL> system_volume_id = None , data_volume_id = None ) : <EOL> instance = Instance . objects . get ( uuid = instance_uuid ) <EOL> cloud = instance . cloud_project_membership . cloud <EOL> with throttle ( key = cloud . auth_url ) : <EOL> backend = cloud . get_backend ( ) <EOL> backend . provision_instance ( <EOL> instance , backend_flavor_id , system_volume_id , data_volume_id ) <EOL> @ shared_task <EOL> @ track_openstack_session <EOL> def openstack_create_security_group ( session , security_group_uuid ) : <EOL> security_group = SecurityGroup . objects . get ( uuid = security_group_uuid ) <EOL> backend = security_group . cloud_project_membership . cloud . get_backend ( ) <EOL> nova = OpenStackBackend . create_nova_client ( session ) <EOL> backend . create_security_group ( security_group , nova = nova ) <EOL> @ shared_task <EOL> @ track_openstack_session <EOL> def openstack_update_security_group ( session , security_group_uuid ) : <EOL> security_group = SecurityGroup . objects . get ( uuid = security_group_uuid ) <EOL> backend = security_group . cloud_project_membership . cloud . get_backend ( ) <EOL> nova = OpenStackBackend . create_nova_client ( session ) <EOL> backend . update_security_group ( security_group , nova = nova ) <EOL> @ shared_task <EOL> @ track_openstack_session <EOL> def openstack_delete_security_group ( session , security_group_uuid ) : <EOL> security_group = SecurityGroup . objects . get ( uuid = security_group_uuid ) <EOL> backend = security_group . cloud_project_membership . cloud . get_backend ( ) <EOL> nova = OpenStackBackend . create_nova_client ( session ) <EOL> backend . delete_security_group ( security_group . backend_id , nova = nova ) </s>
