1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.aws.ec2.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.common.aws.ec2.model.EC2ServiceContext;
22 import org.kuali.common.aws.ec2.model.LaunchInstanceContext;
23 import org.kuali.common.aws.ec2.model.RootVolume;
24 import org.kuali.common.util.Str;
25 import org.kuali.common.util.nullify.NullUtils;
26 import org.kuali.common.util.spring.SpringUtils;
27 import org.kuali.common.util.spring.env.EnvironmentService;
28 import org.springframework.util.Assert;
29
30 import com.amazonaws.regions.RegionUtils;
31 import com.amazonaws.services.ec2.AmazonEC2Client;
32 import com.amazonaws.services.ec2.model.InstanceType;
33 import com.amazonaws.services.ec2.model.Tag;
34 import com.google.common.base.Optional;
35 import com.google.common.collect.ImmutableList;
36
37 public class LaunchUtils {
38
39
40
41
42
43
44 private static final String TYPE_KEY = "ec2.type";
45
46
47 private static final String TAGS_KEY = "ec2.tags";
48
49
50
51
52
53 private static final String ROOT_VOLUME_SIZE_KEY = "ec2.rootVolume.sizeInGigabytes";
54 private static final String ROOT_VOLUME_DELETE_KEY = "ec2.rootVolume.deleteOnTermination";
55
56
57
58
59
60 protected static AmazonEC2Client newAmazonEC2Client(EC2ServiceContext context) {
61 if (context.getConfiguration().isPresent()) {
62 return new AmazonEC2Client(context.getCredentials(), context.getConfiguration().get());
63 } else {
64 return new AmazonEC2Client(context.getCredentials());
65 }
66 }
67
68 public static AmazonEC2Client getClient(EC2ServiceContext context) {
69 AmazonEC2Client client = newAmazonEC2Client(context);
70 if (context.getTimeOffsetInSeconds().isPresent()) {
71 client.setTimeOffset(context.getTimeOffsetInSeconds().get());
72 }
73 client.setRegion(RegionUtils.getRegion(context.getRegion()));
74 if (context.getEndpoint().isPresent()) {
75 client.setEndpoint(context.getEndpoint().get());
76 }
77 return client;
78 }
79
80
81
82
83 public static LaunchInstanceContext getContext(EnvironmentService env, LaunchInstanceContext provided) {
84 throw new UnsupportedOperationException("don't call this method");
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 }
103
104 protected static Optional<RootVolume> getRootVolume(EnvironmentService env, Optional<RootVolume> provided) {
105 throw new UnsupportedOperationException("don't call this method");
106
107
108
109
110
111 }
112
113 protected static Optional<Boolean> getDeleteOnTermination(EnvironmentService env, Optional<RootVolume> provided) {
114 if (env.containsProperty(ROOT_VOLUME_DELETE_KEY)) {
115 return SpringUtils.getOptionalBoolean(env, ROOT_VOLUME_SIZE_KEY);
116 } else {
117 return provided.isPresent() ? provided.get().getDeleteOnTermination() : Optional.<Boolean> absent();
118 }
119 }
120
121 protected static Optional<Integer> getSizeInGigaBytes(EnvironmentService env, Optional<RootVolume> provided) {
122 if (env.containsProperty(ROOT_VOLUME_SIZE_KEY)) {
123 return SpringUtils.getOptionalInteger(env, ROOT_VOLUME_SIZE_KEY);
124 } else {
125 return provided.isPresent() ? provided.get().getSizeInGigabytes() : Optional.<Integer> absent();
126 }
127 }
128
129 protected static List<Tag> getTags(EnvironmentService env, List<Tag> provided) {
130 if (env.containsProperty(TAGS_KEY)) {
131 return getTags(env);
132 } else {
133 return provided;
134 }
135 }
136
137 protected static List<Tag> getTags(EnvironmentService env) {
138 List<String> list = SpringUtils.getNoneSensitiveListFromCSV(env, TAGS_KEY, NullUtils.NONE);
139 List<Tag> tags = new ArrayList<Tag>();
140 for (String element : list) {
141 String[] tokens = Str.splitAndTrim(element, "=");
142 Assert.isTrue(tokens.length == 2, "Expected exactly 2 tokens");
143 String key = tokens[0];
144 String value = tokens[1];
145 Tag tag = new Tag(key, value);
146 tags.add(tag);
147 }
148 return ImmutableList.copyOf(tags);
149 }
150
151 protected static InstanceType getType(EnvironmentService env, InstanceType type) {
152 return InstanceType.fromValue(env.getString(TYPE_KEY, type.toString()));
153 }
154
155 }