Exemplo n.º 1
0
def location_helper(name, search_only=False):
    """
    Location finder by name. If location doesn't exist, create it
    and return the href

    :param str,Element name: location to resolve. If the location is
        by name, it will be retrieved or created, if href, returned
        and if Location, href returned. If None, settings an elements
        location to None will set it to the Default location.
    :param bool search_only: only search for the location, if not found
        do not create
    :return str href: href of location if search_only is not False
    :rtype: str or None
    """
    try:
        return name.href
    except AttributeError:
        if name and name.startswith('http'):
            return name
    except ElementNotFound:
        return Location.create(name=name.name).href if not \
            search_only else None

    # Get all locations; tmp to support earlier 6.x versions.
    if name is not None:
        locations = [
            location for location in Search.objects.entry_point('location')
            if location.name == name
        ]
        if not locations:
            return Location.create(name=name).href if not search_only \
                else None
        return locations[0].href
Exemplo n.º 2
0
def location_helper(name):
    """
    Location finder by name. If location doesn't exist, create it
    and return the href

    :param str,Element name: location to resolve. If the location is
        by name, it will be retrieved or created, if href, returned
        and if Location, href returned. If None, settings an elements
        location to None will set it to the Default location.
    :return str href: href of location
    """
    try:
        return name.href
    except AttributeError:
        if name and name.startswith('http'):
            return name
    except ElementNotFound:
        return Location.get_or_create(name=name.name).href

    # Get all locations; tmp to support earlier 6.x versions
    if name is not None:
        locations = [
            location for location in Location.objects.all()
            if location.name == name
        ]
        if not locations:
            return Location.create(name=name).href

        return locations[0].href
Exemplo n.º 3
0
def location_helper(name, search_only=False):
    """
    Location finder by name. If location doesn't exist, create it
    and return the href

    :param str,Element name: location to resolve. If the location is
        by name, it will be retrieved or created, if href, returned
        and if Location, href returned. If None, settings an elements
        location to None will set it to the Default location.
    :param bool search_only: only search for the location, if not found
        do not create
    :return str href: href of location if search_only is not False
    :rtype: str or None
    """ 
    try:
        return name.href
    except AttributeError:
        if name and name.startswith('http'):
            return name
    except ElementNotFound:
        return Location.create(name=name.name).href if not \
            search_only else None
        
    # Get all locations; tmp to support earlier 6.x versions.
    if name is not None:
        locations = [location for location in Search.objects.entry_point(
            'location') if location.name == name]
        if not locations:
            return Location.create(name=name).href if not search_only \
                else None
        return locations[0].href
Exemplo n.º 4
0
def location_helper(name):
    """
    Location finder by name. If location doesn't exist, create it
    and return the href
    
    :param str name
    :return str href: href of location
    """
    location = None
    try:  #SMC >= 6.1.1, locations now searchable
        location = Location(name).href
    except ElementNotFound:
        location_lst = [
            x for x in search.all_elements_by_type('location')
            if x.get('name') == name
        ]
        # SMC <= SMC 6.1
        if location_lst:
            location = location_lst[0].get('href')
    if location:
        return location
    else:
        return Location.create(name)
Exemplo n.º 5
0
    def testLocation(self):
        locations = list(Search('location').objects.filter('api-location'))
        if locations:
            self.assertIsNone(locations[0].delete())

        if session.api_version <= 6.0:
            self.assertRaises(UnsupportedEntryPoint,
                              lambda: Location.create('api-location'))
        else:
            result = Location.create('api-location')
            self.assertTrue(result.href.startswith('http'))
            location = Location('api-location')
            # No references but test the method that returns list
            if is_min_required_smc_version('6.2.0'):
                self.assertFalse(location.used_on)
            location.delete()
Exemplo n.º 6
0
    def delete(self, location_name=None):
        """
        Delete a contact address by it's location name. Every ElementContactAddress
        has a one to one relationship between a location and an address. If the
        location is not provided, then all contact addresses will be deleted.

        :param str location_name: name of the location to remove
        :raises ElementNotFound: location specified does not exist
        """
        json = {"contact_addresses": []}

        if location_name:
            location_ref = Location(location_name).href
            json.setdefault("contact_addresses", []).extend([
                location.data for location in self
                if location.location_ref != location_ref
            ])

        return self._subelement.make_request(resource="contact_addresses",
                                             method="update",
                                             etag=self._etag,
                                             json=json)
Exemplo n.º 7
0
 def test_location_helper(self):
     result = location_helper('foolocation')
     self.assertTrue(result.startswith('http'))
     Location('foolocation').delete()
Exemplo n.º 8
0
    def create(
        cls,
        name,
        addresses,
        port=9200,
        es_retention_period=30,
        es_shard_number=0,
        es_replica_number=0,
        enable_cluster_sniffer=False,
        location=None,
        comment=None,
        tls_profile=None,
        use_internal_credentials=True,
        tls_credentials=None,
    ):
        """
        Create a Elasticsearch Cluster Server element.

        :param str name: Name of Elasticsearch Cluster
        :param list addresses: comma-separated list of one or more FQDNs or IP addresses
        :param int port: Default port is 9200
        :param int es_retention_period: How much time logs will be kept
        30days default
        :param int es_shard_number: Auto by default, number of shards
        :param int es_replica_number : number of ES replicas
        :param bool enable_cluster_sniffer : Enable cluster sniffer (False
        default)
        :param str location: Specifies the location for the server if there
        is a NAT device between the server and other SMC components.
        :param str comment: Comment for Elasticsearch cluster Server element
        :param str tls_profile: tls profile name to use
        :param bool use_internal_credentials: use internal credentials
        :param str tls_credentials: tls credentials name to use

        :raises CreateElementFailed: Failed to create with reason
        :rtype: ElasticsearchCluster
        """
        json = {
            "name": name,
            "port": port,
            "es_retention_period": es_retention_period,
            "es_shard_number": es_shard_number,
            "es_replica_number": es_replica_number,
            "es_enable_cluster_sniffer": enable_cluster_sniffer,
            "comment": comment,
        }

        addresses_lst = addresses.split(",")
        json.update(addresses=addresses_lst)
        if location:
            location_href = Location(location).href
            json.update(location_ref=location_href)
        if tls_profile:
            tls_profile_ref = tls.TLSProfile(tls_profile).href
            json.update(tls_profile=tls_profile_ref)
        if tls_credentials:
            tls_credentials_ref = tls.TLSServerCredential(tls_credentials).href
            json.update(
                es_tls_settings={
                    "use_internal_credentials": use_internal_credentials,
                    "tls_credentials": tls_credentials_ref,
                })
        else:
            json.update(es_tls_settings={
                "use_internal_credentials": use_internal_credentials
            })

        return ElementCreator(cls, json)