systems.models.System.objects.get

Here are the examples of the python api systems.models.System.objects.get taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

114 Examples 7

Example 1

Project: inventory
Source File: MacroExpansion.py
View license
    def host_macro(self):
        try:
            host = self.operators[1]
            key = self.operators[2]
            system = System.objects.get(hostname=host)
            self.output_text = KeyValue.objects.filter(obj=system).filter(key=key)[0].value
            self.output()
        except Exception, e:
            self.output_text = 'Host/Key Combination Not Found'
            self.output()

Example 2

Project: inventory
Source File: importer.py
View license
    def test_warranty_start_end(self):
        test_csv = """
        hostname,warranty_start,warranty_end,serial,system_type%type_name,allocation%name
        foobob.mozilla.com,2011-03-01,2012-03-12,asdf,foobar,something
        """
        self.client_tst(test_csv, save=True)
        s = System.objects.get(hostname='foobob.mozilla.com')
        self.assertTrue(s.warranty_start)
        self.assertTrue(s.warranty_end)

Example 3

Project: inventory
Source File: importer.py
View license
    def test_override_spaces(self):
        test_csv = """
        hostname , warranty_start,  warranty_end, serial,system_type%type_name,allocation%name
        foobob.mozilla.com,   2011-03-01 , 2012-03-12,asdf,foobar,something
        """
        s = create_fake_host(hostname='foobob.mozilla.com', serial='1234')
        self.client_tst(test_csv, save=True)
        s = System.objects.get(hostname='foobob.mozilla.com')
        self.assertTrue(s.serial, '1234')

Example 4

Project: inventory
Source File: handlers.py
View license
    def delete(self, request, system_id=None):
        try:
            s = System.objects.get(id=system_id)
            id = s.id
            hostname = s.hostname
            s.delete()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i, "hostname":"%s"}' % (id, hostname))
        except:
            resp = rc.NOT_FOUND
            resp.write("Unable to find system")

        return resp

Example 5

Project: inventory
Source File: handlers.py
View license
    def delete(self, request, system_id=None):
        try:
            s = System.objects.get(id=system_id)
            id = s.id
            hostname = s.hostname
            s.delete()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i, "hostname":"%s"}' % (id, hostname))
        except:
            resp = rc.NOT_FOUND
            resp.write("Unable to find system")

        return resp

Example 6

Project: inventory
Source File: importer.py
View license
    def test_primary_attribute2(self):
        create_fake_host(
            hostname='foobob.mozilla.com', asset_tag='YM0090PW9G6'
        )
        test_csv = """
        primary_attribute%asset_tag,warranty_start,warranty_end,purchase_date,serial,system_type%type_name,allocation%name
        YM0090PW9G6,2010-03-24,2013-03-24,2010-03-24,asdf,foobar,something
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(asset_tag='YM0090PW9G6')
        self.assertTrue(s1.warranty_start)
        self.assertTrue(s1.warranty_end)

Example 7

Project: inventory
Source File: truth_handler.py
View license
    def update(self, request, key_value_id=None):
        n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
        if 'system_id' in request.POST:
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
        if 'value' in request.POST:
            n.value = request.POST['value']
        try:
            n.save()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i}' % (n.id))
        except:
            resp = rc.NOT_FOUND
            resp.write('Unable to Create Key/Value Pair')
        return resp

Example 8

Project: inventory
Source File: importer.py
View license
    def test_primary_attribute3(self):
        create_fake_host(
            hostname='foobob.mozilla.com', serial='SC07HT03WDKDJ'
        )
        test_csv = """
        primary_attribute%serial,warranty_start,warranty_end,purchase_date,system_type%type_name,allocation%name
        SC07HT03WDKDJ,2012-06-06,2013-06-06,2012-06-06,foobar,something
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(serial='SC07HT03WDKDJ')
        self.assertEqual(
            datetime.datetime(2012, 6, 6, 0, 0).date(), s1.warranty_start)
        self.assertEqual(
            datetime.datetime(2013, 6, 6, 0, 0).date(), s1.warranty_end)

Example 9

Project: inventory
Source File: import_utils.py
View license
def system_import(blob):
    if 'pk' in blob:
        try:
            system = System.objects.get(pk=blob['pk'])
            return system_update(system, blob)
        except System.DoesNotExist:
            raise BadImportData(
                bad_blob=blob,
                msg='Could not find the System with primary key '
                '{0}.'.format(blob['pk'])
            )
    else:
        recurse_confirm_no_pk(blob)
        system = System()
        return system_update(system, blob)

Example 10

Project: inventory
Source File: MacroExpansion.py
View license
    def host_macro(self):
        try:
            host = self.operators[1]
            key = self.operators[2]
            system = System.objects.get(hostname=host)
            self.output_text = KeyValue.objects.filter(obj=system).filter(key=key)[0].value
            self.output()
        except Exception, e:
            self.output_text = 'Host/Key Combination Not Found'
            self.output()

Example 11

