Exemplo n.º 1
0
    def on_put(self, req, resp, tenant_id, profile_id):
        profile = find_host_profile(self.db, id=profile_id,
                                    when_not_found=_profile_not_found)

        body = load_body(req)
        hostname = body['name']
        ip_address = body['pattern']
Exemplo n.º 2
0
    def on_put(self, req, resp, tenant_id, profile_id):
        #verify the tenant exists
        tenant = find_tenant(self.db, tenant_id=tenant_id,
                             when_not_found=_tenant_not_found)

        #verify the profile exists
        profile = find_host_profile(self.db, id=profile_id,
                                    when_not_found=_profile_not_found)

        #verify the profile belongs to the tenant
        if not profile in tenant.profiles:
            _profile_not_found()

        #load the message
        body = load_body(req)

        #if attributes are present in message, update the profile
        if 'name' in body.keys():
            profile.name = body['name']

        if 'event_producer_ids' in body.keys():
            producer_ids = body['event_producer_ids']

            # if the put is changing event producers
            if producer_ids:

                #abort if any of the event_producers being passed in are not
                # valid event_producers for this tenant
                for producer_id in producer_ids:

                    if producer_id not in \
                            [p.id for p in tenant.event_producers]:
                        _producer_not_found()

                #remove any event producers from the profile
                # that aren't in list of event_producers being passed in
                for producer in profile.event_producers:
                    if producer.id not in producer_ids:
                        profile.event_producers.remove(producer)

                #see if the incoming event_producers are missing from the
                # profile, and
                for pid in producer_ids:
                    if pid not in [p.id for p in profile.event_producers]:
                        profile.event_producers.append(
                            [p for p in tenant.event_producers
                             if p.id == pid][0])

            else:
                profile.event_producers = []

        self.db.commit()
        resp.status = falcon.HTTP_200
Exemplo n.º 3
0
    def on_put(self, req, resp, tenant_id, host_id):
        host = find_host(self.db, host_id,
                         when_not_found=_host_not_found)

        body = load_body(req)
        profile_id = body['profile_id']

        profile = find_host_profile(self.db, id=profile_id,
                                    when_not_found=_profile_not_found)
        host.profile = profile
        self.db.commit()

        resp.status = falcon.HTTP_200
Exemplo n.º 4
0
    def on_get(self, req, resp, tenant_id, profile_id):
        #verify the tenant exists
        tenant = find_tenant(self.db, tenant_id=tenant_id,
                             when_not_found=_tenant_not_found)

        #verify the profile exists
        profile = find_host_profile(self.db, id=profile_id,
                                    when_not_found=_profile_not_found)

        #verify the profile belongs to the tenant
        if not profile in tenant.profiles:
            _profile_not_found()

        resp.status = falcon.HTTP_200
        resp.body = json.dumps(profile.format())
Exemplo n.º 5
0
    def on_delete(self, req, resp, tenant_id, profile_id):
        #verify the tenant exists
        tenant = find_tenant(self.db, tenant_id=tenant_id,
                             when_not_found=_tenant_not_found)

        #verify the profile exists
        profile = find_host_profile(self.db, id=profile_id,
                                    when_not_found=_profile_not_found)

        #verify the profile belongs to the tenant
        if not profile in tenant.profiles:
            _profile_not_found()

        self.db.delete(profile)
        self.db.commit()

        resp.status = falcon.HTTP_200
Exemplo n.º 6
0
    def on_put(self, req, resp, tenant_id, host_id):
        #verify the tenant exists
        tenant = find_tenant(self.db, tenant_id=tenant_id,
                             when_not_found=_tenant_not_found)

        #verify the hosts exists
        host = find_host(self.db, host_id,
                         when_not_found=_host_not_found)

        #verify the host belongs to the tenant
        if not host in tenant.hosts:
            _host_not_found()

        body = load_body(req)

        if 'hostname' in body.keys():
            host.hostname = body['hostname']

        if 'ip_address' in body.keys():
            host.ip_address = body['ip_address']

        if 'profile_id' in body.keys():
            profile_id = body['profile_id']

            #if profile_id has a new value,
            # find the profile and assign it to the host
            if profile_id:
                profile = find_host_profile(self.db, id=profile_id,
                                            when_not_found=_profile_not_found)
                host.profile = profile

            #if the profile id key passed an empty value,
            # then remove the profile form the host
            else:
                host.profile = None

        self.db.commit()

        resp.status = falcon.HTTP_200
Exemplo n.º 7
0
    def on_post(self, req, resp, tenant_id):
        tenant = find_tenant(self.db, tenant_id=tenant_id,
                             when_not_found=_tenant_not_found)

        body = load_body(req)
        hostname = body['hostname']
        ip_address = body['ip_address']

        #if profile id is not in post message, then use a null profile
        if 'profile_id' in body.keys():
            profile_id = body['profile_id']
        else:
            profile_id = None
            profile = None

        #if profile id is in post message, then make sure it is valid profile
        if profile_id:
            profile = find_host_profile(self.db, id=profile_id,
                                        when_not_found=_profile_not_found)

        # Check if the tenant already has a host with this hostname
        for host in tenant.hosts:
            if host.hostname == hostname:
                abort(falcon.HTTP_400,
                      'Host with hostname {0} already exists with'
                      ' id={1}'.format(hostname, host.id))

        # Create the new host definition
        new_host = Host(hostname, ip_address, profile)

        self.db.add(new_host)
        tenant.hosts.append(new_host)
        self.db().commit()

        resp.status = falcon.HTTP_201
        resp.set_header('Location',
                        '/v1/{0}/hosts/{1}'
                        .format(tenant_id, new_host.id))
Exemplo n.º 8
0
    def on_get(self, req, resp, tenant_id, profile_id):
        profile = find_host_profile(self.db, id=profile_id,
                                    when_not_found=_profile_not_found)

        resp.status = falcon.HTTP_200
        resp.body = json.dumps(format_host_profile(profile))