Skip to content

Commit

Permalink
#48 /tags/* API implementation
Browse files Browse the repository at this point in the history
However untag resources won’t work, it looks http client upgrade
required to send DELETE Payload
  • Loading branch information
jeevatkm committed Jun 6, 2016
1 parent ffb9805 commit f2dec13
Show file tree
Hide file tree
Showing 13 changed files with 928 additions and 148 deletions.
285 changes: 198 additions & 87 deletions src/main/java/com/myjeeva/digitalocean/DigitalOcean.java

Large diffs are not rendered by default.

28 changes: 21 additions & 7 deletions src/main/java/com/myjeeva/digitalocean/common/ApiAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/**
* The MIT License
*
* Copyright (c) 2010-2015 Jeevanandam M. (myjeeva.com)
* Copyright (c) 2013-2016 Jeevanandam M. (myjeeva.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
Expand All @@ -18,6 +18,7 @@
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.myjeeva.digitalocean.common;

import com.myjeeva.digitalocean.pojo.Account;
Expand All @@ -40,8 +41,11 @@
import com.myjeeva.digitalocean.pojo.Keys;
import com.myjeeva.digitalocean.pojo.Neighbors;
import com.myjeeva.digitalocean.pojo.Regions;
import com.myjeeva.digitalocean.pojo.Response;
import com.myjeeva.digitalocean.pojo.Sizes;
import com.myjeeva.digitalocean.pojo.Snapshots;
import com.myjeeva.digitalocean.pojo.Tag;
import com.myjeeva.digitalocean.pojo.Tags;

/**
* Enumeration of DigitalOcean RESTful resource information.
Expand Down Expand Up @@ -118,32 +122,42 @@ public enum ApiAction {
AVAILABLE_DOMAINS("/domains", "domains", RequestMethod.GET, Domains.class),
GET_DOMAIN_INFO("/domains/%s", "domain", RequestMethod.GET, Domain.class),
CREATE_DOMAIN("/domains", "domain", RequestMethod.POST, Domain.class),
DELETE_DOMAIN("/domains/%s", "delete", RequestMethod.DELETE, Delete.class),
DELETE_DOMAIN("/domains/%s", "response", RequestMethod.DELETE, Delete.class),


// Domain Record
GET_DOMAIN_RECORDS("/domains/%s/records", "domain_records", RequestMethod.GET, DomainRecords.class),
GET_DOMAIN_RECORD_INFO("/domains/%s/records/%s", "domain_record", RequestMethod.GET, DomainRecord.class),
CREATE_DOMAIN_RECORD("/domains/%s/records", "domain_record", RequestMethod.POST, DomainRecord.class),
UPDATE_DOMAIN_RECORD("/domains/%s/records/%s", "domain_record", RequestMethod.PUT, DomainRecord.class),
DELETE_DOMAIN_RECORD("/domains/%s/records/%s", "delete", RequestMethod.DELETE, Delete.class),
DELETE_DOMAIN_RECORD("/domains/%s/records/%s", "response", RequestMethod.DELETE, Delete.class),


// Key
AVAILABLE_KEYS("/account/keys", "ssh_keys", RequestMethod.GET, Keys.class),
GET_KEY_INFO("/account/keys/%s", "ssh_key", RequestMethod.GET, Key.class),
CREATE_KEY("/account/keys", "ssh_key", RequestMethod.POST, Key.class),
UPDATE_KEY("/account/keys/%s", "ssh_key", RequestMethod.PUT, Key.class),
DELETE_KEY("/account/keys/%s", "delete", RequestMethod.DELETE, Delete.class),
DELETE_KEY("/account/keys/%s", "response", RequestMethod.DELETE, Delete.class),


// Floating IP
FLOATING_IPS("/floating_ips", "floating_ips", RequestMethod.GET, FloatingIPs.class),
CREATE_FLOATING_IP("/floating_ips", "floating_ip", RequestMethod.POST, FloatingIP.class),
GET_FLOATING_IP_INFO("/floating_ips/%s", "floating_ip", RequestMethod.GET, FloatingIP.class),
DELETE_FLOATING_IP("/floating_ips/%s", "delete", RequestMethod.DELETE, Delete.class),
DELETE_FLOATING_IP("/floating_ips/%s", "response", RequestMethod.DELETE, Delete.class),
ASSIGN_FLOATING_IP("/floating_ips/%s/actions", "action", RequestMethod.POST, Action.class),
UNASSIGN_FLOATING_IP("/floating_ips/%s/actions", "action", RequestMethod.POST, Action.class);
UNASSIGN_FLOATING_IP("/floating_ips/%s/actions", "action", RequestMethod.POST, Action.class),


// Tags
AVAILABLE_TAGS("/tags", "tags", RequestMethod.GET, Tags.class),
CREATE_TAG("/tags", "tag", RequestMethod.POST, Tag.class),
GET_TAG("/tags/%s", "tag", RequestMethod.GET, Tag.class),
UPDATE_TAG("/tags/%s", "tag", RequestMethod.PUT, Tag.class),
DELETE_TAG("/tags/%s", "response", RequestMethod.DELETE, Delete.class),
TAG_RESOURCE("/tags/%s/resources", "response", RequestMethod.POST, Response.class),
UNTAG_RESOURCE("/tags/%s/resources", "response", RequestMethod.DELETE, Response.class);


private String path;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/myjeeva/digitalocean/common/Constants.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/**
* The MIT License
*
* Copyright (c) 2010-2015 Jeevanandam M. (myjeeva.com)
* Copyright (c) 2013-2016 Jeevanandam M. (myjeeva.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
Expand All @@ -18,6 +18,7 @@
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.myjeeva.digitalocean.common;

import org.apache.http.HttpHeaders;
Expand Down Expand Up @@ -51,7 +52,7 @@ public interface Constants {
String PARAM_PAGE_NO = "page";
String PARAM_PER_PAGE = "per_page";

String NO_CONTENT_JSON_STRUCT = "{\"delete\": {\"request_status\": true, \"status_code\": %s}}";
String NO_CONTENT_JSON_STRUCT = "{\"response\": {\"request_status\": true, \"status_code\": %s}}";

// HTTP Headers
String HDR_USER_AGENT = "X-User-Agent";
Expand Down
106 changes: 96 additions & 10 deletions src/main/java/com/myjeeva/digitalocean/impl/DigitalOceanClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/**
* The MIT License
*
* Copyright (c) 2010-2015 Jeevanandam M. (myjeeva.com)
* Copyright (c) 2013-2016 Jeevanandam M. (myjeeva.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
Expand All @@ -18,6 +18,7 @@
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.myjeeva.digitalocean.impl;

import java.io.IOException;
Expand All @@ -27,6 +28,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -87,8 +89,13 @@
import com.myjeeva.digitalocean.pojo.Keys;
import com.myjeeva.digitalocean.pojo.Neighbors;
import com.myjeeva.digitalocean.pojo.Regions;
import com.myjeeva.digitalocean.pojo.Resource;
import com.myjeeva.digitalocean.pojo.Resources;
import com.myjeeva.digitalocean.pojo.Response;
import com.myjeeva.digitalocean.pojo.Sizes;
import com.myjeeva.digitalocean.pojo.Snapshots;
import com.myjeeva.digitalocean.pojo.Tag;
import com.myjeeva.digitalocean.pojo.Tags;
import com.myjeeva.digitalocean.serializer.DropletSerializer;

/**
Expand Down Expand Up @@ -347,7 +354,7 @@ public Action rebootDroplet(Integer dropletId) throws DigitalOceanException,
Object[] params = {dropletId};
return (Action) perform(
new ApiRequest(ApiAction.REBOOT_DROPLET, new DropletAction(ActionType.REBOOT), params))
.getData();
.getData();
}

@Override
Expand All @@ -369,7 +376,7 @@ public Action shutdownDroplet(Integer dropletId) throws DigitalOceanException,
Object[] params = {dropletId};
return (Action) perform(
new ApiRequest(ApiAction.SHUTDOWN_DROPLET, new DropletAction(ActionType.SHUTDOWN), params))
.getData();
.getData();
}

@Override
Expand All @@ -379,8 +386,9 @@ public Action powerOffDroplet(Integer dropletId) throws DigitalOceanException,

Object[] params = {dropletId};
return (Action) perform(
new ApiRequest(ApiAction.POWER_OFF_DROPLET, new DropletAction(ActionType.POWER_OFF), params))
.getData();
new ApiRequest(ApiAction.POWER_OFF_DROPLET, new DropletAction(ActionType.POWER_OFF),
params))
.getData();
}

@Override
Expand All @@ -391,7 +399,7 @@ public Action powerOnDroplet(Integer dropletId) throws DigitalOceanException,
Object[] params = {dropletId};
return (Action) perform(
new ApiRequest(ApiAction.POWER_ON_DROPLET, new DropletAction(ActionType.POWER_ON), params))
.getData();
.getData();
}

@Override
Expand Down Expand Up @@ -424,7 +432,7 @@ public Action takeDropletSnapshot(Integer dropletId) throws DigitalOceanExceptio
Object[] params = {dropletId};
return (Action) perform(
new ApiRequest(ApiAction.SNAPSHOT_DROPLET, new DropletAction(ActionType.SNAPSHOT), params))
.getData();
.getData();
}

@Override
Expand Down Expand Up @@ -651,7 +659,8 @@ public Image getImageInfo(Integer imageId) throws DigitalOceanException,
}

@Override
public Image getImageInfo(String slug) throws DigitalOceanException, RequestUnsuccessfulException {
public Image getImageInfo(String slug)
throws DigitalOceanException, RequestUnsuccessfulException {
checkEmptyAndThrowError(slug, "Missing required parameter - slug.");

Object[] params = {slug};
Expand Down Expand Up @@ -697,7 +706,7 @@ public Action convertImage(Integer imageId) throws DigitalOceanException,
Object[] params = {imageId};
return (Action) perform(
new ApiRequest(ApiAction.CONVERT_IMAGE, new ImageAction(ActionType.CONVERT), params))
.getData();
.getData();
}


Expand Down Expand Up @@ -970,6 +979,82 @@ public Action unassignFloatingIP(String ipAddress) throws DigitalOceanException,
params)).getData();
}

// =======================================
// Tags methods
// =======================================

@Override
public Tags getAvailableTags(Integer pageNo, Integer perPage)
throws DigitalOceanException, RequestUnsuccessfulException {
validatePageNo(pageNo);

return (Tags) perform(new ApiRequest(ApiAction.AVAILABLE_TAGS, pageNo, perPage)).getData();
}

@Override
public Tag createTag(String name) throws DigitalOceanException, RequestUnsuccessfulException {
checkEmptyAndThrowError(name, "Missing required parameter - tag name");

return (Tag) perform(
new ApiRequest(ApiAction.CREATE_TAG, new Tag(name))).getData();
}

@Override
public Tag getTag(String name) throws DigitalOceanException, RequestUnsuccessfulException {
checkEmptyAndThrowError(name, "Missing required parameter - tag name");

Object[] params = {name};
return (Tag) perform(new ApiRequest(ApiAction.GET_TAG, params)).getData();
}

@Override
public Tag updateTag(String currentName, String newName)
throws DigitalOceanException, RequestUnsuccessfulException {
checkEmptyAndThrowError(currentName, "Missing required parameter - current tag name");
checkEmptyAndThrowError(newName, "Missing required parameter - new tag name");

Object[] params = {currentName};
return (Tag) perform(new ApiRequest(ApiAction.UPDATE_TAG, new Tag(newName), params)).getData();
}

@Override
public Delete deleteTag(String name) throws DigitalOceanException,
RequestUnsuccessfulException {
checkEmptyAndThrowError(name, "Missing required parameter - tag name");

Object[] params = {name};
return (Delete) perform(new ApiRequest(ApiAction.DELETE_TAG, params)).getData();
}


@Override
public Response tagResources(String name, List<Resource> resources)
throws DigitalOceanException, RequestUnsuccessfulException {
checkEmptyAndThrowError(name, "Missing required parameter - tag name");
if (null == resources || resources.size() == 0) {
throw new IllegalArgumentException(
"Missing required parameter - list of resources for tag");
}

Object[] params = {name};
return (Response) perform(
new ApiRequest(ApiAction.TAG_RESOURCE, new Resources(resources), params)).getData();
}


@Override
public Response untagResources(String name, List<Resource> resources)
throws DigitalOceanException, RequestUnsuccessfulException {
checkEmptyAndThrowError(name, "Missing required parameter - tag name");
if (null == resources || resources.size() == 0) {
throw new IllegalArgumentException(
"Missing required parameter - list of resources for untag");
}

Object[] params = {name};
return (Response) perform(
new ApiRequest(ApiAction.UNTAG_RESOURCE, new Resources(resources), params)).getData();
}


//
Expand Down Expand Up @@ -1292,4 +1377,5 @@ private void initialize() {
this.httpClient = HttpClients.createDefault();
}
}

}
44 changes: 5 additions & 39 deletions src/main/java/com/myjeeva/digitalocean/pojo/Delete.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/**
* The MIT License
*
* Copyright (c) 2010-2015 Jeevanandam M. (myjeeva.com)
* Copyright (c) 2013-2016 Jeevanandam M. (myjeeva.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
Expand All @@ -18,29 +18,22 @@
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.myjeeva.digitalocean.pojo;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import com.google.gson.annotations.SerializedName;

/**
* Represents HTTP Method - DELETE response handling
*
* @author Jeevanandam M. (jeeva@myjeeva.com)
*
* @since v2.0
*/
public class Delete extends RateLimitBase {
public class Delete extends Response {

private static final long serialVersionUID = -3552374545843268569L;

@SerializedName("request_status")
private Boolean isRequestSuccess;

@SerializedName("status_code")
private int statusCode;

/**
* Default Constructor
*/
Expand All @@ -54,39 +47,12 @@ public Delete() {
* @param isRequestSuccess whether delete is success or not
*/
public Delete(Boolean isRequestSuccess) {
this.isRequestSuccess = isRequestSuccess;
super(isRequestSuccess);
}

@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}

/**
* @return the isRequestSuccess
*/
public Boolean getIsRequestSuccess() {
return isRequestSuccess;
}

/**
* @param isRequestSuccess the isRequestSuccess to set
*/
public void setIsRequestSuccess(Boolean isRequestSuccess) {
this.isRequestSuccess = isRequestSuccess;
}

/**
* @return the statusCode
*/
public int getStatusCode() {
return statusCode;
}

/**
* @param statusCode the statusCode to set
*/
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
}
Loading

0 comments on commit f2dec13

Please sign in to comment.