Move valid_kwargs decorator to _utils
It's not really a public symbol. Change-Id: Iab016b93697f1e44fd299d6bfbfc80677030102d
This commit is contained in:
parent
3d66d1f9fb
commit
0ab8b82c6b
@ -30,7 +30,7 @@ API Methods
|
|||||||
`update_RESOURCE()` API methods (as it makes sense).
|
`update_RESOURCE()` API methods (as it makes sense).
|
||||||
|
|
||||||
- For those methods that should behave differently for omitted or None-valued
|
- For those methods that should behave differently for omitted or None-valued
|
||||||
parameters, use the `valid_kwargs` decorator. Notably: all Neutron
|
parameters, use the `_utils.valid_kwargs` decorator. Notably: all Neutron
|
||||||
`update_*` functions.
|
`update_*` functions.
|
||||||
|
|
||||||
- Deleting a resource should return True if the delete succeeded, or False
|
- Deleting a resource should return True if the delete succeeded, or False
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
import contextlib
|
import contextlib
|
||||||
import functools
|
import functools
|
||||||
import hashlib
|
import hashlib
|
||||||
import inspect
|
|
||||||
import logging
|
import logging
|
||||||
import operator
|
import operator
|
||||||
import threading
|
import threading
|
||||||
@ -23,7 +22,6 @@ import time
|
|||||||
|
|
||||||
from cinderclient.v1 import client as cinder_client
|
from cinderclient.v1 import client as cinder_client
|
||||||
from designateclient.v1 import Client as designate_client
|
from designateclient.v1 import Client as designate_client
|
||||||
from decorator import decorator
|
|
||||||
from dogpile import cache
|
from dogpile import cache
|
||||||
import glanceclient
|
import glanceclient
|
||||||
import glanceclient.exc
|
import glanceclient.exc
|
||||||
@ -81,32 +79,6 @@ OBJECT_CONTAINER_ACLS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def valid_kwargs(*valid_args):
|
|
||||||
# This decorator checks if argument passed as **kwargs to a function are
|
|
||||||
# present in valid_args.
|
|
||||||
#
|
|
||||||
# Typically, valid_kwargs is used when we want to distinguish between
|
|
||||||
# None and omitted arguments and we still want to validate the argument
|
|
||||||
# list.
|
|
||||||
#
|
|
||||||
# Example usage:
|
|
||||||
#
|
|
||||||
# @valid_kwargs('opt_arg1', 'opt_arg2')
|
|
||||||
# def my_func(self, mandatory_arg1, mandatory_arg2, **kwargs):
|
|
||||||
# ...
|
|
||||||
#
|
|
||||||
@decorator
|
|
||||||
def func_wrapper(func, *args, **kwargs):
|
|
||||||
argspec = inspect.getargspec(func)
|
|
||||||
for k in kwargs:
|
|
||||||
if k not in argspec.args[1:] and k not in valid_args:
|
|
||||||
raise TypeError(
|
|
||||||
"{f}() got an unexpected keyword argument "
|
|
||||||
"'{arg}'".format(f=inspect.stack()[1][3], arg=k))
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
return func_wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def simple_logging(debug=False):
|
def simple_logging(debug=False):
|
||||||
if debug:
|
if debug:
|
||||||
log_level = logging.DEBUG
|
log_level = logging.DEBUG
|
||||||
@ -630,8 +602,8 @@ class OpenStackCloud(object):
|
|||||||
return user
|
return user
|
||||||
|
|
||||||
# NOTE(Shrews): Keystone v2 supports updating only name, email and enabled.
|
# NOTE(Shrews): Keystone v2 supports updating only name, email and enabled.
|
||||||
@valid_kwargs('name', 'email', 'enabled', 'domain_id', 'password',
|
@_utils.valid_kwargs('name', 'email', 'enabled', 'domain_id', 'password',
|
||||||
'description', 'default_project')
|
'description', 'default_project')
|
||||||
def update_user(self, name_or_id, **kwargs):
|
def update_user(self, name_or_id, **kwargs):
|
||||||
self.list_users.invalidate(self)
|
self.list_users.invalidate(self)
|
||||||
user = self.get_user(name_or_id)
|
user = self.get_user(name_or_id)
|
||||||
@ -3671,10 +3643,10 @@ class OpenStackCloud(object):
|
|||||||
# a dict).
|
# a dict).
|
||||||
return new_subnet['subnet']
|
return new_subnet['subnet']
|
||||||
|
|
||||||
@valid_kwargs('name', 'admin_state_up', 'mac_address', 'fixed_ips',
|
@_utils.valid_kwargs('name', 'admin_state_up', 'mac_address', 'fixed_ips',
|
||||||
'subnet_id', 'ip_address', 'security_groups',
|
'subnet_id', 'ip_address', 'security_groups',
|
||||||
'allowed_address_pairs', 'extra_dhcp_opts', 'device_owner',
|
'allowed_address_pairs', 'extra_dhcp_opts',
|
||||||
'device_id')
|
'device_owner', 'device_id')
|
||||||
def create_port(self, network_id, **kwargs):
|
def create_port(self, network_id, **kwargs):
|
||||||
"""Create a port
|
"""Create a port
|
||||||
|
|
||||||
@ -3735,8 +3707,9 @@ class OpenStackCloud(object):
|
|||||||
return self.manager.submitTask(
|
return self.manager.submitTask(
|
||||||
_tasks.PortCreate(body={'port': kwargs}))['port']
|
_tasks.PortCreate(body={'port': kwargs}))['port']
|
||||||
|
|
||||||
@valid_kwargs('name', 'admin_state_up', 'fixed_ips', 'security_groups',
|
@_utils.valid_kwargs('name', 'admin_state_up', 'fixed_ips',
|
||||||
'allowed_address_pairs', 'extra_dhcp_opts', 'device_owner')
|
'security_groups', 'allowed_address_pairs',
|
||||||
|
'extra_dhcp_opts', 'device_owner')
|
||||||
def update_port(self, name_or_id, **kwargs):
|
def update_port(self, name_or_id, **kwargs):
|
||||||
"""Update a port
|
"""Update a port
|
||||||
|
|
||||||
@ -3904,7 +3877,7 @@ class OpenStackCloud(object):
|
|||||||
"Unavailable feature: security groups"
|
"Unavailable feature: security groups"
|
||||||
)
|
)
|
||||||
|
|
||||||
@valid_kwargs('name', 'description')
|
@_utils.valid_kwargs('name', 'description')
|
||||||
def update_security_group(self, name_or_id, **kwargs):
|
def update_security_group(self, name_or_id, **kwargs):
|
||||||
"""Update a security group
|
"""Update a security group
|
||||||
|
|
||||||
@ -4802,7 +4775,7 @@ class OperatorCloud(OpenStackCloud):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise OpenStackCloudException(str(e))
|
raise OpenStackCloudException(str(e))
|
||||||
|
|
||||||
@valid_kwargs('type', 'service_type', 'description')
|
@_utils.valid_kwargs('type', 'service_type', 'description')
|
||||||
def create_service(self, name, **kwargs):
|
def create_service(self, name, **kwargs):
|
||||||
"""Create a service.
|
"""Create a service.
|
||||||
|
|
||||||
@ -4916,7 +4889,7 @@ class OperatorCloud(OpenStackCloud):
|
|||||||
msg=str(e)))
|
msg=str(e)))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@valid_kwargs('public_url', 'internal_url', 'admin_url')
|
@_utils.valid_kwargs('public_url', 'internal_url', 'admin_url')
|
||||||
def create_endpoint(self, service_name_or_id, url=None, interface=None,
|
def create_endpoint(self, service_name_or_id, url=None, interface=None,
|
||||||
region=None, enabled=True, **kwargs):
|
region=None, enabled=True, **kwargs):
|
||||||
"""Create a Keystone endpoint.
|
"""Create a Keystone endpoint.
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
from decorator import decorator
|
||||||
|
import inspect
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import netifaces
|
import netifaces
|
||||||
@ -287,3 +289,29 @@ def normalize_users(users):
|
|||||||
enabled=user.get('enabled'),
|
enabled=user.get('enabled'),
|
||||||
) for user in users
|
) for user in users
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def valid_kwargs(*valid_args):
|
||||||
|
# This decorator checks if argument passed as **kwargs to a function are
|
||||||
|
# present in valid_args.
|
||||||
|
#
|
||||||
|
# Typically, valid_kwargs is used when we want to distinguish between
|
||||||
|
# None and omitted arguments and we still want to validate the argument
|
||||||
|
# list.
|
||||||
|
#
|
||||||
|
# Example usage:
|
||||||
|
#
|
||||||
|
# @valid_kwargs('opt_arg1', 'opt_arg2')
|
||||||
|
# def my_func(self, mandatory_arg1, mandatory_arg2, **kwargs):
|
||||||
|
# ...
|
||||||
|
#
|
||||||
|
@decorator
|
||||||
|
def func_wrapper(func, *args, **kwargs):
|
||||||
|
argspec = inspect.getargspec(func)
|
||||||
|
for k in kwargs:
|
||||||
|
if k not in argspec.args[1:] and k not in valid_args:
|
||||||
|
raise TypeError(
|
||||||
|
"{f}() got an unexpected keyword argument "
|
||||||
|
"'{arg}'".format(f=inspect.stack()[1][3], arg=k))
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
return func_wrapper
|
||||||
|
Loading…
Reference in New Issue
Block a user