Project: inventory
Source File: importer.py
View license
    def test_override_spaces(self):
        test_csv = """
        hostname , warranty_start,  warranty_end, serial,system_type%type_name,allocation%name
        foobob.mozilla.com,   2011-03-01 , 2012-03-12,asdf,foobar,something
        """
        s = create_fake_host(hostname='foobob.mozilla.com', serial='1234')
        self.client_tst(test_csv, save=True)
        s = System.objects.get(hostname='foobob.mozilla.com')
        self.assertTrue(s.serial, '1234')

Example 12

Project: inventory
Source File: tests.py
View license
    def test_delete_key_by_system(self):
        system = System.objects.get(hostname='fake-hostname2')
        k = KeyValue(obj=system, key='testing', value='blahblah')
        k.save()
        resp = self.client.delete('/en-US/api/v2/keyvalue/?key_type=delete_key_by_system&system=fake-hostname2&key=testing', follow=True)
        try:
            count = KeyValue.objects.get(obj=system, key='testing').count()
        except KeyValue.DoesNotExist:
            count = 0
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(int(json.loads(resp.content.split(" = ")[1])['id']), 14)
        self.assertEqual(count, 0)

Example 13

Project: inventory
Source File: handlers.py
View license
    def delete(self, request, system_id=None):
        try:
            s = System.objects.get(id=system_id)
            id = s.id
            hostname = s.hostname
            s.delete()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i, "hostname":"%s"}' % (id, hostname))
        except:
            resp = rc.NOT_FOUND
            resp.write("Unable to find system")

        return resp

Example 14

Project: inventory
Source File: importer.py
View license
    def test_primary_attribute3(self):
        create_fake_host(
            hostname='foobob.mozilla.com', serial='SC07HT03WDKDJ'
        )
        test_csv = """
        primary_attribute%serial,warranty_start,warranty_end,purchase_date,system_type%type_name,allocation%name
        SC07HT03WDKDJ,2012-06-06,2013-06-06,2012-06-06,foobar,something
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(serial='SC07HT03WDKDJ')
        self.assertEqual(
            datetime.datetime(2012, 6, 6, 0, 0).date(), s1.warranty_start)
        self.assertEqual(
            datetime.datetime(2013, 6, 6, 0, 0).date(), s1.warranty_end)

Example 15

Project: inventory
Source File: importer.py
View license
    def test_system_type(self):
        create_fake_host(
            hostname='foobob1.mozilla.com', serial='xxx'
        )
        test_csv = """
        hostname, system_type % type_name
        foobob1.mozilla.com, foobar
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(hostname='foobob1.mozilla.com')
        self.assertEqual(s1.system_type.type_name, 'foobar')

Example 16

Project: inventory
Source File: importer.py
View license
    def test_quoted(self):
        create_fake_host(
            hostname='foobob.mozilla.com', serial='xxx'
        )
        test_csv = """
        hostname, switch_ports
        foobob.mozilla.com, "sdf,asfd,asfd"
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(hostname='foobob.mozilla.com')
        self.assertEqual(s1.switch_ports, 'sdf,asfd,asfd')

Example 17

Project: inventory
Source File: importer.py
View license
    def test_two_switches(self):
        create_fake_host(
            hostname='foobob.mozilla.com', serial='xxx'
        )
        test_csv = """
        primary_attribute%serial,switch_ports,ram
        xxx,"switch1.r101-10:xe-0/0/36,switch1.r101-10:xe-1/0/36",1000
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(serial='xxx')
        self.assertEqual(
            s1.switch_ports,
            "switch1.r101-10:xe-0/0/36,switch1.r101-10:xe-1/0/36"
        )

Example 18

Project: inventory
Source File: truth_handler.py
View license
    def update(self, request, key_value_id=None):
        n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
        if 'system_id' in request.POST:
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
        if 'value' in request.POST:
            n.value = request.POST['value']
        try:
            n.save()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i}' % (n.id))
        except:
            resp = rc.NOT_FOUND
            resp.write('Unable to Create Key/Value Pair')
        return resp

Example 19

Project: inventory
Source File: handlers.py
View license
    def update(self, request, key_value_id=None):
        n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
        if 'system_id' in request.POST:
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
        if 'value' in request.POST:
            n.value = request.POST['value']
        try:
            n.save()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i}' % (n.id))
        except:
            resp = rc.NOT_FOUND
            resp.write('Unable to Create Key/Value Pair')
        return resp

Example 20

Project: inventory
Source File: import_utils.py
View license
def system_import(blob):
    if 'pk' in blob:
        try:
            system = System.objects.get(pk=blob['pk'])
            return system_update(system, blob)
        except System.DoesNotExist:
            raise BadImportData(
                bad_blob=blob,
                msg='Could not find the System with primary key '
                '{0}.'.format(blob['pk'])
            )
    else:
        recurse_confirm_no_pk(blob)
        system = System()
        return system_update(system, blob)

Example 21

