EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.cloudservers.options]

COVERAGE SUMMARY FOR SOURCE FILE [CreateServerOptions.java]

nameclass, %method, %block, %line, %
CreateServerOptions.java80%  (4/5)76%  (13/17)94%  (346/370)92%  (50.5/55)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateServerOptions$Builder100% (1/1)60%  (3/5)69%  (25/36)67%  (6/9)
CreateServerOptions$Builder (): void 0%   (0/1)0%   (0/3)0%   (0/1)
withSharedIp (String): CreateServerOptions 0%   (0/1)0%   (0/8)0%   (0/2)
withFile (String, byte []): CreateServerOptions 100% (1/1)100% (9/9)100% (2/2)
withMetadata (Map): CreateServerOptions 100% (1/1)100% (8/8)100% (2/2)
withSharedIpGroup (int): CreateServerOptions 100% (1/1)100% (8/8)100% (2/2)
     
class CreateServerOptions$File100% (1/1)33%  (1/3)88%  (56/64)74%  (5.9/8)
getContents (): String 0%   (0/1)0%   (0/3)0%   (0/1)
getPath (): String 0%   (0/1)0%   (0/3)0%   (0/1)
CreateServerOptions$File (String, byte []): void 100% (1/1)97%  (56/58)99%  (5.9/6)
     
class CreateServerOptions100% (1/1)100% (7/7)98%  (243/248)99%  (32.8/33)
withSharedIpGroup (int): CreateServerOptions 100% (1/1)95%  (19/20)98%  (3/3)
withFile (String, byte []): CreateServerOptions 100% (1/1)95%  (20/21)98%  (3/3)
withMetadata (Map): CreateServerOptions 100% (1/1)97%  (98/101)99%  (6.9/7)
CreateServerOptions (): void 100% (1/1)100% (9/9)100% (4/4)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)100% (75/75)100% (12/12)
bindToRequest (HttpRequest, Object): HttpRequest 100% (1/1)100% (6/6)100% (1/1)
withSharedIp (String): CreateServerOptions 100% (1/1)100% (16/16)100% (3/3)
     
