View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.jclouds.aws.ec2.options;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import java.util.Date;
24  
25  import org.jclouds.aws.ec2.domain.SpotInstanceRequest;
26  import org.jclouds.date.DateService;
27  import org.jclouds.date.internal.SimpleDateFormatDateService;
28  import org.jclouds.ec2.options.internal.BaseEC2RequestOptions;
29  
30  /**
31   * Contains options supported in the Form API for the RequestSpotInstances operation. <h2>
32   * Usage</h2> The recommended way validUntil instantiate a RequestSpotInstancesOptions object is
33   * validUntil statically import RequestSpotInstancesOptions.Builder.* and invoke a static creation
34   * method followed by an instance mutator (if needed):
35   * <p/>
36   * <code>
37   * import static org.jclouds.aws.ec2.options.RequestSpotInstancesOptions.Builder.*
38   * <p/>
39   * AWSEC2Client client = // get connection
40   * history = client.getSpotInstanceServices().requestSpotInstancesInRegion("us-east-1",validFrom(yesterday).type("m1.small"));
41   * <code>
42   * 
43   * @author Adrian Cole
44   * @see <a href=
45   *      "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-form-RequestSpotInstances.html"
46   *      />
47   */
48  public class RequestSpotInstancesOptions extends BaseEC2RequestOptions {
49     public static final RequestSpotInstancesOptions NONE = new RequestSpotInstancesOptions();
50     private static final DateService service = new SimpleDateFormatDateService();
51  
52     /**
53      * Start date of the request. If this is a one-time request, the request becomes active at this
54      * date and time and remains active until all instances launch, the request expires, or the
55      * request is canceled. If the request is persistent, the request becomes active at this date and
56      * time and remains active until it expires or is canceled.
57      */
58     public RequestSpotInstancesOptions validFrom(Date start) {
59        formParameters.put("ValidFrom", service.iso8601SecondsDateFormat(checkNotNull(start, "start")));
60        return this;
61     }
62  
63     /**
64      * End date of the request. If this is a one-time request, the request remains active until all
65      * instances launch, the request is canceled, or this date is reached. If the request is
66      * persistent, it remains active until it is canceled or this date and time is reached.
67      */
68     public RequestSpotInstancesOptions validUntil(Date end) {
69        formParameters.put("ValidUntil", service.iso8601SecondsDateFormat(checkNotNull(end, "end")));
70        return this;
71     }
72  
73     /**
74      * Specifies the Spot Instance type.
75      */
76     public RequestSpotInstancesOptions type(SpotInstanceRequest.Type type) {
77        formParameters.put("Type", checkNotNull(type, "type").toString());
78        return this;
79     }
80  
81     /**
82      * Specifies the instance launch group. Launch groups are Spot Instances that launch together and
83      * terminate together.
84      */
85     public RequestSpotInstancesOptions launchGroup(String launchGroup) {
86        formParameters.put("LaunchGroup", checkNotNull(launchGroup, "launchGroup"));
87        return this;
88     }
89  
90     /**
91      * Specifies the Availability Zone group. If you specify the same Availability Zone group for all
92      * Spot Instance requests, all Spot Instances are launched in the same Availability Zone.
93      */
94     public RequestSpotInstancesOptions availabilityZoneGroup(String availabilityZoneGroup) {
95        formParameters.put("AvailabilityZoneGroup", checkNotNull(availabilityZoneGroup, "availabilityZoneGroup"));
96        return this;
97     }
98  
99     public static class Builder {
100       /**
101        * @see RequestSpotInstancesOptions#validFrom
102        */
103       public static RequestSpotInstancesOptions validFrom(Date start) {
104          RequestSpotInstancesOptions options = new RequestSpotInstancesOptions();
105          return options.validFrom(start);
106       }
107 
108       /**
109        * @see RequestSpotInstancesOptions#validUntil
110        */
111       public static RequestSpotInstancesOptions validUntil(Date end) {
112          RequestSpotInstancesOptions options = new RequestSpotInstancesOptions();
113          return options.validUntil(end);
114       }
115 
116       /**
117        * @see RequestSpotInstancesOptions#type
118        */
119       public static RequestSpotInstancesOptions type(SpotInstanceRequest.Type type) {
120          RequestSpotInstancesOptions options = new RequestSpotInstancesOptions();
121          return options.type(type);
122       }
123 
124       /**
125        * @see RequestSpotInstancesOptions#launchGroup(String)
126        */
127       public static RequestSpotInstancesOptions launchGroup(String launchGroup) {
128          RequestSpotInstancesOptions options = new RequestSpotInstancesOptions();
129          return options.launchGroup(launchGroup);
130       }
131 
132       /**
133        * @see RequestSpotInstancesOptions#availabilityZoneGroup
134        */
135       public static RequestSpotInstancesOptions availabilityZoneGroup(String availabilityZoneGroup) {
136          RequestSpotInstancesOptions options = new RequestSpotInstancesOptions();
137          return options.availabilityZoneGroup(availabilityZoneGroup);
138       }
139 
140    }
141 }