使用相同REST调用的不同操作

时间:2016-03-02 13:50:53

标签: java rest amazon-web-services

我正在构建一个与AWS和Google Cloud具有相同功能的应用程序。例如,创建实例,从机器ID启动实例,创建快照,Liting所有实例等。我使用REST调用调用了它。 例如:

import {Component, OnInit} from 'angular2/core';
import {Router} from 'angular2/router';
import {SampleInfoService} from 'app/booking-info/service/sample-info-service';
import {SampleInfoModel} from 'app/booking-info/model/sample-info-model';
import {TranslateService, TranslatePipe} from 'ng2-translate/ng2-translate';
import {HTTP_PROVIDERS} from 'angular2/http';


@Component({
  selector: 'sample-information',
  templateUrl: 'app/sample-info/templates/sample-info.html',
  providers: [SampleInfoService],
  pipes: [TranslatePipe]

})
export class SampleInfoComponent implements OnInit {
    public sampleInformation = {};
    constructor(private _sampleInfoService: SampleInfoService,private _translate: TranslateService) {
            this.initializeTranslateServiceConfig(_translate, 'en')
        }

    getSampleInfo() {
        this._sampleInfoService.getInfo()
                     .subscribe(
                       SampleInfo => {
                            this.sampleInformation = SampleInfo;
                            console.log(this.sampleInformation);
                        },
                       error =>  this.errorMessage = <any>error);
    }

我正在上课时接听这些休息电话。 例如,

<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>

现在我想要的是如何根据需求或选择使用这些相同的调用或其他方法同时使用AWS和Google云功能?

1 个答案:

答案 0 :(得分:1)

怎么样:

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

其中大括号中的变量是路径参数的占位符:

{provider}可以解析为AWS,Google或其他提供商

{imageID}引用唯一的图片ID

示例:

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

如果您使用Spring MVC for REST,控制器可能如下所示:

@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);
    }

}

用于启动图像的HTTP方法可以是POST - 并且应该是在启动图像时不是幂等的。但我假设帽子试图启动已经运行的图像将被忽略。

额外编辑:

如果图像ID在所有提供程序中都是唯一的,则可以缩短有关图像的REST URL:

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