Example #1
0
    def create_third_party_app(self, region, tenant, user, service_cname,
                               endpoints, endpoints_type):
        service_cname = service_cname.rstrip().lstrip()
        is_pass, msg = self.check_service_cname(tenant, service_cname, region)
        if not is_pass:
            return 412, msg, None
        # 初始化
        new_service = self.__init_third_party_app(region, endpoints)
        new_service.tenant_id = tenant.tenant_id
        new_service.service_cname = service_cname
        service_id = make_uuid(tenant.tenant_id)
        service_alias = self.create_service_alias(service_id)
        new_service.service_id = service_id
        new_service.service_alias = service_alias
        new_service.creater = user.pk
        new_service.server_type = ''
        new_service.protocol = 'tcp'
        new_service.save()
        if endpoints_type == "static":
            # 如果只有一个端口,就设定为默认端口,没有或有多个端口,不设置默认端口
            if endpoints:
                from console.views.app_create.source_outer import check_endpoints
                errs, isDomain = check_endpoints(endpoints)
                if errs:
                    return 400, u"组件地址不合法", None
                port_list = []
                prefix = ""
                protocol = "tcp"
                for endpoint in endpoints:
                    if 'https://' in endpoint:
                        endpoint = endpoint.split('https://')[1]
                        prefix = "https"
                        protocol = "http"
                    if 'http://' in endpoint:
                        endpoint = endpoint.split('http://')[1]
                        prefix = "http"
                        protocol = "http"
                    if ':' in endpoint:
                        port_list.append(endpoint.split(':')[1])
                if len(port_list) == 0 and isDomain is True and prefix != "":
                    port_list.append(443 if prefix == "https" else 80)
                port_re = list(set(port_list))
                if len(port_re) == 1:
                    port = int(port_re[0])
                    if port:
                        port_alias = new_service.service_alias.upper().replace(
                            "-", "_") + str(port)
                        service_port = {
                            "tenant_id": tenant.tenant_id,
                            "service_id": new_service.service_id,
                            "container_port": port,
                            "mapping_port": port,
                            "protocol": protocol,
                            "port_alias": port_alias,
                            "is_inner_service": False,
                            "is_outer_service": False
                        }
                        service_port = port_repo.add_service_port(
                            **service_port)

        # 保存endpoints数据
        service_endpoints = {
            "tenant_id": tenant.tenant_id,
            "service_id": new_service.service_id,
            "service_cname": new_service.service_cname,
            "endpoints_info": json.dumps(endpoints),
            "endpoints_type": endpoints_type
        }
        logger.debug('------service_endpoints------------->{0}'.format(
            service_endpoints))
        service_endpoints_repo.add_service_endpoints(service_endpoints)

        ts = TenantServiceInfo.objects.get(service_id=new_service.service_id,
                                           tenant_id=new_service.tenant_id)
        return 200, u"创建成功", ts
Example #2
0
    def create_third_party_app(self, region, tenant, user, service_cname,
                               endpoints, endpoints_type):
        service_cname = service_cname.rstrip().lstrip()
        is_pass, msg = self.check_service_cname(tenant, service_cname, region)
        if not is_pass:
            raise ServiceHandleException(msg=msg,
                                         msg_show="组件名称不合法",
                                         status_code=400,
                                         error_code=400)
        # 初始化
        new_service = self.__init_third_party_app(region, endpoints)
        new_service.tenant_id = tenant.tenant_id
        new_service.service_cname = service_cname
        service_id = make_uuid(tenant.tenant_id)
        service_alias = self.create_service_alias(service_id)
        new_service.service_id = service_id
        new_service.service_alias = service_alias
        new_service.creater = user.pk
        new_service.server_type = ''
        new_service.protocol = 'tcp'
        new_service.save()
        if endpoints_type == "static":
            # 如果只有一个端口,就设定为默认端口,没有或有多个端口,不设置默认端口
            if endpoints:
                from console.views.app_create.source_outer import \
                    check_endpoints
                errs, isDomain = check_endpoints(endpoints)
                if errs:
                    return 400, "组件地址不合法", None
                port_list = []
                prefix = ""
                protocol = "tcp"
                for endpoint in endpoints:
                    if 'https://' in endpoint:
                        endpoint = endpoint.split('https://')[1]
                        prefix = "https"
                        protocol = "http"
                    if 'http://' in endpoint:
                        endpoint = endpoint.split('http://')[1]
                        prefix = "http"
                        protocol = "http"
                    if ':' in endpoint:
                        port_list.append(endpoint.split(':')[1])
                if len(port_list) == 0 and isDomain is True and prefix != "":
                    port_list.append(443 if prefix == "https" else 80)
                port_re = list(set(port_list))
                if len(port_re) == 1:
                    port = int(port_re[0])
                    if port:
                        port_alias = new_service.service_alias.upper().replace(
                            "-", "_") + str(port)
                        service_port = {
                            "tenant_id":
                            tenant.tenant_id,
                            "service_id":
                            new_service.service_id,
                            "container_port":
                            port,
                            "mapping_port":
                            port,
                            "protocol":
                            protocol,
                            "port_alias":
                            port_alias,
                            "is_inner_service":
                            False,
                            "is_outer_service":
                            False,
                            "k8s_service_name":
                            new_service.service_alias + "-" + str(port),
                        }
                        port_repo.add_service_port(**service_port)
                service_endpoints_repo.update_or_create_endpoints(
                    tenant, new_service, endpoints)

        ts = TenantServiceInfo.objects.get(service_id=new_service.service_id,
                                           tenant_id=new_service.tenant_id)
        return ts