def normalise(self, meta, val): iam_spec = iam_specs(val, self.self_type, self.self_name) result = sb.set_options( Service = sb.listof(sb.string_spec()) , Federated = sb.listof(sb.string_spec()) , AWS = sb.listof(sb.string_spec()) ).normalise(meta, val) special = sb.set_options( service = sb.listof(principal_service_spec()) , federated = resource_spec(self.self_type, self.self_name) , iam = iam_spec ).normalise(meta, val) for arg, lst in special.items(): capitalized = arg.capitalize() if arg == 'iam': capitalized = "AWS" result[capitalized].extend(lst) for key, val in list(result.items()): if not val: del result[key] continue # Amazon gets rid of the lists if only one item # And this mucks around with the diffing.... if len(val) is 1: result[key] = val[0] else: result[key] = sorted(val) return result
def normalise(self, meta, val): from harpoon.option_spec.harpoon_specs import HarpoonSpec if "content" in val or "context" in val: spec = sb.set_options(mtime=sb.optional_spec(sb.integer_spec()), dest=sb.required(sb.formatted(sb.string_spec(), formatter=MergedOptionStringFormatter)), content=sb.string_spec(), context=sb.optional_spec(HarpoonSpec().context_spec)) result = spec.normalise(meta, val) if result["content"] != "" and result["context"] is not NotSpecified: raise BadOption("Please don't specify both context and content") mtime = result["mtime"] if mtime is NotSpecified: ctxt = type("Context", (object, ), {"use_git": True})() mtime = meta.everything["mtime"](ctxt) context_name = "{0}-{1}-mtime({2})".format(hashlib.md5(result['content'].encode('utf-8')).hexdigest(), result["dest"].replace("/", "-").replace(" ", "--"), mtime) extra_context = (result["content"], context_name) if result["context"] is not NotSpecified: context_name = "{0}.tar".format(context_name) extra_context = ({"context": result["context"]}, context_name) return Command(("ADD", "{0} {1}".format(context_name, result["dest"])), extra_context) else: spec = sb.set_options( get=sb.required(sb.listof(sb.formatted(sb.string_spec(), formatter=MergedOptionStringFormatter))) , prefix = sb.defaulted(sb.string_spec(), "") ) result = spec.normalise(meta, val) final = [] for val in result["get"]: final.append(Command(("ADD", "{0} {1}/{2}".format(val, result["prefix"], val)))) return final
def normalise(self, meta, val): if "content" in val: spec = sb.set_options(dest=sb.required( sb.formatted(sb.string_spec(), formatter=MergedOptionStringFormatter)), content=sb.string_spec()) result = spec.normalise(meta, val) context_name = "{0}-{1}".format( hashlib.md5(result['content'].encode('utf-8')).hexdigest(), result["dest"].replace("/", "-").replace(" ", "--")) return Command( ("ADD", "{0} {1}".format(context_name, result["dest"])), (result["content"], context_name)) else: spec = sb.set_options(get=sb.required( sb.listof( sb.formatted(sb.string_spec(), formatter=MergedOptionStringFormatter))), prefix=sb.defaulted(sb.string_spec(), "")) result = spec.normalise(meta, val) final = [] for val in result["get"]: final.append( Command( ("ADD", "{0} {1}/{2}".format(val, result["prefix"], val)))) return final
def normalise(self, meta, val): iam_spec = iam_specs(val, self.self_type, self.self_name) result = sb.set_options(Service=sb.listof(sb.string_spec()), Federated=sb.listof(sb.string_spec()), AWS=sb.listof(sb.string_spec())).normalise( meta, val) special = sb.set_options(service=sb.listof(principal_service_spec()), federated=resource_spec( self.self_type, self.self_name), iam=iam_spec).normalise(meta, val) for arg, lst in special.items(): capitalized = arg.capitalize() if arg == 'iam': capitalized = "AWS" result[capitalized].extend(lst) for key, val in list(result.items()): if not val: del result[key] continue # Amazon gets rid of the lists if only one item # And this mucks around with the diffing.... if len(val) is 1: result[key] = val[0] else: result[key] = sorted(val) return result
def normalise(self, meta, val): # Make sure we have integration integration_spec = sb.required(sb.string_choice_spec(["aws", "mock"])) sb.set_options(integration=integration_spec).normalise(meta, val) # Determine the http method and resource name method = meta.key_names()["_key_name_0"] resource_name = meta.key_names()["_key_name_2"] # We have integration if no exception was raised if val['integration'] == "aws": return aws_resource_spec(method, resource_name).normalise(meta, val) else: return mock_resource_spec(method, resource_name).normalise(meta, val)
def setup(self, **kwargs): account_spec = sb.set_options( account_id = sb.required(sb.string_spec()) , role_to_assume = sb.required(sb.string_spec()) ) kwargs = sb.set_options( accounts = sb.required(sb.dictof(sb.string_spec(), account_spec)) , ordered_accounts = sb.required(sb.listof(sb.string_spec())) , cloudability_auth_token = sb.required(sb.any_spec()) ).normalise(Meta({}, []), kwargs) for key, val in kwargs.items(): setattr(self, key, val)
def normalise_filled(self, meta, val): typ = formatted(overridden("{_key_name_1}"), formatter=MergedOptionStringFormatter).normalise(meta, val) name = formatted(overridden("{_key_name_0}"), formatter=MergedOptionStringFormatter).normalise(meta, val) special = {} kls = special.get(typ, GenericNetscalerConfig) formatted_string = formatted(string_spec(), formatter=MergedOptionStringFormatter) formatted_options = dictof(string_spec(), match_spec((six.string_types, formatted_string), fallback=any_spec())) options = dict( typ=overridden(typ) , name=overridden(name) , bindings=dictof(string_spec() , netscaler_binding_spec()) , tags=listof(string_spec()) , options=formatted_options , overrides=formatted_options , binding_options=formatted_options , environments=optional_spec(listof(valid_environment_spec())) ) if typ == "sslcertkey": options["link"] = listof(string_spec()) as_dict = set_options(**options).normalise(meta, val) return kls(**dict((name, as_dict[name]) for name in options))
def normalise_filled(self, meta, value): # Make sure the value is a dictionary with a 'use' option set_options(use=required(string_choice_spec(["kms", "plain", "s3_slip"]))).normalise(meta, value) use = value["use"] formatted_string = formatted(string_spec(), formatter=MergedOptionStringFormatter) if use == "kms" or use == "plain" : kls = authentication_objs.PlainAuthentication if use == "plain" else authentication_objs.KmsAuthentication spec = dict(username=required(formatted_string), password=required(formatted_string)) if use == "kms": spec.update(role=required(formatted_string), region=required(formatted_string)) elif use == "s3_slip": kls = authentication_objs.S3SlipAuthentication spec = dict(role=required(formatted_string), location=required(formatted_string)) return create_spec(kls, **spec).normalise(meta, value)
def setup(self, **kwargs): kwargs = sb.set_options( app_id = sb.required(sb.string_or_int_as_string_spec()) , itunes_country_code = sb.required(sb.string_choice_spec(["au"])) ).normalise(Meta({}, []), kwargs) for key, val in kwargs.items(): setattr(self, key, val)
def authentications_spec(self): """Spec for a group of authentication options""" return container_spec(authentication_objs.Authentication , dictof(string_spec(), set_options( reading = optional_spec(authentication_spec()) , writing = optional_spec(authentication_spec()) ) ) )
def authentications_spec(self): """Spec for a group of authentication options""" return optional_spec( container_spec( authentication_objs.Authentication, dictof( string_spec(), set_options(reading=optional_spec(authentication_spec()), writing=optional_spec( authentication_spec())))))
def normalise(self, meta, val): val = sb.dictionary_spec().normalise(meta, val) if "plain" in val: return val["plain"] elif "kms" in val: val = sb.set_options( kms = sb.required(sb.string_spec()) , location = sb.required(sb.string_spec()) ).normalise(meta, val) return lambda: __import__("boto3").client("kms", val['location']).decrypt(CiphertextBlob=base64.b64decode(val['kms']))['Plaintext'].decode('utf-8')
def make_spec(self): nsd = lambda spec: sb.defaulted(spec, NotSpecified) args = {} for arg, spec in self.args(self.self_type, self.self_name).items(): arg, capitalized = capitalize(arg) args[(arg, capitalized)] = spec kwargs = {} for (arg, capitalized), spec in list(args.items()): kwargs[arg] = nsd(spec) kwargs[capitalized] = sb.any_spec() return args, sb.set_options(**kwargs)
def normalise_filled(self, meta, value): # Make sure the value is a dictionary with a 'use' option set_options(use=required( string_choice_spec(["kms", "plain", "s3_slip"]))).normalise( meta, value) use = value["use"] formatted_string = formatted(string_spec(), formatter=MergedOptionStringFormatter) if use == "kms" or use == "plain": kls = authentication_objs.PlainAuthentication if use == "plain" else authentication_objs.KmsAuthentication spec = dict(username=required(formatted_string), password=required(formatted_string)) if use == "kms": spec.update(role=required(formatted_string), region=required(formatted_string)) elif use == "s3_slip": kls = authentication_objs.S3SlipAuthentication spec = dict(role=required(formatted_string), location=required(formatted_string)) return create_spec(kls, **spec).normalise(meta, value)
def make_spec(self): nsd = lambda spec: sb.defaulted(spec, NotSpecified) args = {} for arg, spec in self.args(self.self_type, self.self_name).items(): arg, capitalized = capitalize(arg) args[(arg, capitalized)] = spec kwargs = {} for (arg, capitalized), spec in list(args.items()): kwargs[arg] = nsd(spec) if capitalized not in kwargs: kwargs[capitalized] = sb.any_spec() filtered_args = dict([((a, c), s) for (a, c), s in args.items() if a and a[0].islower()]) return filtered_args, sb.set_options(**kwargs)
def normalise(self, meta, val): from harpoon.option_spec.harpoon_specs import HarpoonSpec from harpoon.option_spec.image_objs import Image formatted_string = sb.formatted(sb.or_spec(sb.string_spec(), sb.typed(Image)), formatter=MergedOptionStringFormatter) img = val["conf"] = sb.set_options(image = formatted_string).normalise(meta, val)["image"] if isinstance(img, six.string_types): val["conf"] = HarpoonSpec().image_spec.normalise(meta.at("image"), {"commands": ["FROM {0}".format(img)]}) val["conf"].image_name = img return sb.create_spec(CommandContentAddDict , image = sb.overridden(img) , conf = sb.any_spec() , path = formatted_string , images = sb.overridden(meta.everything.get("images", [])) , docker_context = sb.overridden(meta.everything["harpoon"].docker_context) ).normalise(meta, val)
from bespin.formatter import MergedOptionStringFormatter from bespin.errors import BadNetScaler from input_algorithms.spec_base import Spec, dictof, listof, string_spec, container_spec, match_spec, overridden, formatted, set_options, any_spec, optional_spec from input_algorithms.spec_base import NotSpecified from input_algorithms.dictobj import dictobj import requests import logging import json import six import re log = logging.getLogger("bespin.option_spec.netscaler") netscaler_binding_spec = lambda: container_spec(NetscalerBinding, match_spec(((list, ) + six.string_types, listof(string_spec())), (dict, set_options(tagged=listof(string_spec()))))) class valid_environment_spec(Spec): def normalise_filled(self, meta, val): if "environments" not in meta.everything: raise BespinError("Please specify {environments}") val = string_spec().normalise(meta, val) available = list(meta.everything["environments"].keys()) if val not in available: raise BespinError("Please choose a valid environment", meta=meta, wanted=val, available=available) return val class netscaler_config_spec(Spec): def normalise_filled(self, meta, val): typ = formatted(overridden("{_key_name_1}"), formatter=MergedOptionStringFormatter).normalise(meta, val)
return self.ultradns_provider_spec.normalise(meta, val) @memoized_property def ultradns_provider_spec(self): return sb.create_spec(UltraDNSProvider , name = sb.formatted(sb.overridden("{_key_name_1}"), formatter=MergedOptionStringFormatter) , provider_type = sb.required(sb.string_spec()) , username = sb.required(formatted_string) , password = sb.required(formatted_string) ) formatted_string = sb.formatted(sb.string_spec(), formatter=MergedOptionStringFormatter) artifact_command_spec = lambda : sb.create_spec(ArtifactCommand , copy = sb.listof(artifact_path_spec()) , modify = sb.dictof(sb.string_spec(), sb.set_options(append=sb.listof(formatted_string))) , command = sb.listof(formatted_string) , timeout = sb.defaulted(sb.integer_spec(), 600) , temp_dir = sb.defaulted(formatted_string, None) , add_into_tar = sb.listof(artifact_path_spec()) ) params_json_spec = lambda: sb.listof(sb.set_options( ParameterKey = sb.required(sb.any_spec()) , ParameterValue = sb.required(sb.any_spec()) )) params_yaml_spec = lambda: sb.dictionary_spec() stack_json_spec = lambda: sb.set_options( Resources = sb.required(sb.dictof(sb.string_spec(), sb.set_options(Type=sb.required(sb.string_spec()), Properties=sb.optional_spec(sb.dictionary_spec()))))
name=sb.formatted( sb.overridden("{_key_name_1}"), formatter=MergedOptionStringFormatter), provider_type=sb.required(sb.string_spec()), username=sb.required(formatted_string), password=sb.required(formatted_string)) formatted_string = sb.formatted(sb.string_spec(), formatter=MergedOptionStringFormatter) artifact_command_spec = lambda: sb.create_spec( ArtifactCommand, copy=sb.listof(artifact_path_spec()), modify=sb.dictof(sb.string_spec(), sb.set_options(append=sb.listof(formatted_string))), command=sb.listof(formatted_string), timeout=sb.defaulted(sb.integer_spec(), 600), temp_dir=sb.defaulted(formatted_string, None), add_into_tar=sb.listof(artifact_path_spec())) params_json_spec = lambda: sb.listof( sb.set_options(ParameterKey=sb.required(sb.any_spec()), ParameterValue=sb.required(sb.any_spec()))) params_yaml_spec = lambda: sb.dictof( sb.string_spec(), sb.formatted(sb.string_or_int_as_string_spec(), formatter=MergedOptionStringFormatter)) stack_json_spec = lambda: sb.set_options(Resources=sb.required(