Beispiel #1
0
def oo_image_tag_to_rpm_version(version, include_dash=False):
    """ Convert an image tag string to an RPM version if necessary
        Empty strings and strings that are already in rpm version format
        are ignored. Also remove non semantic version components.

        Ex. v3.2.0.10 -> -3.2.0.10
            v1.2.0-rc1 -> -1.2.0
    """
    if not isinstance(version, string_types):
        raise errors.AnsibleFilterError("|failed expects a string or unicode")
    if version.startswith("v"):
        version = version[1:]
        # Strip release from requested version, we no longer support this.
        version = version.split('-')[0]

    if include_dash and version and not version.startswith("-"):
        version = "-" + version

    return version
Beispiel #2
0
def lib_utils_oo_dict_to_keqv_list(data):
    """Take a dict and return a list of k=v pairs

        Input data:
        {'a': 1, 'b': 2}

        Return data:
        ['a=1', 'b=2']
    """
    if not isinstance(data, dict):
        try:
            # This will attempt to convert something that looks like a string
            # representation of a dictionary (including json) into a dictionary.
            data = ast.literal_eval(data)
        except ValueError:
            msg = "|failed expects first param is a dict. Got {}. Type: {}"
            msg = msg.format(str(data), str(type(data)))
            raise errors.AnsibleFilterError(msg)
    return ['='.join(str(e) for e in x) for x in data.items()]
Beispiel #3
0
    def nested_lists(self, data):
        """
        It takes the input list and returns a list pairs:
        [{type, private_ip}]
        """
        if not isinstance(data, list):
            raise errors.AnsibleFilterError("A list is expected")

        retval = []
        for external in data:
            for internal in external['instances']:
                retval.append({
                    'type': internal['tags']['type'],
                    'private_ip': internal['private_ip'],
                    'instance_id': internal['id'],
                    'sg_id': internal['groups'].keys()[0]
                })

        return retval
def merge_array_list(arg):
    """
        Merge multiple arrays into a single array
        :param arg: lists
        :type arg: list
        :return: The final array
        :rtype: list
    """

    # Check if arg is a list
    if type(arg) != list:
        raise errors.AnsibleFilterError('Invalid value type, should be array')

    final_list = []

    for cur_list in arg:
        final_list += cur_list

    return final_list
Beispiel #5
0
def rekey_on_member(data, key, duplicates='error'):
    """
    Rekey a dict of dicts on another member

    May also create a dict from a list of dicts.

    duplicates can be one of ``error`` or ``overwrite`` to specify whether to error out if the key
    value would be duplicated or to overwrite previous entries if that's the case.
    """
    if duplicates not in ('error', 'overwrite'):
        raise errors.AnsibleFilterError(
            "duplicates parameter to rekey_on_member has unknown "
            "value: {0}".format(duplicates))

    new_obj = {}

    if isinstance(data, collections.Mapping):
        iterate_over = data.values()
    elif isinstance(data, collections.Iterable) and not isinstance(
            data, (text_type, binary_type)):
        iterate_over = data
    else:
        raise errors.AnsibleFilterError(
            "Type is not a valid list, set, or dict")

    for item in iterate_over:
        if not isinstance(item, collections.Mapping):
            raise errors.AnsibleFilterError("List item is not a valid dict")

        try:
            key_elem = item[key]
        except KeyError:
            raise errors.AnsibleFilterError(
                "Key {0} was not found".format(key))
        except Exception as e:
            raise errors.AnsibleFilterError(to_native(e))

        # Note: if new_obj[key_elem] exists it will always be a non-empty dict (it will at
        # minimun contain {key: key_elem}
        if new_obj.get(key_elem, None):
            if duplicates == 'error':
                raise errors.AnsibleFilterError(
                    "Key {0} is not unique, cannot correctly turn into "
                    "dict".format(key_elem))
            elif duplicates == 'overwrite':
                new_obj[key_elem] = item
        else:
            new_obj[key_elem] = item

    return new_obj
