Skip to content

Commit 886f3a9

Browse files
author
jeromebridge
committed
Adding more bindings to ClouDNS API.
1 parent 90d5a20 commit 886f3a9

File tree

12 files changed

+388
-59
lines changed

12 files changed

+388
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.settings/
33
/.classpath
44
/.project
5+
/test-output/

src/main/java/com/pennassurancesoftware/cloudns/ClouDns.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.util.List;
44

5+
import com.pennassurancesoftware.cloudns.dto.DomainZone;
6+
import com.pennassurancesoftware.cloudns.dto.DomainZoneStats;
57
import com.pennassurancesoftware.cloudns.dto.NameServer;
8+
import com.pennassurancesoftware.cloudns.type.ZoneType;
69

710
/**
811
* <p>
@@ -17,5 +20,45 @@
1720
*
1821
*/
1922
public interface ClouDns {
23+
/**
24+
* <a href="https://www.cloudns.net/api-help/dns/dns-available-name-servers">Available Name Servers Documentation</a>
25+
*
26+
* @return List of available name servers provided by account with ClouDNS
27+
*/
2028
List<NameServer> getAvailableNameServers();
29+
30+
/**
31+
* <a href="https://www.cloudns.net/api-help/dns/register-domain-zone/">Register Domain Zone Documentation</a>
32+
*
33+
* @param domainName Domain that will be added to the account
34+
* @param type Type of zone the domain should be created as
35+
*
36+
*/
37+
void registerDomainZone( String domainName, ZoneType type );
38+
39+
/**
40+
* <a href="https://www.cloudns.net/api-help/dns/delete-domain-zone/">Delete Domain Zone Documentation</a>
41+
*
42+
*
43+
* @param domainName Domain that will be deleted
44+
*/
45+
void deleteDomainZone( String domainName );
46+
47+
/**
48+
*
49+
* <a href="https://www.cloudns.net/api-help/dns/list-zones/">List Domain Zone Documentation</a>
50+
*
51+
*
52+
* @return List of Domain Zones
53+
*/
54+
List<DomainZone> getDomainZones();
55+
56+
/**
57+
*
58+
* <a href="https://www.cloudns.net/api-help/dns/get-zones-stats/">Domain Zone Stats Documentation</a>
59+
*
60+
*
61+
* @return Statistics about account with ClouDNS
62+
*/
63+
DomainZoneStats getDomainZoneStats();
2164
}

src/main/java/com/pennassurancesoftware/cloudns/client/ApiAction.java

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,71 @@
2020
*/
2121
package com.pennassurancesoftware.cloudns.client;
2222

23+
import com.pennassurancesoftware.cloudns.dto.DomainZone;
24+
import com.pennassurancesoftware.cloudns.dto.DomainZoneStats;
2325
import com.pennassurancesoftware.cloudns.dto.NameServer;
26+
import com.pennassurancesoftware.cloudns.dto.Response;
2427

