Esempio n. 1
0
    def service_import_set_site(self, site_name):
        try:
            if not site_name or site_name.title() == 'None':
                site = None
            else:
                site = Site.objects.get(full_name=site_name)
        except Site.DoesNotExist:
            raise System.DoesNotExist(
                "The site with name '{0}' does not exist".format(site_name))

        self.site = site
Esempio n. 2
0
 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)
Esempio n. 3
0
    def import_services(cls, services):
        """
        Import a json blob that has the same format generated by the
        Service.export_services function. This function may raise a
        System.DoesNotExist, Service.DoesNotExist, ValueError, or
        Site.DoesNotExist excpetion. The caller should handle these accordingly

        This function does not manage transactions itself but should probably
        be ran in a transaction
        """
        for service_blob in services:
            try:
                site = service_blob.get('site', None)
                if site:
                    site = Site.objects.get(full_name=site)
                if 'pk' in service_blob:
                    service = Service.objects.get(pk=service_blob['pk'])
                else:
                    service, created = Service.objects.get_or_create(
                        name=service_blob.get('name', ''), site=site)
            except Service.DoesNotExist:
                raise Service.DoesNotExist(
                    "The service with pk '{0}' does not "
                    "exist".format(service_blob['pk']))
            except Site.DoesNotExist:
                raise System.DoesNotExist(
                    "The service with name '{0}' is asking for a site '{1}' "
                    "that does not exist".format(
                        service_blob.get('name', 'None'),
                        service_blob.get('site', 'None')))

            # set all the fields on the service we are importing
            for field, value in service_blob.iteritems():
                if field == 'systems':
                    service.service_import_set_systems(value)
                elif field == 'parent_service':
                    service.service_import_set_parent_service(value)
                elif field == 'site':
                    service.service_import_set_site(value)
                elif field == 'pk':
                    # lets not let the user shoot themselves in the foot
                    continue
                elif field == 'depends_on':
                    service.service_import_set_depends_on(value)
                else:
                    service.service_import_set_field(field, value)

            service.save()