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.trmk.vcloud_0_8.compute.options;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  import static com.google.common.base.Preconditions.checkState;
23  
24  import java.util.Arrays;
25  import java.util.Map;
26  
27  import org.jclouds.compute.options.TemplateOptions;
28  import org.jclouds.io.Payload;
29  import org.jclouds.util.Preconditions2;
30  
31  /**
32   * Contains options supported in the {@code ComputeService#runNode} operation on
33   * the "trmk-vcloudexpress" provider. <h2>
34   * Usage</h2> The recommended way to instantiate a
35   * TerremarkVCloudTemplateOptions object is to statically import
36   * TerremarkVCloudTemplateOptions.* and invoke a static creation method followed
37   * by an instance mutator (if needed):
38   * <p/>
39   * <code>
40   * import static org.jclouds.vcloud.terremark.compute.options.TerremarkVCloudTemplateOptions.Builder.*;
41   * <p/>
42   * ComputeService client = // get connection
43   * templateBuilder.options(inboundPorts(22, 80, 8080, 443));
44   * Set<? extends NodeMetadata> set = client.runNodesWithTag(tag, 2, templateBuilder.build());
45   * <code>
46   * 
47   * @author Adrian Cole
48   */
49  public class TerremarkVCloudTemplateOptions extends TemplateOptions implements Cloneable {
50     @Override
51     public TerremarkVCloudTemplateOptions clone() {
52        TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
53        copyTo(options);
54        return options;
55     }
56  
57     @Override
58     public void copyTo(TemplateOptions to) {
59        super.copyTo(to);
60        if (to instanceof TerremarkVCloudTemplateOptions) {
61           TerremarkVCloudTemplateOptions eTo = TerremarkVCloudTemplateOptions.class.cast(to);
62           if (noKeyPair)
63              eTo.noKeyPair();
64           if (keyPair != null)
65              eTo.keyPair = keyPair;
66        }
67     }
68  
69     private String keyPair = null;
70     private boolean noKeyPair;
71  
72     public static final TerremarkVCloudTemplateOptions NONE = new TerremarkVCloudTemplateOptions();
73  
74     /**
75      * Specifies the keypair used to run instances with
76      */
77     public TerremarkVCloudTemplateOptions sshKeyFingerprint(String keyPair) {
78        checkNotNull(keyPair, "use noKeyPair option to request boot without a keypair");
79        checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair");
80        Preconditions2.checkNotEmpty(keyPair, "keypair must be non-empty");
81        this.keyPair = keyPair;
82        return this;
83     }
84  
85     /**
86      * Do not use a keypair on instances
87      */
88     public TerremarkVCloudTemplateOptions noKeyPair() {
89        checkState(keyPair == null, "you cannot specify both options keyPair and noKeyPair");
90        this.noKeyPair = true;
91        return this;
92     }
93  
94     public static class Builder {
95  
96        /**
97         * @see TerremarkVCloudTemplateOptions#sshKeyFingerprint
98         */
99        public static TerremarkVCloudTemplateOptions sshKeyFingerprint(String keyPair) {
100          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
101          return TerremarkVCloudTemplateOptions.class.cast(options.sshKeyFingerprint(keyPair));
102       }
103 
104       /**
105        * @see TerremarkVCloudTemplateOptions#noKeyPair
106        */
107       public static TerremarkVCloudTemplateOptions noKeyPair() {
108          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
109          return TerremarkVCloudTemplateOptions.class.cast(options.noKeyPair());
110       }
111 
112       // methods that only facilitate returning the correct object type
113       /**
114        * @see TemplateOptions#inboundPorts
115        */
116       public static TerremarkVCloudTemplateOptions inboundPorts(int... ports) {
117          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
118          return TerremarkVCloudTemplateOptions.class.cast(options.inboundPorts(ports));
119       }
120 
121       /**
122        * @see TemplateOptions#port
123        */
124       public static TerremarkVCloudTemplateOptions blockOnPort(int port, int seconds) {
125          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
126          return TerremarkVCloudTemplateOptions.class.cast(options.blockOnPort(port, seconds));
127       }
128 
129       /**
130        * @see TemplateOptions#blockUntilRunning
131        */
132       public static TerremarkVCloudTemplateOptions blockUntilRunning(boolean blockUntilRunning) {
133          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
134          return TerremarkVCloudTemplateOptions.class.cast(options.blockUntilRunning(blockUntilRunning));
135       }
136 
137       /**
138        * @see TemplateOptions#runScript
139        */
140       public static TerremarkVCloudTemplateOptions runScript(byte[] script) {
141          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
142          return TerremarkVCloudTemplateOptions.class.cast(options.runScript(script));
143       }
144 
145       /**
146        * @see TemplateOptions#installPrivateKey
147        */
148       public static TerremarkVCloudTemplateOptions installPrivateKey(String rsaKey) {
149          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
150          return TerremarkVCloudTemplateOptions.class.cast(options.installPrivateKey(rsaKey));
151       }
152 
153       /**
154        * @see TemplateOptions#authorizePublicKey
155        */
156       public static TerremarkVCloudTemplateOptions authorizePublicKey(String rsaKey) {
157          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
158          return TerremarkVCloudTemplateOptions.class.cast(options.authorizePublicKey(rsaKey));
159       }
160 
161       /**
162        * @see TemplateOptions#userMetadata(Map)
163        */
164       public static TerremarkVCloudTemplateOptions userMetadata(Map<String, String> userMetadata) {
165          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
166          return TerremarkVCloudTemplateOptions.class.cast(options.userMetadata(userMetadata));
167       }
168 
169       /**
170        * @see TemplateOptions#userMetadata(String, String)
171        */
172       public static TerremarkVCloudTemplateOptions userMetadata(String key, String value) {
173          TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
174          return TerremarkVCloudTemplateOptions.class.cast(options.userMetadata(key, value));
175       }
176    }
177 
178    // methods that only facilitate returning the correct object type
179 
180    /**
181     * @see TemplateOptions#blockOnPort
182     */
183    @Override
184    public TerremarkVCloudTemplateOptions blockOnPort(int port, int seconds) {
185       return TerremarkVCloudTemplateOptions.class.cast(super.blockOnPort(port, seconds));
186    }
187 
188    /**
189     * 
190     * special thing is that we do assume if you are passing groups that you have
191     * everything you need already defined. for example, our option inboundPorts
192     * normally creates ingress rules accordingly but if we notice you've
193     * specified securityGroups, we do not mess with rules at all
194     * 
195     * @see TemplateOptions#inboundPorts
196     */
197    @Override
198    public TerremarkVCloudTemplateOptions inboundPorts(int... ports) {
199       return TerremarkVCloudTemplateOptions.class.cast(super.inboundPorts(ports));
200    }
201 
202    /**
203     * @see TemplateOptions#authorizePublicKey(String)
204     */
205    @Override
206    public TerremarkVCloudTemplateOptions authorizePublicKey(String publicKey) {
207       return TerremarkVCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
208    }
209 
210    /**
211     * @see TemplateOptions#authorizePublicKey(Payload)
212     */
213    @Override
214    @Deprecated
215    public TerremarkVCloudTemplateOptions authorizePublicKey(Payload publicKey) {
216       return TerremarkVCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
217    }
218 
219    /**
220     * @see TemplateOptions#installPrivateKey(String)
221     */
222    @Override
223    public TerremarkVCloudTemplateOptions installPrivateKey(String privateKey) {
224       return TerremarkVCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
225    }
226 
227    /**
228     * @see TemplateOptions#installPrivateKey(Payload)
229     */
230    @Override
231    @Deprecated
232    public TerremarkVCloudTemplateOptions installPrivateKey(Payload privateKey) {
233       return TerremarkVCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
234    }
235 
236    /**
237     * @see TemplateOptions#runScript(Payload)
238     */
239    @Override
240    public TerremarkVCloudTemplateOptions runScript(Payload script) {
241       return TerremarkVCloudTemplateOptions.class.cast(super.runScript(script));
242    }
243 
244    /**
245     * @see TemplateOptions#runScript(byte[])
246     */
247    @Override
248    @Deprecated
249    public TerremarkVCloudTemplateOptions runScript(byte[] script) {
250       return TerremarkVCloudTemplateOptions.class.cast(super.runScript(script));
251    }
252 
253    /**
254     * @see TemplateOptions#userMetadata
255     */
256    public TerremarkVCloudTemplateOptions userMetadata(Map<String, String> userMetadata) {
257       return TerremarkVCloudTemplateOptions.class.cast(super.userMetadata(userMetadata));
258    }
259 
260    /**
261     * {@inheritDoc}
262     */
263    @Override
264    public TerremarkVCloudTemplateOptions userMetadata(String key, String value) {
265       return TerremarkVCloudTemplateOptions.class.cast(super.userMetadata(key, value));
266    }
267 
268    /**
269     * @return keyPair to use when running the instance or null, to generate a
270     *         keypair.
271     */
272    public String getSshKeyFingerprint() {
273       return keyPair;
274    }
275 
276    /**
277     * @return true (default) if we are supposed to use a keypair
278     */
279    public boolean shouldAutomaticallyCreateKeyPair() {
280       return !noKeyPair;
281    }
282 
283    @Override
284    public int hashCode() {
285       final int prime = 31;
286       int result = super.hashCode();
287       result = prime * result + ((keyPair == null) ? 0 : keyPair.hashCode());
288       result = prime * result + (noKeyPair ? 1231 : 1237);
289       return result;
290    }
291 
292    @Override
293    public boolean equals(Object obj) {
294       if (this == obj)
295          return true;
296       if (!super.equals(obj))
297          return false;
298       if (getClass() != obj.getClass())
299          return false;
300       TerremarkVCloudTemplateOptions other = (TerremarkVCloudTemplateOptions) obj;
301       if (keyPair == null) {
302          if (other.keyPair != null)
303             return false;
304       } else if (!keyPair.equals(other.keyPair))
305          return false;
306       if (noKeyPair != other.noKeyPair)
307          return false;
308       return true;
309    }
310 
311    @Override
312    public String toString() {
313       return "TerremarkVCloudTemplateOptions [keyPair=" + keyPair + ", noKeyPair=" + noKeyPair + ", inboundPorts="
314             + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey="
315             + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds
316             + ", userMetadata: " + userMetadata + "]";
317    }
318 
319 }