示例#1
0
  @Path("/vm")
  @POST
  @ApiOperation(value = "starts a remote VM defined by a macro script", notes = "")
  @ApiResponses(
      value = {
        @ApiResponse(code = 400, message = "No request id supplied"),
        @ApiResponse(code = 404, message = "Request not found"),
        @ApiResponse(
            code = 500,
            message = "Unable to complete request, see response body for error details")
      })
  public Response startVM(
      @ApiParam(value = "The instance specification to use when starting the VM", required = true)
          InstanceSpec instanceSpec,
      @ApiParam(value = "The authenticated session token", required = true)
          @HeaderParam("CICSTART.session")
          String sessionToken) {

    String cloudName = instanceSpec.getCloud();
    String jobId = instanceSpec.getRequestId();
    String imageName = instanceSpec.getImage();
    String flavorName = instanceSpec.getFlavor();

    Image theImage = null;
    ServiceResponse<List<Image>> getImagesSr = cloudService.getImages(cloudName, sessionToken);
    if (!getImagesSr.isRequestOk()) {
      logger.error(getImagesSr.getMessagesAsStrings());
    }
    List<Image> images = getImagesSr.getPayload();
    for (Image image : images) {

      if (image.name.equals(imageName)) {
        theImage = image;
        break;
      }
    }

    if (theImage == null) {
      return Response.status(400).entity("No image named " + imageName).build();
    }

    Flavor flavor = null;
    try {
      flavor = Flavor.valueOf(flavorName.replaceAll("\\.", "_"));
    } catch (IllegalArgumentException e) {
      return Response.status(400).entity("No flavor named " + flavorName).build();
    }

    ServiceResponse<Instance> sr =
        cloudService.startInstance(cloudName, theImage, flavor, sessionToken, jobId);

    if (sr.isRequestOk()) {
      return Response.ok(sr.getPayload()).build();
    } else {
      logger.warn("VM not started because " + sr.getMessagesAsStrings());
      return Response.status(500).entity(sr.getMessagesAsStrings()).build();
    }
  }
示例#2
0
  @Path("/vm")
  @DELETE
  @ApiOperation(value = "stops a remote VM", notes = "")
  @ApiResponses(
      value = {
        @ApiResponse(code = 400, message = "No request id supplied"),
        @ApiResponse(code = 404, message = "Request not found"),
        @ApiResponse(
            code = 500,
            message = "Unable to complete request, see response body for error details")
      })
  public Response stopVM(
      @ApiParam(value = "The instance to stop", required = true) Instance instance,
      @ApiParam(value = "The authenticated session token", required = true)
          @HeaderParam("CICSTART.session")
          String sessionToken) {

    ServiceResponse<Void> sr = cloudService.stopInstance(instance, sessionToken);

    if (sr.isRequestOk()) {
      return Response.ok(sr.getPayload()).build();
    } else {
      return Response.status(500).entity(sr.getMessagesAsStrings()).build();
    }
  }