Beispiel #6
0
def version_compare(value, version, operator='eq', strict=None, version_type=None):
    ''' Perform a version comparison on a value '''
    op_map = {
        '==': 'eq', '=': 'eq', 'eq': 'eq',
        '<': 'lt', 'lt': 'lt',
        '<=': 'le', 'le': 'le',
        '>': 'gt', 'gt': 'gt',
        '>=': 'ge', 'ge': 'ge',
        '!=': 'ne', '<>': 'ne', 'ne': 'ne'
    }

    type_map = {
        'loose': LooseVersion,
        'strict': StrictVersion,
        'semver': SemanticVersion,
        'semantic': SemanticVersion,
    }

    if strict is not None and version_type is not None:
        raise errors.AnsibleFilterError("Cannot specify both 'strict' and 'version_type'")

    if not value:
        raise errors.AnsibleFilterError("Input version value cannot be empty")

    if not version:
        raise errors.AnsibleFilterError("Version parameter to compare against cannot be empty")

    Version = LooseVersion
    if strict:
        Version = StrictVersion
    elif version_type:
        try:
            Version = type_map[version_type]
        except KeyError:
            raise errors.AnsibleFilterError(
                "Invalid version type (%s). Must be one of %s" % (version_type, ', '.join(map(repr, type_map)))
            )

    if operator in op_map:
        operator = op_map[operator]
    else:
        raise errors.AnsibleFilterError(
            'Invalid operator type (%s). Must be one of %s' % (operator, ', '.join(map(repr, op_map)))
        )

    try:
        method = getattr(py_operator, operator)
        return method(Version(to_text(value)), Version(to_text(version)))
    except Exception as e:
        raise errors.AnsibleFilterError('Version comparison failed: %s' % to_native(e))
Beispiel #7
0
def net_bridge_obj(context, name, inventory_hostname=None):
    """Return a dict representation of a network bridge interface.

    The returned dict is compatible with the interfaces_bridge_interfaces
    variable in the MichaelRigaert.interfaces role.
    """
    device = net_interface(context, name, inventory_hostname)
    if not device:
        raise errors.AnsibleFilterError(
            "Network interface for network '%s' on host '%s' not found" %
            (name, inventory_hostname))
    ip = net_ip(context, name, inventory_hostname)
    if ip is None:
        ip = '0.0.0.0'
    cidr = net_cidr(context, name, inventory_hostname)
    netmask = net_mask(context, name, inventory_hostname)
    gateway = net_gateway(context, name, inventory_hostname)
    vlan = net_vlan(context, name, inventory_hostname)
    mtu = net_mtu(context, name, inventory_hostname)
    ports = net_bridge_ports(context, name, inventory_hostname)
    routes = net_routes(context, name, inventory_hostname)
    if routes:
        routes = [_route_obj(route) for route in routes]
    rules = net_rules(context, name, inventory_hostname)
    bootproto = net_bootproto(context, name, inventory_hostname)
    defroute = net_defroute(context, name, inventory_hostname)
    interface = {
        'device': device,
        'address': ip,
        'netmask': netmask,
        'gateway': gateway,
        'vlan': vlan,
        'mtu': mtu,
        'ports': ports,
        'route': routes,
        'rules': rules,
        'bootproto': bootproto or 'static',
        'defroute': defroute,
        'onboot': 'yes',
    }
    interface = {k: v for k, v in interface.items() if v is not None}
    return interface
Beispiel #8
0
def encode_nginx(data, indent="  ", level=0, block_semicolon=False):
    """Convert Python data structure to Nginx format."""

    # Return value
    rv = ""
    # Indicates the item type [section|line]
    item_type = ""

    for item in data:
        if isinstance(item, dict):
            # Section
            if item_type in ('section', 'line'):
                rv += "\n"

            rv += "%s%s {\n" % (level * indent, item.keys()[0])
            rv += encode_nginx(item.values()[0],
                               level=level + 1,
                               block_semicolon=block_semicolon)
            rv += "%s}%s\n" % (level * indent, ';' if block_semicolon else '')

            item_type = 'section'

        elif isinstance(item, basestring):
            # Normal line
            if item_type == 'section':
                rv += "\n"

            item_type = 'line'

            rv += "%s%s" % (level * indent, item)

            # Do not finish comments with semicolon
            if item.startswith("# "):
                rv += "\n"
            else:
                rv += ";\n"

        else:
            raise errors.AnsibleFilterError("Unexpected data type: %s" %
                                            (type(item)))

    return rv
def construct_var(variable):
    base_path = '/etc/'
    file = '/home/davis/Documents/networkaut/disp.json'
    var = {}
    olddict = {}
    newdict = {}
    innerlist = []
    varlist = getvars_file('show interfaces', base_path + 'disp.json')
    try:
        for ele in varlist:
            var[ele] = variable[ele]
        innerlist = [var]
    except:
        raise errors.AnsibleFilterError('error in construct_var')


#        return "error in construct_var"
#    var={}

    return var
