Esempio n. 1
0
def test_correct_site(client, site):
    '''Tests that a resource gets sent to the correct site_id'''
    n = Network(client=client, site_id=site['id'], cidr='8.8.8.0/24')
    assert n.ensure()
    assert n.existing_resource()['site_id'] == site['id']

    from pynsot.util import get_result
    manual = get_result(client.sites(site['id']).networks('8.8.8.0/24').get())
    assert manual['site_id'] == site['id']
Esempio n. 2
0
def test_correct_site(client, site):
    '''Tests that a resource gets sent to the correct site_id'''
    n = Network(client=client, site_id=site['id'], cidr='8.8.8.0/24')
    assert n.ensure()
    assert n.existing_resource()['site_id'] == site['id']

    from pynsot.util import get_result
    manual = get_result(client.sites(site['id']).networks('8.8.8.0/24').get())
    assert manual['site_id'] == site['id']
Esempio n. 3
0
def test_authentication(client):
    """Test manual client authentication."""
    auth = {
        'email': client.config['email'],
        'secret_key': client.config['secret_key']
    }
    # Good
    resp = client.authenticate.post(auth)
    result = get_result(resp)
    assert 'auth_token' in result
Esempio n. 4
0
def test_authentication(client):
    """Test manual client authentication."""
    auth = {
        'email': client.config['email'],
        'secret_key': client.config['secret_key']
    }
    # Good
    resp = client.authenticate.post(auth)
    result = get_result(resp)
    assert 'auth_token' in result
Esempio n. 5
0
    def closest_parent(self):
        '''Returns resource object of the closest parent network

        Empty dictionary if no parent network

        :returns: Parent resource
        :rtype: pynsot.models.Network or dict
        '''
        self.ensure_client()
        site = getattr(self.client.sites(self['site_id']), self.resource_name)
        cidr = '%s/%s' % (self['network_address'], self['prefix_length'])
        try:
            lookup = get_result(site(cidr).closest_parent.get())
            return Network(raw=lookup)
        except Exception as e:
            self.log_error(e)
            return {}
Esempio n. 6
0
    def closest_parent(self):
        '''Returns resource object of the closest parent network

        Empty dictionary if no parent network

        :returns: Parent resource
        :rtype: pynsot.models.Network or dict
        '''
        self.ensure_client()
        site = getattr(self.client.sites(self['site_id']), self.resource_name)
        cidr = '%s/%s' % (self['network_address'], self['prefix_length'])
        try:
            lookup = get_result(site(cidr).closest_parent.get())
            return Network(raw=lookup)
        except Exception as e:
            self.log_error(e)
            return {}
Esempio n. 7
0
    def existing_resource(self):
        '''Returns upstream resource

        If nothing exists, empty dict is returned. The result of this is cached
        in a property (_existing_resource) for cheaper re-checks. This can be
        cleared via ``.clear_cache()``

        :rtype: dict
        '''
        self.ensure_client()
        if self._existing_resource:
            return self._existing_resource
        else:
            cur = dict(self)
            cur.pop('attributes', None)
            # We pop attributes because the query fails leaving them in
            try:
                # Site client of resource type because NSoT doesn't support
                # passing site_id as a query parameter
                site = getattr(
                    self.client.sites(self['site_id']),
                    self.resource_name,
                )
                lookup = get_result(site.get(**cur))
            except Exception as e:
                self.log_error(e)
                # There might be a better way to do this. If the NSoT server is
                # unreachable or otherwise the lookup fails it'll log properly
                # and return as if nothing exists. If the resource actually
                # exists but due to reachability it couldn't confirm some
                # action may be unnecessarily took user-side.
                #
                # Honestly, though, I think this is a decent trade-off
                self._existing_resource = {}
                return self._existing_resource

            existing = len(lookup) > 0
            if existing:
                # This is where state will be kept for this
                self._existing_resource = lookup[0]
                return self._existing_resource
            else:
                self._existing_resource = {}
                return self._existing_resource
