| 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.rimuhosting.miro.compute.suppliers; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | import java.util.regex.Matcher; |
| 23 | import java.util.regex.Pattern; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.compute.domain.Image; |
| 31 | import org.jclouds.compute.domain.ImageBuilder; |
| 32 | import org.jclouds.compute.domain.OperatingSystem; |
| 33 | import org.jclouds.compute.domain.OsFamily; |
| 34 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 35 | import org.jclouds.domain.Credentials; |
| 36 | import org.jclouds.logging.Logger; |
| 37 | import org.jclouds.rimuhosting.miro.RimuHostingClient; |
| 38 | |
| 39 | import com.google.common.base.Supplier; |
| 40 | import com.google.common.collect.Sets; |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * @author Adrian Cole |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class RimuHostingImageSupplier implements Supplier<Set<? extends Image>> { |
| 48 | public static final Pattern RIMU_PATTERN = Pattern.compile("([a-zA-Z]+) ?([0-9.]+) .*"); |
| 49 | private final RimuHostingClient sync; |
| 50 | |
| 51 | @Inject |
| 52 | RimuHostingImageSupplier(RimuHostingClient sync) { |
| 53 | this.sync = sync; |
| 54 | } |
| 55 | |
| 56 | @Resource |
| 57 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 58 | protected Logger logger = Logger.NULL; |
| 59 | |
| 60 | @Override |
| 61 | public Set<? extends Image> get() { |
| 62 | final Set<Image> images = Sets.newHashSet(); |
| 63 | logger.debug(">> providing images"); |
| 64 | for (org.jclouds.rimuhosting.miro.domain.Image from : sync.getImageList()) { |
| 65 | ImageBuilder builder = new ImageBuilder(); |
| 66 | builder.ids(from.getId() + ""); |
| 67 | builder.name(from.getDescription()); |
| 68 | builder.description(from.getDescription()); |
| 69 | builder.operatingSystem(parseOs(from)); |
| 70 | builder.defaultCredentials(new Credentials("root", null)); |
| 71 | images.add(builder.build()); |
| 72 | } |
| 73 | logger.debug("<< images(%d)", images.size()); |
| 74 | return images; |
| 75 | } |
| 76 | |
| 77 | protected OperatingSystem parseOs(final org.jclouds.rimuhosting.miro.domain.Image from) { |
| 78 | OsFamily osFamily = null; |
| 79 | String osName = from.getId(); |
| 80 | String osArch = null; |
| 81 | String osVersion = null; |
| 82 | String osDescription = from.getDescription(); |
| 83 | boolean is64Bit = from.getId().indexOf("64") != -1; |
| 84 | |
| 85 | Matcher matcher = RIMU_PATTERN.matcher(osDescription); |
| 86 | if (matcher.find()) { |
| 87 | try { |
| 88 | osFamily = OsFamily.fromValue(matcher.group(1).toLowerCase()); |
| 89 | osVersion = matcher.group(2).toLowerCase(); |
| 90 | } catch (IllegalArgumentException e) { |
| 91 | logger.debug("<< didn't match os(%s)", osDescription); |
| 92 | } |
| 93 | } |
| 94 | return new OperatingSystem(osFamily, osName, osVersion, osArch, osDescription, is64Bit); |
| 95 | } |
| 96 | } |