Beispiel #10
0
def _net_interface_type(context, name, inventory_hostname):
    """Return a string describing the network interface type.

    Possible types include 'ether', 'bridge', 'bond'.
    """
    bridge_ports = net_bridge_ports(context, name, inventory_hostname)
    bond_slaves = net_bond_slaves(context, name, inventory_hostname)
    if bridge_ports is not None and bond_slaves is not None:
        raise errors.AnsibleFilterError(
            "Network %s on host %s has both bridge ports and bond slaves "
            "defined" %
            (name,
             utils.get_hostvar(context, 'inventory_hostname',
                               inventory_hostname)))
    if bridge_ports is None and bond_slaves is None:
        return 'ether'
    if bridge_ports is not None:
        return 'bridge'
    if bond_slaves is not None:
        return 'bond'
Beispiel #11
0
def lib_utils_oo_dict_to_list_of_dict(data, key_title='key', value_title='value'):
    """Take a dict and arrange them as a list of dicts

       Input data:
       {'region': 'infra', 'test_k': 'test_v'}

       Return data:
       [{'key': 'region', 'value': 'infra'}, {'key': 'test_k', 'value': 'test_v'}]

       Written for use of the oc_label module
    """
    if not isinstance(data, dict):
        # pylint: disable=line-too-long
        raise errors.AnsibleFilterError("|failed expects first param is a dict. Got %s. Type: %s" % (str(data), str(type(data))))

    rval = []
    for label in data.items():
        rval.append({key_title: label[0], value_title: label[1]})

    return rval
Beispiel #12
0
def get_netorigin(url):
    """Return the netloc from a URL.

    If the input value is not a value URL the method will raise an Ansible
    filter exception.

    :param url: the URL to parse
    :type url: ``str``
    :returns: ``str``
    """
    try:
        parsed_url = urlparse.urlparse(url)
        netloc = parsed_url.netloc
        scheme = parsed_url.scheme
    except Exception as exp:
        raise errors.AnsibleFilterError(
            'Failed to return the netorigin of: "%s"' % str(exp)
        )
    else:
        return '%s://%s' % (scheme, netloc)
Beispiel #13
0
def expand_config(config_data):
    try:
        all_data = copy.deepcopy(config_data)
        top_level_categories = all_data.keys()

        # iterate over all data, looking for dictionaries
        # expand each dictionary when found
        for key, value in all_data.items():
            if isinstance(value, dict):
                expand_object(value, top_level_categories, all_data)
            elif isinstance(value, list):
                for item in value:
                    expand_object(item, top_level_categories, all_data)
            else:
                continue
        return all_data
    except Exception, e:
        raise errors.AnsibleFilterError(
            'expand_config plugin error: {0}, config_data={1}'.format(
                str(e), str(config_data)))
Beispiel #14
0
def net_configdrive_network_device(context, name, inventory_hostname=None):
    device = net_interface(context, name, inventory_hostname)
    if not device:
        raise errors.AnsibleFilterError(
            "Network interface for network '%s' on host '%s' not found" %
            (name, inventory_hostname))
    ip = net_ip(context, name, inventory_hostname)
    cidr = net_cidr(context, name, inventory_hostname)
    netmask = net_mask(context, name, inventory_hostname)
    gateway = net_gateway(context, name, inventory_hostname)
    bootproto = 'static' if ip is not None else 'dhcp'
    interface = {
        'device': device,
        'address': ip,
        'netmask': netmask,
        'gateway': gateway,
        'bootproto': bootproto,
    }
    interface = {k: v for k, v in interface.items() if v is not None}
    return interface
def _initial_cluster_lookup_short(_list, category='devops', deployment='local', port=2380):
    '''
    Return a comma (no spaces!) separated list of Consul initial cluster
    members. The "no spaces" is because this is used as a single command line
    argument.

    This filter only works if DNS resolution is available. To prevent templating
    errors, such a failure returns a null string. Calling programs can and should
    check for this error before attempting to use the results.

    a = ['node01','node02','node03']
    _initial_cluster_lookup_short(a)
    'node01=http://192.168.56.21:2380,node02=http://192.168.56.22:2380,node03=http://192.168.56.23:2380'

    $ ansible -i inventory/ -m debug -a msg="{{ groups.consul|initial_cluster() }}" node01.devops.local
    node01.devops.local | SUCCESS => {
      "changed": false,
      "msg": "node03=http://node03.devops.local:2380,node02=http://node02.devops.local:2380,node01=http://node01.devops.local:2380"
    }

    '''

    if type(_list) == type([]):
        try:
            return ','.join(
                ['{0}=http://{1}:{2}'.format(
                    i.decode('utf-8'),
                    socket.gethostbyname('{0}.{1}.{2}'.format(
                        i.decode('utf-8'),
                        category,
                        deployment)
                    ),
                    port) for i in _list]
            )
        except Exception as e:
            #raise errors.AnsibleFilterError(
            #    'initial_cluster() could not perform lookups: {0}'.format(str(e))
            #)
            return ''
    else:
        raise errors.AnsibleFilterError('Unrecognized input arguments to initial_cluster()')
