def test_circular(self): FLAGS = Flags('ONE', 'TWO') registry = FlagRegistry() @registry.register(flag=FLAGS.ONE, depends_on=FLAGS.TWO, key='one') def method_one(): pass @registry.register(flag=FLAGS.TWO, depends_on=FLAGS.ONE, key='two') def method_two(): pass with self.assertRaises(Exception) as context: registry._do_method_pass([method_one, method_two], 0, dict(), False, flags=FLAGS.ONE | FLAGS.TWO) self.assertTrue( 'Circular Dependency Error' in str(context.exception)) with self.assertRaises(Exception) as context: registry._calculate_dependency_flag(method_two) self.assertTrue( 'Circular Dependency Error' in str(context.exception))
def test_get_method_flag(self): FLAGS = Flags("PETS", "FARM_ANIMALS", "WILD_ANIMALS", "OTHER") registry = FlagRegistry() @registry.register( flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=("pets", "farm", "wild"), ) def some_method(): return "cat", "pig", "rhino" @registry.register(flag=FLAGS.OTHER, depends_on=FLAGS.PETS, key="other") def method_other(): pass method_flag, method_dependencies = registry._get_method_flag(some_method) self.assertEqual( method_flag, FLAGS.PETS | FLAGS.FARM_ANIMALS | FLAGS.WILD_ANIMALS ) self.assertEqual(method_dependencies, 0) method_flag, method_dependencies = registry._get_method_flag(method_other) self.assertEqual(method_flag, FLAGS.OTHER) self.assertEqual(method_dependencies, FLAGS.PETS)
def test_do_method_pass_requires_another_pass(self): FLAGS = Flags("ONE", "TWO") registry = FlagRegistry() @registry.register(flag=FLAGS.ONE, key="one") def method_one(): return 1 @registry.register(flag=FLAGS.TWO, depends_on=FLAGS.ONE, key="two") def method_two(data): # Note: Any method that sets depends_on must take an argument holding the datastructure being built out return 2 result = dict() next_method_queue, executed_flag = registry._do_method_pass( [method_two, method_one], 0, result, False, flags=FLAGS.ONE | FLAGS.TWO ) self.assertEqual(len(next_method_queue), 1) self.assertEqual(executed_flag, FLAGS.ONE) self.assertEqual(set(result.keys()), set(["one"])) next_method_queue, executed_flag = registry._do_method_pass( next_method_queue, executed_flag, result, False, flags=FLAGS.ONE | FLAGS.TWO ) self.assertEqual(len(next_method_queue), 0) self.assertEqual(executed_flag, FLAGS.ONE | FLAGS.TWO) self.assertEqual(set(result.keys()), set(["one", "two"]))
def test_register_multiple_return_value(self): FLAGS = Flags('PETS', 'FARM_ANIMALS', 'WILD_ANIMALS') registry = FlagRegistry() @registry.register(flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=('pets', 'farm', 'wild')) def some_method(): return 'cat', 'pig', 'rhino' self.assertEqual(len(registry.r.keys()), 1) entries = registry.r[some_method] self.assertEqual(len(entries), 3) pet_entry = entries[0] farm_entry = entries[1] wild_entry = entries[2] self.assertEqual(pet_entry['flag'], FLAGS.PETS) self.assertEqual(farm_entry['flag'], FLAGS.FARM_ANIMALS) self.assertEqual(wild_entry['flag'], FLAGS.WILD_ANIMALS) self.assertEqual(pet_entry['key'], 'pets') self.assertEqual(farm_entry['key'], 'farm') self.assertEqual(wild_entry['key'], 'wild') self.assertEqual(pet_entry['rtv_ix'], 0) self.assertEqual(farm_entry['rtv_ix'], 1) self.assertEqual(wild_entry['rtv_ix'], 2) for entry in entries: self.assertEqual(entry['depends_on'], 0)
def test_register_multiple_return_value(self): FLAGS = Flags("PETS", "FARM_ANIMALS", "WILD_ANIMALS") registry = FlagRegistry() @registry.register( flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=("pets", "farm", "wild"), ) def some_method(): return "cat", "pig", "rhino" self.assertEqual(len(registry.r.keys()), 1) entries = registry.r[some_method] self.assertEqual(len(entries), 3) pet_entry = entries[0] farm_entry = entries[1] wild_entry = entries[2] self.assertEqual(pet_entry["flag"], FLAGS.PETS) self.assertEqual(farm_entry["flag"], FLAGS.FARM_ANIMALS) self.assertEqual(wild_entry["flag"], FLAGS.WILD_ANIMALS) self.assertEqual(pet_entry["key"], "pets") self.assertEqual(farm_entry["key"], "farm") self.assertEqual(wild_entry["key"], "wild") self.assertEqual(pet_entry["rtv_ix"], 0) self.assertEqual(farm_entry["rtv_ix"], 1) self.assertEqual(wild_entry["rtv_ix"], 2) for entry in entries: self.assertEqual(entry["depends_on"], 0)
def test_flags(self): FLAGS_1 = Flags('BASE') self.assertEqual(FLAGS_1.BASE, 1) self.assertEqual(FLAGS_1.ALL, 1) FLAGS_2 = Flags('BASE', 'FEATURE_ONE') self.assertEqual(FLAGS_2.BASE, 1) self.assertEqual(FLAGS_2.FEATURE_ONE, 2) self.assertEqual(FLAGS_2.ALL, 3) FLAGS_3 = Flags('BASE', 'FEATURE_ONE', 'FEATURE_TWO') self.assertEqual(FLAGS_3.BASE, 1) self.assertEqual(FLAGS_3.FEATURE_ONE, 2) self.assertEqual(FLAGS_3.FEATURE_TWO, 4) self.assertEqual(FLAGS_3.ALL, 7) self.assertEqual(str(FLAGS_3), "OrderedDict([('BASE', 1), ('FEATURE_ONE', 2), ('FEATURE_TWO', 4), ('ALL', 7), ('None', 0), ('NONE', 0)])")
def test_get_method_flag(self): FLAGS = Flags('PETS', 'FARM_ANIMALS', 'WILD_ANIMALS', 'OTHER') registry = FlagRegistry() @registry.register(flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=('pets', 'farm', 'wild')) def some_method(): return 'cat', 'pig', 'rhino' @registry.register(flag=FLAGS.OTHER, depends_on=FLAGS.PETS, key='other') def method_other(): pass method_flag, method_dependencies = registry._get_method_flag( some_method) self.assertEqual(method_flag, FLAGS.PETS | FLAGS.FARM_ANIMALS | FLAGS.WILD_ANIMALS) self.assertEqual(method_dependencies, 0) method_flag, method_dependencies = registry._get_method_flag( method_other) self.assertEqual(method_flag, FLAGS.OTHER) self.assertEqual(method_dependencies, FLAGS.PETS)
def test_register_single_return_value(self): FLAGS = Flags("ONE", "TWO") registry = FlagRegistry() @registry.register(flag=FLAGS.ONE) def some_method(): pass self.assertEqual(len(registry.r.keys()), 1) @registry.register(flag=FLAGS.TWO) def some_other_method(): pass self.assertEqual(len(registry.r.keys()), 2)
def test_validate_flags(self): FLAGS = Flags("ONE", "TWO", "THREE", "FOUR") registry = FlagRegistry() @registry.register(flag=FLAGS.ONE, key="one") def method_one(): pass @registry.register(flag=FLAGS.TWO, depends_on=FLAGS.ONE, key="two") def method_two(): pass @registry.register(flag=FLAGS.THREE, key="three") def method_three(): pass @registry.register(flag=FLAGS.FOUR, depends_on=FLAGS.TWO, key="four") def method_four(): pass self.assertEqual( registry._calculate_dependency_flag(method_four), FLAGS.ONE | FLAGS.TWO ) self.assertEqual( set(registry._find_methods_matching_flag(FLAGS.ONE | FLAGS.THREE)), set([method_one, method_three]), ) self.assertEqual( set(registry._find_methods_matching_flag(FLAGS.FOUR)), set([method_four]) ) # Asking for FOUR, which depends on TWO, which depends on ONE flag = FLAGS.FOUR self.assertEqual( registry._validate_flags(flag), FLAGS.ONE | FLAGS.TWO | FLAGS.FOUR ) flag = FLAGS.THREE self.assertEqual(registry._validate_flags(flag), FLAGS.THREE) flag = FLAGS.TWO self.assertEqual(registry._validate_flags(flag), FLAGS.ONE | FLAGS.TWO)
def test_validate_flags(self): FLAGS = Flags('ONE', 'TWO', 'THREE', 'FOUR') registry = FlagRegistry() @registry.register(flag=FLAGS.ONE, key='one') def method_one(): pass @registry.register(flag=FLAGS.TWO, depends_on=FLAGS.ONE, key='two') def method_two(): pass @registry.register(flag=FLAGS.THREE, key='three') def method_three(): pass @registry.register(flag=FLAGS.FOUR, depends_on=FLAGS.TWO, key='four') def method_four(): pass self.assertEqual(registry._calculate_dependency_flag(method_four), FLAGS.ONE | FLAGS.TWO) self.assertEqual( set(registry._find_methods_matching_flag(FLAGS.ONE | FLAGS.THREE)), set([method_one, method_three])) self.assertEqual(set(registry._find_methods_matching_flag(FLAGS.FOUR)), set([method_four])) # Asking for FOUR, which depends on TWO, which depends on ONE flag = FLAGS.FOUR self.assertEqual(registry._validate_flags(flag), FLAGS.ONE | FLAGS.TWO | FLAGS.FOUR) flag = FLAGS.THREE self.assertEqual(registry._validate_flags(flag), FLAGS.THREE) flag = FLAGS.TWO self.assertEqual(registry._validate_flags(flag), FLAGS.ONE | FLAGS.TWO)
:platform: Unix :copyright: (c) 2018 by Netflix Inc., see AUTHORS for more :license: Apache, see LICENSE for more details. .. moduleauthor:: Mike Grima <*****@*****.**> """ from cloudaux import get_iso_string from cloudaux.aws.iam import get_policy, get_managed_policy_document from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags from cloudaux.orchestration.aws import _conn_from_args from cloudaux.orchestration.aws import _get_name_from_structure from cloudaux.orchestration.aws.iam import MissingFieldException registry = FlagRegistry() FLAGS = Flags('BASE') @registry.register(flag=FLAGS.BASE) def get_base(managed_policy, **conn): """Fetch the base Managed Policy. This includes the base policy and the latest version document. :param managed_policy: :param conn: :return: """ managed_policy['_version'] = 1 arn = _get_name_from_structure(managed_policy, 'Arn')
""" from cloudaux import CloudAux, get_iso_string from cloudaux.aws.iam import get_account_authorization_details from cloudaux.aws.iam import get_user_inline_policies from cloudaux.aws.iam import get_user_access_keys from cloudaux.aws.iam import get_user_login_profile from cloudaux.aws.iam import get_user_managed_policies from cloudaux.aws.iam import get_user_mfa_devices from cloudaux.aws.iam import get_user_signing_certificates from cloudaux.orchestration.aws import _get_name_from_structure, _conn_from_args from cloudaux.orchestration import modify from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'ACCESS_KEYS', 'INLINE_POLICIES', 'MANAGED_POLICIES', 'MFA_DEVICES', 'LOGIN_PROFILE', 'SIGNING_CERTIFICATES') @registry.register(flag=FLAGS.ACCESS_KEYS, depends_on=FLAGS.BASE, key='access_keys') def get_access_keys(user, **conn): return get_user_access_keys(user, **conn) @registry.register(flag=FLAGS.INLINE_POLICIES, depends_on=FLAGS.BASE, key='inline_policies') def get_inline_policies(user, **conn): return get_user_inline_policies(user, **conn)
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more :license: Apache, see LICENSE for more details. .. moduleauthor:: Mike Grima <*****@*****.**> """ from botocore.exceptions import ClientError from cloudaux.aws.ec2 import describe_vpcs, describe_dhcp_options, describe_vpc_classic_link, \ describe_vpc_classic_link_dns_support, describe_internet_gateways, describe_vpc_peering_connections, \ describe_subnets, describe_route_tables, describe_network_acls, describe_vpc_attribute, describe_flow_logs from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags from cloudaux.exceptions import CloudAuxException registry = FlagRegistry() FLAGS = Flags('BASE', 'INTERNET_GATEWAY', 'CLASSIC_LINK', 'VPC_PEERING_CONNECTIONS', 'SUBNETS', 'ROUTE_TABLES', 'NETWORK_ACLS', 'FLOW_LOGS') @registry.register(flag=FLAGS.FLOW_LOGS, depends_on=FLAGS.BASE, key="flow_logs") def get_vpc_flow_logs(vpc, **conn): """Gets the VPC Flow Logs for a VPC""" fl_result = describe_flow_logs(Filters=[{"Name": "resource-id", "Values": [vpc["id"]]}], **conn) fl_ids = [] for fl in fl_result: fl_ids.append(fl["FlowLogId"]) return fl_ids @registry.register(flag=FLAGS.CLASSIC_LINK, depends_on=FLAGS.BASE, key="classic_link")
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more :license: Apache, see LICENSE for more details. .. moduleauthor:: Mike Grima <*****@*****.**> """ from cloudaux import get_iso_string from cloudaux.aws.iam import get_group as get_group_api, list_group_policies, get_group_policy_document, \ list_attached_group_managed_policies from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags from cloudaux.orchestration import modify from cloudaux.orchestration.aws import _conn_from_args from cloudaux.orchestration.aws.iam import MissingFieldException registry = FlagRegistry() FLAGS = Flags('BASE', 'INLINE_POLICIES', 'MANAGED_POLICIES', 'USERS') @registry.register(flag=FLAGS.INLINE_POLICIES, key='inline_policies') def get_inline_policies(group, **conn): """Get the inline policies for the group.""" policy_list = list_group_policies(group['GroupName']) policy_documents = {} for policy in policy_list: policy_documents[policy] = get_group_policy_document( group['GroupName'], policy, **conn) return policy_documents
from cloudaux import CloudAux from cloudaux.aws.iam import get_role_managed_policies, get_role_inline_policies, get_role_instance_profiles, \ get_account_authorization_details from cloudaux.orchestration.aws import _get_name_from_structure, _conn_from_args from cloudaux.orchestration import modify from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'MANAGED_POLICIES', 'INLINE_POLICIES', 'INSTANCE_PROFILES') @registry.register(flag=FLAGS.MANAGED_POLICIES, key='managed_policies') def get_managed_policies(role, **conn): return get_role_managed_policies(role, **conn) @registry.register(flag=FLAGS.INLINE_POLICIES, key='inline_policies') def get_inline_policies(role, **conn): return get_role_inline_policies(role, **conn) @registry.register(flag=FLAGS.INSTANCE_PROFILES, key='instance_profiles') def get_instance_profiles(role, **conn): return get_role_instance_profiles(role, **conn) @registry.register(flag=FLAGS.BASE) def _get_base(role, **conn): """
from cloudaux.aws.events import describe_rule from cloudaux.aws.events import list_targets_by_rule from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'DESCRIBE', 'TARGETS') @registry.register(flag=FLAGS.TARGETS, key='targets') def list_targets(rule, **conn): return list_targets_by_rule(Rule=rule['name'], **conn) @registry.register(flag=FLAGS.DESCRIBE) def get_rule_description(rule, **conn): rule = describe_rule(Name=rule['name'], **conn) rule_detail = None if rule.get('ScheduleExpression', None): rule_detail = rule['ScheduleExpression'] else: rule_detail = rule['EventPattern'] return { 'description': rule['Description'], 'state': rule['State'], 'rule': rule_detail }
from cloudaux.aws.glacier import describe_vault, get_vault_access_policy, list_tags_for_vault from cloudaux.decorators import modify_output from cloudaux.orchestration.aws.arn import ARN from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'POLICY', 'TAGS') @registry.register(flag=FLAGS.BASE) def _get_base(vault_obj, **conn): base_fields = ['VaultARN', 'VaultName', 'CreationDate', 'NumberOfArchives', 'SizeInBytes'] if not all(field in vault_obj for field in base_fields): vault_obj = describe_vault(vault_name=vault_obj['VaultName'], **conn) vault_obj['_version'] = 1 # sometimes it's expected that the item contains 'Arn' and not 'VaultARN' vault_obj['Arn'] = vault_obj['VaultARN'] return vault_obj @registry.register(flag=FLAGS.POLICY, key='Policy') def _get_vault_access_policy(vault_obj, **conn): return get_vault_access_policy(vault_name=vault_obj['VaultName'], **conn) @registry.register(flag=FLAGS.TAGS, key='Tags') def _list_tags_for_vault(vault_obj, **conn): return list_tags_for_vault(vault_name=vault_obj['VaultName'], **conn)
from cloudaux.aws.elb import * from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'ATTRIBUTES', 'TAGS', 'POLICIES', 'POLICY_TYPES') def _reformat_policy(policy): """ Policies returned from boto3 are massive, ugly, and difficult to read. This method flattens and reformats the policy. :param policy: Result from invoking describe_load_balancer_policies(...) :return: Returns a tuple containing policy_name and the reformatted policy dict. """ policy_name = policy['PolicyName'] ret = {} ret['type'] = policy['PolicyTypeName'] attrs = policy['PolicyAttributeDescriptions'] if ret['type'] != 'SSLNegotiationPolicyType': return policy_name, ret attributes = dict() for attr in attrs: attributes[attr['AttributeName']] = attr['AttributeValue'] ret['protocols'] = dict() ret['protocols']['sslv2'] = bool(attributes.get('Protocol-SSLv2')) ret['protocols']['sslv3'] = bool(attributes.get('Protocol-SSLv3'))
from cloudaux.aws.sqs import get_queue_url, get_queue_attributes, list_queue_tags, list_dead_letter_source_queues from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags import logging from cloudaux.orchestration.aws import ARN logger = logging.getLogger('cloudaux') registry = FlagRegistry() FLAGS = Flags('BASE', 'TAGS', 'DEAD_LETTER_SOURCE_QUEUES') @registry.register(flag=FLAGS.TAGS, key='tags') def get_sqs_tags(sqs_queue, **conn): return list_queue_tags(QueueUrl=sqs_queue["QueueUrl"], **conn) @registry.register(flag=FLAGS.DEAD_LETTER_SOURCE_QUEUES, key='dead_letter_source_queues') def get_dead_letter_queues(sqs_queue, **conn): return list_dead_letter_source_queues(QueueUrl=sqs_queue["QueueUrl"], **conn) @registry.register(flag=FLAGS.BASE) def get_base(sqs_queue, **conn): sqs_queue["Attributes"] = get_queue_attributes( QueueUrl=sqs_queue["QueueUrl"], AttributeNames=["All"], **conn)
from cloudaux.aws.lambda_function import * from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags import json from cloudaux.orchestration.aws import ARN registry = FlagRegistry() FLAGS = Flags('BASE', 'ALIASES', 'EVENT_SOURCE_MAPPINGS', 'VERSIONS', 'TAGS', 'POLICY') @registry.register(flag=FLAGS.POLICY, depends_on=FLAGS.VERSIONS, key='policy') def _get_policy(lambda_function, **conn): """Get LambdaFunction Policies. (there can be many of these!) Lambda Function Policies are overly complicated. They can be attached to a label, a version, and there is also a default policy. This method attempts to gather all three types. AWS returns an exception if the policy requested does not exist. We catch and ignore these exceptions. """ policies = dict(Versions=dict(), Aliases=dict(), DEFAULT=dict()) for version in [v['Version'] for v in lambda_function['versions']]: try: policies['Versions'][version] = get_policy(FunctionName=lambda_function['FunctionName'], Qualifier=version, **conn) policies['Versions'][version] = json.loads(policies['Versions'][version]) except Exception as e: pass
""" .. module: cloudaux.openstack.orchestration.security_group :platform: Unix :copyright: Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. See AUTHORS for more :license: Apache, see LICENSE for more details. .. moduleauthor:: Michael Stair <*****@*****.**> """ from cloudaux.openstack.utils import list_items from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('RULES', 'INSTANCES') @registry.register(flag=FLAGS.INSTANCES, depends_on=FLAGS.RULES, key='assigned_to') def get_instances(security_group, **kwargs): detail = kwargs.pop('instance_detail', 'FULL') kwargs['service'] = 'compute' kwargs['generator'] = 'servers' instances = list_items(**kwargs) sg_instances = {} for instance in instances: for group in instance.security_groups: if group['name'] not in sg_instances: sg_instances[group['name']] = [instance] else: sg_instances[group['name']].append(instance)
from cloudaux.aws.elbv2 import * from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'LISTENERS', 'RULES', 'ATTRIBUTES', 'TAGS', 'TARGET_GROUPS', 'TARGET_GROUP_ATTRIBUTES', 'TARGET_GROUP_HEALTH') @registry.register(flag=FLAGS.LISTENERS, depends_on=FLAGS.BASE, key='listeners') def get_listeners(alb, **conn): return describe_listeners(load_balancer_arn=alb['LoadBalancerArn'], **conn) @registry.register(flag=FLAGS.RULES, depends_on=FLAGS.LISTENERS, key='rules') def get_rules(alb, **conn): rules = list() for listener in alb['listeners']: rules.extend(describe_rules(listener_arn=listener['ListenerArn'], **conn)) return rules @registry.register(flag=FLAGS.ATTRIBUTES, depends_on=FLAGS.BASE, key='attributes') def get_attributes(alb, **conn): return describe_load_balancer_attributes(alb['LoadBalancerArn'], **conn) @registry.register(flag=FLAGS.TAGS, depends_on=FLAGS.BASE, key='tags') def get_tags(alb, **conn): return describe_tags([alb['LoadBalancerArn']], **conn)
def test_build_out(self): FLAGS = Flags("PEOPLE", "HOBBIES") registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key="people") def method_people(*args): return dict(simon="123", george="234") @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key="hobbies") def method_hobbies(result): hobbies = { "123": ["mountain biking", "skiing"], "234": ["snail collecting", "roaring like a dinosaur"], } return_value = dict() for person, uid in result["people"].items(): return_value[person] = hobbies.get(uid) return return_value # Simple example # - No dependencies # - Single Flag # - No starts_with dict # - Default pass_dictionary value (False) result = registry.build_out(FLAGS.PEOPLE) self.assertEqual(result, dict(people=dict(simon="123", george="234"))) # Only send the leaf node of a dependency chain. Rely on registry to calculate dependencies. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result = registry.build_out(FLAGS.HOBBIES) self.assertEqual( result, dict( people=dict(simon="123", george="234"), hobbies=dict( simon=["mountain biking", "skiing"], george=["snail collecting", "roaring like a dinosaur"], ), ), ) # Explicitly send all required methods in the dependency chain. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result_1 = registry.build_out(FLAGS.PEOPLE | FLAGS.HOBBIES) self.assertEqual(result, result_1) FLAGS = Flags("PETS", "FARM_ANIMALS", "WILD_ANIMALS") registry = FlagRegistry() @registry.register( flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=("pets", "farm", "wild"), ) def some_method(): return "cat", "pig", "rhino" # Multiple Return Value Flag Subset result_2 = registry.build_out(FLAGS.PETS | FLAGS.FARM_ANIMALS) self.assertEqual(set(result_2.keys()), set(["pets", "farm"])) FLAGS = Flags("ONE") registry = FlagRegistry() @registry.register(flag=FLAGS.ONE) def method_one(): return dict(tanya="redAlert") # Let the registry build our results dictionary. (Lacks start_with) # Use the default value for pass_dictionary (False) result_3 = registry.build_out(FLAGS.ONE) self.assertEqual(result_3, dict(tanya="redAlert")) # Pass in our own results dictionary (start_with=somedict) # Use the default value for pass_dictionary (False) somedict = dict(somekey="asdf", anotherkey="defg") result_4 = registry.build_out(FLAGS.ONE, start_with=somedict) self.assertEqual(set(result_4.keys()), set(["tanya", "somekey", "anotherkey"])) # Pass in our own results dictionary (somedict=somedict) # Set pass_dictionary to True FLAGS = Flags("WINNER") registry = FlagRegistry() @registry.register(flag=FLAGS.WINNER) def method_winner(data): return dict(winner=data["name"]) somedict = dict(name="george") result_4 = registry.build_out( FLAGS.WINNER, start_with=somedict, pass_datastructure=True ) self.assertEqual(result_4, dict(winner="george", name="george")) # Let the registry build our results dictionary. (Lacks start_with) # Set pass_datastructure to True FLAGS = Flags("Cookies") registry = FlagRegistry() @registry.register(flag=FLAGS.Cookies) def method_cookies(data): return dict(cookies_remaining=len(data.keys())) result_4 = registry.build_out(FLAGS.ALL, pass_datastructure=True) self.assertEqual(result_4, dict(cookies_remaining=0)) # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags("ACK") registry = FlagRegistry() @registry.register(flag=FLAGS.ACK, key="salutation") def some_salutation(data): return data["hello"] starting_dict = dict(hello="goodbye") result = registry.build_out( FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict ) self.assertEqual(result, dict(hello="goodbye", salutation="goodbye")) # Dependent Method # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags("PEOPLE", "HOBBIES") registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key="people") def method_people1(*args): return dict(simon="123", george="234") @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key="hobbies") def method_hobbies1(result): hobbies = { "123": ["mountain biking", "skiing"], "234": ["snail collecting", "roaring like a dinosaur"], } return_value = dict() for person, uid in result["people"].items(): return_value[person] = hobbies.get(uid) return return_value starting_dict = dict(hello="goodbye") result = registry.build_out( FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict ) self.assertEqual( result, dict( hello="goodbye", people=dict(simon="123", george="234"), hobbies=dict( simon=["mountain biking", "skiing"], george=["snail collecting", "roaring like a dinosaur"], ), ), )
from cloudaux.aws.s3 import get_bucket_resource from cloudaux.aws.s3 import list_bucket_analytics_configurations from cloudaux.aws.s3 import list_bucket_metrics_configurations from cloudaux.aws.s3 import list_bucket_inventory_configurations from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags from botocore.exceptions import ClientError import logging import json logger = logging.getLogger('cloudaux') registry = FlagRegistry() FLAGS = Flags('BASE', 'GRANTS', 'GRANT_REFERENCES', 'OWNER', 'LIFECYCLE', 'LOGGING', 'POLICY', 'TAGS', 'VERSIONING', 'WEBSITE', 'CORS', 'NOTIFICATIONS', 'ACCELERATION', 'REPLICATION', 'ANALYTICS', 'METRICS', 'INVENTORY', 'CREATED_DATE') @registry.register(flag=(FLAGS.GRANTS, FLAGS.GRANT_REFERENCES, FLAGS.OWNER), key=('grants', 'grant_references', 'owner')) def get_grants(bucket_name, include_owner=True, **conn): acl = get_bucket_acl(Bucket=bucket_name, **conn) grantees = {} grantee_ref = {} for grant in acl['Grants']: grantee = grant['Grantee'] display_name = grantee.get('DisplayName') if display_name == 'None' or display_name == 'null':
def test_build_out(self): FLAGS = Flags('PEOPLE', 'HOBBIES') registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key='people') def method_people(*args): return dict(simon='123', george='234') @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key='hobbies') def method_hobbies(result): hobbies = { '123': ['mountain biking', 'skiing'], '234': ['snail collecting', 'roaring like a dinosaur'] } return_value = dict() for person, uid in result['people'].items(): return_value[person] = hobbies.get(uid) return return_value # Simple example # - No dependencies # - Single Flag # - No starts_with dict # - Default pass_dictionary value (False) result = registry.build_out(FLAGS.PEOPLE) self.assertEqual(result, dict(people=dict(simon='123', george='234'))) # Only send the leaf node of a dependency chain. Rely on registry to calculate dependencies. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result = registry.build_out(FLAGS.HOBBIES) self.assertEqual( result, dict(people=dict(simon='123', george='234'), hobbies=dict( simon=['mountain biking', 'skiing'], george=['snail collecting', 'roaring like a dinosaur']))) # Explicitly send all required methods in the dependency chain. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result_1 = registry.build_out(FLAGS.PEOPLE | FLAGS.HOBBIES) self.assertEqual(result, result_1) FLAGS = Flags('PETS', 'FARM_ANIMALS', 'WILD_ANIMALS') registry = FlagRegistry() @registry.register(flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=('pets', 'farm', 'wild')) def some_method(): return 'cat', 'pig', 'rhino' # Multiple Return Value Flag Subset result_2 = registry.build_out(FLAGS.PETS | FLAGS.FARM_ANIMALS) self.assertEqual(set(result_2.keys()), set(['pets', 'farm'])) FLAGS = Flags('ONE') registry = FlagRegistry() @registry.register(flag=FLAGS.ONE) def method_one(): return dict(tanya='redAlert') # Let the registry build our results dictionary. (Lacks start_with) # Use the default value for pass_dictionary (False) result_3 = registry.build_out(FLAGS.ONE) self.assertEqual(result_3, dict(tanya='redAlert')) # Pass in our own results dictionary (start_with=somedict) # Use the default value for pass_dictionary (False) somedict = dict(somekey='asdf', anotherkey='defg') result_4 = registry.build_out(FLAGS.ONE, start_with=somedict) self.assertEqual(set(result_4.keys()), set(['tanya', 'somekey', 'anotherkey'])) # Pass in our own results dictionary (somedict=somedict) # Set pass_dictionary to True FLAGS = Flags('WINNER') registry = FlagRegistry() @registry.register(flag=FLAGS.WINNER) def method_winner(data): return dict(winner=data['name']) somedict = dict(name='george') result_4 = registry.build_out(FLAGS.WINNER, start_with=somedict, pass_datastructure=True) self.assertEqual(result_4, dict(winner='george', name='george')) # Let the registry build our results dictionary. (Lacks start_with) # Set pass_datastructure to True FLAGS = Flags('Cookies') registry = FlagRegistry() @registry.register(flag=FLAGS.Cookies) def method_cookies(data): return dict(cookies_remaining=len(data.keys())) result_4 = registry.build_out(FLAGS.ALL, pass_datastructure=True) self.assertEqual(result_4, dict(cookies_remaining=0)) # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags('ACK') registry = FlagRegistry() @registry.register(flag=FLAGS.ACK, key='salutation') def some_salutation(data): return data['hello'] starting_dict = dict(hello="goodbye") result = registry.build_out(FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict) self.assertEqual(result, dict(hello='goodbye', salutation='goodbye')) # Dependent Method # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags('PEOPLE', 'HOBBIES') registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key='people') def method_people1(*args): return dict(simon='123', george='234') @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key='hobbies') def method_hobbies1(result): hobbies = { '123': ['mountain biking', 'skiing'], '234': ['snail collecting', 'roaring like a dinosaur'] } return_value = dict() for person, uid in result['people'].items(): return_value[person] = hobbies.get(uid) return return_value starting_dict = dict(hello="goodbye") result = registry.build_out(FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict) self.assertEqual( result, dict(hello='goodbye', people=dict(simon='123', george='234'), hobbies=dict( simon=['mountain biking', 'skiing'], george=['snail collecting', 'roaring like a dinosaur'])))
from cloudaux.gcp.iam import get_iam_policy, get_serviceaccount, get_serviceaccount_keys from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'KEYS', 'POLICY') @registry.register(flag=FLAGS.KEYS, key='keys') def get_keys(service_account, **conn): return get_serviceaccount_keys(service_account=service_account, **conn) @registry.register(flag=FLAGS.POLICY, key='policy') def get_policy(service_account, **conn): return get_iam_policy(service_account=service_account, **conn) @registry.register(flag=FLAGS.BASE) def _get_base(service_account, **conn): sa = get_serviceaccount(service_account=service_account, **conn) sa['_version'] = 1 return sa @modify_output def get_serviceaccount_complete(service_account, flags=FLAGS.ALL, **conn): return registry.build_out(flags, service_account, **conn)
from cloudaux.aws.ec2 import describe_images from cloudaux.aws.ec2 import describe_image_attribute from cloudaux.decorators import modify_output from flagpole import FlagRegistry, Flags registry = FlagRegistry() FLAGS = Flags('BASE', 'KERNEL', 'RAMDISK', 'LAUNCHPERMISSION', 'PRODUCTCODES') @registry.register(flag=FLAGS.KERNEL) def get_kernel(image, **conn): attribute = describe_image_attribute( Attribute='kernel', ImageId=image['ImageId'], **conn) return dict(KernelId=attribute['KernelId']) @registry.register(flag=FLAGS.RAMDISK) def get_ramdisk(image, **conn): attribute = describe_image_attribute( Attribute='ramdisk', ImageId=image['ImageId'], **conn) return dict(RamdiskId=attribute['RamdiskId']) @registry.register(flag=FLAGS.LAUNCHPERMISSION) def get_launch_permission(image, **conn): attribute = describe_image_attribute( Attribute='launchPermission', ImageId=image['ImageId'], **conn) return dict(LaunchPermissions=attribute['LaunchPermissions']) @registry.register(flag=FLAGS.PRODUCTCODES)