2528
/**
2629
* Enumeration of Tutum RESTful resource information.
2730
*/
2831
public enum ApiAction {
2932

3033
AVAILABLE_NAME_SERVERS("/available-name-servers.json", RequestMethod.GET, NameServer[].class),
31-
// GET_ACTION("/action/%s", RequestMethod.GET, Action.class),
32-
// PROVIDERS("/provider", RequestMethod.GET, Providers.class),
33-
// GET_PROVIDER("/provider/%s", RequestMethod.GET, Provider.class),
34-
// REGIONS("/region", RequestMethod.GET, Regions.class),
35-
// GET_REGION("/region/%s/%s", RequestMethod.GET, Region.class),
36-
// NODETYPES("/nodetype", RequestMethod.GET, NodeTypes.class),
37-
// GET_NODETYPE("/nodetype/%s/%s", RequestMethod.GET, NodeType.class),
38-
// NODECLUSTERS("/nodecluster", RequestMethod.GET, NodeClusters.class),
39-
// GET_NODECLUSTER("/nodecluster/%s", RequestMethod.GET, NodeCluster.class),
40-
// CREATE_NODECLUSTER("/nodecluster/", RequestMethod.POST, NodeCluster.class),
41-
// DEPLOY_NODECLUSTER("/nodecluster/%s/deploy/", RequestMethod.POST, NodeCluster.class),
42-
// UPDATE_NODECLUSTER("/nodecluster/%s/", RequestMethod.PATCH, NodeCluster.class),
43-
// UPGRADE_DOCKER_NODECLUSTER("/nodecluster/%s/docker-upgrade/", RequestMethod.POST, NodeCluster.class),
44-
// TERMINATE_NODECLUSTER("/nodecluster/%s/", RequestMethod.DELETE, NodeCluster.class),
45-
// NODES("/node", RequestMethod.GET, Nodes.class),
46-
// GET_NODE("/node/%s", RequestMethod.GET, Node.class),
47-
// DEPLOY_NODE("/node/%s/deploy/", RequestMethod.POST, Node.class),
48-
// UPDATE_NODE("/node/%s/", RequestMethod.PATCH, Node.class),
49-
// UPGRADE_DOCKER_NODE("/node/%s/docker-upgrade/", RequestMethod.POST, Node.class),
50-
// TERMINATE_NODE("/node/%s/", RequestMethod.DELETE, Node.class),
51-
// CREATE_TOKEN("/token/", RequestMethod.POST, Token.class),
52-
// SERVICES("/service", RequestMethod.GET, Services.class),
53-
// CREATE_SERVICE("/service/", RequestMethod.POST, Service.class),
54-
// GET_SERVICE("/service/%s", RequestMethod.GET, Service.class),
55-
// GET_SERVICE_LOGS("/service/%s/logs/", RequestMethod.GET, Logs.class),
56-
// UPDATE_SERVICE("/service/%s/", RequestMethod.PATCH, Service.class),
57-
// START_SERVICE("/service/%s/start/", RequestMethod.POST, Service.class),
58-
// STOP_SERVICE("/service/%s/stop/", RequestMethod.POST, Service.class),
59-
// REDEPLOY_SERVICE("/service/%s/redeploy/", RequestMethod.POST, Service.class),
60-
// TERMINATE_SERVICE("/service/%s/", RequestMethod.DELETE, Service.class),
61-
// CONTAINERS("/container", RequestMethod.GET, Containers.class),
62-
// GET_CONTAINER("/container/%s", RequestMethod.GET, Container.class),
63-
// GET_CONTAINER_LOGS("/container/%s/logs/", RequestMethod.GET, Logs.class),
64-
// START_CONTAINER("/container/%s/start/", RequestMethod.POST, Container.class),
65-
// STOP_CONTAINER("/container/%s/stop/", RequestMethod.POST, Container.class),
66-
// TERMINATE_CONTAINER("/container/%s/", RequestMethod.DELETE, Container.class),
67-
// VOLUMEGROUPS("/volumegroup", RequestMethod.GET, VolumeGroups.class),
68-
// GET_VOLUMEGROUP("/volumegroup/%s", RequestMethod.GET, VolumeGroup.class),
69-
// VOLUMES("/volume", RequestMethod.GET, Volumes.class),
70-
// GET_VOLUME("/volume/%s", RequestMethod.GET, Volume.class),
71-
// TAGS("/%s/%s/tags/", RequestMethod.GET, Tags.class),
72-
// TAG_RESOURCE("/%s/%s/tags/", RequestMethod.POST, Tag[].class),
73-
// DELETE_TAG("/%s/%s/tags/%s/", RequestMethod.DELETE, Tag.class),
74-
// WEBHOOK_HANDLERS("/service/%s/webhook/handler/", RequestMethod.GET, WebhookHandlers.class),
75-
// CREATE_WEBHOOK_HANDLER("/service/%s/webhook/handler/", RequestMethod.POST, WebhookHandler[].class),
76-
// GET_WEBHOOK_HANDLER("/service/%s/webhook/handler/%s/", RequestMethod.GET, WebhookHandler.class),
77-
// DELETE_WEBHOOK_HANDLER("/service/%s/webhook/handler/%s/", RequestMethod.DELETE, WebhookHandler.class),
78-
// CALL_WEBHOOK_HANDLER("/service/%s/webhook/handler/%s/call/", RequestMethod.POST, WebhookHandler.class),
34+
REGISTER_DOMAIN_ZONE("/register.json?domain-name=%s&zone-type=%s", RequestMethod.GET, Response.class),
35+
DELETE_DOMAIN_ZONE("/delete.json?domain-name=%s", RequestMethod.GET, Response.class),
36+
GET_DOMAIN_ZONES("/list-zones.json?page=%s&rows-per-page=%s", RequestMethod.GET, DomainZone[].class),
37+
GET_DOMAIN_ZONES_PAGE_COUNT("/get-pages-count.json?&rows-per-page=%s", RequestMethod.GET, Integer.class),
38+
GET_DOMAIN_ZONE_STATS("/get-zones-stats.json", RequestMethod.GET, DomainZoneStats.class),
39+
40+
// GET_ACTION("/action/%s", RequestMethod.GET, Action.class),
41+
// PROVIDERS("/provider", RequestMethod.GET, Providers.class),
42+
// GET_PROVIDER("/provider/%s", RequestMethod.GET, Provider.class),
43+
// REGIONS("/region", RequestMethod.GET, Regions.class),
44+
// GET_REGION("/region/%s/%s", RequestMethod.GET, Region.class),
45+
// NODETYPES("/nodetype", RequestMethod.GET, NodeTypes.class),
46+
// GET_NODETYPE("/nodetype/%s/%s", RequestMethod.GET, NodeType.class),
47+
// NODECLUSTERS("/nodecluster", RequestMethod.GET, NodeClusters.class),
48+
// GET_NODECLUSTER("/nodecluster/%s", RequestMethod.GET, NodeCluster.class),
49+
// CREATE_NODECLUSTER("/nodecluster/", RequestMethod.POST, NodeCluster.class),
50+
// DEPLOY_NODECLUSTER("/nodecluster/%s/deploy/", RequestMethod.POST, NodeCluster.class),
51+
// UPDATE_NODECLUSTER("/nodecluster/%s/", RequestMethod.PATCH, NodeCluster.class),
52+
// UPGRADE_DOCKER_NODECLUSTER("/nodecluster/%s/docker-upgrade/", RequestMethod.POST, NodeCluster.class),
53+
// TERMINATE_NODECLUSTER("/nodecluster/%s/", RequestMethod.DELETE, NodeCluster.class),
54+
// NODES("/node", RequestMethod.GET, Nodes.class),
55+
// GET_NODE("/node/%s", RequestMethod.GET, Node.class),
56+
// DEPLOY_NODE("/node/%s/deploy/", RequestMethod.POST, Node.class),
57+
// UPDATE_NODE("/node/%s/", RequestMethod.PATCH, Node.class),
58+
// UPGRADE_DOCKER_NODE("/node/%s/docker-upgrade/", RequestMethod.POST, Node.class),
59+
// TERMINATE_NODE("/node/%s/", RequestMethod.DELETE, Node.class),
60+
// CREATE_TOKEN("/token/", RequestMethod.POST, Token.class),
61+
// SERVICES("/service", RequestMethod.GET, Services.class),
62+
// CREATE_SERVICE("/service/", RequestMethod.POST, Service.class),
63+
// GET_SERVICE("/service/%s", RequestMethod.GET, Service.class),
64+
// GET_SERVICE_LOGS("/service/%s/logs/", RequestMethod.GET, Logs.class),
65+
// UPDATE_SERVICE("/service/%s/", RequestMethod.PATCH, Service.class),
66+
// START_SERVICE("/service/%s/start/", RequestMethod.POST, Service.class),
67+
// STOP_SERVICE("/service/%s/stop/", RequestMethod.POST, Service.class),
68+
// REDEPLOY_SERVICE("/service/%s/redeploy/", RequestMethod.POST, Service.class),
69+
// TERMINATE_SERVICE("/service/%s/", RequestMethod.DELETE, Service.class),
70+
// CONTAINERS("/container", RequestMethod.GET, Containers.class),
71+
// GET_CONTAINER("/container/%s", RequestMethod.GET, Container.class),
72+
// GET_CONTAINER_LOGS("/container/%s/logs/", RequestMethod.GET, Logs.class),
73+
// START_CONTAINER("/container/%s/start/", RequestMethod.POST, Container.class),
74+
// STOP_CONTAINER("/container/%s/stop/", RequestMethod.POST, Container.class),
75+
// TERMINATE_CONTAINER("/container/%s/", RequestMethod.DELETE, Container.class),
76+
// VOLUMEGROUPS("/volumegroup", RequestMethod.GET, VolumeGroups.class),
77+
// GET_VOLUMEGROUP("/volumegroup/%s", RequestMethod.GET, VolumeGroup.class),
78+
// VOLUMES("/volume", RequestMethod.GET, Volumes.class),
79+
// GET_VOLUME("/volume/%s", RequestMethod.GET, Volume.class),
80+
// TAGS("/%s/%s/tags/", RequestMethod.GET, Tags.class),
81+
// TAG_RESOURCE("/%s/%s/tags/", RequestMethod.POST, Tag[].class),
82+
// DELETE_TAG("/%s/%s/tags/%s/", RequestMethod.DELETE, Tag.class),
83+
// WEBHOOK_HANDLERS("/service/%s/webhook/handler/", RequestMethod.GET, WebhookHandlers.class),
84+
// CREATE_WEBHOOK_HANDLER("/service/%s/webhook/handler/", RequestMethod.POST, WebhookHandler[].class),
85+
// GET_WEBHOOK_HANDLER("/service/%s/webhook/handler/%s/", RequestMethod.GET, WebhookHandler.class),
86+
// DELETE_WEBHOOK_HANDLER("/service/%s/webhook/handler/%s/", RequestMethod.DELETE, WebhookHandler.class),
87+
// CALL_WEBHOOK_HANDLER("/service/%s/webhook/handler/%s/call/", RequestMethod.POST, WebhookHandler.class),
7988

8089
;
8190

src/main/java/com/pennassurancesoftware/cloudns/client/ClouDnsClient.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,19 @@
3939
import com.google.gson.JsonParser;
4040
import com.google.gson.JsonSyntaxException;
4141
import com.pennassurancesoftware.cloudns.ClouDns;
42+
import com.pennassurancesoftware.cloudns.dto.DomainZone;
43+
import com.pennassurancesoftware.cloudns.dto.DomainZoneStats;
4244
import com.pennassurancesoftware.cloudns.dto.NameServer;
45+
import com.pennassurancesoftware.cloudns.dto.Response;
4346
import com.pennassurancesoftware.cloudns.exception.ClouDnsException;
4447
import com.pennassurancesoftware.cloudns.exception.RequestUnsuccessfulException;
48+
import com.pennassurancesoftware.cloudns.type.ZoneType;
4549

4650
/** ClouDNS client wrapper methods Implementation */
4751
public class ClouDnsClient implements ClouDns {
48-
private Gson deserialize;
52+
private static final Integer DEFAULT_PAGE_SIZE = 100;
4953

54+
private Gson deserialize;
5055
private JsonParser jsonParser;
5156
private Logger LOG = LoggerFactory.getLogger( ClouDnsClient.class );
5257
private Gson serialize;
@@ -80,14 +85,37 @@ public ClouDnsClient( String authId, String authPassword, HttpClient httpClient
8085
initialize();
8186
}
8287

88+
@Override
89+
public void deleteDomainZone( String domainName ) {
90+
final Object[] params = { domainName };
91+
final Response result = ( Response )perform( new ApiRequest( ApiAction.DELETE_DOMAIN_ZONE, params ) ).getData();
92+
validateResponse( result );
93+
}
94+
8395
@Override
8496
public List<NameServer> getAvailableNameServers() {
8597
final NameServer[] result = ( NameServer[] )perform( new ApiRequest( ApiAction.AVAILABLE_NAME_SERVERS ) ).getData();
8698
return Arrays.asList( result );
8799
}
88100

89-
private String getAuthParamString() {
90-
return String.format( "auth-id=%s&auth-password=%s", authId, authPassword );
101+
@Override
102+
public List<DomainZone> getDomainZones() {
103+
final Object[] params = { 1, DEFAULT_PAGE_SIZE };
104+
final DomainZone[] result = ( DomainZone[] )perform( new ApiRequest( ApiAction.GET_DOMAIN_ZONES, params ) ).getData();
105+
return Arrays.asList( result );
106+
}
107+
108+
@Override
109+
public DomainZoneStats getDomainZoneStats() {
110+
final DomainZoneStats result = ( DomainZoneStats )perform( new ApiRequest( ApiAction.GET_DOMAIN_ZONE_STATS ) ).getData();
111+
return result;
112+
}
113+
114+
@Override
115+
public void registerDomainZone( String domainName, ZoneType type ) {
116+
final Object[] params = { domainName, type.value() };
117+
final Response result = ( Response )perform( new ApiRequest( ApiAction.REGISTER_DOMAIN_ZONE, params ) ).getData();
118+
validateResponse( result );
91119
}
92120

93121
private String createPath( ApiRequest request ) {
@@ -96,7 +124,7 @@ private String createPath( ApiRequest request ) {
96124
path = path.startsWith( "/" ) ? path.substring( 1 ) : path;
97125
final StringBuffer result = new StringBuffer();
98126
result.append( String.format( "/dns/%s", path ) );
99-
result.append( path.contains( "?" ) ? "" : "?" );
127+
result.append( path.contains( "?" ) ? "&" : "?" );
100128
result.append( getAuthParamString() );
101129
return result.toString();
102130
}
@@ -232,6 +260,10 @@ private String executeHttpRequest( HttpRequestBase request ) {
232260
return response;
233261
}
234262

263+
private String getAuthParamString() {
264+
return String.format( "auth-id=%s&auth-password=%s", authId, authPassword );
265+
}
266+
235267
private Header[] getRequestHeaders() {
236268
Header[] headers =
237269
{
@@ -341,4 +373,10 @@ private String readString( StringEntity entity ) {
341373
throw new RuntimeException( "Error reading String Entity", exception );
342374
}
343375
}
376+
377+
private void validateResponse( Response result ) {
378+
if( !result.isSuccess() ) {
379+
throw new RuntimeException( String.format( "Reponse failure: %s", result.getStatusDescription() ) );
380+
}
381+
}
344382
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.pennassurancesoftware.cloudns.dto;
2+
3+
import java.io.Serializable;
4+
5+
import com.google.gson.annotations.SerializedName;
6+
import com.pennassurancesoftware.cloudns.type.ZoneType;
7+
import com.pennassurancesoftware.cloudns.util.EnumerationUtils;
8+
9+
/** Represents a Domain Zone */
10+
public class DomainZone implements Serializable {
11+
private static final long serialVersionUID = 6478375355864290807L;
12+
13+
@SerializedName("name")
14+
private String domainName;
15+
private String type;
16+
17+
public String getDomainName() {
18+
return domainName;
19+
}
20+
21+
public ZoneType getType() {
22+
return EnumerationUtils.lookup( ZoneType.class, type );
23+
}
24+
25+
public void setDomainName( String domainName ) {
26+
this.domainName = domainName;
27+
}
28+
29+
public void setType( ZoneType type ) {
30+
this.type = type.value();
31+
}
32+
33+
@Override
34+
public String toString() {
35+
final StringBuffer result = new StringBuffer();
36+
result.append( String.format( "%s(%s)", domainName, type ) );
37+
return result.toString();
38+
}
39+
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.pennassurancesoftware.cloudns.dto;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class DomainZoneStats {
6+
@SerializedName("count")
7+
private Integer numberOfZones;
8+
private Integer limit;
9+
10+
@Override
11+
public String toString() {
12+
return String.format( "%s of %s Domain Zones Used", numberOfZones, limit );
13+
}
14+
15+
public Integer getNumberOfZones() {
16+
return numberOfZones;
17+
}
18+
19+
public void setNumberOfZones( Integer numberOfZones ) {
20+
this.numberOfZones = numberOfZones;
21+
}
22+
23+
public Integer getLimit() {
24+
return limit;
25+
}
26+
27+
public void setLimit( Integer limit ) {
28+
this.limit = limit;
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.pennassurancesoftware.cloudns.dto;
2+
3+
import java.io.Serializable;
4+
5+
import com.pennassurancesoftware.cloudns.type.ResponseStatus;
6+
import com.pennassurancesoftware.cloudns.util.EnumerationUtils;
7+
8+
public class Response implements Serializable {
9+
private static final long serialVersionUID = -7704014510595579496L;
10+
11+
private String status;
12+
private String statusDescription;
13+
14+
@Override
15+
public String toString() {
16+
return String.format( "%s(%s)", status, statusDescription );
17+
}
18+
19+
public ResponseStatus getStatus() {
20+
return EnumerationUtils.lookup( ResponseStatus.class, status );
21+
}
22+
23+
public void setStatus( ResponseStatus status ) {
24+
this.status = status.value();
25+
}
26+
27+
public String getStatusDescription() {
28+
return statusDescription;
29+
}
30+
31+
public void setStatusDescription( String statusDescription ) {
32+
this.statusDescription = statusDescription;
33+
}
34+
35+
public boolean isSuccess() {
36+
return ResponseStatus.Success.equals( getStatus() );
37+
}
38+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.pennassurancesoftware.cloudns.type;
2+
3+
public interface CodeEnum {
4+
5+
public String value();
6+
}

0 commit comments

Comments
 (0)