def get_instances_tag_name_by_tags(region=None,
                          state=None, profile=None, **tags):

    """Retrieve instances tag Name by 1 or multiple tags.

    """
    filters = list()
    instances = list()
    client = aws_client(region, 'ec2', profile)
    for key, val in tags.items():
        filters.append(
            {
                'Name': "tag:{0}".format(key),
                'Values': [val]
            }
        )

    if state:
        filters.append(
            {
                'Name': "instance-state-name",
                'Values': [state]
            }
        )
    try:
        reservations = client.describe_instances(Filters=filters)\
                ['Reservations']
        for reservation in reservations:
            for instance in reservation['Instances']:
                for tag in instance['Tags']:
                    if tag['Key'] == 'Name':
                        instances.append(tag['Value'])

        if not reservations:
            raise errors.AnsibleFilterError(
                "No instance was found with the following tags {0} in region {1}" .format(
                    tags, region))
        else:
            return instances
    except Exception as e:
        raise e
Beispiel #17
0
    def oo_image_tag_to_rpm_version(version, include_dash=False):
        """ Convert an image tag string to an RPM version if necessary
            Empty strings and strings that are already in rpm version format
            are ignored. Also remove non semantic version components.

            Ex. v3.2.0.10 -> -3.2.0.10
                v1.2.0-rc1 -> -1.2.0
        """
        if not isinstance(version, basestring):
            raise errors.AnsibleFilterError(
                "|failed expects a string or unicode")
        # TODO: Do we need to make this actually convert v1.2.0-rc1 into 1.2.0-0.rc1
        # We'd need to be really strict about how we build the RPM Version+Release
        if version.startswith("v"):
            version = version.replace("v", "")
            version = version.split('-')[0]

            if include_dash:
                version = "-" + version

        return version
Beispiel #18
0
def get_attr(data, attribute=None):
    """ This looks up dictionary attributes of the form a.b.c and returns
        the value.

        If the key isn't present, None is returned.
        Ex: data = {'a': {'b': {'c': 5}}}
            attribute = "a.b.c"
            returns 5
    """
    if not attribute:
        raise errors.AnsibleFilterError("|failed expects attribute to be set")

    ptr = data
    for attr in attribute.split('.'):
        if attr in ptr:
            ptr = ptr[attr]
        else:
            ptr = None
            break

    return ptr
 def certificates_to_synchronize(hostvars):
     ''' Return certificates to synchronize based on facts. '''
     if not issubclass(type(hostvars), dict):
         raise errors.AnsibleFilterError(
             "|failed expects hostvars is a dict")
     certs = [
         'admin.crt', 'admin.key', 'admin.kubeconfig',
         'master.kubelet-client.crt', 'master.kubelet-client.key',
         'openshift-registry.crt', 'openshift-registry.key',
         'openshift-registry.kubeconfig', 'openshift-router.crt',
         'openshift-router.key', 'openshift-router.kubeconfig',
         'serviceaccounts.private.key', 'serviceaccounts.public.key'
     ]
     if bool(hostvars['openshift']['common']['version_gte_3_1_or_1_1']):
         certs += ['master.proxy-client.crt', 'master.proxy-client.key']
     if not bool(hostvars['openshift']['common']['version_gte_3_2_or_1_2']):
         certs += [
             'openshift-master.crt', 'openshift-master.key',
             'openshift-master.kubeconfig'
         ]
     return certs
def get_elasticache_endpoint(region, name, profile=None):
    """Retrieve the endpoint name of the elasticache cluster.
    Args:
        region (str): The AWS region.
        name (str): The name of the elasticache cluster.

    Basic Usage:
        >>> endpoint = get_elasticache_endpoint('us-west-2', 'test')
        dns_name
    """
    client = aws_client(region, 'elasticache', profile)
    try:
        return client.describe_cache_clusters(
            CacheClusterId=name
        )['CacheClusters'][0]['ConfigurationEndpoint']['Address']
    except Exception as e:
        if isinstance(e, botocore.exceptions.ClientError):
            raise e
        else:
            raise errors.AnsibleFilterError(
                'Could not retreive ip for {0}: {1}'.format(name, str(e)))
