Ejemplo n.º 1
0
    def patch(self, project_id):
        """
        """

        try:
            current_user_id = get_jwt_identity()
            current_user_roles = get_jwt_claims()['roles']

            project_schema = ProjectSchema(only=("name", "description"),
                                           partial=True)

            project_data = request.get_json()

            validate_project_data, errors = project_schema.load(project_data)

            existing_project = False

            if errors:
                return dict(status='fail', message=errors), 400

            if 'name' in validate_project_data:
                existing_project = Project.find_first(
                    name=validate_project_data['name'],
                    owner_id=current_user_id)

            if existing_project:
                return dict(
                    status='fail',
                    message=
                    f'project with name {validate_project_data["name"]} already exists'
                ), 409

            project = Project.get_by_id(project_id)

            if not project:
                return dict(status='fail',
                            message=f'Project {project_id} not found'), 404

            if not is_owner_or_admin(project, current_user_id,
                                     current_user_roles):
                return dict(status='fail', message='unauthorised'), 403

            updated = Project.update(project, **validate_project_data)

            if not updated:
                return dict(status='fail',
                            message='internal server error'), 500

            return dict(
                status='success',
                message=f'project {project_id} updated successfully'), 200

        except Exception as e:
            return dict(status='fail', message=str(e)), 500
Ejemplo n.º 2
0
    def post(self):
        """
        """

        current_user_id = get_jwt_identity()
        current_user_roles = get_jwt_claims()['roles']

        project_schema = ProjectSchema()

        project_data = request.get_json()

        validated_project_data, errors = project_schema.load(project_data)

        if errors:
            return dict(status='fail', message=errors), 400

        if not has_role(current_user_roles, 'administrator'):
            validated_project_data['owner_id'] = current_user_id

        # check if project already exists
        existing_project = Project.find_first(
            name=validated_project_data['name'],
            owner_id=validated_project_data['owner_id'])

        if existing_project:
            return dict(
                status='fail',
                message=
                f'project with name {validated_project_data["name"]} already exists'
            ), 409

        try:
            validated_project_data['alias'] =\
                create_alias(validated_project_data['name'])
            namespace_name = validated_project_data['alias']
            cluster_id = validated_project_data['cluster_id']
            cluster = Cluster.get_by_id(cluster_id)

            if not cluster:
                return dict(status='fail',
                            message=f'cluster {cluster_id} not found'), 404

            kube_host = cluster.host
            kube_token = cluster.token

            kube_client = create_kube_clients(kube_host, kube_token)

            # create namespace in cluster
            cluster_namespace = kube_client.kube.create_namespace(
                client.V1Namespace(metadata=client.V1ObjectMeta(
                    name=namespace_name)))
            # create project in database
            if cluster_namespace:

                ingress_name = f"{validated_project_data['alias']}-ingress"

                ingress_meta = client.V1ObjectMeta(name=ingress_name)

                ingress_default_rule = client.ExtensionsV1beta1IngressRule(
                    host="traefik-ui.cranecloud.io",
                    http=client.ExtensionsV1beta1HTTPIngressRuleValue(paths=[
                        client.ExtensionsV1beta1HTTPIngressPath(
                            path="/*",
                            backend=client.ExtensionsV1beta1IngressBackend(
                                service_name="traefik-web-ui-ext",
                                service_port=80))
                    ]))

                ingress_spec = client.ExtensionsV1beta1IngressSpec(
                    rules=[ingress_default_rule])

                ingress_body = client.ExtensionsV1beta1Ingress(
                    metadata=ingress_meta, spec=ingress_spec)

                kube_client.extension_api.create_namespaced_ingress(
                    namespace=namespace_name, body=ingress_body)

                project = Project(**validated_project_data)

                saved = project.save()

                if not saved:
                    # delete the namespace
                    kube_client.kube.delete_namespace(namespace_name)
                    return dict(status='fail',
                                message='Internal Server Error'), 500

            new_project_data, errors = project_schema.dump(project)

            return dict(status='success',
                        data=dict(project=new_project_data)), 201

        except client.rest.ApiException as e:
            return dict(status='fail', message=e.body), e.status

        except Exception as err:
            return dict(status='fail', message=str(err)), 500