Exemplo n.º 1
0
    def create(self, request, project_id):
        slz = serializers.CreateCLBSLZ(data=request.data)
        slz.is_valid(raise_exception=True)
        data = slz.validated_data
        # 校验集群权限
        clb_utils.can_use_cluster(request, project_id, data['cluster_id'])

        # 通过clb_name渲染deployment
        # 替换clb中的'_'为'-', 后缀使用6位随机字符,并且长度限制为253以内,以满足后台的限制
        # resource_name包含: clb_name[:246] + '-' + random(6)
        replaced_name = data['clb_name'].replace('_', '-')
        data[
            'resource_name'] = f'{replaced_name[:246]}-{get_random_string(6).lower()}'
        data['creator'] = data['updator'] = request.user.username
        data['project_id'] = request.project.project_id
        data['vpc_id'] = self.get_vpc_id(request, data['region'],
                                         data['clb_name'])

        # 创建并返回记录
        with client.ContextActivityLogClient(
                project_id=project_id,
                user=request.user.username,
                resource_type='lb',
                resource=data['clb_name'],
                description=_("集群:{}, 创建云lb controler").format(
                    data['cluster_id']),
        ).log_add():
            record = CloudLoadBlancer.objects.create(data)
            data = CloudLoadBlancer.objects.parse_record(record)

        return response.Response(data)
Exemplo n.º 2
0
    def post(self, request, project_id, clb_id):
        # 获取配置
        record = CloudLoadBlancer.objects.retrieve_record(clb_id)
        # 校验使用集群权限
        clb_utils.can_use_cluster(request, project_id, record['cluster_id'])
        # 获取 repo 地址
        repo_domain = paas_cc.get_jfrog_domain(request.user.token.access_token,
                                               project_id,
                                               record['cluster_id'])
        if not repo_domain:
            repo_domain = settings.DEFAUT_MESOS_LB_JFROG_DOMAIN
        record['repo_domain'] = repo_domain
        mesos_json = json.loads(render_to_string('mesos.json', record))

        with client.ContextActivityLogClient(
                project_id=project_id,
                user=request.user.username,
                resource_type='lb',
                resource=record['resource_name'],
                description=_("集群:{}, 创建clb关联deployment:{}").format(
                    record['cluster_id'], record['resource_name']),
        ).log_add():
            clb_utils.create_mesos_deployment(request.user.token.access_token,
                                              project_id, record['cluster_id'],
                                              record['namespace'], mesos_json)
        # 更改状态
        self.update_clb_status(clb_id, clb_constants.CLB_CREATED_STATUS)

        return response.Response()
Exemplo n.º 3
0
    def update(self, request, project_id, clb_id):
        slz = serializers.UpdateCLBSLZ(data=request.data)
        slz.is_valid(raise_exception=True)
        data = slz.validated_data
        # 校验使用集群权限
        clb_utils.can_use_cluster(request, project_id, data['cluster_id'])

        with client.ContextActivityLogClient(
                project_id=project_id,
                user=request.user.username,
                resource_type='lb',
                resource=data['clb_name'],
                description=_("集群:{}, 更新clb:{}").format(
                    data['cluster_id'], data['clb_name']),
        ).log_delete():
            CloudLoadBlancer.objects.update(clb_id, data)
            data = CloudLoadBlancer.objects.retrieve_record(clb_id)

        return response.Response(data)
Exemplo n.º 4
0
    def delete(self, request, project_id, clb_id):
        # 获取操作对象
        record = CloudLoadBlancer.objects.retrieve(clb_id)
        if record.status not in clb_constants.ALLOW_UPDATE_DELETE_STATUS_LIST:
            raise error_codes.CheckFailed(_('当前clb状态不允许进行删除操作'))
        # 校验使用集群权限
        clb_utils.can_use_cluster(request, project_id, record.cluster_id)

        with client.ContextActivityLogClient(
                project_id=project_id,
                user=request.user.username,
                resource_type='lb',
                resource=record.clb_name,
                description=_("集群:{}, 删除clb:{}").format(
                    record.cluster_id, record.clb_name),
        ).log_delete():
            record.delete()

        return response.Response()
Exemplo n.º 5
0
    def delete(self, request, project_id, clb_id):
        record = CloudLoadBlancer.objects.retrieve_record(clb_id)
        # 校验使用集群权限
        clb_utils.can_use_cluster(request, project_id, record['cluster_id'])

        with client.ContextActivityLogClient(
                project_id=project_id,
                user=request.user.username,
                resource_type='lb',
                resource=record['resource_name'],
                description=_("集群:{}, 删除clb关联deployment:{}").format(
                    record['cluster_id'], record['resource_name']),
        ).log_delete():
            clb_utils.delete_mesos_deployment(
                request.user.token.access_token,
                project_id,
                record['cluster_id'],
                record['namespace'],
                record['resource_name'],
            )
        # 更新状态
        self.update_clb_status(clb_id, clb_constants.CLB_DELETED_STATUS)

        return response.Response()