Project: inventory
Source File: api.py
View license
    def hydrate(self, bundle):
        if 'system_hostname' in bundle.data and 'system' in bundle.data:
            bundle.errors = ("Please only specify a system via the 'system' "
                             "xor the 'system_hostname' parameter")
        if 'system_hostname' in bundle.data:
            system_hostname = bundle.data.get('system_hostname')
            try:
                system = System.objects.get(hostname=system_hostname)
                bundle.data['system'] = system
            except System.DoesNotExist:
                bundle.errors['system'] = (
                    "Couldn't find system with hostname {0}"
                    .format(system_hostname))
        super(StaticRegResource, self).hydrate(bundle)
        return bundle

Example 22

Project: inventory
Source File: handlers.py
View license
    def delete(self, request, system_id=None):
        try:
            s = System.objects.get(id=system_id)
            id = s.id
            hostname = s.hostname
            s.delete()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i, "hostname":"%s"}' % (id, hostname))
        except:
            resp = rc.NOT_FOUND
            resp.write("Unable to find system")

        return resp

Example 23

Project: inventory
Source File: models.py
View license
    def service_import_set_systems(self, hostnames):
        # Note: Make sure to call save() after calling this function
        self.systems.clear()
        for hostname in hostnames:
            try:
                system = System.objects.get(hostname=hostname)
            except System.DoesNotExist:
                raise System.DoesNotExist(
                    "The system with hostname '{0}' does not "
                    "exist".format(hostname)
                )
            self.systems.add(system)

Example 24

Project: inventory
Source File: tests.py
View license
    def test_delete_key_by_system(self):
        system = System.objects.get(hostname='fake-hostname2')
        k = KeyValue(obj=system, key='testing', value='blahblah')
        k.save()
        resp = self.client.delete('/en-US/api/v2/keyvalue/?key_type=delete_key_by_system&system=fake-hostname2&key=testing', follow=True)
        try:
            count = KeyValue.objects.get(obj=system, key='testing').count()
        except KeyValue.DoesNotExist:
            count = 0
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(int(json.loads(resp.content.split(" = ")[1])['id']), 14)
        self.assertEqual(count, 0)

Example 25

Project: inventory
Source File: truth_handler.py
View license
    def update(self, request, key_value_id=None):
        n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
        if 'system_id' in request.POST:
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
        if 'value' in request.POST:
            n.value = request.POST['value']
        try:
            n.save()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i}' % (n.id))
        except:
            resp = rc.NOT_FOUND
            resp.write('Unable to Create Key/Value Pair')
        return resp

Example 26

Project: inventory
Source File: importer.py
View license
    def test_override(self):
        test_csv = """
        hostname,warranty_start,warranty_end,serial,system_type%type_name,allocation%name
        foobob.mozilla.com,2011-03-01,2012-03-12,asdf,foobar,something
        """
        s = create_fake_host(hostname='foobob.mozilla.com', serial='1234')
        self.client_tst(test_csv, save=True)
        s = System.objects.get(hostname='foobob.mozilla.com')
        self.assertTrue(s.serial, '1234')

Example 27

Project: inventory
Source File: handlers.py
View license
    def delete(self, request, system_id=None):
        try:
            s = System.objects.get(id=system_id)
            id = s.id
            hostname = s.hostname
            s.delete()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i, "hostname":"%s"}' % (id, hostname))
        except:
            resp = rc.NOT_FOUND
            resp.write("Unable to find system")

        return resp

Example 28

Project: inventory
Source File: importer.py
View license
    def test_primary_attribute2(self):
        create_fake_host(
            hostname='foobob.mozilla.com', asset_tag='YM0090PW9G6'
        )
        test_csv = """
        primary_attribute%asset_tag,warranty_start,warranty_end,purchase_date,serial,system_type%type_name,allocation%name
        YM0090PW9G6,2010-03-24,2013-03-24,2010-03-24,asdf,foobar,something
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(asset_tag='YM0090PW9G6')
        self.assertTrue(s1.warranty_start)
        self.assertTrue(s1.warranty_end)

Example 29

Project: inventory
Source File: api.py
View license
    def hydrate(self, bundle):
        if 'system_hostname' in bundle.data and 'system' in bundle.data:
            bundle.errors = ("Please only specify a system via the 'system' "
                             "xor the 'system_hostname' parameter")
        if 'system_hostname' in bundle.data:
            system_hostname = bundle.data.get('system_hostname')
            try:
                system = System.objects.get(hostname=system_hostname)
                bundle.data['system'] = system
            except System.DoesNotExist:
                bundle.errors['system'] = (
                    "Couldn't find system with hostname {0}"
                    .format(system_hostname))
        super(StaticRegResource, self).hydrate(bundle)
        return bundle

Example 30

Project: inventory
Source File: importer.py
View license
    def test_primary_attribute(self):
        s = create_fake_host(hostname='foobob.mozilla.com')
        test_csv = """
        primary_attribute%hostname,hostname,warranty_start,warranty_end,serial,system_type%type_name,allocation%name
        foobob.mozilla.com,foobar.mozilla.com,2012-01-01,2012-01-01,asdf,foobar,something
        """
        self.client_tst(test_csv, save=True, primary_attr='asset_tag')
        # The primary_attr kwarg shouldn't affect anything

        s1 = System.objects.get(pk=s.pk)
        self.assertEqual('foobar.mozilla.com', s1.hostname)

Example 31

Project: inventory
Source File: importer.py
View license
    def test_two_switches(self):
        create_fake_host(
            hostname='foobob.mozilla.com', serial='xxx'
        )
        test_csv = """
        primary_attribute%serial,switch_ports,ram
        xxx,"switch1.r101-10:xe-0/0/36,switch1.r101-10:xe-1/0/36",1000
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(serial='xxx')
        self.assertEqual(
            s1.switch_ports,
            "switch1.r101-10:xe-0/0/36,switch1.r101-10:xe-1/0/36"
        )

