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

COVERAGE SUMMARY FOR SOURCE FILE [CreateServerOptions.java]

nameclass, %method, %block, %line, %
CreateServerOptions.java80%  (4/5)77%  (10/13)95%  (275/290)94%  (36.6/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateServerOptions$Builder100% (1/1)67%  (2/3)85%  (17/20)80%  (4/5)
CreateServerOptions$Builder (): void 0%   (0/1)0%   (0/3)0%   (0/1)
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)
     
class CreateServerOptions100% (1/1)100% (5/5)98%  (180/184)99%  (20.9/21)
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% (47/47)100% (6/6)
bindToRequest (HttpRequest, Object): HttpRequest 100% (1/1)100% (6/6)100% (1/1)
     
class CreateServerOptions$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
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 CreateServerOptions$ServerRequest100% (1/1)100% (2/2)100% (22/22)100% (6/6)
CreateServerOptions$ServerRequest (CreateServerOptions, String, String, Strin... 100% (1/1)100% (15/15)100% (5/5)
CreateServerOptions$ServerRequest (CreateServerOptions, String, String, Strin... 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.openstack.nova.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.encryption.internal.Base64;
32import org.jclouds.http.HttpRequest;
33import org.jclouds.rest.MapBinder;
34import org.jclouds.rest.binders.BindToJsonPayload;
35 
36import com.google.common.collect.ImmutableMap;
37import com.google.common.collect.Lists;
38import com.google.common.collect.Maps;
39 
40/**
41 * 
42 * @author Adrian Cole
43 * 
44 */
45public class CreateServerOptions implements MapBinder {
46   @Inject
47   private BindToJsonPayload jsonBinder;
48 
49   static class File {
50      private final String path;
51      private final String contents;
52 
53      public File(String path, byte[] contents) {
54         this.path = checkNotNull(path, "path");
55         this.contents = Base64.encodeBytes(checkNotNull(contents, "contents"));
56         checkArgument(path.getBytes().length < 255, String.format(
57                  "maximum length of path is 255 bytes.  Path specified %s is %d bytes", path, path.getBytes().length));
58         checkArgument(contents.length < 10 * 1024, String.format(
59                  "maximum size of the file is 10KB.  Contents specified is %d bytes", contents.length));
60      }
61 
62      public String getContents() {
63         return contents;
64      }
65 
66      public String getPath() {
67         return path;
68      }
69 
70   }
71 
72   @SuppressWarnings("unused")
73   private class ServerRequest {
74      final String name;
75      final String imageRef;
76      final String flavorRef;
77      Map<String, String> metadata;
78      List<File> personality;
79 
80      private ServerRequest(String name, String imageRef, String flavorRef) {
81         this.name = name;
82         this.imageRef = imageRef;
83         this.flavorRef = flavorRef;
84      }
85 
86   }
87 
88   private Map<String, String> metadata = Maps.newHashMap();
89   private List<File> files = Lists.newArrayList();
90 
91   @Override
92   public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
93      ServerRequest server = new ServerRequest(checkNotNull(postParams.get("name"), "name parameter not present"),
94               checkNotNull(postParams.get("imageRef"), "imageRef parameter not present"), checkNotNull(postParams
95                        .get("flavorRef"), "flavorRef parameter not present"));
96      if (metadata.size() > 0)
97         server.metadata = metadata;
98      if (files.size() > 0)
99         server.personality = files;
100 
101      return bindToRequest(request, ImmutableMap.of("server", server));
102   }
103 
104   /**
105    * You may further customize a cloud server by injecting data into the file system of the cloud
106    * server itself. This is useful, for example, for inserting ssh keys, setting configuration
107    * files, or storing data that you want to retrieve from within the instance itself. It is
108    * intended to provide a minimal amount of launch-time personalization. If significant
109    * customization is required, a custom image should be created. The max size of the file path
110    * data is 255 bytes while the max size of the file contents is 10KB. Note that the file contents
111    * should be encoded as a Base64 string and the 10KB limit refers to the number of bytes in the
112    * decoded data not the number of characters in the encoded data. The maximum number of file
113    * path/content pairs that can be supplied is 5. Any existing files that match the specified file
114    * will be renamed to include the extension bak followed by a time stamp. For example, the file
115    * /etc/passwd will be backed up as /etc/passwd.bak.1246036261.5785. All files will have root and
116    * the root group as owner and group owner, respectively and will allow user and group read
117    * access only (-r--r-----).
118    */
119   public CreateServerOptions withFile(String path, byte[] contents) {
120      checkState(files.size() < 5, "maximum number of files allowed is 5");
121      files.add(new File(path, contents));
122      return this;
123   }
124 
125   /**
126    * Custom cloud server metadata can also be supplied at launch time. This metadata is stored in
127    * the API system where it is retrievable by querying the API for server status. The maximum size
128    * of the metadata key and value is each 255 bytes and the maximum number of key-value pairs that
129    * can be supplied per server is 5.
130    */
131   public CreateServerOptions withMetadata(Map<String, String> metadata) {
132      checkNotNull(metadata, "metadata");
133      checkArgument(metadata.size() <= 5, "you cannot have more then 5 metadata values.  You specified: "
134               + metadata.size());
135      for (Entry<String, String> entry : metadata.entrySet()) {
136         checkArgument(entry.getKey().getBytes().length < 255, String.format(
137                  "maximum length of metadata key is 255 bytes.  Key specified %s is %d bytes", entry.getKey(), entry
138                           .getKey().getBytes().length));
139         checkArgument(entry.getKey().getBytes().length < 255, String.format(
140                  "maximum length of metadata value is 255 bytes.  Value specified for %s (%s) is %d bytes", entry
141                           .getKey(), entry.getValue(), entry.getValue().getBytes().length));
142      }
143      this.metadata = metadata;
144      return this;
145   }
146 
147   public static class Builder {
148 
149      /**
150       * @see CreateServerOptions#withFile(String,byte [])
151       */
152      public static CreateServerOptions withFile(String path, byte[] contents) {
153         CreateServerOptions options = new CreateServerOptions();
154         return options.withFile(path, contents);
155      }
156 
157      /**
158       * @see CreateServerOptions#withMetadata(Map<String, String>)
159       */
160      public static CreateServerOptions withMetadata(Map<String, String> metadata) {
161         CreateServerOptions options = new CreateServerOptions();
162         return options.withMetadata(metadata);
163      }
164   }
165 
166   @Override
167   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
168      return jsonBinder.bindToRequest(request, input);
169   }
170}

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