001/**
002 * Copyright 2004-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.aws.ec2.model;
017
018import static com.google.common.base.Optional.absent;
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import org.kuali.common.core.build.ValidatingBuilder;
022import org.kuali.common.util.FormatUtils;
023
024import com.amazonaws.ClientConfiguration;
025import com.amazonaws.auth.AWSCredentials;
026import com.amazonaws.regions.RegionUtils;
027import com.amazonaws.services.ec2.model.Region;
028import com.google.common.base.Optional;
029
030public final class EC2ServiceContext {
031
032        private final AWSCredentials credentials;
033        private final int sleepIntervalMillis;
034        private final int initialPauseMillis;
035        private final int terminationTimeoutMillis;
036        private final String region;
037        private final Optional<String> endpoint;
038        private final Optional<ClientConfiguration> configuration;
039        private final Optional<Integer> timeOffsetInSeconds;
040
041        public static EC2ServiceContext create(AWSCredentials credentials, Region region) {
042                return builder(credentials).withRegion(region.getRegionName()).build();
043        }
044
045        public static EC2ServiceContext create(AWSCredentials credentials) {
046                return builder(credentials).build();
047        }
048
049        public static Builder builder(AWSCredentials credentials) {
050                return new Builder(credentials);
051        }
052
053        private EC2ServiceContext(Builder builder) {
054                this.credentials = builder.credentials;
055                this.sleepIntervalMillis = builder.sleepIntervalMillis;
056                this.initialPauseMillis = builder.initialPauseMillis;
057                this.terminationTimeoutMillis = builder.terminationTimeoutMillis;
058                this.region = builder.region;
059                this.endpoint = builder.endpoint;
060                this.configuration = builder.configuration;
061                this.timeOffsetInSeconds = builder.timeOffsetInSeconds;
062        }
063
064        public static class Builder extends ValidatingBuilder<EC2ServiceContext> {
065
066                private final AWSCredentials credentials;
067                private int sleepIntervalMillis = FormatUtils.getMillisAsInt("15s");
068                private int initialPauseMillis = FormatUtils.getMillisAsInt("1s");
069                private int terminationTimeoutMillis = FormatUtils.getMillisAsInt("15m");
070                private String region = Regions.DEFAULT_REGION.getName();
071                private Optional<String> endpoint = absent();
072                private Optional<ClientConfiguration> configuration = absent();
073                private Optional<Integer> timeOffsetInSeconds = absent();
074
075                public Builder(AWSCredentials credentials) {
076                        this.credentials = credentials;
077                }
078
079                public Builder withSleepIntervalMillis(int sleepIntervalMillis) {
080                        this.sleepIntervalMillis = sleepIntervalMillis;
081                        return this;
082                }
083
084                public Builder withInitialPauseMillis(int initialPauseMillis) {
085                        this.initialPauseMillis = initialPauseMillis;
086                        return this;
087                }
088
089                public Builder withTerminationTimeoutMillis(int terminationTimeoutMillis) {
090                        this.terminationTimeoutMillis = terminationTimeoutMillis;
091                        return this;
092                }
093
094                public Builder withRegion(String region) {
095                        this.region = region;
096                        return this;
097                }
098
099                public Builder withEndpoint(Optional<String> endpoint) {
100                        this.endpoint = endpoint;
101                        return this;
102                }
103
104                public Builder withConfiguration(Optional<ClientConfiguration> configuration) {
105                        this.configuration = configuration;
106                        return this;
107                }
108
109                public Builder withTimeOffsetInSeconds(Optional<Integer> timeOffsetInSeconds) {
110                        this.timeOffsetInSeconds = timeOffsetInSeconds;
111                        return this;
112                }
113
114                @Override
115                public EC2ServiceContext build() {
116                        EC2ServiceContext context = validate(new EC2ServiceContext(this));
117                        checkNotNull(RegionUtils.getRegion(context.getRegion()), "region %s is unknown", context.getRegion());
118                        return context;
119                }
120
121                public int getSleepIntervalMillis() {
122                        return sleepIntervalMillis;
123                }
124
125                public void setSleepIntervalMillis(int sleepIntervalMillis) {
126                        this.sleepIntervalMillis = sleepIntervalMillis;
127                }
128
129                public int getInitialPauseMillis() {
130                        return initialPauseMillis;
131                }
132
133                public void setInitialPauseMillis(int initialPauseMillis) {
134                        this.initialPauseMillis = initialPauseMillis;
135                }
136
137                public int getTerminationTimeoutMillis() {
138                        return terminationTimeoutMillis;
139                }
140
141                public void setTerminationTimeoutMillis(int terminationTimeoutMillis) {
142                        this.terminationTimeoutMillis = terminationTimeoutMillis;
143                }
144
145                public String getRegion() {
146                        return region;
147                }
148
149                public void setRegion(String region) {
150                        this.region = region;
151                }
152
153                public Optional<String> getEndpoint() {
154                        return endpoint;
155                }
156
157                public void setEndpoint(Optional<String> endpoint) {
158                        this.endpoint = endpoint;
159                }
160
161                public Optional<ClientConfiguration> getConfiguration() {
162                        return configuration;
163                }
164
165                public void setConfiguration(Optional<ClientConfiguration> configuration) {
166                        this.configuration = configuration;
167                }
168
169                public Optional<Integer> getTimeOffsetInSeconds() {
170                        return timeOffsetInSeconds;
171                }
172
173                public void setTimeOffsetInSeconds(Optional<Integer> timeOffsetInSeconds) {
174                        this.timeOffsetInSeconds = timeOffsetInSeconds;
175                }
176
177                public AWSCredentials getCredentials() {
178                        return credentials;
179                }
180        }
181
182        public AWSCredentials getCredentials() {
183                return credentials;
184        }
185
186        public int getSleepIntervalMillis() {
187                return sleepIntervalMillis;
188        }
189
190        public int getInitialPauseMillis() {
191                return initialPauseMillis;
192        }
193
194        public int getTerminationTimeoutMillis() {
195                return terminationTimeoutMillis;
196        }
197
198        public String getRegion() {
199                return region;
200        }
201
202        public Optional<String> getEndpoint() {
203                return endpoint;
204        }
205
206        public Optional<ClientConfiguration> getConfiguration() {
207                return configuration;
208        }
209
210        public Optional<Integer> getTimeOffsetInSeconds() {
211                return timeOffsetInSeconds;
212        }
213
214}