Esempio n. 8
0
    def existing_resource(self):
        '''Returns upstream resource

        If nothing exists, empty dict is returned. The result of this is cached
        in a property (_existing_resource) for cheaper re-checks. This can be
        cleared via ``.clear_cache()``

        :rtype: dict
        '''
        self.ensure_client()
        if self._existing_resource:
            return self._existing_resource
        else:
            cur = dict(self)
            cur.pop('attributes', None)
            # We pop attributes because the query fails leaving them in
            try:
                # Site client of resource type because NSoT doesn't support
                # passing site_id as a query parameter
                site = getattr(
                    self.client.sites(self['site_id']),
                    self.resource_name,
                )
                lookup = get_result(site.get(**cur))
            except Exception as e:
                self.log_error(e)
                # There might be a better way to do this. If the NSoT server is
                # unreachable or otherwise the lookup fails it'll log properly
                # and return as if nothing exists. If the resource actually
                # exists but due to reachability it couldn't confirm some
                # action may be unnecessarily took user-side.
                #
                # Honestly, though, I think this is a decent trade-off
                self._existing_resource = {}
                return self._existing_resource

            existing = len(lookup) > 0
            if existing:
                # This is where state will be kept for this
                self._existing_resource = lookup[0]
                return self._existing_resource
            else:
                self._existing_resource = {}
                return self._existing_resource
Esempio n. 9
0
def test_payload_not_none_raw_and_not(client, site):
    '''Make sure payload gets set fine for both EZ and raw approaches

    Also make sure both instances are the same, using comparisons
    '''
    n = Network(client=client, site_id=site['id'], cidr='8.8.8.0/24')
    assert n.payload
    assert n.ensure()

    n2 = Network(
        raw=get_result(client.sites(site['id']).networks('8.8.8.0/24').get()))

    assert n2.payload

    # Test some magic methods on raw-init'd instance
    assert len(n2)
    assert n == n2
    assert n2.keys()
    assert n2.items()
Esempio n. 10
0
def test_clear_cache_on_change(client, site):
    '''Test that cache is cleared on any change to the instance'''
    site_id = site['id']
    c = client

    n = Network(client=c, site_id=site_id, cidr='8.8.8.0/24')
    assert n.ensure()
    assert n.exists()

    non_existing_site = get_result(c.sites.get())[-1]['id'] + 1000
    n['site_id'] = non_existing_site
    # First assert that the cache property was cleared
    assert not n._existing_resource
    assert not n.existing_resource()
    # assert that the resource isn't successfully looked up if site doesn't
    # match
    assert not n.exists()
    # Change site back and test
    n['site_id'] = site_id
    assert n.exists()
Esempio n. 11
0
def test_clear_cache_on_change(client, site):
    '''Test that cache is cleared on any change to the instance'''
    site_id = site['id']
    c = client

    n = Network(client=c, site_id=site_id, cidr='8.8.8.0/24')
    assert n.ensure()
    assert n.exists()

    non_existing_site = get_result(c.sites.get())[-1]['id'] + 1000
    n['site_id'] = non_existing_site
    # First assert that the cache property was cleared
    assert not n._existing_resource
    assert not n.existing_resource()
    # assert that the resource isn't successfully looked up if site doesn't
    # match
    assert not n.exists()
    # Change site back and test
    n['site_id'] = site_id
    assert n.exists()
Esempio n. 12
0
def test_payload_not_none_raw_and_not(client, site):
    '''Make sure payload gets set fine for both EZ and raw approaches

    Also make sure both instances are the same, using comparisons
    '''
    n = Network(client=client, site_id=site['id'], cidr='8.8.8.0/24')
    assert n.payload
    assert n.ensure()

    n2 = Network(
        raw=get_result(
            client.sites(site['id']).networks('8.8.8.0/24').get()
        )
    )

    assert n2.payload

    # Test some magic methods on raw-init'd instance
    assert len(n2)
    assert n == n2
    assert n2.keys()
    assert n2.items()