View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.aws.ec2.model;
17  
18  import static com.google.common.base.Optional.absent;
19  import static com.google.common.collect.Lists.newArrayList;
20  
21  import java.util.List;
22  
23  import org.kuali.common.aws.ec2.model.security.KualiSecurityGroup;
24  import org.kuali.common.core.build.ValidatingBuilder;
25  import org.kuali.common.core.ssh.PublicKey;
26  import org.kuali.common.util.FormatUtils;
27  
28  import com.amazonaws.services.ec2.model.BlockDeviceMapping;
29  import com.amazonaws.services.ec2.model.InstanceType;
30  import com.amazonaws.services.ec2.model.Tag;
31  import com.google.common.base.Optional;
32  import com.google.common.collect.ImmutableList;
33  
34  public final class LaunchInstanceContext {
35  
36  	private final String ami;
37  
38  	// To launch an instance the only part of the KeyPair object that is *required* is the key name
39  	// If you supply the name only (ie no public key), you must have already registered a key pair with Amazon under that name
40  	// If you supply both the name and the public key, the launch routine automatically registers a key pair with Amazon
41  	private final PublicKey publicKey;
42  	private final InstanceType type;
43  	private final ImmutableList<KualiSecurityGroup> securityGroups;
44  
45  	// If true, permissions on any existing security groups are overridden by the permissions from the security groups provided here.
46  	// This happens prior to the instance being launched
47  	private final boolean overrideExistingSecurityGroupPermissions;
48  	private final ImmutableList<Tag> tags;
49  	private final Optional<String> availabilityZone;
50  	private final int timeoutMillis;
51  	private final boolean preventTermination;
52  	private final boolean ebsOptimized;
53  	private final boolean enableMonitoring;
54  
55  	// Default root volume size is provided by the AMI
56  	private final Optional<RootVolume> rootVolume;
57  	private final ImmutableList<BlockDeviceMapping> additionalMappings;
58  
59  	private LaunchInstanceContext(Builder builder) {
60  		this.ami = builder.ami;
61  		this.publicKey = builder.publicKey;
62  		this.type = builder.type;
63  		this.securityGroups = ImmutableList.copyOf(builder.securityGroups);
64  		this.overrideExistingSecurityGroupPermissions = builder.overrideExistingSecurityGroupPermissions;
65  		this.tags = ImmutableTag.copyOf(builder.tags);
66  		this.availabilityZone = builder.availabilityZone;
67  		this.timeoutMillis = builder.timeoutMillis;
68  		this.preventTermination = builder.preventTermination;
69  		this.ebsOptimized = builder.ebsOptimized;
70  		this.enableMonitoring = builder.enableMonitoring;
71  		this.rootVolume = builder.rootVolume;
72  		this.additionalMappings = ImmutableBlockDeviceMapping.copyOf(builder.additionalMappings);
73  	}
74  
75  	public static Builder builder(String ami, PublicKey publicKey) {
76  		return new Builder(ami, publicKey);
77  	}
78  
79  	public static class Builder extends ValidatingBuilder<LaunchInstanceContext> {
80  
81  		// Required
82  		private final String ami;
83  		private final PublicKey publicKey;
84  
85  		// Optional
86  		private InstanceType type = InstanceType.M3Medium;
87  		private List<KualiSecurityGroup> securityGroups = newArrayList();
88  		private boolean overrideExistingSecurityGroupPermissions;
89  		private List<Tag> tags = newArrayList();
90  		private Optional<String> availabilityZone = absent();
91  		private int timeoutMillis = FormatUtils.getMillisAsInt("15m");
92  		private boolean preventTermination = false;
93  		private boolean ebsOptimized = false;
94  		private boolean enableMonitoring = false;
95  		private Optional<RootVolume> rootVolume = absent();
96  		private List<BlockDeviceMapping> additionalMappings = newArrayList();
97  
98  		public Builder(String ami, PublicKey publicKey) {
99  			this.ami = ami;
100 			this.publicKey = publicKey;
101 		}
102 
103 		public Builder withType(InstanceType type) {
104 			this.type = type;
105 			return this;
106 		}
107 
108 		public Builder withAdditionalMappings(List<BlockDeviceMapping> additionalMappings) {
109 			this.additionalMappings = additionalMappings;
110 			return this;
111 		}
112 
113 		public Builder withSecurityGroups(List<KualiSecurityGroup> securityGroups) {
114 			this.securityGroups = securityGroups;
115 			return this;
116 		}
117 
118 		public Builder withSecurityGroup(KualiSecurityGroup securityGroup) {
119 			return withSecurityGroups(ImmutableList.of(securityGroup));
120 		}
121 
122 		public Builder withOverrideExistingSecurityGroupPermissions(boolean overrideExistingSecurityGroupPermissions) {
123 			this.overrideExistingSecurityGroupPermissions = overrideExistingSecurityGroupPermissions;
124 			return this;
125 		}
126 
127 		public Builder withTags(List<Tag> tags) {
128 			this.tags = tags;
129 			return this;
130 		}
131 
132 		public Builder withAvailabilityZone(Optional<String> availabilityZone) {
133 			this.availabilityZone = availabilityZone;
134 			return this;
135 		}
136 
137 		public Builder withTimeoutMillis(int timeoutMillis) {
138 			this.timeoutMillis = timeoutMillis;
139 			return this;
140 		}
141 
142 		public Builder withPreventTermination(boolean preventTermination) {
143 			this.preventTermination = preventTermination;
144 			return this;
145 		}
146 
147 		public Builder withEbsOptimized(boolean ebsOptimized) {
148 			this.ebsOptimized = ebsOptimized;
149 			return this;
150 		}
151 
152 		public Builder withEnableMonitoring(boolean enableMonitoring) {
153 			this.enableMonitoring = enableMonitoring;
154 			return this;
155 		}
156 
157 		public Builder withRootVolume(Optional<RootVolume> rootVolume) {
158 			this.rootVolume = rootVolume;
159 			return this;
160 		}
161 
162 		public Builder withRootVolume(RootVolume rootVolume) {
163 			return withRootVolume(Optional.of(rootVolume));
164 		}
165 
166 		@Override
167 		public LaunchInstanceContext build() {
168 			return validate(new LaunchInstanceContext(this));
169 		}
170 
171 		public InstanceType getType() {
172 			return type;
173 		}
174 
175 		public void setType(InstanceType type) {
176 			this.type = type;
177 		}
178 
179 		public List<KualiSecurityGroup> getSecurityGroups() {
180 			return securityGroups;
181 		}
182 
183 		public void setSecurityGroups(List<KualiSecurityGroup> securityGroups) {
184 			this.securityGroups = securityGroups;
185 		}
186 
187 		public boolean isOverrideExistingSecurityGroupPermissions() {
188 			return overrideExistingSecurityGroupPermissions;
189 		}
190 
191 		public void setOverrideExistingSecurityGroupPermissions(boolean overrideExistingSecurityGroupPermissions) {
192 			this.overrideExistingSecurityGroupPermissions = overrideExistingSecurityGroupPermissions;
193 		}
194 
195 		public List<Tag> getTags() {
196 			return tags;
197 		}
198 
199 		public void setTags(List<Tag> tags) {
200 			this.tags = tags;
201 		}
202 
203 		public Optional<String> getAvailabilityZone() {
204 			return availabilityZone;
205 		}
206 
207 		public void setAvailabilityZone(Optional<String> availabilityZone) {
208 			this.availabilityZone = availabilityZone;
209 		}
210 
211 		public int getTimeoutMillis() {
212 			return timeoutMillis;
213 		}
214 
215 		public void setTimeoutMillis(int timeoutMillis) {
216 			this.timeoutMillis = timeoutMillis;
217 		}
218 
219 		public boolean isPreventTermination() {
220 			return preventTermination;
221 		}
222 
223 		public void setPreventTermination(boolean preventTermination) {
224 			this.preventTermination = preventTermination;
225 		}
226 
227 		public boolean isEbsOptimized() {
228 			return ebsOptimized;
229 		}
230 
231 		public void setEbsOptimized(boolean ebsOptimized) {
232 			this.ebsOptimized = ebsOptimized;
233 		}
234 
235 		public boolean isEnableMonitoring() {
236 			return enableMonitoring;
237 		}
238 
239 		public void setEnableMonitoring(boolean enableMonitoring) {
240 			this.enableMonitoring = enableMonitoring;
241 		}
242 
243 		public Optional<RootVolume> getRootVolume() {
244 			return rootVolume;
245 		}
246 
247 		public void setRootVolume(Optional<RootVolume> rootVolume) {
248 			this.rootVolume = rootVolume;
249 		}
250 
251 		public String getAmi() {
252 			return ami;
253 		}
254 
255 		public PublicKey getPublicKey() {
256 			return publicKey;
257 		}
258 	}
259 
260 	public String getAmi() {
261 		return ami;
262 	}
263 
264 	public PublicKey getPublicKey() {
265 		return publicKey;
266 	}
267 
268 	public InstanceType getType() {
269 		return type;
270 	}
271 
272 	public List<KualiSecurityGroup> getSecurityGroups() {
273 		return securityGroups;
274 	}
275 
276 	public boolean isOverrideExistingSecurityGroupPermissions() {
277 		return overrideExistingSecurityGroupPermissions;
278 	}
279 
280 	public List<Tag> getTags() {
281 		return tags;
282 	}
283 
284 	public Optional<String> getAvailabilityZone() {
285 		return availabilityZone;
286 	}
287 
288 	public int getTimeoutMillis() {
289 		return timeoutMillis;
290 	}
291 
292 	public boolean isPreventTermination() {
293 		return preventTermination;
294 	}
295 
296 	public boolean isEbsOptimized() {
297 		return ebsOptimized;
298 	}
299 
300 	public boolean isEnableMonitoring() {
301 		return enableMonitoring;
302 	}
303 
304 	public Optional<RootVolume> getRootVolume() {
305 		return rootVolume;
306 	}
307 
308 	public List<BlockDeviceMapping> getAdditionalMappings() {
309 		return additionalMappings;
310 	}
311 
312 }