示例#1
0
    def create_record(self,
                      domain: str,
                      sub_domain: str,
                      value: str,
                      record_type: str,
                      line: str,
                      ttl: int = 300):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets'
        else:
            return "The domain doesn't exist"
        if line == '电信':
            line = 'Dianxin'
        elif line == '联通':
            line = 'Liantong'
        elif line == '移动':
            line = 'Yidong'
        data = {
            "description": "",
            "weight": 1,
            "line": line,
            "name": domain if "@" == sub_domain else
            (sub_domain + '.' + domain),
            "records": [value],
            "ttl": ttl,
            "type": record_type
        }
        r = signer.HttpRequest('POST', url, body=json.dumps(data))
        self.sign.Sign(r)

        #替换为标准格式
        # {
        #   "result":True,
        #   "message":{...}
        # }
        resp = requests.post(url, headers=r.headers, data=r.body)
        #print(resp.content)
        try:
            resp.raise_for_status()
            res = json.loads(resp.content.decode('utf-8', 'ignore'))
        except HTTPError as httpError:
            response_status_code = httpError.response.status_code
            #response_header_params = httpError.response.headers
            #request_id = response_header_params["X-Request-Id"]
            response_body = httpError.response.text
            return {
                "result": False,
                "message":
                "错误码:%s,错误描述:%s" % (response_status_code, response_body)
            }
        except:
            pass

        data = {}
        data["result"] = 'id' in res
        data["message"] = res['status']
        if data["message"] == 'PENDING_DELETE':
            data["message"] = 'success'
        return data
示例#2
0
    def get_record(self, domain, length, sub_domain, record_type):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets?limit=' + str(
                length)
        else:
            return "The domain doesn't exist"
        r = signer.HttpRequest('GET', url)
        self.sign.Sign(r)

        resp = requests.get(url, headers=r.headers)
        try:
            resp.raise_for_status()
            res = json.loads(resp.content.decode('utf-8', 'ignore'))
        except:
            pass

        #替换为标准格式
        # {
        #   "data":{
        #     "records":[
        #       {
        #         "id":"record_id",
        #         "line":"线路",
        #         "value":"ip值"
        #       }
        #     ]
        #   }
        # }
        records = []
        try:
            for i in range(0, len(res['recordsets'])):
                #由于华为api的查询参数无法对subdomain和type进行过滤,所以只能在结果中进行判断
                if (res['recordsets'][i]['name'].split('.')[0] == sub_domain or
                    (res['recordsets'][i]['name'].startswith(domain)
                     and "@" == sub_domain)
                    ) and res['recordsets'][i]['type'] == record_type and len(
                        res['recordsets'][i]['records']) > 0:
                    for ip in res['recordsets'][i]['records']:
                        line = res['recordsets'][i]['line']
                        if line == 'Dianxin':
                            line = '电信'
                        elif line == 'Liantong':
                            line = '联通'
                        elif line == 'Yidong':
                            line = '移动'
                        records.append({
                            'id': res['recordsets'][i]['id'],
                            'line': line,
                            'value': ip
                        })
        except:
            pass

        return {"data": {"records": records}}
示例#3
0
 def get_zone_id(self, domain):
     url = 'https://dns.myhuaweicloud.com/v2/zones?type=public'
     r = signer.HttpRequest('GET', url)
     self.sign.Sign(r)
     res = json.loads(
         requests.get(url, headers=r.headers).text.decode('utf-8'))
     # print(res)
     zone_id = ''
     for i in range(0, len(res['zones'])):
         if domain == res['zones'][i]['name'][:-1]:
             zone_id = res['zones'][i]['id']
     if zone_id != '':
         return zone_id
     else:
         return "The domain doesn't exist"
示例#4
0
    def del_record(self, domain, record):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2/zones/' + zone_id + '/recordsets/' + record
        else:
            return "The domain doesn't exist"
        r = signer.HttpRequest('DELETE', url)
        self.sign.Sign(r)

        #替换为标准格式
        # {
        #   "result":True,
        #   "message":{...}
        # }
        resp = requests.delete(url, headers=r.headers)
        try:
            resp.raise_for_status()
            res = json.loads(resp.content.decode('utf-8', 'ignore'))
        except HTTPError as httpError:
            response_status_code = httpError.response.status_code
            #response_header_params = httpError.response.headers
            #request_id = response_header_params["X-Request-Id"]
            response_body = httpError.response.text
            return {
                "result": False,
                "message":
                "错误码:%s,错误描述:%s" % (response_status_code, response_body)
            }
        except:
            pass
        # print(res['status'])
        #try:
        #    if res['status'] == 'PENDING_DELETE':
        #        return 'success'
        #except:
        #    return res
        data = {}
        data["result"] = 'id' in res
        data["message"] = res['status']
        if data["message"] == 'PENDING_DELETE':
            data["message"] = 'success'
        return data
示例#5
0
    def change_record(self, domain: str, record_id: str, value: str, ttl: int):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets/' + record_id
        else:
            return "The domain doesn't exist"
        data = {
            "records": [value],
            "ttl": ttl,
        }
        r = signer.HttpRequest('PUT', url, body=json.dumps(data))
        self.sign.Sign(r)
        #替换为标准格式
        # {
        #   "result":True,
        #   "message":{...}
        # }
        resp = requests.put(url, headers=r.headers, data=r.body)
        try:
            resp.raise_for_status()
            res = json.loads(resp.text.decode('utf-8'))
        except HTTPError as httpError:
            response_status_code = httpError.response.status_code
            #response_header_params = httpError.response.headers
            #request_id = response_header_params["X-Request-Id"]
            #response_body = httpError.response.text
            return {
                "result": False,
                "message":
                "错误码:%s,错误描述:%s" % (response_status_code, response_body)
            }
        except:
            pass

        data = {}
        data["result"] = 'id' in ret
        data["message"] = res['status']
        if data["message"] == 'PENDING_DELETE':
            data["message"] = 'success'
        return data