| 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.openstack.nova.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static com.google.common.base.Preconditions.checkState; |
| 24 | |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import java.util.Map.Entry; |
| 28 | |
| 29 | import javax.inject.Inject; |
| 30 | |
| 31 | import org.jclouds.encryption.internal.Base64; |
| 32 | import org.jclouds.http.HttpRequest; |
| 33 | import org.jclouds.rest.MapBinder; |
| 34 | import org.jclouds.rest.binders.BindToJsonPayload; |
| 35 | |
| 36 | import com.google.common.collect.ImmutableMap; |
| 37 | import com.google.common.collect.Lists; |
| 38 | import com.google.common.collect.Maps; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | * |
| 44 | */ |
| 45 | public 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 | } |