Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class SomeDummyClassWithArgs(object):
def __init__(self, start, increment=1):
self.count = start
self.increment = increment
@intercept(event_pattern="*", priority=2499)
def return_incremented(self, event_args, *args, **kwargs):
self.count += self.increment
return self.count
factory = CloudProviderFactory()
factory.middleware.add_middleware_class(SomeDummyClassWithArgs,
start_count,
increment=increment)
provider_name = cb_helpers.get_env("CB_TEST_PROVIDER", "aws")
first_prov = factory.create_provider(provider_name, {})
# Any dispatched event should be intercepted and increment the count
first_prov.storage.volumes.get("anything")
self.assertEqual(first_prov.networking.networks.get("anything"),
start_count + 2*increment)
second_prov = factory.create_provider(provider_name, {})
# This count should be independent of the previous one
self.assertEqual(second_prov.networking.networks.get("anything"),
start_count + increment)
def test_middleware_inherited_events(self):
class SomeDummyClass(object):
@intercept(event_pattern="*", priority=2499)
def return_goodbye(self, event_args, *args, **kwargs):
return "goodbye"
def return_hello(event_args, *args, **kwargs):
return "hello"
factory = CloudProviderFactory()
factory.middleware.add(SomeDummyClass())
factory.middleware.events.intercept("*", 2490, return_hello)
provider_name = cb_helpers.get_env("CB_TEST_PROVIDER", "aws")
prov = factory.create_provider(provider_name, {})
# Any dispatched event should be intercepted and return "hello" instead
self.assertEqual(prov.networking.networks.get("anything"),
"hello")
'image': cb_helpers.get_env('CB_IMAGE_OS',
'c66bdfa1-62b1-43be-8964-e9ce208ac6a5'),
"vm_type": cb_helpers.get_env('CB_VM_TYPE_OS', 'm1.tiny'),
"placement": cb_helpers.get_env('CB_PLACEMENT_OS', 'nova'),
},
'GCPCloudProvider': {
'image': cb_helpers.get_env(
'CB_IMAGE_GCP',
'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/'
'global/images/ubuntu-1710-artful-v20180126'),
'vm_type': cb_helpers.get_env('CB_VM_TYPE_GCP', 'f1-micro'),
'placement': cb_helpers.get_env('GCP_DEFAULT_ZONE', 'us-central1-a'),
},
"AzureCloudProvider": {
"placement":
cb_helpers.get_env('CB_PLACEMENT_AZURE', 'eastus'),
"image":
cb_helpers.get_env('CB_IMAGE_AZURE',
'Canonical:UbuntuServer:16.04.0-LTS:latest'),
"vm_type":
cb_helpers.get_env('CB_VM_TYPE_AZURE', 'Basic_A2'),
}
}
def get_provider_test_data(provider, key):
if "AWSCloudProvider" in provider.name:
return TEST_DATA_CONFIG.get("AWSCloudProvider").get(key)
elif "OpenStackCloudProvider" in provider.name:
return TEST_DATA_CONFIG.get("OpenStackCloudProvider").get(key)
elif "GCPCloudProvider" in provider.name:
return TEST_DATA_CONFIG.get("GCPCloudProvider").get(key)
'GCPCloudProvider': {
'image': cb_helpers.get_env(
'CB_IMAGE_GCP',
'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/'
'global/images/ubuntu-1710-artful-v20180126'),
'vm_type': cb_helpers.get_env('CB_VM_TYPE_GCP', 'f1-micro'),
'placement': cb_helpers.get_env('GCP_DEFAULT_ZONE', 'us-central1-a'),
},
"AzureCloudProvider": {
"placement":
cb_helpers.get_env('CB_PLACEMENT_AZURE', 'eastus'),
"image":
cb_helpers.get_env('CB_IMAGE_AZURE',
'Canonical:UbuntuServer:16.04.0-LTS:latest'),
"vm_type":
cb_helpers.get_env('CB_VM_TYPE_AZURE', 'Basic_A2'),
}
}
def get_provider_test_data(provider, key):
if "AWSCloudProvider" in provider.name:
return TEST_DATA_CONFIG.get("AWSCloudProvider").get(key)
elif "OpenStackCloudProvider" in provider.name:
return TEST_DATA_CONFIG.get("OpenStackCloudProvider").get(key)
elif "GCPCloudProvider" in provider.name:
return TEST_DATA_CONFIG.get("GCPCloudProvider").get(key)
elif "AzureCloudProvider" in provider.name:
return TEST_DATA_CONFIG.get("AzureCloudProvider").get(key)
return None
def __init__(self, config, middleware_manager=None):
super(OpenStackCloudProvider, self).__init__(config,
middleware_manager)
# Initialize cloud connection fields
self.username = self._get_config_value(
'os_username', get_env('OS_USERNAME', None))
self.password = self._get_config_value(
'os_password', get_env('OS_PASSWORD', None))
self.project_name = self._get_config_value(
'os_project_name', get_env('OS_PROJECT_NAME', None)
or get_env('OS_TENANT_NAME', None))
self.auth_url = self._get_config_value(
'os_auth_url', get_env('OS_AUTH_URL', None))
self.region_name = self._get_config_value(
'os_region_name', get_env('OS_REGION_NAME', None))
self.project_domain_name = self._get_config_value(
'os_project_domain_name',
get_env('OS_PROJECT_DOMAIN_NAME', None))
self.user_domain_name = self._get_config_value(
'os_user_domain_name',
get_env('OS_USER_DOMAIN_NAME', None))
# Service connections, lazily initialized
self._nova = None
def _connect_swift(self, options=None):
"""
Get an OpenStack Swift (object store) client connection.
:param options: A dictionary of options from which values will be
passed to the connection.
:return: A Swift client connection using the auth credentials held by
the OpenStackCloudProvider instance
"""
clean_options = self._clean_options(options,
swift_client.Connection.__init__)
storage_url = self._get_config_value(
'os_storage_url', get_env('OS_STORAGE_URL', None))
auth_token = self._get_config_value(
'os_auth_token', get_env('OS_AUTH_TOKEN', None))
if storage_url and auth_token:
clean_options['preauthurl'] = storage_url
clean_options['preauthtoken'] = auth_token
else:
clean_options['authurl'] = self.auth_url
clean_options['session'] = self._keystone_session
return swift_client.Connection(**clean_options)
def _connect_nova_region(self, region_name):
"""Get an OpenStack Nova (compute) client object."""
# Force reauthentication with Keystone
self._cached_keystone_session = None
api_version = self._get_config_value(
'os_compute_api_version',
get_env('OS_COMPUTE_API_VERSION', 2))
service_name = self._get_config_value(
'nova_service_name',
get_env('NOVA_SERVICE_NAME', None))
if self.config.debug_mode:
nova_shell.OpenStackComputeShell().setup_debugging(True)
nova = nova_client.Client(
api_version, session=self._keystone_session,
auth_url=self.auth_url,
region_name=region_name,
service_name=service_name,
http_log_debug=True if self.config.debug_mode else False)
return nova
def _connect_swift(self, options=None):
"""
Get an OpenStack Swift (object store) client connection.
:param options: A dictionary of options from which values will be
passed to the connection.
:return: A Swift client connection using the auth credentials held by
the OpenStackCloudProvider instance
"""
clean_options = self._clean_options(options,
swift_client.Connection.__init__)
storage_url = self._get_config_value(
'os_storage_url', get_env('OS_STORAGE_URL', None))
auth_token = self._get_config_value(
'os_auth_token', get_env('OS_AUTH_TOKEN', None))
if storage_url and auth_token:
clean_options['preauthurl'] = storage_url
clean_options['preauthtoken'] = auth_token
else:
clean_options['authurl'] = self.auth_url
clean_options['session'] = self._keystone_session
return swift_client.Connection(**clean_options)