JAVA实现对阿里云DNS的解析管理

网友投稿 341 2022-09-03


JAVA实现对阿里云DNS的解析管理

目录1、阿里云DNS的SDK依赖2、第一个方法:创建SDK客户端实例3、第二个方法:返回指定的记录ID(RecordId)4、第三个方法:添加或修改指定的记录5、第四个方法:删除指定的记录6、完整代码

1、阿里云DNS的SDK依赖

com.aliyun

tea-openapi

0.0.19

com.aliyun

alidns20150109

2.0.1

2、第一个方法:创建SDK客户端实例

所有解析记录的操作都要通过这个客户端实例来进行,所以要首先创建这个实例,需要阿里云的AccessKey(AppId和AppSecret)

/**

*

* 创建客户端实例

*

*

* @return

* @throws Exception

*/

private Client createClient() throws Exception{

AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey参数

if(api == null) throw new ErrException("未配置阿里云API参数!");

Config config = new Config();

config.accessKeyId = api.getAppId();

config.accessKeySecret = api.getAppSecret();

config.endpoint = "alidns.cn-beijing.aliyuncs.com";

return new Client(config);

}

3、第二个方法:返回指定的记录ID(RecordId)

在阿里云的SDK中,对解析记录进行修改和删除时,都需要传入 RecordId 这个参数,所以提前写一个获取记录ID的方法。

/**

*

* 返回指定主机记录的ID,不存在时返回null

*

*

* @param DomainName

* @param RR 记录名称

* @return

*/

private String getRecId(Client client, String DomainName, String RR){

String recId = null;

try {

DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();

request.setDomainName(DomainName);

request.setRRKeyWord(RR);

DescribeDomainRecordsResponse response = client.describeDomainRecords(request);

if(response.getBody().getTotalCount() > 0){

List recs = response.getBody().getDomainRecords().getRecord();

for(DescribeDomainRecordsResponseBodyDomainRecordsRecord rec: recs){

if(rec.getRR().equalsIgnoreCase(RR)){

recId = rec.getRecordId();

break;

}

}

}

} catch (Exception e) {

}

return recId;

}

4、第三个方法:添加或修改指定的记录

方便起见,这里我将添加和修改集成到了一个方法,相当于SaveOrUpdate。

/**

*

* 添加或修改解析记录

*

*

* @param DomainName 域名

* @param RR 记录名称

* @param Type 记录类型(A、AAAA、MX、TXT、CNAME)

* @param Value 记录值

*/

public void update(String DomainName, String RR, String Type, String Value){

try {

if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)为空!");

if(EStr.isEmpty(RR)) throw new RuntimeException("主机记录(RR)为空!");

if(EStr.isEmpty(Type)) throw new RuntimeException("记录类型(Type)为空!");

if(EStr.isEmpty(Value)) throw new RuntimeException("记录值(Value)为空!");

Client client = createClient();

String recId = getRecId(client, DomainName, RR);

if(EStr.isNull(recId)){ //添加

AddDomainRecordRequest request = new AddDomainRecordRequest();

request.setDomainName(DomainName);

request.setRR(RR);

request.setType(Type);

request.setValue(Value);

AddDomainRecordResponse response = client.addDomainRecord(request);

recId = response.getBody().getRecordId();

}else{ //修改

UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();

request.setRecordId(recId);

request.setRR(RR);

request.setType(Type);

request.setValue(Value);

UpdateDomainRecordResponse response = client.updateDomainRecord(request);

recId = response.getBody().getRecordId();

}

renderjson(Result.success("recId", recId));

} catch (Exception e) {

renderJson(Result.fail(e.getMessage()));

}

}

5、第四个方法:删除指定的记录

这个很简单,根据查找到的RecordId直接删除即可。

/**

*

* 删除记录

*

*

* @param DomainName

* @param RR

*/

public void remove(String DomainName, String RR){

try {

if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)为空!");

if(EStr.isEmpty(RR)) throw new RuntimeException("主机记录(RR)为空!");

Client client = createClient();

String recId = getRecId(client, DomainName, RR);

if(EStr.isNull(recId)){

renderJson(Result.success("recId", null));

}else{

DeleteDomainRecordRequest request = new DeleteDomainRecordRequest();

request.setRecordId(recId);

DeleteDomainRecordResponse response = client.deleteDomainRecord(request);

renderJson(Result.success("recId", response.getBody().getRecordId()));

}

} catch (Exception e) {

renderJson(Result.fail(e.getMessage()));

}

}

6、完整代码

package itez.alidns.controller;

import java.util.List;

import com.aliyun.alidns20150109.Client;

import com.aliyun.alidns20150109.models.AddDomainRecordRequest;

