Beispiel #1
0
def createServiceHandler():
    '''
    create a new service
    It is handler for POST /services
    '''
    inputs = request.get_json()
    try:
        ServiceCreate_schema_validator.validate(inputs)
    except jsonschema.ValidationError as e:
        print(e)
        return jsonify(code=400, message="bad request body"), 400

    try:
        TemplateClass = tcol.get(inputs['template'])
    except TemplateNotFoundError:
        return jsonify(code=404,
                       message="template '%s' not found" %
                       inputs['template']), 404
    except TemplateConflictError:
        return jsonify(
            code=4090,
            message=
            "template with name '%s' is confusing, can't select template based only on its name, please use full UID"
            % inputs['template']), 409
    except ValueError as err:
        return jsonify(code=400, message=err.args[0]), 400

    try:
        service = tcol.instantiate_service(TemplateClass, inputs.get('name'),
                                           inputs.get('data', {}))
    except scol.ServiceConflictError:
        service = None
        return jsonify(code=409,
                       message="a service with name '%s' already exists" %
                       inputs['name']), 409
    except Exception as err:
        service = None
        return jsonify(code=500, message=str(err)), 500

    output = service_view(service)
    try:
        output['secret'] = auth.user_jwt.create({'service_guid': service.guid})
    except auth.user_jwt.SigningKeyNotFoundError as err:
        return jsonify(
            code=500,
            message='error creating user secret: no signing key available'
        ), 500
    except Exception as err:
        return jsonify(code=500,
                       message='error creating user secret: %s' %
                       str(err)), 500

    if inputs.get('public') is True:
        scol.set_service_public(service.guid)

    return jsonify(output), 201
Beispiel #2
0
    def create(self, template_uid, service_name=None, data=None, public=False):
        """
        Instantiate a service from a template on the local 0-robot

        :param template_uid: UID of the template to use a base class for the service
        :type template_uid: str
        :param service_name: name of the service, needs to be unique within the robot instance. If not specified a name is genrated, defaults to None
        :type service_name: str, optional
        :param data: data of the service to create, defaults to None
        :type data: dict
        :param public: is set to true, the service will be public, so anyone can schedule action on it, defaults to False
        :type public: bool, optional
        :return: the service created
        :rtype: Service object of the type of the template
        """
        # we can create a service locally, the local robot has the template
        template = tcol.get(template_uid)
        service = tcol.instantiate_service(template, service_name, data)
        if public:
            scol.set_service_public(service)
        return service
 def test_set_service_public(self):
     s1 = FakeService('111', 's1')
     scol.add(s1)
     assert scol.is_service_public(s1.guid) is False
     scol.set_service_public(s1.guid)
     assert scol.is_service_public(s1.guid)