PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/providers/gogrid/src/main/java/org/jclouds/gogrid/compute/functions/ServerImageToImage.java

http://github.com/jclouds/jclouds
Java | 99 lines | 70 code | 12 blank | 17 comment | 7 complexity | bf50ea1c85531eed7bc3d752875394ed MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.gogrid.compute.functions;
  18. import java.util.Map;
  19. import java.util.regex.Matcher;
  20. import java.util.regex.Pattern;
  21. import javax.annotation.Resource;
  22. import javax.inject.Inject;
  23. import javax.inject.Named;
  24. import javax.inject.Singleton;
  25. import org.jclouds.compute.domain.Image;
  26. import org.jclouds.compute.domain.ImageBuilder;
  27. import org.jclouds.compute.domain.OperatingSystem;
  28. import org.jclouds.compute.domain.OsFamily;
  29. import org.jclouds.compute.domain.Image.Status;
  30. import org.jclouds.compute.reference.ComputeServiceConstants;
  31. import org.jclouds.compute.util.ComputeServiceUtils;
  32. import org.jclouds.gogrid.domain.ServerImage;
  33. import org.jclouds.gogrid.domain.ServerImageState;
  34. import org.jclouds.logging.Logger;
  35. import com.google.common.base.Function;
  36. @Singleton
  37. public class ServerImageToImage implements Function<ServerImage, Image> {
  38. public static final Pattern GOGRID_OS_PATTERN = Pattern.compile("([a-zA-Z]*).*");
  39. public static final Pattern GOGRID_VERSION_PATTERN = Pattern.compile(".* ([0-9.]+) .*");
  40. @Resource
  41. @Named(ComputeServiceConstants.COMPUTE_LOGGER)
  42. protected Logger logger = Logger.NULL;
  43. private final Map<ServerImageState, Status> toPortableImageStatus;
  44. private final Map<OsFamily, Map<String, String>> osVersionMap;
  45. @Inject
  46. ServerImageToImage(Map<ServerImageState, Image.Status> toPortableImageStatus,
  47. Map<OsFamily, Map<String, String>> osVersionMap) {
  48. this.toPortableImageStatus = toPortableImageStatus;
  49. this.osVersionMap = osVersionMap;
  50. }
  51. protected OperatingSystem parseOs(ServerImage from) {
  52. OsFamily osFamily = null;
  53. String osName = from.getOs().getName();
  54. String osArch = from.getArchitecture().getDescription();
  55. String osVersion = null;
  56. String osDescription = from.getOs().getDescription();
  57. boolean is64Bit = from.getOs().getName().indexOf("64") != -1 || from.getDescription().indexOf("64") != -1;
  58. if (osName.startsWith("Windows")) {
  59. osFamily = OsFamily.WINDOWS;
  60. } else {
  61. Matcher matcher = GOGRID_OS_PATTERN.matcher(from.getName());
  62. if (matcher.find()) {
  63. try {
  64. osFamily = OsFamily.fromValue(matcher.group(1).toLowerCase());
  65. } catch (IllegalArgumentException e) {
  66. logger.debug("<< didn't match os(%s)", from.getName());
  67. }
  68. }
  69. }
  70. Matcher matcher = GOGRID_VERSION_PATTERN.matcher(osName);
  71. if (matcher.find()) {
  72. osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap);
  73. }
  74. // TODO determine DC images are in
  75. return new OperatingSystem(osFamily, osName, osVersion, osArch, osDescription, is64Bit);
  76. }
  77. @Override
  78. public Image apply(ServerImage from) {
  79. ImageBuilder builder = new ImageBuilder();
  80. builder.ids(from.getId() + "");
  81. builder.name(from.getFriendlyName());
  82. builder.description(from.getDescription());
  83. builder.operatingSystem(parseOs(from));
  84. builder.status(toPortableImageStatus.get(from.getState()));
  85. return builder.build();
  86. }
  87. }