Beispiel #21
0
def isatty(devices):

    ret_list = []

    for device in devices:
        devpath = "/dev/tty" + device

        if not os.path.exists(devpath):
            return None

        try:
            output = subprocess.check_output(
                ['/sbin/udevadm', 'info', '-x', devpath])
        except Exception as e:
            raise errors.AnsibleFilterError("Failed to open device %s, %s" %
                                            (devpath, str(e)))

        if "/devices/pnp" in output:
            ret_list.append(device)

    return ret_list
def get_vpc_id_by_name(name, region, profile=None):
    """
    Args:
        name (str): The name of the vpc you are retrieving the id for.
        region (str): The AWS region.

    Basic Usage:
        >>> vpc_name = 'test'
        >>> aws_region = 'us-west-2'
        >>> vpc_id = get_vpc_id_by_name(vpc_name, aws_region)
        'vpc-1234567'

    Returns:
        VPC ID
    """
    client = aws_client(region, 'ec2', profile)
    params = {
        "Filters": [
            {
                "Name": "tag-key",
                "Values": ["Name"]
            },
            {
                "Name": "tag-value",
                "Values": [name]
            }
        ]
    }
    try:
        vpc_id = client.describe_vpcs(**params)['Vpcs'][0]['VpcId']
        return vpc_id

    except Exception as e:
        if isinstance(e, botocore.exceptions.ClientError):
            raise(e)
        else:
            raise errors.AnsibleFilterError(
                "VPC ID for VPC name {0} was not found in region {1}: {2}"
                .format(name, region, str(e))
            )
Beispiel #23
0
def get_encrypted_password(password, hashtype='sha512', salt=None):

    # TODO: find a way to construct dynamically from system
    cryptmethod = {
        'md5': '1',
        'blowfish': '2a',
        'sha256': '5',
        'sha512': '6',
    }

    if hashtype in cryptmethod:
        if salt is None:
            r = SystemRandom()
            if hashtype in ['md5']:
                saltsize = 8
            else:
                saltsize = 16
            salt = ''.join([
                r.choice(string.ascii_letters + string.digits)
                for _ in range(saltsize)
            ])

        if not HAS_PASSLIB:
            if sys.platform.startswith('darwin'):
                raise errors.AnsibleFilterError(
                    '|password_hash requires the passlib python module to generate password hashes on Mac OS X/Darwin'
                )
            saltstring = "$%s$%s" % (cryptmethod[hashtype], salt)
            encrypted = crypt.crypt(password, saltstring)
        else:
            if hashtype == 'blowfish':
                cls = passlib.hash.bcrypt
            else:
                cls = getattr(passlib.hash, '%s_crypt' % hashtype)

            encrypted = cls.encrypt(password, salt=salt)

        return encrypted

    return None
Beispiel #24
0
    def oo_openshift_env(hostvars):
        ''' Return facts which begin with "openshift_" and translate
            legacy facts to their openshift_env counterparts.

            Ex: hostvars = {'openshift_fact': 42,
                            'theyre_taking_the_hobbits_to': 'isengard'}
                returns  = {'openshift_fact': 42}
        '''
        if not issubclass(type(hostvars), dict):
            raise errors.AnsibleFilterError("|failed expects hostvars is a dict")

        facts = {}
        regex = re.compile('^openshift_.*')
        for key in hostvars:
            if regex.match(key):
                facts[key] = hostvars[key]

        migrations = {'openshift_router_selector': 'openshift_hosted_router_selector'}
        for old_fact, new_fact in migrations.iteritems():
            if old_fact in facts and new_fact not in facts:
                facts[new_fact] = facts[old_fact]
        return facts
