Beispiel #1
0
def deleteDomain(req):
    if req.method == 'POST':
        ids = req.POST.get('domain_ids')
        id_list = ids.split(',')
        username = req.COOKIES.get('username')
        for i in id_list:
            if i:
                id_obj = Domain.objects.filter(id=i)
                domain_name = id_obj[0].domain_name
                delete_obj = DiLianManager()
                cname_obj = CName()
                status, reason, resp = delete_obj.delete(id_obj[0].distribution_id, id_obj[0].etag)
                if status == 200:
                    update_time = datetime.datetime.now()
                    id_obj.update(domain_status='Deleted', update_time=update_time)
                    cname_obj.del_cname(domain_name)
                    LOG.info('User %s delete domain %s' % (username, domain_name))
                    result = 1
                else:
                    result = Etree.fromstring(resp).find("Message").text
    return HttpResponse(result)
Beispiel #2
0
def commonResult(cdnAct, domain=None, domain_id=None):
    # domain 和 domain_id 肯定会有一个传进来,根据这个去 DB 获取 cdn_clients
    cdn_clients = ''
    if domain:
        cdn_clients = getCdnClientsFromDB(domain=domain)
    if domain_id:
        cdn_clients = getCdnClientsFromDB(domain_id=domain_id)
    if not cdn_clients:
        return jsonify({"error": "sp can not found"}), 404
    all_cdn_len = len(cdn_clients)
    success_cdn = 0
    res = {}
    for i in cdn_clients:
        aaa = ''
        exec "import ztcdn.api." + i + ".api.domainApi as aaa"
        DomainApi = getattr(aaa, 'DomainApi')
        api = DomainApi()
        result = ''
        has_success = 0
        if cdnAct == "find":
            # 传入了 domain_id, 这个 id 是 zt_domain_id
            _id = getSpDomainId(domain_id, i)
            result = api.find(_id)
            if result.getRet() == 0:
                success_cdn += 1
                has_success = 1
                saveFindDomainToDB(result.domain, i, domain_id)

        if cdnAct == "modify":
            # 传入了 domain 对象
            domain_id = domain.domainId
            _id = getSpDomainId(domain_id, i)
            c = copy.deepcopy(domain)
            c.domainId = _id
            result = api.modify(c)
            if result.getRet() == 0:
                success_cdn += 1
                # 来一遍 find 以更新库
                find_result = api.find(_id)
                if find_result.getRet() == 0:
                    saveFindDomainToDB(find_result.domain, i, domain_id, is_updated=True)

        if cdnAct == "delete":
            _id = getSpDomainId(domain_id, i)
            result = api.delete(_id)
            if result.getRet() == 0:
                success_cdn += 1
                saveDeleteSpDomainToDB(_id)

        if has_success == 1:
            res[i] = {
                "ret": 0,
                "msg": 'OK',
            }
        else:
            res[i] = {
                "ret": result.getRet(),
                "msg": result.getMsg(),
            }
        if result.domain:
            res[i]["domain"] = {
                "domain_name": result.domain.domainName,
                "domain_status": result.domain.status,
                "domain_cname": result.domain.cname,
                "domain_id": result.domain.domainId,
                "enabled": result.domain.enabled,
            }
            if hasattr(result.domain, 'testUrl'):
                res[i]["domain"]["testUrl"] = result.domain.testUrl
            if hasattr(result.domain, 'serviceType'):
                res[i]["domain"]["serviceType"] = result.domain.serviceType
            if hasattr(result.domain, 'comment'):
                res[i]["domain"]["comment"] = result.domain.comment
            if result.domain.originConfig != None:
                res[i]["domain"]["originIps"] = result.domain.originConfig.originIps
            if result.domain.etag != None:
                res[i]["domain"]["etag"] = result.domain.etag
            if result.domain.cacheBehaviors:
                cacheBehaviors = []
                for c in result.domain.cacheBehaviors:
                    cacheBehavior = {}
                    cacheBehavior["pathPattern"] = c.pathPattern
                    cacheBehavior["ignoreCacheControl"] = c.ignoreCacheControl
                    cacheBehavior["cacheTtl"] = c.cacheTtl
                    cacheBehaviors.append(cacheBehavior)
                res[i]["domain"]["cacheBehaviors"] = cacheBehaviors
    res["domain"] = {
        "domain_id": domain_id,
    }
    res["successRate"] = success_cdn / all_cdn_len * 100
    if cdnAct == "delete" and success_cdn == all_cdn_len:
        # 各厂商CDN都删除成功,设置主表状态为Delete
        # 并且删除 CName
        cname = CName()
        cname.del_cname(domain_id)
        saveDeleteDomainToDB(domain_id)
    if success_cdn != all_cdn_len:
        res_error = {"error": result.getMsg()}
        return jsonify(res_error), result.getRet()
    else:
        return jsonify(res)