class CreateServerOptions$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class CreateServerOptions$ServerRequest100% (1/1)100% (2/2)100% (22/22)100% (6/6)
CreateServerOptions$ServerRequest (CreateServerOptions, String, int, int): void 100% (1/1)100% (15/15)100% (5/5)
CreateServerOptions$ServerRequest (CreateServerOptions, String, int, int, Cre... 100% (1/1)100% (7/7)100% (1/1)

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 */
19package org.jclouds.cloudservers.options;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23import static com.google.common.base.Preconditions.checkState;
24 
25import java.util.List;
26import java.util.Map;
27import java.util.Map.Entry;
28 
29import javax.inject.Inject;
30 
31import org.jclouds.cloudservers.domain.Addresses;
32import org.jclouds.encryption.internal.Base64;
33import org.jclouds.http.HttpRequest;
34import org.jclouds.rest.MapBinder;
35import org.jclouds.rest.binders.BindToJsonPayload;
36 
37import com.google.common.collect.ImmutableMap;
38import com.google.common.collect.Lists;
39import com.google.common.collect.Maps;
40 
41/**
42 * 
43 * @author Adrian Cole
44 * 
45 */
46public class CreateServerOptions implements MapBinder {
47   @Inject
48   private BindToJsonPayload jsonBinder;
49 
50   static class File {
51      private final String path;
52      private final String contents;
53 
54      public File(String path, byte[] contents) {
55         this.path = checkNotNull(path, "path");
56         this.contents = Base64.encodeBytes(checkNotNull(contents, "contents"));
57         checkArgument(path.getBytes().length < 255, String.format(
58                  "maximum length of path is 255 bytes.  Path specified %s is %d bytes", path, path.getBytes().length));
59         checkArgument(contents.length < 10 * 1024, String.format(
60                  "maximum size of the file is 10KB.  Contents specified is %d bytes", contents.length));
61      }
62 
63      public String getContents() {
64         return contents;
65      }
66 
67      public String getPath() {
68         return path;
69      }
70 
71   }
72 
73   @SuppressWarnings("unused")
74   private class ServerRequest {
75      final String name;
76      final int imageId;
77      final int flavorId;
78      Map<String, String> metadata;
79      List<File> personality;
80      Integer sharedIpGroupId;
81      Addresses addresses;
82 
83      private ServerRequest(String name, int imageId, int flavorId) {
84         this.name = name;
85         this.imageId = imageId;
86         this.flavorId = flavorId;
87      }
88 
89   }
90 
91   private Map<String, String> metadata = Maps.newHashMap();
92   private List<File> files = Lists.newArrayList();
93   private Integer sharedIpGroupId;
94   private String publicIp;
95 
96   @Override
97   public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
98      ServerRequest server = new ServerRequest(checkNotNull(postParams.get("name"), "name parameter not present"),
99               Integer.parseInt(checkNotNull(postParams.get("imageId"), "imageId parameter not present")), Integer
100                        .parseInt(checkNotNull(postParams.get("flavorId"), "flavorId parameter not present")));
101      if (metadata.size() > 0)
102         server.metadata = metadata;
103      if (files.size() > 0)
104         server.personality = files;
105      if (sharedIpGroupId != null)
106         server.sharedIpGroupId = this.sharedIpGroupId;
107      if (publicIp != null) {
108         server.addresses = new Addresses();
109         server.addresses.getPublicAddresses().add(publicIp);
110         server.addresses.setPrivateAddresses(null);
111      }
112      return bindToRequest(request, ImmutableMap.of("server", server));
113   }
114 
115   /**
116    * You may further customize a cloud server by injecting data into the file system of the cloud
117    * server itself. This is useful, for example, for inserting ssh keys, setting configuration
118    * files, or storing data that you want to retrieve from within the instance itself. It is
119    * intended to provide a minimal amount of launch-time personalization. If significant
120    * customization is required, a custom image should be created. The max size of the file path
121    * data is 255 bytes while the max size of the file contents is 10KB. Note that the file contents
122    * should be encoded as a Base64 string and the 10KB limit refers to the number of bytes in the
123    * decoded data not the number of characters in the encoded data. The maximum number of file
124    * path/content pairs that can be supplied is 5. Any existing files that match the specified file
125    * will be renamed to include the extension bak followed by a time stamp. For example, the file
126    * /etc/passwd will be backed up as /etc/passwd.bak.1246036261.5785. All files will have root and
127    * the root group as owner and group owner, respectively and will allow user and group read
128    * access only (-r--r-----).
129    */
130   public CreateServerOptions withFile(String path, byte[] contents) {
131      checkState(files.size() < 5, "maximum number of files allowed is 5");
132      files.add(new File(path, contents));
133      return this;
134   }
135 
136   /**
137    * A shared IP group is a collection of servers that can share IPs with other members of the
138    * group. Any server in a group can share one or more public IPs with any other server in the
139    * group. With the exception of the first server in a shared IP group, servers must be launched
140    * into shared IP groups. A server may only be a member of one shared IP group.
141    * 
142    * <p/>
143    * Servers in the same shared IP group can share public IPs for various high availability and
144    * load balancing configurations. To launch an HA server, include the optional sharedIpGroupId
145    * element and the server will be launched into that shared IP group.
146    * <p />
147    * 
148    * Note: sharedIpGroupId is an optional parameter and for optimal performance, should ONLY be
149    * specified when intending to share IPs between servers.
150    * 
151    * @see #withSharedIp(String)
152    */
153   public CreateServerOptions withSharedIpGroup(int id) {
154      checkArgument(id > 0, "id must be positive or zero.  was: " + id);
155      this.sharedIpGroupId = id;
156      return this;
157   }
158 
159   /**
160    * Custom cloud server metadata can also be supplied at launch time. This metadata is stored in
161    * the API system where it is retrievable by querying the API for server status. The maximum size
162    * of the metadata key and value is each 255 bytes and the maximum number of key-value pairs that
163    * can be supplied per server is 5.
164    */
165   public CreateServerOptions withMetadata(Map<String, String> metadata) {
166      checkNotNull(metadata, "metadata");
167      checkArgument(metadata.size() <= 5, "you cannot have more then 5 metadata values.  You specified: "
168               + metadata.size());
169      for (Entry<String, String> entry : metadata.entrySet()) {
170         checkArgument(entry.getKey().getBytes().length < 255, String.format(
171                  "maximum length of metadata key is 255 bytes.  Key specified %s is %d bytes", entry.getKey(), entry
172                           .getKey().getBytes().length));
173         checkArgument(entry.getKey().getBytes().length < 255, String.format(
174                  "maximum length of metadata value is 255 bytes.  Value specified for %s (%s) is %d bytes", entry
175                           .getKey(), entry.getValue(), entry.getValue().getBytes().length));
176      }
177      this.metadata = metadata;
178      return this;
179   }
180 
181   /**
182    * Public IP addresses can be shared across multiple servers for use in various high availability
183    * scenarios. When an IP address is shared to another server, the cloud network restrictions are
184    * modified to allow each server to listen to and respond on that IP address (you may optionally
185    * specify that the target server network configuration be modified). Shared IP addresses can be
186    * used with many standard heartbeat facilities (e.g. keepalived) that monitor for failure and
187    * manage IP failover.
188    * 
189    * <p/>
190    * If you intend to use a shared IP on the server being created and have no need for a separate
191    * public IP address, you may launch the server into a shared IP group and specify an IP address
192    * from that shared IP group to be used as its public IP. You can accomplish this by specifying
193    * the public shared IP address in your request. This is optional and is only valid if
194    * sharedIpGroupId is also supplied.
195    */
196   public CreateServerOptions withSharedIp(String publicIp) {
197      checkState(sharedIpGroupId != null, "sharedIp is invalid unless a shared ip group is specified.");
198      this.publicIp = checkNotNull(publicIp, "ip");
199      return this;
200   }
201 
202   public static class Builder {
203 
204      /**
205       * @see CreateServerOptions#withFile(String,byte [])
206       */
207      public static CreateServerOptions withFile(String path, byte[] contents) {
208         CreateServerOptions options = new CreateServerOptions();
209         return options.withFile(path, contents);
210      }
211 
212      /**
213       * @see CreateServerOptions#withSharedIpGroup(int)
214       */
215      public static CreateServerOptions withSharedIpGroup(int id) {
216         CreateServerOptions options = new CreateServerOptions();
217         return options.withSharedIpGroup(id);
218      }
219 
220      /**
221       * @see CreateServerOptions#withMetadata(Map<String, String>)
222       */
223      public static CreateServerOptions withMetadata(Map<String, String> metadata) {
224         CreateServerOptions options = new CreateServerOptions();
225         return options.withMetadata(metadata);
226      }
227 
228      /**
229       * @see CreateServerOptions#withSharedIp(String)
230       */
231      public static CreateServerOptions withSharedIp(String publicIp) {
232         CreateServerOptions options = new CreateServerOptions();
233         return options.withSharedIp(publicIp);
234      }
235 
236   }
237 
238   @Override
239   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
240      return jsonBinder.bindToRequest(request, input);
241   }
242}

[all classes][org.jclouds.cloudservers.options]
EMMA 2.0.5312 (C) Vladimir Roubtsov