/providers/gogrid/src/main/java/org/jclouds/gogrid/compute/suppliers/GoGridImageSupplier.java
Java | 116 lines | 81 code | 12 blank | 23 comment | 8 complexity | 222a205d0f339fe09f915a5d01e3e40d MD5 | raw file
- /**
- * Licensed to jclouds, Inc. (jclouds) under one or more
- * contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. jclouds licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- package org.jclouds.gogrid.compute.suppliers;
- import java.util.Map;
- import java.util.Set;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.annotation.Resource;
- import javax.inject.Inject;
- import javax.inject.Named;
- import javax.inject.Singleton;
- import org.jclouds.compute.domain.Image;
- import org.jclouds.compute.domain.ImageBuilder;
- import org.jclouds.compute.domain.OperatingSystem;
- import org.jclouds.compute.domain.OsFamily;
- import org.jclouds.compute.reference.ComputeServiceConstants;
- import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
- import org.jclouds.compute.util.ComputeServiceUtils;
- import org.jclouds.gogrid.GoGridClient;
- import org.jclouds.gogrid.domain.ServerImage;
- import org.jclouds.logging.Logger;
- import com.google.common.base.Supplier;
- import com.google.common.collect.Sets;
- /**
- *
- * @author Adrian Cole
- */
- @Singleton
- public class GoGridImageSupplier implements Supplier<Set<? extends Image>> {
- public static final Pattern GOGRID_OS_PATTERN = Pattern.compile("([a-zA-Z]*).*");
- public static final Pattern GOGRID_VERSION_PATTERN = Pattern.compile(".* ([0-9.]+) .*");
- @Resource
- @Named(ComputeServiceConstants.COMPUTE_LOGGER)
- protected Logger logger = Logger.NULL;
- private final GoGridClient sync;
- private final PopulateDefaultLoginCredentialsForImageStrategy authenticator;
- private final Map<OsFamily, Map<String, String>> osVersionMap;
- @Inject
- GoGridImageSupplier(GoGridClient sync, PopulateDefaultLoginCredentialsForImageStrategy authenticator,
- Map<OsFamily, Map<String, String>> osVersionMap) {
- this.osVersionMap = osVersionMap;
- this.sync = sync;
- this.authenticator = authenticator;
- }
- @Override
- public Set<? extends Image> get() {
- final Set<Image> images = Sets.newHashSet();
- logger.debug(">> providing images");
- Set<ServerImage> allImages = sync.getImageServices().getImageList();
- for (ServerImage from : allImages) {
- ImageBuilder builder = new ImageBuilder();
- builder.ids(from.getId() + "");
- builder.name(from.getFriendlyName());
- builder.description(from.getDescription());
- builder.defaultCredentials(authenticator.execute(from));
- builder.operatingSystem(parseOs(from));
- images.add(builder.build());
- }
- logger.debug("<< images(%d)", images.size());
- return images;
- }
- protected OperatingSystem parseOs(ServerImage from) {
- OsFamily osFamily = null;
- String osName = from.getOs().getName();
- String osArch = from.getArchitecture().getDescription();
- String osVersion = null;
- String osDescription = from.getOs().getDescription();
- boolean is64Bit = (from.getOs().getName().indexOf("64") != -1 || from.getDescription().indexOf("64") != -1);
- if (osName.startsWith("Windows")) {
- osFamily = OsFamily.WINDOWS;
- } else {
- Matcher matcher = GOGRID_OS_PATTERN.matcher(from.getName());
- if (matcher.find()) {
- try {
- osFamily = OsFamily.fromValue(matcher.group(1).toLowerCase());
- } catch (IllegalArgumentException e) {
- logger.debug("<< didn't match os(%s)", from.getName());
- }
- }
- }
- Matcher matcher = GOGRID_VERSION_PATTERN.matcher(osName);
- if (matcher.find()) {
- osVersion = ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(1), osVersionMap);
- }
- // TODO determine DC images are in
- return new OperatingSystem(osFamily, osName, osVersion, osArch, osDescription, is64Bit);
- }
- }