Example 32

Project: inventory
Source File: importer.py
View license
    def test_quoted(self):
        create_fake_host(
            hostname='foobob.mozilla.com', serial='xxx'
        )
        test_csv = """
        hostname, switch_ports
        foobob.mozilla.com, "sdf,asfd,asfd"
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(hostname='foobob.mozilla.com')
        self.assertEqual(s1.switch_ports, 'sdf,asfd,asfd')

Example 33

Project: inventory
Source File: importer.py
View license
    def test_primary_attribute(self):
        s = create_fake_host(hostname='foobob.mozilla.com')
        test_csv = """
        primary_attribute%hostname,hostname,warranty_start,warranty_end,serial,system_type%type_name,allocation%name
        foobob.mozilla.com,foobar.mozilla.com,2012-01-01,2012-01-01,asdf,foobar,something
        """
        self.client_tst(test_csv, save=True, primary_attr='asset_tag')
        # The primary_attr kwarg shouldn't affect anything

        s1 = System.objects.get(pk=s.pk)
        self.assertEqual('foobar.mozilla.com', s1.hostname)

Example 34

Project: inventory
Source File: importer.py
View license
    def test_override(self):
        test_csv = """
        hostname,warranty_start,warranty_end,serial,system_type%type_name,allocation%name
        foobob.mozilla.com,2011-03-01,2012-03-12,asdf,foobar,something
        """
        s = create_fake_host(hostname='foobob.mozilla.com', serial='1234')
        self.client_tst(test_csv, save=True)
        s = System.objects.get(hostname='foobob.mozilla.com')
        self.assertTrue(s.serial, '1234')

Example 35

Project: inventory
Source File: importer.py
View license
    def test_system_type(self):
        create_fake_host(
            hostname='foobob1.mozilla.com', serial='xxx'
        )
        test_csv = """
        hostname, system_type % type_name
        foobob1.mozilla.com, foobar
        """
        self.client_tst(test_csv, save=True)
        # The primary_attr kwarg shouldn't affect anything
        s1 = System.objects.get(hostname='foobob1.mozilla.com')
        self.assertEqual(s1.system_type.type_name, 'foobar')

Example 36

Project: inventory
Source File: importer.py
View license
    def test_warranty_start_end(self):
        test_csv = """
        hostname,warranty_start,warranty_end,serial,system_type%type_name,allocation%name
        foobob.mozilla.com,2011-03-01,2012-03-12,asdf,foobar,something
        """
        self.client_tst(test_csv, save=True)
        s = System.objects.get(hostname='foobob.mozilla.com')
        self.assertTrue(s.warranty_start)
        self.assertTrue(s.warranty_end)

Example 37

Project: inventory
Source File: models.py
View license
    def service_import_set_systems(self, hostnames):
        # Note: Make sure to call save() after calling this function
        self.systems.clear()
        for hostname in hostnames:
            try:
                system = System.objects.get(hostname=hostname)
            except System.DoesNotExist:
                raise System.DoesNotExist(
                    "The system with hostname '{0}' does not "
                    "exist".format(hostname)
                )
            self.systems.add(system)

Example 38

Project: inventory
Source File: truth_handler.py
View license
    def update(self, request, key_value_id=None):
        n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
        if 'system_id' in request.POST:
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
        if 'value' in request.POST:
            n.value = request.POST['value']
        try:
            n.save()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i}' % (n.id))
        except:
            resp = rc.NOT_FOUND
            resp.write('Unable to Create Key/Value Pair')
        return resp

Example 39

Project: inventory
Source File: handlers.py
View license
    def delete(self, request, system_id=None):
        try:
            s = System.objects.get(id=system_id)
            id = s.id
            hostname = s.hostname
            s.delete()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i, "hostname":"%s"}' % (id, hostname))
        except:
            resp = rc.NOT_FOUND
            resp.write("Unable to find system")

        return resp

Example 40

Project: inventory
Source File: handlers.py
View license
    def update(self, request, key_value_id=None):
        n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
        if 'system_id' in request.POST:
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
        if 'value' in request.POST:
            n.value = request.POST['value']
        try:
            n.save()
            resp = rc.ALL_OK
            resp.write('json = {"id":%i}' % (n.id))
        except:
            resp = rc.NOT_FOUND
            resp.write('Unable to Create Key/Value Pair')
        return resp

Example 41

Project: inventory
Source File: keyvalue_handler.py
View license
    def create(self, request, key_value_id=None):
        if 'system_id' in request.POST:
            n = KeyValue()
            system = System.objects.get(id=request.POST['system_id'])
            n.obj = system
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        elif 'truth_name' in request.POST:
            n = TruthKeyValue()
            truth = Truth.objects.get(name=request.POST['truth_name'])
            n.truth = truth
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        else:
            resp = rc.NOT_FOUND
            resp.write('system_id or truth_name required')

Example 42

Project: inventory
Source File: handlers.py
View license
    def read(self, request, key_value_id=None):
        base = Truth.expanded_objects
        if 'key' in request.GET:
            base = base.filter(key=request.GET['key'])
        if 'value' in request.GET:
            base = base.filter(value=request.GET['value'])
        if 'id' in request.GET:
            base = base.filter(id=request.GET['id'])
        if key_value_id is not None:
            base = base.filter(id=key_value_id)
        if 'truth_id' in request.GET:
            try:
                truth = System.objects.get(id=request.GET['truth_id'])
                base = base.filter(truth=truth)
            except Exception, e:
                resp = rc.NOT_FOUND
                return resp

        for row in base:
            matches = re.match("\$\{(.*)\}", row.value)
            if matches is not None:
                m = MacroExpansion(matches.group(1))
                row.value = m.output()
        return base

Example 43

Project: inventory
Source File: keyvalue_handler.py
View license
    def update(self, request, key_value_id=None):
        ###TODO This whole method is not functioning correctly. Just for version 2. Not getting the system_id or truth_id from the poster firefox plugin
        if 'system_id' in request.POST:
            n = None
            key_validated, validation_error_string = self.validate(request.POST['key'], request.POST['value']) 
            if key_validated is False:
                resp = rc.FORBIDDEN
                resp.write('Validation Failed for %s %s' % (request.POST['key'], validation_error_string) )
                return resp
            try:
                n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
                system = System.objects.get(id=request.POST['system_id'])
            except:
                resp = rc.NOT_FOUND
                resp.write('Neither system_id or truth_id found')

            if n is not None:
                n.obj = system
            if 'value' in request.POST and n is not None:
                n.value = request.POST['value']
            if n is not None:
                try:
                    n.save()
                    resp = rc.ALL_OK
                    resp.write('json = {"id":%i}' % (n.id))
                except:
                    resp = rc.NOT_FOUND
                    resp.write('Unable to Create Key/Value Pair')
                return resp
        elif 'truth_id' in request.POST:
            try:
                truth = Truth.objects.get(name=key_value_id)
                na = TruthKeyValue.objects.get(truth=truth,key=request.POST['key'])
                if 'value' in request.POST:
                    n.value = request.POST['value']
            except:
                pass
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except Exception, e:
                resp = rc.NOT_FOUND
                resp.write('Unable to Update Key/Value Pair %s' % e)
            return resp
        else:
            resp = rc.NOT_FOUND
            resp.write('Neither system_id or truth_id found')
            return resp

Example 44

Project: inventory
Source File: keyvalue_handler.py
View license
    def read(self, request, key_value_id=None):
        #if keystore get var is set return the whole keystore
        if 'keystore' in request.GET:
            #if key get var is set return the keystore based on the existance of this key
            if 'key' in request.GET:
                base = KeyValue.objects.filter(key=request.GET['keystore']).filter(keyvalue_set__contains=request.GET['key'])
                tmp_list = []
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.system.hostname, r.key)
                    tmp_list[key_name] = r.value
            if 'key' not in request.GET:
                tree = KeyValueTree(request.GET['keystore']).final
                return tree
        elif 'key_type' in request.GET:
            key_type = request.GET['key_type']
            tmp_list = []
            if key_type == 'dhcp_scopes':
                #Get keystores from truth that have dhcp.is_scope = True
                base = TruthKeyValue.objects.filter(key='dhcp.is_scope',value='True')
                #Iterate through the list and get all of the key/value pairs
                for row in base:
                    keyvalue = TruthKeyValue.objects.filter(truth=row.truth)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_list.append(tmp_dict)
                return tmp_list

            if key_type == 'system_by_reverse_dns_zone':

                #Get keystores from truth that have dhcp.is_scope = True
                keyvalue_pairs = KeyValue.objects.filter(key__contains='reverse_dns_zone',value=request.GET['zone']).filter(key__startswith='nic.')
                #Iterate through the list and get all of the key/value pairs
                tmp_list = []
                for row in keyvalue_pairs:
                    keyvalue = KeyValue.objects.filter(obj=row.system)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_dict['hostname'] = row.system.hostname
                    appendable = True
                    for the_items in tmp_list:
                        if 'hostname' not in the_items:
                            appendable = True
                        elif the_items['hostname'] == row.system.hostname:
                            appendable = False
                    if appendable is True:
                        tmp_list.append(tmp_dict)
                    #tmp_list = list(set(tmp_list))
                return tmp_list
            if key_type == 'system_by_scope':
                #Get keystores from truth that have dhcp.is_scope = True
                keyvalue_pairs = KeyValue.objects.filter(key__contains='dhcp_scope',value=request.GET['scope']).filter(key__startswith='nic.')
                #Iterate through the list and get all of the key/value pairs
                tmp_list = []
                for row in keyvalue_pairs:
                    keyvalue = KeyValue.objects.filter(obj=row.system)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_dict['hostname'] = row.system.hostname
                    appendable = True
                    for the_items in tmp_list:
                        if 'hostname' not in the_items:
                            appendable = True
                        elif the_items['hostname'] == row.system.hostname:
                            appendable = False
                    if appendable is True:
                        tmp_list.append(tmp_dict)
                    #tmp_list = list(set(tmp_list))
                return tmp_list
            if key_type == 'adapters_by_system':
                #Get keystores from truth that have dhcp.is_scope = True
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        if matches.group(1) not in adapter_ids:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    ipv4_address = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    try:
                        final_list.append({
                            'system_hostname':system.hostname,
                            'ipv4_address':ipv4_address,
                            'adapter_name':adapter_name,
                            'mac_address':mac_address,
                            'dhcp_hostname':dhcp_hostname,
                            'dhcp_filename':dhcp_filename}
                            )
                    except:
                        pass
                #tmp_list.append(tmp_dict)
                return final_list
            if key_type == 'adapters_by_system_and_zone':
                #Get keystores from truth that have dhcp.is_scope = True
                zone = request.GET['zone']
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        dhcp_scope_match = 'nic.%s.reverse_dns_zone.0' % matches.group(1)
                        if matches.group(1) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == zone:
                        #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    dhcp_domain_name = ''
                    ipv4_address = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict:
                        dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a]
                    final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address})
                #tmp_list.append(tmp_dict)
                return final_list
            if key_type == 'adapters_by_system_and_scope':
                #Get keystores from truth that have dhcp.is_scope = True
                dhcp_scope = request.GET['dhcp_scope']
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        dhcp_scope_match = 'nic.%s.dhcp_scope.0' % matches.group(1)
                        if matches.group(1) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == dhcp_scope:
                        #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    dhcp_domain_name = ''
                    ipv4_address = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict and 'nic.%s.option_hostname.0' % a not in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if  'nic.%s.option_hostname.0' % a not in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.option_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict:
                        dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a]
                    final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address,  'adapter_name':adapter_name, 'mac_address':mac_address, 'dhcp_hostname':dhcp_hostname, 'dhcp_filename':dhcp_filename, 'dhcp_domain_name':dhcp_domain_name})
                #tmp_list.append(tmp_dict)
                return final_list
        elif 'key' in request.GET and request.GET['key'] > '':
            tmp_list = {}
            try:
                base = KeyValue.objects.filter(key=request.GET['key'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.system.hostname, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass
            try:
                base = TruthKeyValue.objects.filter(key=request.GET['key'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'truth:%s:%s' % (r.truth.name, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass

            return tmp_list
        elif 'value' in request.GET:
            tmp_list = {}
            try:
                base = KeyValue.objects.filter(value=request.GET['value'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.system.hostname, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass
            try:
                base = TruthKeyValue.objects.filter(value=request.GET['value'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'truth:%s:%s' % (r.truth.name, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass

            return tmp_list

Example 45

Project: inventory
Source File: handlers.py
View license
    def create(self, request, key_value_id=None):
        if 'system_id' in request.POST:
            n = KeyValue()
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        if 'truth_name' in request.POST:
            n = TruthKeyValue()
            truth = Truth.objects.get(name=request.POST['truth_name'])
            n.truth = truth
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp

Example 46

Project: inventory
Source File: keyvalue_handler.py
View license
    def update(self, request, key_value_id=None):
        ###TODO This whole method is not functioning correctly. Just for version 2. Not getting the system_id or truth_id from the poster firefox plugin
        if 'system_id' in request.POST:
            n = None
            key_validated, validation_error_string = self.validate(request.POST['key'], request.POST['value']) 
            if key_validated is False:
                resp = rc.FORBIDDEN
                resp.write('Validation Failed for %s %s' % (request.POST['key'], validation_error_string) )
                return resp
            try:
                n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
                system = System.objects.get(id=request.POST['system_id'])
            except:
                resp = rc.NOT_FOUND
                resp.write('Neither system_id or truth_id found')

            if n is not None:
                n.obj = system
            if 'value' in request.POST and n is not None:
                n.value = request.POST['value']
            if n is not None:
                try:
                    n.save()
                    resp = rc.ALL_OK
                    resp.write('json = {"id":%i}' % (n.id))
                except:
                    resp = rc.NOT_FOUND
                    resp.write('Unable to Create Key/Value Pair')
                return resp
        elif 'truth_id' in request.POST:
            try:
                truth = Truth.objects.get(name=key_value_id)
                na = TruthKeyValue.objects.get(truth=truth,key=request.POST['key'])
                if 'value' in request.POST:
                    n.value = request.POST['value']
            except:
                pass
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except Exception, e:
                resp = rc.NOT_FOUND
                resp.write('Unable to Update Key/Value Pair %s' % e)
            return resp
        else:
            resp = rc.NOT_FOUND
            resp.write('Neither system_id or truth_id found')
            return resp

Example 47

Project: inventory
Source File: keyvalue_handler.py
View license
    def read(self, request, key_value_id=None):
        #if keystore get var is set return the whole keystore
        if 'keystore' in request.GET:
            #if key get var is set return the keystore based on the existance of this key
            if 'key' in request.GET:
                base = KeyValue.objects.filter(key=request.GET['keystore']).filter(keyvalue_set__contains=request.GET['key'])
                tmp_list = []
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.system.hostname, r.key)
                    tmp_list[key_name] = r.value
            if 'key' not in request.GET:
                tree = KeyValueTree(request.GET['keystore']).final
                return tree
        elif 'key_type' in request.GET:
            key_type = request.GET['key_type']
            tmp_list = []
            if key_type == 'dhcp_scopes':
                #Get keystores from truth that have dhcp.is_scope = True
                base = TruthKeyValue.objects.filter(key='dhcp.is_scope',value='True')
                #Iterate through the list and get all of the key/value pairs
                for row in base:
                    keyvalue = TruthKeyValue.objects.filter(truth=row.truth)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_list.append(tmp_dict)
                return tmp_list

            if key_type == 'system_by_reverse_dns_zone':

                #Get keystores from truth that have dhcp.is_scope = True
                keyvalue_pairs = KeyValue.objects.filter(key__contains='reverse_dns_zone',value=request.GET['zone']).filter(key__startswith='nic.')
                #Iterate through the list and get all of the key/value pairs
                tmp_list = []
                for row in keyvalue_pairs:
                    keyvalue = KeyValue.objects.filter(obj=row.system)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_dict['hostname'] = row.system.hostname
                    appendable = True
                    for the_items in tmp_list:
                        if 'hostname' not in the_items:
                            appendable = True
                        elif the_items['hostname'] == row.system.hostname:
                            appendable = False
                    if appendable is True:
                        tmp_list.append(tmp_dict)
                    #tmp_list = list(set(tmp_list))
                return tmp_list
            if key_type == 'system_by_scope':
                #Get keystores from truth that have dhcp.is_scope = True
                keyvalue_pairs = KeyValue.objects.filter(key__contains='dhcp_scope',value=request.GET['scope']).filter(key__startswith='nic.')
                #Iterate through the list and get all of the key/value pairs
                tmp_list = []
                for row in keyvalue_pairs:
                    keyvalue = KeyValue.objects.filter(obj=row.system)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_dict['hostname'] = row.system.hostname
                    appendable = True
                    for the_items in tmp_list:
                        if 'hostname' not in the_items:
                            appendable = True
                        elif the_items['hostname'] == row.system.hostname:
                            appendable = False
                    if appendable is True:
                        tmp_list.append(tmp_dict)
                    #tmp_list = list(set(tmp_list))
                return tmp_list
            if key_type == 'adapters_by_system':
                #Get keystores from truth that have dhcp.is_scope = True
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        if matches.group(1) not in adapter_ids:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    ipv4_address = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    try:
                        final_list.append({
                            'system_hostname':system.hostname,
                            'ipv4_address':ipv4_address,
                            'adapter_name':adapter_name,
                            'mac_address':mac_address,
                            'dhcp_hostname':dhcp_hostname,
                            'dhcp_filename':dhcp_filename}
                            )
                    except:
                        pass
                #tmp_list.append(tmp_dict)
                return final_list
            if key_type == 'adapters_by_system_and_zone':
                #Get keystores from truth that have dhcp.is_scope = True
                zone = request.GET['zone']
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        dhcp_scope_match = 'nic.%s.reverse_dns_zone.0' % matches.group(1)
                        if matches.group(1) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == zone:
                        #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    dhcp_domain_name = ''
                    ipv4_address = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict:
                        dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a]
                    final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address})
                #tmp_list.append(tmp_dict)
                return final_list
            if key_type == 'adapters_by_system_and_scope':
                #Get keystores from truth that have dhcp.is_scope = True
                dhcp_scope = request.GET['dhcp_scope']
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        dhcp_scope_match = 'nic.%s.dhcp_scope.0' % matches.group(1)
                        if matches.group(1) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == dhcp_scope:
                        #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    dhcp_domain_name = ''
                    ipv4_address = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict and 'nic.%s.option_hostname.0' % a not in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if  'nic.%s.option_hostname.0' % a not in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.option_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict:
                        dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a]
                    final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address,  'adapter_name':adapter_name, 'mac_address':mac_address, 'dhcp_hostname':dhcp_hostname, 'dhcp_filename':dhcp_filename, 'dhcp_domain_name':dhcp_domain_name})
                #tmp_list.append(tmp_dict)
                return final_list
        elif 'key' in request.GET and request.GET['key'] > '':
            tmp_list = {}
            try:
                base = KeyValue.objects.filter(key=request.GET['key'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.system.hostname, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass
            try:
                base = TruthKeyValue.objects.filter(key=request.GET['key'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'truth:%s:%s' % (r.truth.name, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass

            return tmp_list
        elif 'value' in request.GET:
            tmp_list = {}
            try:
                base = KeyValue.objects.filter(value=request.GET['value'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.system.hostname, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass
            try:
                base = TruthKeyValue.objects.filter(value=request.GET['value'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'truth:%s:%s' % (r.truth.name, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass

            return tmp_list

Example 48

Project: inventory
Source File: handlers.py
View license
    def update(self, request, key_value_id=None):
        if 'system_id' in request.POST:
            n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        else:
            try:
                truth = Truth.objects.get(name=key_value_id)
                n = TruthKeyValue.objects.get(truth=truth,key=request.POST['key'])
                if 'value' in request.POST:
                    n.value = request.POST['value']
            except:
                pass
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp

Example 49

Project: inventory
Source File: keyvalue_handler.py
View license
    def delete(self, request, key_value_id=None):
        if 'key_type' in request.GET and request.GET['key_type'] == 'delete_all_network_adapters':
            #Get keystores from truth that have dhcp.is_scope = True
            try:
                system_hostname = request.GET['system_hostname']
                system = System.objects.get(hostname=system_hostname)
                KeyValue.objects.filter(key__startswith='nic', obj=system).delete()
                resp = rc.ALL_OK
                resp.write('json = {"id":"0"}')
            except:
                resp = rc.NOT_FOUND
                resp.write('json = {"error_message":"Unable to Delete}')

            return resp
        if 'key_type' in request.GET and request.GET['key_type'] == 'delete_network_adapter':
            #Get keystores from truth that have dhcp.is_scope = True
            try:
                adapter_number = request.GET['adapter_number']
                system_hostname = request.GET['system_hostname']
                system = System.objects.get(hostname=system_hostname)
                KeyValue.objects.filter(key__startswith='nic.%s' % adapter_number, obj=system).delete()
                #KeyValue.objects.filter(key__startswith='nic.0', system=system).delete()
                resp = rc.ALL_OK
                resp.write('json = {"id":"14"}')
            except:
                resp = rc.NOT_FOUND
                resp.write('json = {"error_message":"Unable to Delete}')

            return resp

        if 'key_type' not in request.GET:
            if 'system_id' in request.GET:
                try:
                    n = KeyValue.objects.get(id=key_value_id)
                    n.delete()
                    resp = rc.ALL_OK
                    resp.write('json = {"id":"%s"}' % str(key_value_id))
                except:
                    resp = rc.NOT_FOUND
                return resp
            if 'truth_id' in request.GET:
                try:
                    n = TruthKeyValue.objects.get(id=key_value_id)
                    n.delete()
                    resp = rc.ALL_OK
                    resp.write('json = {"id":"%s"}' % str(key_value_id))
                except:
                    resp = rc.NOT_FOUND
                return resp
        
        resp = rc.ALL_OK
        resp.write('json = {"id":"1"}')
        return resp

Example 50

Project: inventory
Source File: keyvalue_handler.py
View license
    def delete(self, request, key_value_id=None):
        if 'key_type' in request.GET and request.GET['key_type'] == 'delete_all_network_adapters':
            #Get keystores from truth that have dhcp.is_scope = True
            try:
                system_hostname = request.GET['system_hostname']
                system = System.objects.get(hostname=system_hostname)
                KeyValue.objects.filter(key__startswith='nic', obj=system).delete()
                resp = rc.ALL_OK
                resp.write('json = {"id":"0"}')
            except:
                resp = rc.NOT_FOUND
                resp.write('json = {"error_message":"Unable to Delete}')

            return resp
        if 'key_type' in request.GET and request.GET['key_type'] == 'delete_network_adapter':
            #Get keystores from truth that have dhcp.is_scope = True
            try:
                adapter_number = request.GET['adapter_number']
                system_hostname = request.GET['system_hostname']
                system = System.objects.get(hostname=system_hostname)
                KeyValue.objects.filter(key__startswith='nic.%s' % adapter_number, obj=system).delete()
                #KeyValue.objects.filter(key__startswith='nic.0', system=system).delete()
                resp = rc.ALL_OK
                resp.write('json = {"id":"14"}')
            except:
                resp = rc.NOT_FOUND
                resp.write('json = {"error_message":"Unable to Delete}')

            return resp

        if 'key_type' not in request.GET:
            if 'system_id' in request.GET:
                try:
                    n = KeyValue.objects.get(id=key_value_id)
                    n.delete()
                    resp = rc.ALL_OK
                    resp.write('json = {"id":"%s"}' % str(key_value_id))
                except:
                    resp = rc.NOT_FOUND
                return resp
            if 'truth_id' in request.GET:
                try:
                    n = TruthKeyValue.objects.get(id=key_value_id)
                    n.delete()
                    resp = rc.ALL_OK
                    resp.write('json = {"id":"%s"}' % str(key_value_id))
                except:
                    resp = rc.NOT_FOUND
                return resp
        
        resp = rc.ALL_OK
        resp.write('json = {"id":"1"}')
        return resp