Skip to content

Commit

Permalink
Sdk0.29 (#36)
Browse files Browse the repository at this point in the history
* Prepare for sdk0.29

* Update for SDK 0.29

* praise pep8 gods
  • Loading branch information
gtema authored Jun 7, 2019
1 parent 829e303 commit e3dc9f7
Show file tree
Hide file tree
Showing 62 changed files with 286 additions and 1,211 deletions.
12 changes: 1 addition & 11 deletions doc/source/user/proxies/volume_backup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,8 @@ The VBS high-level interface is available through the ``volume_backup``
member of a :class:`~openstack.connection.Connection` object. The
``volume_backup`` member will only be added if the ``otcextensions.sdk.register_otc_extensions(conn)`` method is called.

Backup Operations
^^^^^^^^^^^^^^^^^
The Backup itself is an OpenStack entity and supported natively as block_storage.Backup.

.. autoclass:: otcextensions.sdk.volume_backup.v2._proxy.Proxy

.. automethod:: otcextensions.sdk.volume_backup.v2._proxy.Proxy.backups
.. automethod:: otcextensions.sdk.volume_backup.v2._proxy.Proxy.get_backup
.. automethod:: otcextensions.sdk.volume_backup.v2._proxy.Proxy.create_backup
.. automethod:: otcextensions.sdk.volume_backup.v2._proxy.Proxy.delete_backup
.. automethod:: otcextensions.sdk.volume_backup.v2._proxy.Proxy.restore_backup
.. automethod:: otcextensions.sdk.volume_backup.v2._proxy.Proxy.wait_for_backup
.. automethod:: otcextensions.sdk.volume_backup.v2._proxy.Proxy.wait_for_backup_delete

Backup Policy Operations
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
14 changes: 7 additions & 7 deletions otcextensions/osclient/auto_scaling/v1/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ def get_parser(self, prog_name):
'(Repeat multiple times)')
)
parser.add_argument(
'--subnetwork',
metavar='<subnetwork_id>',
'--subnet',
metavar='<subnet_id>',
action='append',
required=True,
help=_('Network ID of the subnet'
Expand All @@ -197,10 +197,10 @@ def get_parser(self, prog_name):
'(Repeat multiple times)')
)
parser.add_argument(
'--network_id',
metavar='<network_id>',
'--router',
metavar='<router>',
required=True,
help=_('Network (VPC) ID')
help=_('Router (VPC) ID')
)
parser.add_argument(
'--audit_method',
Expand Down Expand Up @@ -241,10 +241,10 @@ def take_action(self, parsed_args):

args = {}
args['name'] = parsed_args.name
args['vpc_id'] = parsed_args.network_id
args['vpc_id'] = parsed_args.router

subnets = []
for subnet in parsed_args.subnetwork:
for subnet in parsed_args.subnet:
subnets.append({'id': subnet})
args['networks'] = subnets

Expand Down
4 changes: 3 additions & 1 deletion otcextensions/osclient/dms/v1/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
client = self.app.client_manager.dms

queue = client.find_queue(parsed_args.queue)

data = client.groups(
queue=parsed_args.queue,
queue=queue.id,
include_deadletter=parsed_args.include_deadletter)

if parsed_args.include_deadletter:
Expand Down
6 changes: 0 additions & 6 deletions otcextensions/osclient/obs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@

DEFAULT_API_VERSION = '1'

OBS_API_TYPE = 'object'
OBS_API_SERVICE_NAME = 'objectstorage'
OBS_API_VERSIONS = {
'1': 'otcextensions.obs.v1.api.API',
}

API_VERSION_OPTION = 'os_obs_api_version'
API_NAME = "obs"
API_VERSIONS = {
Expand Down
185 changes: 78 additions & 107 deletions otcextensions/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@
# under the License.
import importlib
import os
import types
import warnings

from openstack import _log
from openstack import connection
from openstack import service_description
from openstack import utils


Expand Down Expand Up @@ -81,7 +77,6 @@
'dns': {
'service_type': 'dns',
'replace_system': True,
# 'append_project_id': True,
},
'kms': {
'service_type': 'kms',
Expand All @@ -91,15 +86,17 @@
'service_type': 'obs',
'require_ak': True,
'endpoint_service_type': 'object',
'set_endpoint_override': True
},
'rds': {
'service_type': 'rds',
# 'additional_headers': {'content-type': 'application/json'},
'append_project_id': True,
},
'volume_backup': {
'service_type': 'vbs',
'service_type': 'volume_backup',
'append_project_id': True,
'endpoint_service_type': 'vbs',
},
}

Expand All @@ -115,8 +112,8 @@ def _get_descriptor(service_name):
desc_class = _find_service_description_class(service_type)
# _logger.debug('descriptor class %s' % desc_class)
descriptor_args = {
'service_type': service.get('endpoint_service_type', service_type)
# 'service_type': service_type
'service_type': service.get('endpoint_service_type', service_type),
'aliases': [service.get('service_type', service_type)]
}

if not desc_class.supported_versions:
Expand Down Expand Up @@ -155,121 +152,95 @@ def _find_service_description_class(service_type):
module_name = service_type.replace('-', '_') + '_service'
class_name = ''.join(
[part.capitalize() for part in module_name.split('_')])
try:
import_name = '.'.join([package_name, module_name])
service_description_module = importlib.import_module(import_name)
except ImportError as e:
# ImportWarning is ignored by default. This warning is here
# as an opt-in for people trying to figure out why something
# didn't work.
warnings.warn(
"Could not import {service_type} service description: {e}".format(
service_type=service_type, e=str(e)),
ImportWarning)
return service_description.ServiceDescription
# try:
import_name = '.'.join([package_name, module_name])
service_description_module = importlib.import_module(import_name)
# except ImportError as e:
# # ImportWarning is ignored by default. This warning is here
# # as an opt-in for people trying to figure out why something
# # didn't work.
# _logger.warn("Could not import {service_type} "
# "service description: {e}".format(
# service_type=service_type, e=str(e)),
# ImportWarning)
# return service_description.ServiceDescription
# There are no cases in which we should have a module but not the class
# inside it.
service_description_class = getattr(service_description_module, class_name)
return service_description_class


def patch_connection(target):
# descriptors are not working out of box for runtime attributes
# So we need to inject them. Additionally we need to override some
# properties of the proxy

def get_otc_proxy(self, service_name=None, service=None):
_logger.debug('get_otc_proxy is %s, %s, %s' %
(self, service_name, service))

if service['service_type'] not in self._proxies:
# Initialize proxy and inject required properties
descriptor = _get_descriptor(service_name)
if not descriptor:
_logger.error('descriptor for service %s is missing' %
service_name)
return

proxy = descriptor.__get__(self, descriptor)

# Set additional_headers into the proxy
if 'additional_headers' in service:
proxy.additional_headers = service.get('additional_headers')

# If service requires AK/SK - inject them
if service.get('require_ak', False):
_logger.debug('during registration found that ak is required')
config = self.config.config

ak = config.get('ak', None)
sk = config.get('sk', None)

if not ak:
ak = os.getenv('S3_ACCESS_KEY_ID', None)
if not sk:
sk = os.getenv('S3_SECRET_ACCESS_KEY', None)

if ak and sk:
proxy._set_ak(ak=ak, sk=sk)
else:
_logger.error('AK/SK pair is not available')
return

# Set endpoint_override
endpoint_override = service.get('endpoint_override', None)
if endpoint_override:
_logger.debug('Setting endpoint_override into the %s.Proxy' %
service_name)
proxy.endpoint_override = endpoint_override

# Ensure EP contain %project_id
append_project_id = service.get('append_project_id', False)
if append_project_id:
ep = proxy.get_endpoint_data().catalog_url
project_id = proxy.get_project_id()
if ep and not ep.rstrip('/').endswith('\\%(project_id)s') \
and not ep.rstrip('/').endswith('$(tenant_id)s') \
and not ep.rstrip('/').endswith(project_id):
proxy.endpoint_override = \
utils.urljoin(ep, '%(project_id)s')
else:
proxy = self._proxies[service['service_type']]

return proxy

connection.Connection.get_otc_proxy = types.MethodType(
get_otc_proxy, target)


def inject_service_to_sdk(conn, service_name, service):
"""Inject service into the SDK space
For some reason it should be a separate function
"""
setattr(
conn.__class__,
service_name,
property(
fget=lambda self: self.get_otc_proxy(service_name, service)
)
)


def register_otc_extensions(conn, **kwargs):
def load(conn, **kwargs):
"""Register supported OTC services and make them known to the OpenStackSDK
:param conn: An established OpenStack cloud connection
:returns: none
"""
patch_connection(conn)
conn.authorize()
project_id = conn._get_project_info().id

for (service_name, service) in OTC_SERVICES.items():
_logger.debug('trying to register service %s' % service_name)

if service.get('replace_system', False):
conn._proxies.pop(service_name, None)

inject_service_to_sdk(conn, service_name, service)
# system_proxy = getattr(conn, service['service_type'])
# for service_type in system_proxy.all_types:
if service['service_type'] in conn._proxies:
del conn._proxies[service['service_type']]
# print(hasattr(conn, service_name))
# delattr(conn, service['service_type'])

sd = _get_descriptor(service_name)

conn.add_service(sd)

if service.get('append_project_id', False):
# If service requires project_id, but it is not present in the
# service catalog - set endpoint_override
ep = conn.endpoint_for(sd.service_type)
if ep and not ep.rstrip('/').endswith('\\%(project_id)s') \
and not ep.rstrip('/').endswith('$(tenant_id)s') \
and not ep.rstrip('/').endswith(project_id):
conn.config.config[
'_'.join([
sd.service_type.lower().replace('-', '_'),
'endpoint_override'
])
] = utils.urljoin(ep, '%(project_id)s')
elif service.get('set_endpoint_override', False):
# We need to set endpoint_override for OBS, since otherwise it
# fails dramatically
ep = conn.endpoint_for(sd.service_type)
conn.config.config[
'_'.join([
sd.service_type.lower().replace('-', '_'),
'endpoint_override'
])
] = utils.urljoin(ep)

# If service requires AK/SK - inject them
if service.get('require_ak', False):
_logger.debug('during registration found that ak is required')
config = conn.config.config

proxy = getattr(conn, service_name)

ak = config.get('ak', None)
sk = config.get('sk', None)

if not ak:
ak = os.getenv('S3_ACCESS_KEY_ID', None)
if not sk:
sk = os.getenv('S3_SECRET_ACCESS_KEY', None)

if ak and sk:
proxy._set_ak(ak=ak, sk=sk)
else:
_logger.error('AK/SK pair is not available')
# return

return None


register_otc_extensions = load
5 changes: 2 additions & 3 deletions otcextensions/sdk/anti_ddos/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
# License for the specific language governing permissions and limitations
# under the License.

# from openstack import proxy
from otcextensions.sdk import sdk_proxy
from openstack import proxy
from otcextensions.sdk.anti_ddos.v1 import alert_config as _alert
from otcextensions.sdk.anti_ddos.v1 import config as _config
from otcextensions.sdk.anti_ddos.v1 import floating_ip as _floating_ip
from otcextensions.sdk.anti_ddos.v1 import status as _status


class Proxy(sdk_proxy.Proxy):
class Proxy(proxy.Proxy):

skip_discovery = True

Expand Down
6 changes: 2 additions & 4 deletions otcextensions/sdk/anti_ddos/v1/alert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
# under the License.
from openstack import resource

from otcextensions.sdk import sdk_resource


class AlertConfig(sdk_resource.Resource):
class AlertConfig(resource.Resource):

base_path = '/warnalert/alertconfig/query'

# capabilities
allow_get = True
allow_fetch = True

# Properties
#: warn alert config information
Expand Down
4 changes: 1 addition & 3 deletions otcextensions/sdk/anti_ddos/v1/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
# under the License.
from openstack import resource

from otcextensions.sdk import sdk_resource


class Config(sdk_resource.Resource):
class Config(resource.Resource):

base_path = '/antiddos/query_config_list'

Expand Down
Loading

0 comments on commit e3dc9f7

Please sign in to comment.