import com.aliyun.alidns20150109.models.AddDomainRecordResponse;

import com.aliyun.alidns20150109.models.DeleteDomainRecordRequest;

import com.aliyun.alidns20150109.models.DeleteDomainRecordResponse;

import com.aliyun.alidns20150109.models.DescribeDomainRecordsRequest;

import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponse;

import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord;

import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;

import com.aliyun.alidns20150109.models.UpdateDomainRecordResponse;

import com.aliyun.teaopenapi.models.Config;

import itez.core.wrapper.controller.ControllerDefine;

import itez.core.wrapper.controller.EController;

import itez.kit.EStr;

import itez.kit.exception.ErrException;

import itez.kit.restful.Result;

import itez.plat.main.model.CompWx;

import itez.plat.main.service.CompWxService;

import itez.weixin.api.ApiConfigKit.ConfigType;

/**

*

* 阿里云DNS解析

* 示例:http://localhost/alidns/update?DomainName=domain.com&RR=test&Type=A&Value=8.8.8.8

*

*

*

Copyright(C) 2017-2022 上游科技

*

* @author Z.Mingyu

* @date 2022年1月12日 下午2:38:31

*/

@ControllerDefine(key = "/alidns", summary = "阿里云DNS解析", view = "/")

public class AliDnsController extends EController {

/**

*

* 添加或修改解析记录

*

*

* @param DomainName 域名

* @param RR 记录名称

* @param Type 记录类型(A、AAAA、MX、TXT、CNAME)

* @param Value 记录值

*/

public void update(String DomainName, String RR, String Type, String Value){

try {

if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)为空!");

if(EStr.isEmpty(RR)) throw new RuntimeException("主机记录(RR)为空!");

if(EStr.isEmpty(Type)) throw new RuntimeException("记录类型(Type)为空!");

if(EStr.isEmpty(Value)) throw new RuntimeException("记录值(Value)为空!");

Client client = createClient();

String recId = getRecId(client, DomainName, RR);

if(EStr.isNull(recId)){ //添加

AddDomainRecordRequest request = new AddDomainRecordRequest();

request.setDomainName(DomainName);

request.setRR(RR);

request.setType(Type);

request.setValue(Value);

AddDomainRecordResponse response = client.addDomainRecord(request);

recId = response.getBody().getRecordId();

}else{ //修改

UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();

request.setRecordId(recId);

request.setRR(RR);

request.setType(Type);

request.setValue(Value);

UpdateDomainRecordResponse response = client.updateDomainRecord(request);

recId = response.getBody().getRecordId();

}

renderJson(Result.success("recId", recId));

} catch (Exception e) {

renderJson(Result.fail(e.getMessage()));

}

}

/**

*

* 删除记录

*

*

* @param DomainName

* @param RR

*/

public void remove(String DomainName, String RR){

try {

if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)为空!");

if(EStr.isEmpty(RR)) throw new RuntimeException("主机记录(RR)为空!");

Client client = createClient();

String recId = getRecId(client, DomainName, RR);

if(EStr.isNull(recId)){

renderJson(Result.success("recId", null));

}else{

DeleteDomainRecordRequest request = new DeleteDomainRecordRequest();

request.setRecordId(recId);

DeleteDomainRecordResponse response = client.deleteDomainRecord(request);

renderJson(Result.success("recId", response.getBody().getRecordId()));

}

} catch (Exception e) {

renderJson(Result.fail(e.getMessage()));

}

}

/**

*

* 返回指定主机记录的ID,不存在时返回null

*

*

* @param Domaihttp://nName

* @param RR 记录名称

* @return

*/

private String getRecId(Client client, String DomainName, String RR){

String recId = null;

try {

DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();

request.setDomainName(DomainName);

request.setRRKeyWord(RR);

DescribeDomainRecordsResponse response = client.describeDomainRecords(request);

if(response.getBody().getTotalCount() > 0){

List recs = response.getBody().getDomainRecords().getRecord();

for(DescribeDomainRecordsResponseBodyDomainRecordsRecord rec: recs){

if(rec.getRR().equalsIgnoreCase(RR)){

recId = rec.getRecordId();

break;

}

}

}

} catch (Exception e) {

}

return recId;

}

/**

*

* 创建客户端实例

*

*

* @return

* @throws Exception

*/

private Client createClient() throws Exception{

AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey参数

if(api == null) throw new ErrException("未配置阿里云API参数!");

Config config = new Config();

config.accessKeyId = api.getAppId();

config.accessKeySecret = api.getAppSecret();

config.endpoint = "alidns.cn-beijing.aliyuncs.com";

return new Client(config);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:baselines算法库baselines/bench/monitor.py模块分析
下一篇:根据baselines库修改的运行输入参数的解析代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~