PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/providers/gogrid/src/main/java/org/jclouds/gogrid/compute/suppliers/GoGridImageSupplier.java

https://github.com/arohner/jclouds
Java | 116 lines | 81 code | 12 blank | 23 comment | 8 complexity | 222a205d0f339fe09f915a5d01e3e40d MD5 | raw file
  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.gogrid.compute.suppliers;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. import javax.annotation.Resource;
  25. import javax.inject.Inject;
  26. import javax.inject.Named;
  27. import javax.inject.Singleton;
  28. import org.jclouds.compute.domain.Image;
  29. import org.jclouds.compute.domain.ImageBuilder;
  30. import org.jclouds.compute.domain.OperatingSystem;
  31. import org.jclouds.compute.domain.OsFamily;
  32. import org.jclouds.compute.reference.ComputeServiceConstants;
  33. import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
  34. import org.jclouds.compute.util.ComputeServiceUtils;
  35. import org.jclouds.gogrid.GoGridClient;
  36. import org.jclouds.gogrid.domain.ServerImage;
  37. import org.jclouds.logging.Logger;
  38. import com.google.common.base.Supplier;
  39. import com.google.common.collect.Sets;
  40. /**
  41. *
  42. * @author Adrian Cole
  43. */
  44. @Singleton
  45. public class GoGridImageSupplier implements Supplier<Set<? extends Image>> {
  46. public static final Pattern GOGRID_OS_PATTERN = Pattern.compile("([a-zA-Z]*).*");
  47. public static final Pattern GOGRID_VERSION_PATTERN = Pattern.compile(".* ([0-9.]+) .*");
  48. @Resource
  49. @Named(ComputeServiceConstants.COMPUTE_LOGGER)
  50. protected Logger logger = Logger.NULL;
  51. private final GoGridClient sync;
  52. private final PopulateDefaultLoginCredentialsForImageStrategy authenticator;
  53. private final Map<OsFamily, Map<String, String>> osVersionMap;
  54. @Inject
  55. GoGridImageSupplier(GoGridClient sync, PopulateDefaultLoginCredentialsForImageStrategy authenticator,
  56. Map<OsFamily, Map<String, String>> osVersionMap) {
  57. this.osVersionMap = osVersionMap;
  58. this.sync = sync;
  59. this.authenticator = authenticator;
  60. }
  61. @Override
  62. public Set<? extends Image> get() {
  63. final Set<Image> images = Sets.newHashSet();
  64. logger.debug(">> providing images");
  65. Set<ServerImage> allImages = sync.getImageServices().getImageList();
  66. for (ServerImage from : allImages) {
  67. ImageBuilder builder = new ImageBuilder();
  68. builder.ids(from.getId() + "");
  69. builder.name(from.getFriendlyName());
  70. builder.description(from.getDescription());
  71. builder.defaultCredentials(authenticator.execute(from));
  72. builder.operatingSystem(parseOs(from));
  73. images.add(builder.build());
  74. }
  75. logger.debug("<< images(%d)", images.size());
  76. return images;
  77. }
  78. protected OperatingSystem parseOs(ServerImage from) {
  79. OsFamily osFamily = null;
  80. String osName = from.getOs().getName();
  81. String osArch = from.getArchitecture().getDescription();
  82. String osVersion = null;
  83. String osDescription = from.getOs().getDescription();
  84. boolean is64Bit = (from.getOs().getName().indexOf("64") != -1 || from.getDescription().indexOf("64") != -1);
  85. if (osName.startsWith("Windows")) {
  86. osFamily = OsFamily.WINDOWS;
  87. } else {
  88. Matcher matcher = GOGRID_OS_PATTERN.matcher(from.getName());
  89. if (matcher.find()) {
  90. try {
  91. osFamily = OsFamily.fromValue(matcher.group(1).toLowerCase());
  92. } catch (IllegalArgumentException e) {
  93. logger.debug("<< didn't match os(%s)", from.getName());
  94. }
  95. }
  96. }
  97. Matcher matcher = GOGRID_VERSION_PATTERN.matcher(osName);
  98. if (matcher.find()) {
  99. osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap);
  100. }
  101. // TODO determine DC images are in
  102. return new OperatingSystem(osFamily, osName, osVersion, osArch, osDescription, is64Bit);
  103. }
  104. }