AmazonEC2Cloud.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package hudson.plugins.ec2;
  2. import hudson.Extension;
  3. import hudson.util.FormValidation;
  4. import hudson.util.ListBoxModel;
  5. import java.io.IOException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.List;
  9. import java.util.Locale;
  10. import javax.servlet.ServletException;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.kohsuke.stapler.DataBoundConstructor;
  13. import org.kohsuke.stapler.QueryParameter;
  14. import org.kohsuke.stapler.StaplerResponse;
  15. import com.amazonaws.services.ec2.AmazonEC2;
  16. import com.amazonaws.services.ec2.model.DescribeRegionsResult;
  17. import com.amazonaws.services.ec2.model.Region;
  18. /**
  19. * The original implementation of {@link EC2Cloud}.
  20. *
  21. * @author Kohsuke Kawaguchi
  22. */
  23. public class AmazonEC2Cloud extends EC2Cloud {
  24. /**
  25. * Represents the region. Can be null for backward compatibility reasons.
  26. */
  27. private String region;
  28. // Used when running unit tests
  29. public static boolean testMode;
  30. @DataBoundConstructor
  31. public AmazonEC2Cloud(String accessId, String secretKey, String region, String privateKey, String instanceCapStr, List<SlaveTemplate> templates) {
  32. super("ec2-"+region, accessId, secretKey, privateKey, instanceCapStr, templates);
  33. this.region = region;
  34. }
  35. public String getRegion() {
  36. if (region == null)
  37. region = DEFAULT_EC2_HOST; // Backward compatibility
  38. // Handles pre 1.14 region names that used the old AwsRegion enum, note we don't change
  39. // the region here to keep the meta-data compatible in the case of a downgrade (is that right?)
  40. if (region.indexOf('_') > 0)
  41. return region.replace('_', '-').toLowerCase(Locale.ENGLISH);
  42. return region;
  43. }
  44. public static URL getEc2EndpointUrl(String region) {
  45. try {
  46. return new URL("https://" + region + "." + EC2_URL_HOST + "/");
  47. } catch (MalformedURLException e) {
  48. throw new Error(e); // Impossible
  49. }
  50. }
  51. @Override
  52. public URL getEc2EndpointUrl() {
  53. return getEc2EndpointUrl(getRegion());
  54. }
  55. @Override
  56. public URL getS3EndpointUrl() {
  57. try {
  58. return new URL("https://"+getRegion()+".s3.amazonaws.com/");
  59. } catch (MalformedURLException e) {
  60. throw new Error(e); // Impossible
  61. }
  62. }
  63. @Extension
  64. public static class DescriptorImpl extends EC2Cloud.DescriptorImpl {
  65. @Override
  66. public String getDisplayName() {
  67. return "Amazon EC2";
  68. }
  69. public ListBoxModel doFillRegionItems(@QueryParameter String accessId,
  70. @QueryParameter String secretKey) throws IOException,
  71. ServletException {
  72. ListBoxModel model = new ListBoxModel();
  73. if (testMode) {
  74. model.add(DEFAULT_EC2_HOST);
  75. return model;
  76. }
  77. if (!StringUtils.isEmpty(accessId) && !StringUtils.isEmpty(secretKey)) {
  78. AmazonEC2 client = connect(accessId, secretKey, new URL(
  79. "http://ec2.amazonaws.com"));
  80. DescribeRegionsResult regions = client.describeRegions();
  81. List<Region> regionList = regions.getRegions();
  82. for (Region r : regionList) {
  83. model.add(r.getRegionName(), r.getRegionName());
  84. }
  85. }
  86. return model;
  87. }
  88. public FormValidation doTestConnection(
  89. @QueryParameter String region,
  90. @QueryParameter String accessId,
  91. @QueryParameter String secretKey,
  92. @QueryParameter String privateKey) throws IOException, ServletException {
  93. return super.doTestConnection(getEc2EndpointUrl(region),accessId,secretKey,privateKey);
  94. }
  95. public FormValidation doGenerateKey(
  96. StaplerResponse rsp, @QueryParameter String region, @QueryParameter String accessId, @QueryParameter String secretKey) throws IOException, ServletException {
  97. return super.doGenerateKey(rsp,getEc2EndpointUrl(region),accessId,secretKey);
  98. }
  99. }
  100. }