def get_subnet_ids(vpc_id, cidrs=None, region=None, profile=None):
    """
    Args:
        vpc_id (str): The vpc id in which the subnet you are looking
            for lives in.
    Kwargs:
        cidrs (list): The list of cidrs that you are performing the search on.
        region (str): The AWS region.

    Basic Usage:
        >>> cidrs = ['10.100.10.0/24', '10.100.12.0/24', '10.100.11.0/24']
        >>> vpc_id = 'vpc-123456'
        >>> aws_region = 'us-west-2'
        >>> get_subnet_ids(vpc_id, cidrs, aws_region)
        [u'subnet-123456', u'subnet-765432', u'subnet-123456']

    Returns:
        List of subnet ids
    """
    subnet_ids = list()
    client = aws_client(region, 'ec2', profile)
    params = {
        'Filters': [{
            'Name': 'vpc-id',
            'Values': [vpc_id],
        }]
    }
    if cidrs:
        params['Filters'].append({
            'Name': 'cidrBlock',
            'Values': cidrs,
        })
    subnets = (sorted(client.describe_subnets(**params)['Subnets'],
                      key=lambda subnet: subnet['AvailabilityZone']))
    if subnets:
        subnet_ids = map(lambda subnet: subnet['SubnetId'], subnets)
        return subnet_ids
    else:
        raise errors.AnsibleFilterError("No subnets were found")
def get_all_route_table_ids(region, profile=None):
    """
    Args:
        vpc_id (str): The vpc you want to exclude routes from.

    Basic Usage:
        >>> get_all_route_table_ids("us-west-2")
        ['rtb-1234567']

    Returns:
        List of route table ids
    """
    route_ids = list()
    client = aws_client(region, 'ec2', profile)
    params = {'Filters': [{'Name': 'association.main', 'Values': ['false']}]}
    routes = client.describe_route_tables(**params)
    if routes:
        for route in routes['RouteTables']:
            route_ids.append(route['RouteTableId'])
        return route_ids
    else:
        raise errors.AnsibleFilterError("No routes were found")
def get_account_id(region, profile=None):
    """ Retrieve the AWS account id.
    Args:
        region (str): The AWS region.

    Basic Usage:
        >>> region = 'us-west-2'
        >>> account_id = get_account_id(region)
        12345667899

    Returns:
        String
    """
    client = aws_client(region, 'iam', profile)
    try:
        account_id = client.list_users()['Users'][0]['Arn'].split(':')[4]
        return account_id
    except Exception as e:
        if isinstance(e, botocore.exceptions.ClientError):
            raise e
        else:
            raise errors.AnsibleFilterError("Failed to retrieve account id")
def get_redshift_endpoint(region, name, profile=None):
    """Retrieve the endpoint name of the redshift cluster.
    Args:
        region (str): The AWS region.
        name (str): The name of the redshift cluster.

    Basic Usage:
        >>> import boto3
        >>> redshift = boto3.client('redshift', 'us-west-2')
        >>> endpoint = get_redshift_endpoint(region, 'test')
        dns_name
    """
    client = aws_client(region, 'redshift', profile)
    try:
        return client.describe_clusters(
            ClusterIdentifier=name)['Clusters'][0]['Endpoint']['Address']
    except Exception as e:
        if isinstance(e, botocore.exceptions.ClientError):
            raise e
        else:
            raise errors.AnsibleFilterError(
                'Could not retreive ip for {0}: {1}'.format(name, str(e)))
def get_sqs(name, key='arn', region=None, profile=None):
    """ Retrieve the arn or url a SQS Queue.
    Args:
        name: (str): The SQS name.

    Kwargs:
        key (str): The key you want returned from the SQS Queue (url or arn)
            default=arn
        region (str): The AWS region.
        profile (str): The ~/.aws/credentials profile name to use

    Basic Usage:
        >>> arn = get_sqs('test', region='us-west-2')
        'arn:aws:sqs:us-west-2:12345678910:test'

    Returns:
        String
    """
    client = aws_client(region, 'sqs', profile)
    try:
        url = client.get_queue_url(QueueName=name)['QueueUrl']
        if key == 'arn':
            attributes = (
                client.get_queue_attributes(
                    QueueUrl=url, AttributeNames=['QueueArn']
                )['Attributes']
            )
            return_key = attributes['QueueArn']
        else:
            return_key = url
    except Exception as e:
        if isinstance(e, botocore.exceptions.ClientError):
            raise e
        else:
            raise errors.AnsibleFilterError(
                "SQS Queue {0} was not found".format(name)
            )

    return return_key
def reverse_www(value):
    ''' Add or remove www subdomain '''

    # Check if value is a list and parse each item
    if isinstance(value, (list, tuple, types.GeneratorType)):
        values = []
        for item in value:
            values.append(reverse_www(item))
        return values

    # Add or remove www
    elif isinstance(value, string_types):
        if value.startswith('www.'):
            return value[4:]
        else:
            return 'www.{0}'.format(value)

    # Handle invalid input type
    else:
        raise errors.AnsibleFilterError(
            'The reverse_www filter expects a string or list of strings, got '
            + repr(value))