| 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.softlayer.compute.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.jclouds.compute.ComputeService; |
| 27 | import org.jclouds.compute.options.TemplateOptions; |
| 28 | import org.jclouds.io.Payload; |
| 29 | import org.jclouds.softlayer.features.VirtualGuestClient; |
| 30 | |
| 31 | import com.google.common.net.InternetDomainName; |
| 32 | |
| 33 | /** |
| 34 | * Contains options supported by the |
| 35 | * {@link ComputeService#createNodesInGroup(String, int, TemplateOptions)} and |
| 36 | * {@link ComputeService#runNodesWithTag(String, int, TemplateOptions)} |
| 37 | * operations on the <em>gogrid</em> provider. |
| 38 | * |
| 39 | * <h2>Usage</h2> The recommended way to instantiate a |
| 40 | * {@link SoftLayerTemplateOptions} object is to statically import |
| 41 | * {@code SoftLayerTemplateOptions.*} and invoke a static creation method |
| 42 | * followed by an instance mutator (if needed): |
| 43 | * <p> |
| 44 | * |
| 45 | * <pre> |
| 46 | * import static org.jclouds.compute.options.SoftLayerTemplateOptions.Builder.*; |
| 47 | * ComputeService client = // get connection |
| 48 | * templateBuilder.options(inboundPorts(22, 80, 8080, 443)); |
| 49 | * Set<? extends NodeMetadata> set = client.runNodesWithTag(tag, 2, templateBuilder.build()); |
| 50 | * </pre> |
| 51 | * |
| 52 | * @author Adrian Cole |
| 53 | */ |
| 54 | public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneable { |
| 55 | |
| 56 | protected String domainName = "jclouds.org"; |
| 57 | |
| 58 | @Override |
| 59 | public SoftLayerTemplateOptions clone() { |
| 60 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 61 | copyTo(options); |
| 62 | return options; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void copyTo(TemplateOptions to) { |
| 67 | super.copyTo(to); |
| 68 | if (to instanceof SoftLayerTemplateOptions) { |
| 69 | SoftLayerTemplateOptions eTo = SoftLayerTemplateOptions.class.cast(to); |
| 70 | eTo.domainName(domainName); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * will replace the default domain used when ordering virtual guests. Note |
| 76 | * this needs to contain a public suffix! |
| 77 | * |
| 78 | * @see VirtualGuestClient#orderVirtualGuest |
| 79 | * @see InternetDomainName#hasPublicSuffix |
| 80 | */ |
| 81 | public TemplateOptions domainName(String domainName) { |
| 82 | checkNotNull(domainName, "domainName was null"); |
| 83 | checkArgument(InternetDomainName.from(domainName).hasPublicSuffix(), "domainName %s has no public suffix", |
| 84 | domainName); |
| 85 | this.domainName = domainName; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | public String getDomainName() { |
| 90 | return domainName; |
| 91 | } |
| 92 | |
| 93 | public static final SoftLayerTemplateOptions NONE = new SoftLayerTemplateOptions(); |
| 94 | |
| 95 | public static class Builder { |
| 96 | |
| 97 | /** |
| 98 | * @see #domainName |
| 99 | */ |
| 100 | public static SoftLayerTemplateOptions domainName(String domainName) { |
| 101 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 102 | return SoftLayerTemplateOptions.class.cast(options.domainName(domainName)); |
| 103 | } |
| 104 | |
| 105 | // methods that only facilitate returning the correct object type |
| 106 | |
| 107 | /** |
| 108 | * @see TemplateOptions#inboundPorts(int...) |
| 109 | */ |
| 110 | public static SoftLayerTemplateOptions inboundPorts(int... ports) { |
| 111 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 112 | return SoftLayerTemplateOptions.class.cast(options.inboundPorts(ports)); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @see TemplateOptions#blockOnPort(int, int) |
| 117 | */ |
| 118 | public static SoftLayerTemplateOptions blockOnPort(int port, int seconds) { |
| 119 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 120 | return SoftLayerTemplateOptions.class.cast(options.blockOnPort(port, seconds)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @see TemplateOptions#runScript(Payload) |
| 125 | */ |
| 126 | public static SoftLayerTemplateOptions runScript(Payload script) { |
| 127 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 128 | return SoftLayerTemplateOptions.class.cast(options.runScript(script)); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @see TemplateOptions#installPrivateKey(Payload) |
| 133 | */ |
| 134 | @Deprecated |
| 135 | public static SoftLayerTemplateOptions installPrivateKey(Payload rsaKey) { |
| 136 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 137 | return SoftLayerTemplateOptions.class.cast(options.installPrivateKey(rsaKey)); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @see TemplateOptions#authorizePublicKey(Payload) |
| 142 | */ |
| 143 | @Deprecated |
| 144 | public static SoftLayerTemplateOptions authorizePublicKey(Payload rsaKey) { |
| 145 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 146 | return SoftLayerTemplateOptions.class.cast(options.authorizePublicKey(rsaKey)); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @see TemplateOptions#userMetadata(Map) |
| 151 | */ |
| 152 | public static SoftLayerTemplateOptions userMetadata(Map<String, String> userMetadata) { |
| 153 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 154 | return SoftLayerTemplateOptions.class.cast(options.userMetadata(userMetadata)); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @see TemplateOptions#userMetadata(String, String) |
| 159 | */ |
| 160 | public static SoftLayerTemplateOptions userMetadata(String key, String value) { |
| 161 | SoftLayerTemplateOptions options = new SoftLayerTemplateOptions(); |
| 162 | return SoftLayerTemplateOptions.class.cast(options.userMetadata(key, value)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // methods that only facilitate returning the correct object type |
| 167 | |
| 168 | /** |
| 169 | * @see TemplateOptions#blockOnPort(int, int) |
| 170 | */ |
| 171 | @Override |
| 172 | public SoftLayerTemplateOptions blockOnPort(int port, int seconds) { |
| 173 | return SoftLayerTemplateOptions.class.cast(super.blockOnPort(port, seconds)); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @see TemplateOptions#inboundPorts(int...) |
| 178 | */ |
| 179 | @Override |
| 180 | public SoftLayerTemplateOptions inboundPorts(int... ports) { |
| 181 | return SoftLayerTemplateOptions.class.cast(super.inboundPorts(ports)); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @see TemplateOptions#authorizePublicKey(String) |
| 186 | */ |
| 187 | @Override |
| 188 | public SoftLayerTemplateOptions authorizePublicKey(String publicKey) { |
| 189 | return SoftLayerTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @see TemplateOptions#authorizePublicKey(Payload) |
| 194 | */ |
| 195 | @Override |
| 196 | @Deprecated |
| 197 | public SoftLayerTemplateOptions authorizePublicKey(Payload publicKey) { |
| 198 | return SoftLayerTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @see TemplateOptions#installPrivateKey(String) |
| 203 | */ |
| 204 | @Override |
| 205 | public SoftLayerTemplateOptions installPrivateKey(String privateKey) { |
| 206 | return SoftLayerTemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @see TemplateOptions#installPrivateKey(Payload) |
| 211 | */ |
| 212 | @Override |
| 213 | @Deprecated |
| 214 | public SoftLayerTemplateOptions installPrivateKey(Payload privateKey) { |
| 215 | return SoftLayerTemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @see TemplateOptions#runScript(Payload) |
| 220 | */ |
| 221 | @Override |
| 222 | public SoftLayerTemplateOptions runScript(Payload script) { |
| 223 | return SoftLayerTemplateOptions.class.cast(super.runScript(script)); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @see TemplateOptions#runScript(byte[]) |
| 228 | */ |
| 229 | @Override |
| 230 | @Deprecated |
| 231 | public SoftLayerTemplateOptions runScript(byte[] script) { |
| 232 | return SoftLayerTemplateOptions.class.cast(super.runScript(script)); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * {@inheritDoc} |
| 237 | */ |
| 238 | @Override |
| 239 | public SoftLayerTemplateOptions userMetadata(Map<String, String> userMetadata) { |
| 240 | return SoftLayerTemplateOptions.class.cast(super.userMetadata(userMetadata)); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * {@inheritDoc} |
| 245 | */ |
| 246 | @Override |
| 247 | public SoftLayerTemplateOptions userMetadata(String key, String value) { |
| 248 | return SoftLayerTemplateOptions.class.cast(super.userMetadata(key, value)); |
| 249 | } |
| 250 | } |