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