public SpotPrice unmarshall(StaxUnmarshallerContext context) throws Exception {
    SpotPrice spotPrice = new SpotPrice();
    int originalDepth = context.getCurrentDepth();
    int targetDepth = originalDepth + 1;

    if (context.isStartOfDocument()) targetDepth += 1;

    while (true) {
      XMLEvent xmlEvent = context.nextEvent();
      if (xmlEvent.isEndDocument()) return spotPrice;

      if (xmlEvent.isAttribute() || xmlEvent.isStartElement()) {

        if (context.testExpression("instanceType", targetDepth)) {
          spotPrice.setInstanceType(StringStaxUnmarshaller.getInstance().unmarshall(context));
          continue;
        }

        if (context.testExpression("productDescription", targetDepth)) {
          spotPrice.setProductDescription(StringStaxUnmarshaller.getInstance().unmarshall(context));
          continue;
        }

        if (context.testExpression("spotPrice", targetDepth)) {
          spotPrice.setSpotPrice(StringStaxUnmarshaller.getInstance().unmarshall(context));
          continue;
        }

        if (context.testExpression("timestamp", targetDepth)) {
          spotPrice.setTimestamp(DateStaxUnmarshaller.getInstance().unmarshall(context));
          continue;
        }

        if (context.testExpression("availabilityZone", targetDepth)) {
          spotPrice.setAvailabilityZone(StringStaxUnmarshaller.getInstance().unmarshall(context));
          continue;
        }
      } else if (xmlEvent.isEndElement()) {
        if (context.getCurrentDepth() < originalDepth) {
          return spotPrice;
        }
      }
    }
  }
예제 #2
0
    /*
     * Check the current Spot price of the selected instance type for the selected region
     */
    public FormValidation doCurrentSpotPrice(
        @QueryParameter boolean useInstanceProfileForCredentials,
        @QueryParameter String credentialsId,
        @QueryParameter String region,
        @QueryParameter String type,
        @QueryParameter String zone)
        throws IOException, ServletException {

      String cp = "";
      String zoneStr = "";

      // Connect to the EC2 cloud with the access id, secret key, and
      // region queried from the created cloud
      AWSCredentialsProvider credentialsProvider =
          EC2Cloud.createCredentialsProvider(useInstanceProfileForCredentials, credentialsId);
      AmazonEC2 ec2 =
          EC2Cloud.connect(credentialsProvider, AmazonEC2Cloud.getEc2EndpointUrl(region));

      if (ec2 != null) {

        try {
          // Build a new price history request with the currently
          // selected type
          DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest();
          // If a zone is specified, set the availability zone in the
          // request
          // Else, proceed with no availability zone which will result
          // with the cheapest Spot price
          if (getAvailabilityZones(ec2).contains(zone)) {
            request.setAvailabilityZone(zone);
            zoneStr = zone + " availability zone";
          } else {
            zoneStr = region + " region";
          }

          /*
           * Iterate through the AWS instance types to see if can find a match for the databound String type.
           * This is necessary because the AWS API needs the instance type string formatted a particular way
           * to retrieve prices and the form gives us the strings in a different format. For example "T1Micro"
           * vs "t1.micro".
           */
          InstanceType ec2Type = null;

          for (InstanceType it : InstanceType.values()) {
            if (it.name().equals(type)) {
              ec2Type = it;
              break;
            }
          }

          /*
           * If the type string cannot be matched with an instance type, throw a Form error
           */
          if (ec2Type == null) {
            return FormValidation.error("Could not resolve instance type: " + type);
          }

          Collection<String> instanceType = new ArrayList<String>();
          instanceType.add(ec2Type.toString());
          request.setInstanceTypes(instanceType);
          request.setStartTime(new Date());

          // Retrieve the price history request result and store the
          // current price
          DescribeSpotPriceHistoryResult result = ec2.describeSpotPriceHistory(request);

          if (!result.getSpotPriceHistory().isEmpty()) {
            SpotPrice currentPrice = result.getSpotPriceHistory().get(0);

            cp = currentPrice.getSpotPrice();
          }

        } catch (AmazonServiceException e) {
          return FormValidation.error(e.getMessage());
        }
      }
      /*
       * If we could not return the current price of the instance display an error Else, remove the additional
       * zeros from the current price and return it to the interface in the form of a message
       */
      if (cp.isEmpty()) {
        return FormValidation.error("Could not retrieve current Spot price");
      } else {
        cp = cp.substring(0, cp.length() - 3);

        return FormValidation.ok(
            "The current Spot price for a " + type + " in the " + zoneStr + " is $" + cp);
      }
    }