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