Aakash Parashar
Aakash Parashar

Reputation: 386

Different actions using same REST calls

I am building an application which has same functionalities both from AWS and Google Cloud. For example Creating an instance , Launching instance from Machine ID, Creating snapshot, Liting all Instances,etc. Which I called using REST calls. For example:

<form method="post" action="rest/create/new" class="form-inline">
            <label for="user">User Name</label> <input type="text" id="user"
                name="user" class="form-control" size="50"
                placeholder="Enter Username">
            <button type="submit" class="btn btn-info">Start New Machine</button>
        </form>
        <form method="post" action="rest/launch/start" class="form-inline">
            <label for="AMId">Launch Existing Machine</label><br> <input
                type="text" id="AMId" name="AMId" class="form-control" size="50"
                placeholder="Enter Instance ID">
            <button type="submit" class="btn btn-info">Launch</button>
            <br>
        </form>
        <br> <br>

        <form method="get" action="rest/create/listAll" class="form-inline">
            <label>Show All EC2 Instances</label><br>
            <button type="submit" class="btn btn-info btn-lg">Show All</button>
        </form>
        <br> <br>

        <form method="post" **action="rest/create/listRunning"**>
            <label>Show All Running EC2 Instances</label><br>
            <button type="submit" class="btn btn-info">Show All Running</button>
        </form>
        <br> <br>

        <form method="post" action="rest/create/terminate" class="form-inline">
            <label for="terminateID">Enter Instance ID</label><br> <input
                type="text" id="terminateID" name="terminateID" class="form-control"
                size="50" placeholder="Enter Machine ID">
            <button type="submit" class="btn btn-info">Terminate</button>
        </form>
        <br> <br>

And I am catching these rest calls in classes. For example,

@GET
    @Path("/listAll")
    public Response getAllAvailableImages(@Context ServletContext context)

Now what I want is how can I use both AWS and google cloud features, using these same calls or some other method,depending on requirement or choice?

Upvotes: 3

Views: 123

Answers (1)

morsor
morsor

Reputation: 1323

How about:

GET /{provider}/images
POST /{provider}/images/{imageID}/start

where the variables in braces are placeholders for path parameters:

{provider} may resolve to AWS, Google or another provider

{imageID} references a unique image ID

Examples:

GET /AWS/images   (gets all AWS images)
POST /GoogleCloud/images  (creates new Google Cloud image)
POST /OpenStack/images/gfhdh45ff4/terminate  (terminates a specific image)

If you are using Spring MVC for REST, the controller could look like:

@RestController
public class ImageController {

    @Autowired
    private ImageService imageService;

    @RequestMapping(value = "/{provider}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public List<Image> getImages(@PathVariable String provider) {
        return imageService.getImagesByProvider(provider);
    }

    @RequestMapping(value = "/{provider}", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Image createNewImage(@PathVariable String provider, @RequestBody Image image) {
        return imageService.createImageForProvider(provider, image);
    }

    @RequestMapping(value = "/{provider}/images/{imageId}/start", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void startImageAtProvider(@PathVariable String provider, @PathVariable String imageId) {
        return imageService.startImageAtProvider(provider, imageId);
    }

}

The HTTP method for starting the image could be POST - and should be if starting an image is not idempotent. But I am assuming hat attempting to start an image which is already running will just be ignored.

Extra edit:

If the image IDs are unique across ALL providers, you could shorten the REST URLs regarding images:

POST /images/gfhdh45ff4/terminate  (terminates a specific image)

Upvotes: 1

Related Questions