def xmlToFlowPointList(ret, reportType): ''' 返回xml 转换成 带 FlowPoint对象列表的FlowProcessResult对象''' global X_CNC_REQUEST_ID, X_CNC_LOCATION requestId = ret.getheader(X_CNC_REQUEST_ID) isoFormat = getDateFormat(reportType) xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) flowPointListNode = util.getChildNode(doc, 'flow-report') flowSummary = util.getChildNodeText(flowPointListNode, 'flow-summary') flowPointList = [] flowDataList = util.getChildNodeList(flowPointListNode, 'flow-data') for flowNode in flowDataList: pointStr = util.getChildNodeText(flowNode, 'timestamp') gaga = re.sub('24:00:00', '00:00:00', pointStr) point = datetime.datetime.strptime(gaga, isoFormat) flow = util.getChildNodeText(flowNode, 'flow') flowPoint = FlowPoint(point, flow) flowPointList.append(flowPoint) return FlowProcessResult(ret.status, 'OK', xCncRequestId=requestId, flowPoints=flowPointList, flowSummary=flowSummary)
def makeHeaders(self, uri): ''' 组装头部 ''' global X_CNC_DATE headers = self.headers.copy() headers[X_CNC_DATE] = util.getRFCTime() key = util.hashPassword(headers[X_CNC_DATE], self.apiKey) headers['Authorization'] = "Basic " + base64.standard_b64encode(self.user + ':' + key) return headers
def makeHeaders(self, uri): ''' 组装头部 ''' global X_CNC_DATE headers = self.headers.copy() headers[X_CNC_DATE] = util.getRFCTime() key = util.hashPassword(headers[X_CNC_DATE], self.apiKey) headers['Authorization'] = "Basic " + base64.standard_b64encode( self.user + ':' + key) return headers
def xmlToDomainList(ret): ''' 返回xml 转换成 带 Domain对象列表的ProcessResult对象, 在查询用户下所有频道时候使用''' global X_CNC_REQUEST_ID, X_CNC_LOCATION requestId = ret.getheader(X_CNC_REQUEST_ID) xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) domainListNode = util.getChildNode(doc, 'domain-list') domainList = [] domainSummaryList = util.getChildNodeList(domainListNode, 'domain-summary') for domainNode in domainSummaryList: domainId = util.getChildNodeText(domainNode, 'domain-id') cname = util.getChildNodeText(domainNode, 'cname') domainName = util.getChildNodeText(domainNode, 'domain-name') status = util.getChildNodeText(domainNode, 'status') serviceType = util.getChildNodeText(domainNode, "service-type") enabled = util.getChildNodeText(domainNode, 'enabled') == 'true' cdnServiceStatus = util.getChildNodeText( domainNode, 'cdn-service-status') == 'true' domainSummary = DomainSummary(domainId, domainName, cname, status, enabled, serviceType, cdnServiceStatus) domainList.append(domainSummary) return ProcessResult(0, 'OK', xCncRequestId=requestId, domainSummarys=domainList)
def parseVisitControlRulesList(nodeList): vistControlRulesList = [] for node in nodeList: pathPattern = util.getChildNodeText(node, 'path-pattern') allowNullReffer = util.getChildNodeText(node, 'allownullreferer') validReferRootNode = util.getChildNode(node, "valid-referers") validRNode = util.getChildNodeList(validReferRootNode, 'referer') validRefers = [] for ref in validRNode: validRefers.append(util.getChildNodeText(ref, "referer")) invalidReferRootNode = util.getChildNode(node, "invalid-referers") invalidRNode = util.getChildNodeList(invalidReferRootNode, 'referer') invalidRefers = [] for ref in invalidRNode: invalidRefers.append(util.getChildNodeText(ref, "referer")) forbiddenIps = splitStr(util.getChildNodeText(node, 'forbidden-ips')) visitControlRule = VisitControlRule(pathPattern, allowNullReffer, validRefers, invalidRefers, forbiddenIps) vistControlRulesList.append(visitControlRule) return vistControlRulesList
def parseQueryStringSettingListNode(nodeList): queryStringSettingList = [] for queryStringSetting in nodeList: pathPattern = util.getChildNodeText(queryStringSetting, 'path-pattern') ignoreQueryStringStr = util.getChildNodeText(queryStringSetting, 'ignore-query-string') if ignoreQueryStringStr == "false": ignoreQueryString = False else: ignoreQueryString = True queryStringSetting = QueryStringSetting(pathPattern, ignoreQueryString) queryStringSettingList.append(queryStringSetting) return queryStringSettingList
def parseCacheBehaviorList(nodeList): cacheBehaviorList = [] for cacheBehavior in nodeList: pathPattern = util.getChildNodeText(cacheBehavior, 'path-pattern') priority = util.getChildNodeText(cacheBehavior, 'priority') ignoreCacheControlStr = util.getChildNodeText(cacheBehavior, 'ignore-cache-control') if ignoreCacheControlStr == "false": ignoreCacheControl = False else: ignoreCacheControl = True cacheTTL = util.getChildNodeText(cacheBehavior, 'cache-ttl') cacheBehavior = CacheBehavior(pathPattern, ignoreCacheControl, cacheTTL) cacheBehaviorList.append(cacheBehavior) return cacheBehaviorList
def parseAdvOriginConfigList(nodeList): advOriginConfigList = [] for advOriginConfigNode in nodeList: ispsList = util.getChildNodeText(advOriginConfigNode, 'isp') masterIpsList = util.getChildNodeText(advOriginConfigNode, 'master-ips') backupIpsList = util.getChildNodeText(advOriginConfigNode, 'backup-ips') detectUrl = util.getChildNodeText(advOriginConfigNode, 'detect-url') detectPeriod = util.getChildNodeText(advOriginConfigNode, 'detect-period') isps = splitStr(ispsList) masterIps = splitStr(masterIpsList) backupIps = splitStr(backupIpsList) advOriginConfig = AdvOriginConfig(isps = isps, masterIps = masterIps, backupIps = backupIps, detectUrl = detectUrl, detectPeriod = detectPeriod) advOriginConfigList.append(advOriginConfig) return advOriginConfigList
def xmlToSuccess(ret): ''' 返回xml 转换成 成功返回的ProcessResult对象''' global X_CNC_REQUEST_ID, X_CNC_LOCATION, X_CNC_CNAME requestId = ret.getheader(X_CNC_REQUEST_ID) location = ret.getheader(X_CNC_LOCATION) cname = ret.getheader(X_CNC_CNAME) msg = util.getReturnXmlMsg(ret) return ProcessResult(ret.status, msg, xCncRequestId = requestId, location = location, cname = cname)
def appendParams(url, reportForm): dateFrom = reportForm.getDateFrom() dateTo = reportForm.getDateTo() reportType = reportForm.getReportType() originUrl = url + "?" if dateFrom or dateTo or type: url = url + "?" if dateFrom: url = url + "datefrom=" + util.getRFC3339Time(str(dateFrom)).replace('+', '%2B') if dateTo: if url == originUrl: url = originUrl + "dateto=" + util.getRFC3339Time(dateTo).replace('+', '%2B') else: url = url + "&dateto=" + util.getRFC3339Time(dateTo).replace('+', '%2B') if reportType: if url == originUrl: url = originUrl + "type=" + reportType else: url = url + "&type=" + reportType return url
def parseAdvOriginConfigList(nodeList): advOriginConfigList = [] for advOriginConfigNode in nodeList: ispsList = util.getChildNodeText(advOriginConfigNode, 'isp') masterIpsList = util.getChildNodeText(advOriginConfigNode, 'master-ips') backupIpsList = util.getChildNodeText(advOriginConfigNode, 'backup-ips') detectUrl = util.getChildNodeText(advOriginConfigNode, 'detect-url') detectPeriod = util.getChildNodeText(advOriginConfigNode, 'detect-period') isps = splitStr(ispsList) masterIps = splitStr(masterIpsList) backupIps = splitStr(backupIpsList) advOriginConfig = AdvOriginConfig(isps=isps, masterIps=masterIps, backupIps=backupIps, detectUrl=detectUrl, detectPeriod=detectPeriod) advOriginConfigList.append(advOriginConfig) return advOriginConfigList
def xmlToSuccess(ret): ''' 返回xml 转换成 成功返回的ProcessResult对象''' global X_CNC_REQUEST_ID, X_CNC_LOCATION, X_CNC_CNAME requestId = ret.getheader(X_CNC_REQUEST_ID) location = ret.getheader(X_CNC_LOCATION) cname = ret.getheader(X_CNC_CNAME) msg = util.getReturnXmlMsg(ret) return ProcessResult(ret.status, msg, xCncRequestId=requestId, location=location, cname=cname)
def xmlToHitPointList(ret, reportType): ''' 返回xml 转换成 带 HitPoint对象列表的HitProcessResult对象 ''' global X_CNC_REQUEST_ID, X_CNC_LOCATION requestId = ret.getheader(X_CNC_REQUEST_ID) isoFormat = getDateFormat(reportType) xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) hitPointListNode = util.getChildNode(doc, 'hit-report') hitPointList = [] hitDataList = util.getChildNodeList(hitPointListNode, 'hit-data') for hitNode in hitDataList: pointStr = util.getChildNodeText(hitNode, 'timestamp') gaga = re.sub('24:00:00', '00:00:00', pointStr) point = datetime.datetime.strptime(gaga, isoFormat) hit = util.getChildNodeText(hitNode, 'hit') hitPoint = HitPoint(point, hit) hitPointList.append(hitPoint) return HitProcessResult(ret.status, 'OK', xCncRequestId = requestId, hitPoints = hitPointList)
def xmlToLogList(ret): ''' 返回xml 转换成 带Log对象列表的LogProcessResult对象 ''' global X_CNC_REQUEST_ID requestId = ret.getheader(X_CNC_REQUEST_ID) isoFormat = "%Y-%m-%d-%H%M" xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) logListNode = util.getChildNode(doc, 'logs') logList = [] logDataList = util.getChildNodeList(logListNode, 'log') for logNode in logDataList: dateFromStr = util.getChildNodeText(logNode, 'datefrom') dateToStr = util.getChildNodeText(logNode, 'dateto') url = util.getChildNodeText(logNode, 'log-url') fileSize = util.getChildNodeText(logNode, 'file-size') dateFrom = datetime.datetime.strptime(dateFromStr, isoFormat) dateTo = datetime.datetime.strptime(dateToStr, isoFormat) log = Log(dateFrom, dateTo, url, fileSize) logList.append(log) return LogProcessResult(ret.status, 'OK', xCncRequestId=requestId, logs=logList)
def xmlToLogList(ret): ''' 返回xml 转换成 带Log对象列表的LogProcessResult对象 ''' global X_CNC_REQUEST_ID requestId = ret.getheader(X_CNC_REQUEST_ID) isoFormat = "%Y-%m-%d-%H%M" xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) logListNode = util.getChildNode(doc, 'logs') logList = [] logDataList = util.getChildNodeList(logListNode, 'log') for logNode in logDataList: dateFromStr = util.getChildNodeText(logNode, 'datefrom') dateToStr = util.getChildNodeText(logNode, 'dateto') url = util.getChildNodeText(logNode, 'log-url') fileSize = util.getChildNodeText(logNode, 'file-size') dateFrom = datetime.datetime.strptime(dateFromStr, isoFormat) dateTo = datetime.datetime.strptime(dateToStr, isoFormat) log = Log(dateFrom, dateTo, url, fileSize) logList.append(log) return LogProcessResult(ret.status, 'OK', xCncRequestId = requestId, logs = logList);
def xmlToFlowPointList(ret, reportType): ''' 返回xml 转换成 带 FlowPoint对象列表的FlowProcessResult对象''' global X_CNC_REQUEST_ID, X_CNC_LOCATION requestId = ret.getheader(X_CNC_REQUEST_ID) isoFormat = getDateFormat(reportType) xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) flowPointListNode = util.getChildNode(doc, 'flow-report') flowSummary = util.getChildNodeText(flowPointListNode, 'flow-summary') flowPointList = [] flowDataList = util.getChildNodeList(flowPointListNode, 'flow-data') for flowNode in flowDataList: pointStr = util.getChildNodeText(flowNode, 'timestamp') gaga = re.sub('24:00:00', '00:00:00', pointStr) point = datetime.datetime.strptime(gaga, isoFormat) flow = util.getChildNodeText(flowNode, 'flow') flowPoint = FlowPoint(point, flow) flowPointList.append(flowPoint) return FlowProcessResult(ret.status, 'OK', xCncRequestId = requestId, flowPoints = flowPointList, flowSummary = flowSummary);
def appendParams(url, reportForm): dateFrom = reportForm.getDateFrom() dateTo = reportForm.getDateTo() reportType = reportForm.getReportType() originUrl = url + "?" if dateFrom or dateTo or type: url = url + "?" if dateFrom: url = url + "datefrom=" + util.getRFC3339Time(str(dateFrom)).replace( '+', '%2B') if dateTo: if url == originUrl: url = originUrl + "dateto=" + util.getRFC3339Time(dateTo).replace( '+', '%2B') else: url = url + "&dateto=" + util.getRFC3339Time(dateTo).replace( '+', '%2B') if reportType: if url == originUrl: url = originUrl + "type=" + reportType else: url = url + "&type=" + reportType return url
def xmlToHitPointList(ret, reportType): ''' 返回xml 转换成 带 HitPoint对象列表的HitProcessResult对象 ''' global X_CNC_REQUEST_ID, X_CNC_LOCATION requestId = ret.getheader(X_CNC_REQUEST_ID) isoFormat = getDateFormat(reportType) xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) hitPointListNode = util.getChildNode(doc, 'hit-report') hitPointList = [] hitDataList = util.getChildNodeList(hitPointListNode, 'hit-data') for hitNode in hitDataList: pointStr = util.getChildNodeText(hitNode, 'timestamp') gaga = re.sub('24:00:00', '00:00:00', pointStr) point = datetime.datetime.strptime(gaga, isoFormat) hit = util.getChildNodeText(hitNode, 'hit') hitPoint = HitPoint(point, hit) hitPointList.append(hitPoint) return HitProcessResult(ret.status, 'OK', xCncRequestId=requestId, hitPoints=hitPointList)
def listAll(self): ''' 获取加速所有域名列表 @rtype: ProcessResult对象 @return: 通过ProcessResult.getDomainSummarys()获取DomainSummary对象的实例列表 ''' url = self.HOST + "/api/domain" try: post = '' ret = util.httpReqeust(url, post, self.makeHeaders(url), "GET") if ret.status == 200: return xmlToDomainList(ret) else: return xmlToFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return ProcessResult(-1, str(e))
def getLog(self, reportForm, domainId): ''' 获取某域名的日志下载链接 @type reportForm: ReportForm @param reportForm: 请求的起止时间 @rtype: LogProcessResult @return: 通过LogProcessResult.getLogs() 获得查询后的Log对象实例列表 ''' url = self.HOST + "/api/report/" + str(domainId) + "/log" try: url = appendParams(url, reportForm) ret = util.httpReqeust(url, "", self.makeHeaders(), "GET") if ret.status == 200: return xmlToLogList(ret) else: return getLogXmlToFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return LogProcessResult(-1, str(e))
def enable(self, domainId): ''' 启用加速域名 @param domainId : 指定启用的域名ID @rtype: ProcessResult对象 @return: 返回ProcessResult对象 ''' url = self.HOST + "/api/domain/" + str(domainId) try: post = '' ret = util.httpReqeust(url, post, self.makeHeaders(url), "ENABLE") if ret.status == 202: return xmlToSuccess(ret) else: return xmlToFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return ProcessResult(-1, str(e))
def add(self, domain): ''' 创建加速域名 @param domain: 新增加速域名构建的Domain对象实例 @rtype: ProcessResult对象 @return: 通过ProcessResult.getLocation()新域名的url ''' url = self.HOST + "/api/domain" try: post = domainToXml(domain) #print post ret = util.httpReqeust(url, post, self.makeHeaders(url), "POST") if ret.status == 202: return xmlToSuccess(ret) else: return xmlToFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return ProcessResult(-1, str(e))
def xmlToDomainList(ret): ''' 返回xml 转换成 带 Domain对象列表的ProcessResult对象, 在查询用户下所有频道时候使用''' global X_CNC_REQUEST_ID, X_CNC_LOCATION requestId = ret.getheader(X_CNC_REQUEST_ID) xmlString = ret.read().decode("utf-8") logging.debug("response:" + xmlString) doc = minidom.parseString(xmlString) domainListNode = util.getChildNode(doc, 'domain-list') domainList = [] domainSummaryList = util.getChildNodeList(domainListNode, 'domain-summary') for domainNode in domainSummaryList: domainId = util.getChildNodeText(domainNode, 'domain-id') cname = util.getChildNodeText(domainNode, 'cname') domainName = util.getChildNodeText(domainNode, 'domain-name') status = util.getChildNodeText(domainNode, 'status') serviceType = util.getChildNodeText(domainNode, "service-type") enabled = util.getChildNodeText(domainNode, 'enabled') == 'true' cdnServiceStatus = util.getChildNodeText(domainNode, 'cdn-service-status') == 'true' domainSummary = DomainSummary(domainId, domainName, cname, status, enabled, serviceType, cdnServiceStatus) domainList.append(domainSummary) return ProcessResult(0, 'OK', xCncRequestId = requestId, domainSummarys = domainList);
def find(self, domainId): ''' 获取加速域名配置 @type domainId: str @param domainId : 指定查找的域名ID @rtype: ProcessResult对象 @return: 通过ProcessResult.getDomain()返回指定的域名信息的Domain实例 ''' url = self.HOST + "/api/domain/" + str(domainId) try: post = '' ret = util.httpReqeust(url, post, self.makeHeaders(url), "GET") if ret.status == 200: return xmlToDomain(ret) else: return xmlToFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return ProcessResult(-1, str(e))
def getHitReport(self, reportForm, domainId = None): ''' 获取某域名请求数报表 如果domainId 为None,表示 查汇总信息 @type reportForm: ReportForm @param reportForm: 请求的时间和报表粒度 @rtype: HitProcessResult @return: 通过HitProcessResult.getHitPoints() 获得返回的结果 ''' if domainId == None: url = self.HOST + "/api/report/hit" else: url = self.HOST + "/api/report/" + str(domainId) + "/hit" try: url = appendParams(url, reportForm) ret = util.httpReqeust(url, "", self.makeHeaders(), "GET") if ret.status == 200: return xmlToHitPointList(ret, reportForm.getReportType()) else: return getHitReportXmlToDefaultFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return HitProcessResult(-1, str(e))
def getHitReport(self, reportForm, domainId=None): ''' 获取某域名请求数报表 如果domainId 为None,表示 查汇总信息 @type reportForm: ReportForm @param reportForm: 请求的时间和报表粒度 @rtype: HitProcessResult @return: 通过HitProcessResult.getHitPoints() 获得返回的结果 ''' if domainId == None: url = self.HOST + "/api/report/hit" else: url = self.HOST + "/api/report/" + str(domainId) + "/hit" try: url = appendParams(url, reportForm) ret = util.httpReqeust(url, "", self.makeHeaders(), "GET") if ret.status == 200: return xmlToHitPointList(ret, reportForm.getReportType()) else: return getHitReportXmlToDefaultFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return HitProcessResult(-1, str(e))
def modify(self, domain): ''' 修改加速域名配置 @type domain: Domain @param domain : 构建需要修改的域名的Domain实例, domain中必须设置domanId字段 @rtype: ProcessResult对象 @return: 返回ProcessResult对象 ''' if domain.domainId is None: raise '请设置domainId字段' url = self.HOST + "/api/domain/" + str(domain.domainId) try: post = domainToXml(domain) #print post ret = util.httpReqeust(url, post, self.makeHeaders(url), "PUT") if ret.status == 202: return xmlToSuccess(ret) else: return xmlToFailure(ret) except Exception, e: traceback.print_exc(file=sys.stdout) return ProcessResult(-1, str(e))
def xmlToFailure(ret): msg = util.getReturnXmlMsg(ret) return ProcessResult(ret.status, ret.reason + ":" + msg)
def getHitReportXmlToDefaultFailure(ret): msg = util.getReturnXmlMsg(ret) return HitProcessResult(ret.status, ret.reason + ":" + msg)
def domainToXml(domain): ''' Domain 对象 转换成 xml ''' doc = dom.getDOMImplementation().createDocument('', 'domain', '') domainNode = util.getChildNode(doc, 'domain') util.addElement(doc, domainNode, 'version', "1.0.0") if domain.domainName is not None: util.addElement(doc, domainNode, 'domain-name', domain.domainName) if domain.serviceType is not None: util.addElement(doc, domainNode, 'service-type', domain.serviceType) if domain.comment is not None: util.addElement(doc, domainNode, 'comment', domain.comment) if domain.serviceAreas is not None: util.addElement(doc, domainNode, 'service-areas', domain.serviceAreas) else: util.addElement(doc, domainNode, 'service-areas', 'cn') if domain.originConfig is not None: originConfigNode = util.addElement(doc, domainNode, 'origin-config') if domain.originConfig.originIps is not None: originIps = domain.originConfig.originIps util.addElement(doc, originConfigNode, 'origin-ips', ';'.join(originIps)) if domain.originConfig.originDomainName is not None: util.addElement(doc, originConfigNode, 'origin-domain-name', domain.originConfig.originDomainName) if domain.originConfig.advOriginConfigs is not None: advOriginConfigsNode = util.addElement(doc, originConfigNode, 'adv-origin-configs') for advOriginConfig in domain.originConfig.advOriginConfigs: isps = advOriginConfig.isps advOriginConfigNode = util.addElement(doc, advOriginConfigsNode, 'adv-origin-config') util.addElement(doc, advOriginConfigNode, 'isp', ';'.join(isps)) util.addElement(doc, advOriginConfigNode, 'master-ips', ';'.join(advOriginConfig.masterIps)) util.addElement(doc, advOriginConfigNode, 'backup-ips', ';'.join(advOriginConfig.backupIps)) util.addElement(doc, advOriginConfigNode, 'detect-url', advOriginConfig.detectUrl) util.addElement(doc, advOriginConfigNode, 'detect-period', advOriginConfig.detectPeriod) if domain.queryStringSettings is not None: queryStringSettingsNode = util.addElement(doc, domainNode, 'query-string-settings') for queryStringSetting in domain.queryStringSettings: queryStringSettingNode = util.addElement(doc, queryStringSettingsNode, 'query-string-setting') util.addElement(doc, queryStringSettingNode, 'path-pattern', queryStringSetting.pathPattern) if queryStringSetting.ignoreQueryString == False: util.addElement(doc, queryStringSettingNode, 'ignore-query-string', "false") else: util.addElement(doc, queryStringSettingNode, 'ignore-query-string', "true") if domain.cacheBehaviors is not None: cacheBehaviorsNode = util.addElement(doc, domainNode, 'cache-behaviors') for cacheBehavior in domain.cacheBehaviors: cacheBehaviorNode = util.addElement(doc, cacheBehaviorsNode, 'cache-behavior') util.addElement(doc, cacheBehaviorNode, 'path-pattern', cacheBehavior.pathPattern) if cacheBehavior.ignoreCacheControl == False: util.addElement(doc, cacheBehaviorNode, 'ignore-cache-control', "false") else: util.addElement(doc, cacheBehaviorNode, 'ignore-cache-control', "true") util.addElement(doc, cacheBehaviorNode, 'cache-ttl', cacheBehavior.cacheTtl) if domain.visitControlRules is not None: visitControlRulesNode = util.addElement(doc, domainNode, 'visit-control-rules') for visitControl in domain.visitControlRules: visitControlNode = util.addElement(doc, visitControlRulesNode, "visit-control-rule") if visitControl.allowNullReferer == True: util.addElement(doc, visitControlNode, 'allownullreferer', "true") elif visitControl.allowNullReferer == False: util.addElement(doc, visitControlNode, 'allownullreferer', "false") util.addElement(doc, visitControlNode, 'path-pattern', visitControl.pathPattern) validRNode = util.addElement(doc, visitControlNode, 'valid-referers') validReferers = visitControl.validReferers if validReferers is not None and len(validReferers) > 0: for referer in validReferers: util.addElement(doc, validRNode, 'referer', referer) invalidRNode = util.addElement(doc, visitControlNode, 'invalid-referers') invalidReferers = visitControl.invalidReferers if invalidReferers is not None and len(invalidReferers) > 0: for referer in invalidReferers: util.addElement(doc, invalidRNode, 'referer', referer) forbiddenIps = visitControl.forbiddenIps if forbiddenIps is not None and len(forbiddenIps) > 0: util.addElement(doc, visitControlNode, 'forbidden-ips', ';'.join(visitControl.forbiddenIps)) return doc.toprettyxml(indent="", newl="", encoding='utf-8')
def xmlToDomain(ret): ''' 返回xml 转换成 带 Domain对象的ProcessResult对象, 在查询频道信息的时候使用''' global X_CNC_REQUEST_ID, X_CNC_LOCATION, logger requestId = ret.getheader(X_CNC_REQUEST_ID) xmlString = ret.read().decode("utf-8") logger.debug("response:" + xmlString) doc = minidom.parseString(xmlString) domainNode = util.getChildNode(doc, 'domain') domainName = util.getChildNodeText(domainNode, 'domain-name') domainId = util.getChildNodeText(domainNode, 'domain-id') serviceType = util.getChildNodeText(domainNode, 'service-type') comment = util.getChildNodeText(domainNode, 'comment') serviceAreas = util.getChildNodeText(domainNode, 'service-areas') enabled = util.getChildNodeText(domainNode, 'enabled') cname = util.getChildNodeText(domainNode, 'cname') status = util.getChildNodeText(domainNode, 'status') domain = Domain(domainName=domainName, serviceType=serviceType, domainId=domainId, comment=comment, serviceAreas=serviceAreas, enabled=enabled, cname=cname, status=status) originConfigNode = util.getChildNode(domainNode, 'origin-config') if originConfigNode is not None: originIpsStr = util.getChildNodeText(originConfigNode, 'origin-ips') originIps = splitStr(originIpsStr) originDomainName = util.getChildNodeText(originConfigNode, 'origin-domain-name') advOriginConfigListRootNode = util.getChildNode( originConfigNode, 'adv-origin-configs') if advOriginConfigListRootNode is not None: advOriginConfigListNode = util.getChildNodeList( advOriginConfigListRootNode, 'adv-origin-config') advOriginConfigs = [] if advOriginConfigListNode is not None: advOriginConfigs = parseAdvOriginConfigList( advOriginConfigListNode) originConfig = OriginConfig(originIps, originDomainName, advOriginConfigs) domain.originConfig = originConfig else: originConfig = OriginConfig(originIps, originDomainName) domain.originConfig = originConfig queryStringSettingListRootNode = util.getChildNode( domainNode, 'query-string-settings') if queryStringSettingListRootNode is not None: queryStringSettingListNode = util.getChildNodeList( queryStringSettingListRootNode, 'query-string-setting') if queryStringSettingListNode is not None: queryStringSettingList = parseQueryStringSettingListNode( queryStringSettingListNode) domain.queryStringSettings = queryStringSettingList cacheBehaviorListRootNode = util.getChildNode(domainNode, 'cache-behaviors') if cacheBehaviorListRootNode is not None: cacheBehaviorListNode = util.getChildNodeList( cacheBehaviorListRootNode, 'cache-behavior') if cacheBehaviorListNode is not None: cacheBehaviorList = parseCacheBehaviorList(cacheBehaviorListNode) domain.cacheBehaviors = cacheBehaviorList visitControlRulesListRootNode = util.getChildNode(domainNode, 'visit-control-rules') if visitControlRulesListRootNode is not None: visitControlRulesListNode = util.getChildNodeList( visitControlRulesListRootNode, 'visit-control-rule') if visitControlRulesListNode is not None: visitControlRulesList = parseVisitControlRulesList( visitControlRulesListNode) domain.visitControlRules = visitControlRulesList return ProcessResult(0, 'OK', xCncRequestId=requestId, domain=domain)
def domainToXml(domain): ''' Domain 对象 转换成 xml ''' doc = dom.getDOMImplementation().createDocument('', 'domain', '') domainNode = util.getChildNode(doc, 'domain') util.addElement(doc, domainNode, 'version', "1.0.0") if domain.domainName is not None: util.addElement(doc, domainNode, 'domain-name', domain.domainName) if domain.serviceType is not None: util.addElement(doc, domainNode, 'service-type', domain.serviceType) if domain.comment is not None: util.addElement(doc, domainNode, 'comment', domain.comment) if domain.serviceAreas is not None: util.addElement(doc, domainNode, 'service-areas', domain.serviceAreas) else: util.addElement(doc, domainNode, 'service-areas', 'cn') if domain.originConfig is not None: originConfigNode = util.addElement(doc, domainNode, 'origin-config') if domain.originConfig.originIps is not None: originIps = domain.originConfig.originIps util.addElement(doc, originConfigNode, 'origin-ips', ';'.join(originIps)) if domain.originConfig.originDomainName is not None: util.addElement(doc, originConfigNode, 'origin-domain-name', domain.originConfig.originDomainName) if domain.originConfig.advOriginConfigs is not None: advOriginConfigsNode = util.addElement(doc, originConfigNode, 'adv-origin-configs') for advOriginConfig in domain.originConfig.advOriginConfigs: isps = advOriginConfig.isps advOriginConfigNode = util.addElement(doc, advOriginConfigsNode, 'adv-origin-config') util.addElement(doc, advOriginConfigNode, 'isp', ';'.join(isps)) util.addElement(doc, advOriginConfigNode, 'master-ips', ';'.join(advOriginConfig.masterIps)) util.addElement(doc, advOriginConfigNode, 'backup-ips', ';'.join(advOriginConfig.backupIps)) util.addElement(doc, advOriginConfigNode, 'detect-url', advOriginConfig.detectUrl) util.addElement(doc, advOriginConfigNode, 'detect-period', advOriginConfig.detectPeriod) if domain.queryStringSettings is not None: queryStringSettingsNode = util.addElement(doc, domainNode, 'query-string-settings') for queryStringSetting in domain.queryStringSettings: queryStringSettingNode = util.addElement(doc, queryStringSettingsNode, 'query-string-setting') util.addElement(doc, queryStringSettingNode, 'path-pattern', queryStringSetting.pathPattern) if queryStringSetting.ignoreQueryString == False: util.addElement(doc, queryStringSettingNode, 'ignore-query-string', "false") else: util.addElement(doc, queryStringSettingNode, 'ignore-query-string', "true") if domain.cacheBehaviors is not None: cacheBehaviorsNode = util.addElement(doc, domainNode, 'cache-behaviors') for cacheBehavior in domain.cacheBehaviors: cacheBehaviorNode = util.addElement(doc, cacheBehaviorsNode, 'cache-behavior') util.addElement(doc, cacheBehaviorNode, 'path-pattern', cacheBehavior.pathPattern) if cacheBehavior.ignoreCacheControl == False: util.addElement(doc, cacheBehaviorNode, 'ignore-cache-control', "false") else: util.addElement(doc, cacheBehaviorNode, 'ignore-cache-control', "true") util.addElement(doc, cacheBehaviorNode, 'cache-ttl', cacheBehavior.cacheTtl) if domain.visitControlRules is not None: visitControlRulesNode = util.addElement(doc, domainNode, 'visit-control-rules') for visitControl in domain.visitControlRules: visitControlNode = util.addElement(doc, visitControlRulesNode, "visit-control-rule") if visitControl.allowNullReferer == True: util.addElement(doc, visitControlNode, 'allownullreferer', "true") elif visitControl.allowNullReferer == False: util.addElement(doc, visitControlNode, 'allownullreferer', "false") util.addElement(doc, visitControlNode, 'path-pattern', visitControl.pathPattern) validRNode = util.addElement(doc, visitControlNode, 'valid-referers') validReferers = visitControl.validReferers if validReferers is not None and len(validReferers) > 0 : for referer in validReferers: util.addElement(doc, validRNode, 'referer', referer) invalidRNode = util.addElement(doc, visitControlNode, 'invalid-referers') invalidReferers = visitControl.invalidReferers if invalidReferers is not None and len(invalidReferers) > 0 : for referer in invalidReferers: util.addElement(doc, invalidRNode, 'referer', referer) forbiddenIps = visitControl.forbiddenIps if forbiddenIps is not None and len(forbiddenIps) > 0: util.addElement(doc, visitControlNode, 'forbidden-ips', ';'.join(visitControl.forbiddenIps)) return doc.toprettyxml(indent = "", newl="", encoding = 'utf-8')
def xmlToDomain(ret): ''' 返回xml 转换成 带 Domain对象的ProcessResult对象, 在查询频道信息的时候使用''' global X_CNC_REQUEST_ID, X_CNC_LOCATION, logger requestId = ret.getheader(X_CNC_REQUEST_ID) xmlString = ret.read().decode("utf-8") logger.debug("response:" + xmlString) doc = minidom.parseString(xmlString) domainNode = util.getChildNode(doc, 'domain') domainName = util.getChildNodeText(domainNode, 'domain-name') domainId = util.getChildNodeText(domainNode, 'domain-id') serviceType = util.getChildNodeText(domainNode, 'service-type') comment = util.getChildNodeText(domainNode, 'comment') serviceAreas = util.getChildNodeText(domainNode, 'service-areas') enabled = util.getChildNodeText(domainNode, 'enabled') cname = util.getChildNodeText(domainNode, 'cname') status = util.getChildNodeText(domainNode, 'status') domain = Domain(domainName = domainName, serviceType = serviceType, domainId = domainId, comment = comment, serviceAreas = serviceAreas, enabled = enabled, cname = cname, status = status) originConfigNode = util.getChildNode(domainNode, 'origin-config') if originConfigNode is not None: originIpsStr = util.getChildNodeText(originConfigNode, 'origin-ips') originIps = splitStr(originIpsStr) originDomainName = util.getChildNodeText(originConfigNode, 'origin-domain-name') advOriginConfigListRootNode = util.getChildNode(originConfigNode, 'adv-origin-configs') if advOriginConfigListRootNode is not None: advOriginConfigListNode = util.getChildNodeList(advOriginConfigListRootNode, 'adv-origin-config') advOriginConfigs = [] if advOriginConfigListNode is not None: advOriginConfigs = parseAdvOriginConfigList(advOriginConfigListNode) originConfig = OriginConfig(originIps, originDomainName, advOriginConfigs) domain.originConfig = originConfig else: originConfig = OriginConfig(originIps, originDomainName) domain.originConfig = originConfig queryStringSettingListRootNode = util.getChildNode(domainNode, 'query-string-settings') if queryStringSettingListRootNode is not None: queryStringSettingListNode = util.getChildNodeList(queryStringSettingListRootNode, 'query-string-setting') if queryStringSettingListNode is not None: queryStringSettingList = parseQueryStringSettingListNode(queryStringSettingListNode) domain.queryStringSettings = queryStringSettingList cacheBehaviorListRootNode = util.getChildNode(domainNode, 'cache-behaviors') if cacheBehaviorListRootNode is not None: cacheBehaviorListNode = util.getChildNodeList(cacheBehaviorListRootNode, 'cache-behavior') if cacheBehaviorListNode is not None: cacheBehaviorList = parseCacheBehaviorList(cacheBehaviorListNode) domain.cacheBehaviors = cacheBehaviorList visitControlRulesListRootNode = util.getChildNode(domainNode, 'visit-control-rules') if visitControlRulesListRootNode is not None: visitControlRulesListNode = util.getChildNodeList(visitControlRulesListRootNode, 'visit-control-rule') if visitControlRulesListNode is not None: visitControlRulesList = parseVisitControlRulesList(visitControlRulesListNode) domain.visitControlRules = visitControlRulesList return ProcessResult(0, 'OK', xCncRequestId = requestId, domain = domain);