def extract_args(params): """ Extract any arguments passed as parameters through the API and return them as a dictionary. This allows us to filter the passed args and do type conversion where appropriate """ kwargs = {} try: timeout_mins = int(params.get(api.PARAM_TIMEOUT, 0)) except (ValueError, TypeError): logger.exception(_("create timeout conversion")) else: if timeout_mins > 0: kwargs[api.PARAM_TIMEOUT] = timeout_mins if api.PARAM_DISABLE_ROLLBACK in params: disable_rollback = params.get(api.PARAM_DISABLE_ROLLBACK) if str(disable_rollback).lower() == "true": kwargs[api.PARAM_DISABLE_ROLLBACK] = True elif str(disable_rollback).lower() == "false": kwargs[api.PARAM_DISABLE_ROLLBACK] = False else: raise ValueError( _("Unexpected value for parameter" " %(name)s : %(value)s") % dict(name=api.PARAM_DISABLE_ROLLBACK, value=disable_rollback) ) adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA) if adopt_data: adopt_data = template_format.simple_parse(adopt_data) if not isinstance(adopt_data, dict): raise ValueError(_('Unexpected adopt data "%s". Adopt data must be a dict.') % adopt_data) kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data return kwargs
def extract_args(params): ''' Extract any arguments passed as parameters through the API and return them as a dictionary. This allows us to filter the passed args and do type conversion where appropriate ''' kwargs = {} timeout_mins = params.get(api.PARAM_TIMEOUT) if timeout_mins not in ('0', 0, None): try: timeout = int(timeout_mins) except (ValueError, TypeError): logger.exception(_('Timeout conversion failed')) else: if timeout > 0: kwargs[api.PARAM_TIMEOUT] = timeout else: raise ValueError(_('Invalid timeout value %s') % timeout) if api.PARAM_DISABLE_ROLLBACK in params: disable_rollback = extract_bool(params[api.PARAM_DISABLE_ROLLBACK]) kwargs[api.PARAM_DISABLE_ROLLBACK] = disable_rollback adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA) if adopt_data: adopt_data = template_format.simple_parse(adopt_data) if not isinstance(adopt_data, dict): raise ValueError( _('Unexpected adopt data "%s". Adopt data must be a dict.') % adopt_data) kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data return kwargs
def template(self): """ Get template file contents, either inline, from stack adopt data or from a URL, in JSON or YAML format. """ template_data = None if rpc_api.PARAM_ADOPT_STACK_DATA in self.data: adopt_data = self.data[rpc_api.PARAM_ADOPT_STACK_DATA] try: adopt_data = template_format.simple_parse(adopt_data) return adopt_data['template'] except (ValueError, KeyError) as ex: err_reason = _('Invalid adopt data: %s') % ex raise exc.HTTPBadRequest(err_reason) elif self.PARAM_TEMPLATE in self.data: template_data = self.data[self.PARAM_TEMPLATE] if isinstance(template_data, dict): return template_data elif self.PARAM_TEMPLATE_URL in self.data: url = self.data[self.PARAM_TEMPLATE_URL] LOG.debug('TemplateUrl %s' % url) try: template_data = urlfetch.get(url) except IOError as ex: err_reason = _('Could not retrieve template: %s') % ex raise exc.HTTPBadRequest(err_reason) if template_data is None: if self.patch: return None else: raise exc.HTTPBadRequest(_("No template specified")) with self.parse_error_check('Template'): return template_format.parse(template_data)
def extract_args(params): """Extract arguments passed as parameters and return them as a dictionary. Extract any arguments passed as parameters through the API and return them as a dictionary. This allows us to filter the passed args and do type conversion where appropriate """ kwargs = {} timeout_mins = params.get(rpc_api.PARAM_TIMEOUT) if timeout_mins not in ('0', 0, None): try: timeout = int(timeout_mins) except (ValueError, TypeError): LOG.exception(_LE('Timeout conversion failed')) else: if timeout > 0: kwargs[rpc_api.PARAM_TIMEOUT] = timeout else: raise ValueError(_('Invalid timeout value %s') % timeout) name = rpc_api.PARAM_DISABLE_ROLLBACK if name in params: disable_rollback = param_utils.extract_bool(name, params[name]) kwargs[name] = disable_rollback name = rpc_api.PARAM_SHOW_DELETED if name in params: params[name] = param_utils.extract_bool(name, params[name]) adopt_data = params.get(rpc_api.PARAM_ADOPT_STACK_DATA) if adopt_data: try: adopt_data = template_format.simple_parse(adopt_data) except ValueError as exc: raise ValueError(_('Invalid adopt data: %s') % exc) kwargs[rpc_api.PARAM_ADOPT_STACK_DATA] = adopt_data tags = params.get(rpc_api.PARAM_TAGS) if tags: if not isinstance(tags, list): raise ValueError(_('Invalid tags, not a list: %s') % tags) for tag in tags: if not isinstance(tag, six.string_types): raise ValueError(_('Invalid tag, "%s" is not a string') % tag) if len(tag) > 80: raise ValueError( _('Invalid tag, "%s" is longer than 80 ' 'characters') % tag) # Comma is not allowed as per the API WG tagging guidelines if ',' in tag: raise ValueError(_('Invalid tag, "%s" contains a comma') % tag) kwargs[rpc_api.PARAM_TAGS] = tags return kwargs
def test_profile_create(self): self._create_profile(self.t) expect_kwargs = { 'name': 'SenlinProfile', 'metadata': None, 'spec': template_format.simple_parse(profile_spec) } self.senlin_mock.create_profile.assert_called_once_with( **expect_kwargs)
def extract_args(params): """Extract arguments passed as parameters and return them as a dictionary. Extract any arguments passed as parameters through the API and return them as a dictionary. This allows us to filter the passed args and do type conversion where appropriate """ kwargs = {} timeout_mins = params.get(rpc_api.PARAM_TIMEOUT) if timeout_mins not in ('0', 0, None): try: timeout = int(timeout_mins) except (ValueError, TypeError): LOG.exception(_LE('Timeout conversion failed')) else: if timeout > 0: kwargs[rpc_api.PARAM_TIMEOUT] = timeout else: raise ValueError(_('Invalid timeout value %s') % timeout) name = rpc_api.PARAM_DISABLE_ROLLBACK if name in params: disable_rollback = param_utils.extract_bool(name, params[name]) kwargs[name] = disable_rollback name = rpc_api.PARAM_SHOW_DELETED if name in params: params[name] = param_utils.extract_bool(name, params[name]) adopt_data = params.get(rpc_api.PARAM_ADOPT_STACK_DATA) if adopt_data: try: adopt_data = template_format.simple_parse(adopt_data) except ValueError as exc: raise ValueError(_('Invalid adopt data: %s') % exc) kwargs[rpc_api.PARAM_ADOPT_STACK_DATA] = adopt_data tags = params.get(rpc_api.PARAM_TAGS) if tags: if not isinstance(tags, list): raise ValueError(_('Invalid tags, not a list: %s') % tags) for tag in tags: if not isinstance(tag, six.string_types): raise ValueError(_('Invalid tag, "%s" is not a string') % tag) if len(tag) > 80: raise ValueError(_('Invalid tag, "%s" is longer than 80 ' 'characters') % tag) # Comma is not allowed as per the API WG tagging guidelines if ',' in tag: raise ValueError(_('Invalid tag, "%s" contains a comma') % tag) kwargs[rpc_api.PARAM_TAGS] = tags return kwargs
def handle_create(self): spec = template_format.simple_parse(self.properties[self.SPEC]) params = { 'name': (self.properties[self.NAME] or self.physical_resource_name()), 'spec': spec, 'metadata': self.properties[self.METADATA], } profile = self.client().create(models.Profile, params) self.resource_id_set(profile['id'])
def validate(self): super(Group, self).validate() launchconf = self.properties[self.LAUNCH_CONFIGURATION] lcargs = launchconf[self.LAUNCH_CONFIG_ARGS] server_args = lcargs.get(self.LAUNCH_CONFIG_ARGS_SERVER) st_args = lcargs.get(self.LAUNCH_CONFIG_ARGS_STACK) # launch_server and launch_stack are required and mutually exclusive. if ((not server_args and not st_args) or (server_args and st_args)): msg = ( _('Must provide one of %(server)s or %(stack)s in %(conf)s') % { 'server': self.LAUNCH_CONFIG_ARGS_SERVER, 'stack': self.LAUNCH_CONFIG_ARGS_STACK, 'conf': self.LAUNCH_CONFIGURATION }) raise exception.StackValidationFailed(msg) lb_args = lcargs.get(self.LAUNCH_CONFIG_ARGS_LOAD_BALANCERS) lbs = copy.deepcopy(lb_args) for lb in lbs: lb_port = lb.get(self.LAUNCH_CONFIG_ARGS_LOAD_BALANCER_PORT) lb_id = lb[self.LAUNCH_CONFIG_ARGS_LOAD_BALANCER_ID] if not lb_port: # check if lb id is a valid RCV3 pool id if not self._check_rackconnect_v3_pool_exists(lb_id): msg = _('Could not find RackConnectV3 pool ' 'with id %s') % (lb_id) raise exception.StackValidationFailed(msg) if st_args: st_tmpl = st_args.get(self.LAUNCH_CONFIG_ARGS_STACK_TEMPLATE) st_tmpl_url = st_args.get( self.LAUNCH_CONFIG_ARGS_STACK_TEMPLATE_URL) st_env = st_args.get(self.LAUNCH_CONFIG_ARGS_STACK_ENVIRONMENT) # template and template_url are required and mutually exclusive. if ((not st_tmpl and not st_tmpl_url) or (st_tmpl and st_tmpl_url)): msg = _('Must provide one of template or template_url.') raise exception.StackValidationFailed(msg) if st_tmpl: st_files = st_args.get(self.LAUNCH_CONFIG_ARGS_STACK_FILES) try: tmpl = template_format.simple_parse(st_tmpl) templatem.Template(tmpl, files=st_files, env=st_env) except Exception as exc: msg = (_('Encountered error while loading template: %s') % six.text_type(exc)) raise exception.StackValidationFailed(msg)
def validate(self): super(Group, self).validate() launchconf = self.properties[self.LAUNCH_CONFIGURATION] lcargs = launchconf[self.LAUNCH_CONFIG_ARGS] server_args = lcargs.get(self.LAUNCH_CONFIG_ARGS_SERVER) st_args = lcargs.get(self.LAUNCH_CONFIG_ARGS_STACK) # launch_server and launch_stack are required and mutually exclusive. if (not server_args and not st_args) or (server_args and st_args): msg = _("Must provide one of %(server)s or %(stack)s in %(conf)s") % { "server": self.LAUNCH_CONFIG_ARGS_SERVER, "stack": self.LAUNCH_CONFIG_ARGS_STACK, "conf": self.LAUNCH_CONFIGURATION, } raise exception.StackValidationFailed(msg) lb_args = lcargs.get(self.LAUNCH_CONFIG_ARGS_LOAD_BALANCERS) lbs = copy.deepcopy(lb_args) for lb in lbs: lb_port = lb.get(self.LAUNCH_CONFIG_ARGS_LOAD_BALANCER_PORT) lb_id = lb[self.LAUNCH_CONFIG_ARGS_LOAD_BALANCER_ID] if not lb_port: # check if lb id is a valid RCV3 pool id if not self._check_rackconnect_v3_pool_exists(lb_id): msg = _("Could not find RackConnectV3 pool " "with id %s") % (lb_id) raise exception.StackValidationFailed(msg) if st_args: st_tmpl = st_args.get(self.LAUNCH_CONFIG_ARGS_STACK_TEMPLATE) st_tmpl_url = st_args.get(self.LAUNCH_CONFIG_ARGS_STACK_TEMPLATE_URL) st_env = st_args.get(self.LAUNCH_CONFIG_ARGS_STACK_ENVIRONMENT) # template and template_url are required and mutually exclusive. if (not st_tmpl and not st_tmpl_url) or (st_tmpl and st_tmpl_url): msg = _("Must provide one of template or template_url.") raise exception.StackValidationFailed(msg) if st_tmpl: st_files = st_args.get(self.LAUNCH_CONFIG_ARGS_STACK_FILES) try: tmpl = template_format.simple_parse(st_tmpl) templatem.Template(tmpl, files=st_files, env=st_env) except Exception as exc: msg = _("Encountered error while loading template: %s") % six.text_type(exc) raise exception.StackValidationFailed(msg)
def extract_args(params): ''' Extract any arguments passed as parameters through the API and return them as a dictionary. This allows us to filter the passed args and do type conversion where appropriate ''' kwargs = {} timeout_mins = params.get(api.PARAM_TIMEOUT) if timeout_mins not in ('0', 0, None): try: timeout = int(timeout_mins) except (ValueError, TypeError): logger.exception(_('Timeout conversion failed')) else: if timeout > 0: kwargs[api.PARAM_TIMEOUT] = timeout else: raise ValueError(_('Invalid timeout value %s') % timeout) if api.PARAM_DISABLE_ROLLBACK in params: disable_rollback = params.get(api.PARAM_DISABLE_ROLLBACK) if str(disable_rollback).lower() == 'true': kwargs[api.PARAM_DISABLE_ROLLBACK] = True elif str(disable_rollback).lower() == 'false': kwargs[api.PARAM_DISABLE_ROLLBACK] = False else: raise ValueError( _('Unexpected value for parameter' ' %(name)s : %(value)s') % dict(name=api.PARAM_DISABLE_ROLLBACK, value=disable_rollback)) adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA) if adopt_data: adopt_data = template_format.simple_parse(adopt_data) if not isinstance(adopt_data, dict): raise ValueError( _('Unexpected adopt data "%s". Adopt data must be a dict.') % adopt_data) kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data return kwargs
def template(self): """Get template file contents. Get template file contents, either inline, from stack adopt data or from a URL, in JSON or YAML format. """ template_data = None if rpc_api.PARAM_ADOPT_STACK_DATA in self.data: adopt_data = self.data[rpc_api.PARAM_ADOPT_STACK_DATA] try: adopt_data = template_format.simple_parse(adopt_data) template_format.validate_template_limit( six.text_type(adopt_data['template'])) return adopt_data['template'] except (ValueError, KeyError) as ex: err_reason = _('Invalid adopt data: %s') % ex raise exc.HTTPBadRequest(err_reason) elif self.PARAM_TEMPLATE in self.data: template_data = self.data[self.PARAM_TEMPLATE] if isinstance(template_data, dict): template_format.validate_template_limit( six.text_type(template_data)) return template_data elif self.PARAM_TEMPLATE_URL in self.data: url = self.data[self.PARAM_TEMPLATE_URL] LOG.debug('TemplateUrl %s' % url) try: template_data = urlfetch.get(url) except IOError as ex: err_reason = _('Could not retrieve template: %s') % ex raise exc.HTTPBadRequest(err_reason) if template_data is None: if self.patch: return None else: raise exc.HTTPBadRequest(_("No template specified")) with self.parse_error_check('Template'): return template_format.parse(template_data)
def extract_args(params): ''' Extract any arguments passed as parameters through the API and return them as a dictionary. This allows us to filter the passed args and do type conversion where appropriate ''' kwargs = {} timeout_mins = params.get(api.PARAM_TIMEOUT) if timeout_mins not in ('0', 0, None): try: timeout = int(timeout_mins) except (ValueError, TypeError): LOG.exception(_('Timeout conversion failed')) else: if timeout > 0: kwargs[api.PARAM_TIMEOUT] = timeout else: raise ValueError(_('Invalid timeout value %s') % timeout) if api.PARAM_DISABLE_ROLLBACK in params: disable_rollback = param_utils.extract_bool( params[api.PARAM_DISABLE_ROLLBACK]) kwargs[api.PARAM_DISABLE_ROLLBACK] = disable_rollback if api.PARAM_SHOW_DELETED in params: params[api.PARAM_SHOW_DELETED] = param_utils.extract_bool( params[api.PARAM_SHOW_DELETED]) adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA) if adopt_data: adopt_data = template_format.simple_parse(adopt_data) if not isinstance(adopt_data, dict): raise ValueError( _('Unexpected adopt data "%s". Adopt data must be a dict.') % adopt_data) kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data return kwargs
def extract_args(params): ''' Extract any arguments passed as parameters through the API and return them as a dictionary. This allows us to filter the passed args and do type conversion where appropriate ''' kwargs = {} timeout_mins = params.get(api.PARAM_TIMEOUT) if timeout_mins not in ('0', 0, None): try: timeout = int(timeout_mins) except (ValueError, TypeError): LOG.exception(_('Timeout conversion failed')) else: if timeout > 0: kwargs[api.PARAM_TIMEOUT] = timeout else: raise ValueError(_('Invalid timeout value %s') % timeout) if api.PARAM_DISABLE_ROLLBACK in params: disable_rollback = param_utils.extract_bool( params[api.PARAM_DISABLE_ROLLBACK]) kwargs[api.PARAM_DISABLE_ROLLBACK] = disable_rollback if api.PARAM_SHOW_DELETED in params: params[api.PARAM_SHOW_DELETED] = param_utils.extract_bool( params[api.PARAM_SHOW_DELETED]) adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA) if adopt_data: try: adopt_data = template_format.simple_parse(adopt_data) except ValueError as exc: raise ValueError(_('Invalid adopt data: %s') % exc) kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data return kwargs
def _parse_spec(self, spec): if self._spec is None: self._spec = template_format.simple_parse(spec) return self._spec
def __init__(self, id='some_id', spec=None): self.id = id self.name = "SenlinProfile" self.metadata = {} self.spec = spec or template_format.simple_parse(profile_spec)