util.py

The util.py file is sort of a junk drawer right now… but..

class botoform.util.BotoConnections(region_name=None, profile_name=None)

Central Management of boto3 client and resource connection objects.

azones

Return a list of available AZ names for active AWS profile/region.

profile_name
refresh_boto_connections()

Attach related Boto3 clients and resources.

region_name
setup_session_and_refresh_connections()
class botoform.util.BotoformDumper(stream, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None)

A custom YAML dumper that is pretty.

increase_indent(flow=False, indentless=False)
yaml_representers = {None: <unbound method SafeRepresenter.represent_undefined>, <type 'module'>: <unbound method Representer.represent_module>, <type 'classobj'>: <unbound method Representer.represent_name>, <type 'function'>: <unbound method Representer.represent_name>, <type 'builtin_function_or_method'>: <unbound method Representer.represent_name>, <type 'int'>: <unbound method SafeRepresenter.represent_int>, <type 'long'>: <unbound method Representer.represent_long>, <type 'float'>: <unbound method SafeRepresenter.represent_float>, <type 'complex'>: <unbound method Representer.represent_complex>, <type 'unicode'>: <unbound method SafeRepresenter.represent_unicode>, <type 'set'>: <unbound method SafeRepresenter.represent_set>, <type 'NoneType'>: <unbound method SafeRepresenter.represent_none>, <type 'bool'>: <unbound method SafeRepresenter.represent_bool>, <type 'type'>: <unbound method Representer.represent_name>, <type 'tuple'>: <unbound method SafeRepresenter.represent_list>, <type 'dict'>: <unbound method SafeRepresenter.represent_dict>, <type 'str'>: <unbound method SafeRepresenter.represent_str>, <type 'list'>: <unbound method SafeRepresenter.represent_list>, <type 'datetime.datetime'>: <unbound method SafeRepresenter.represent_datetime>, <type 'datetime.date'>: <unbound method SafeRepresenter.represent_date>}
class botoform.util.Log(desired_level='debug', syslog=True, stdout=True, program='botoform')

Handles emitting logs to syslog and stdout.

emit(message, level='info')

Emit message if level meets requirement.

message:
Any object that has a __str__ method.
level:
The level or severity of this message (default info)
levels

Return a list of levels on and beyond desired_level.

botoform.util.collection_len(collection)
botoform.util.collection_to_list(collection)
botoform.util.dict_to_key_value(data, sep='=', pair_sep=', ')

Return a string representation of a dictionary.

by default this function will turn:

{'key1':'value1','key2':'value2'}

into:

key1=value1,key2=value2
Parameters:
  • data – The dictionary to convert into a string.
  • sep – Optional, string to separate keys and values (Default ‘=’)
  • pair_sep – Optional, string to separate key/value pairs (Default ‘,’)
Returns:

a string representation of the given dictionary.

botoform.util.generate_password(size=9, pool=None)

Return a system generated password.

Parameters:
  • size – The desired length of the password to generate. (Default 9)
  • pool – Pool of chars to choose from. (Default digits and letters [upper/lower])
Returns:

String (raw password)

botoform.util.get_block_device_map_from_role_config(role_cfg)

accept role config data and return a Boto3 friendly BlockDeviceMappings.

botoform.util.get_ids(objects)

Return a list of ids from a list of objects.

Parameters:objects – A list of objects all of whom have an id attribute.
Returns:A list of ids
botoform.util.get_port_range(raw_range, ip_protocol='tcp')

Returns a (from_port, to_port) tuple.

Examples:

>>> get_port_range(443)
(443, 443)

>>> get_port_range('all')
(1, 65535)

>>> get_port_range('5000-5009')
(5000, 5009)

>>> get_port_range(' 8080')
(8080, 8080)

>>> get_port_range('tacobell', ip_protocol='icmp')
(-1, -1)
Parameters:
  • raw_range – A string or integer.
  • ip_protocol – Optional, ‘tcp’, ‘udp’, ‘icpm’ (Default ‘tcp’)
Returns:

(from_port, to_port)

botoform.util.id_to_human(id_string)

Turn an id into a human readable hash digest.

Parameters:id_string – The subject string to generate a human hash of.
>>> id_to_human('i-ceebb70c')
'friendisland'
botoform.util.key_value_to_dict(key_value_list, sep='=', pair_sep=', ')

Return a dictionary from a list of key/value strings.

turns key_value_list, like:

key_value_list = ['a=1,b=2', 'c=3, d=4', 'e=5']

into a dict, like:

{'a':'1', 'b':'2', 'c':'3', 'd':'4', 'e':'5'}
Parameters:
  • key_value_list – The list of key/value strings to convert into a dict.
  • sep – Optional, string which separates keys and values (Default ‘=’)
  • pair_sep – Optional, string which separates key/value pairs (Default ‘,’)
Returns:

A string representation of the given dictionary.

botoform.util.make_filter(key, values)

Return a filter document expected by Boto3.

Parameters:
  • key – The key name for this new filter document.
  • values – A value or a list of values to filter/match on.
Returns:

A filter document (list/dict) in the form that Boto3 expects.

botoform.util.make_tag_dict(ec2_object)

Return a dictionary of existing tags.

Parameters:ec2_object – A tagable Boto3 object with a tags attribute.
Returns:A dictionary where tag names are keys and tag values are values.
botoform.util.map_filter_false(function, items)

Works like map but automatically filters out untruthy items.

>>> map_filter_false(lambda i : i, [1,2,None,3,False,0,4])
[1, 2, 3, 4]
Parameters:
  • function – the function to map items through
  • items – a list of items to map through the function
Returns:

list

botoform.util.merge_pages(key, pages)

Merge boto3 paginated results into single list.

Parameters:
  • key – The document key to merge from all pages.
  • pages – An iterator of page documents.
Returns:

A single flat list containing results of all pages.

botoform.util.normalize_sg_port(sg_rule_tuple)

accept a security group rule tuple, return normalized port range.

botoform.util.normalize_sg_rules(sg_rules)

accept a list of security group rule tuples, return list with normalized port ranges.

botoform.util.output_formatter(data, output_format='newline')

Print data in the optional output_format.

botoform.util.reflect_attrs(child, parent, skip_attrs=None)

Composition Magic: reflect all missing parents attributes into child.

Parameters:
  • child – Object to receive attributes.
  • parent – Object to source attributes from.
  • skip_attrs – Optional list of attrs strings to not reflect.
Returns:

None

botoform.util.snake_to_camel_case(name, answers=None)

Accept a snake_case string and return a CamelCase string.

For example:

>>> snake_to_camel_case('cidr_block')
'CidrBlock'
botoform.util.tag_filter(tag_key, tag_values)

Return a tag filter document expected by boto3.

Parameters:
  • tag_key – A tag key or name.
  • tag_values – The tag value or a list of values to filter on.
Returns:

A filter document (list/dict) in the form that Boto3 expects.

botoform.util.update_tags(*args, **kw)

Add or update tags to reflect given keyword args

Parameters:
  • ec2_object – A tagable Boto3 object with a tags attribute.
  • **kwargs – key=value where key is tag name, value is tag value.
Returns:

None

botoform.util.write_private_key(key_pair)

Write private key to filesystem.

Parameters:key_pair – The Boto3 KeyPair object to write to filesystem.
Returns:None