| 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.trmk.vcloud_0_8.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.compute.util.ComputeServiceUtils.parseOsFamilyOrUnrecognized; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.regex.Matcher; |
| 26 | import java.util.regex.Pattern; |
| 27 | |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.compute.domain.OperatingSystem; |
| 32 | import org.jclouds.compute.domain.OsFamily; |
| 33 | import org.jclouds.compute.util.ComputeServiceUtils; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | |
| 37 | /** |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | @Singleton |
| 41 | public class ParseOsFromVAppTemplateName implements Function<String, OperatingSystem> { |
| 42 | protected static final Pattern OS_PATTERN = Pattern.compile("(([^ ]*) ([0-9.]+) ?.*)"); |
| 43 | |
| 44 | protected final Map<OsFamily, Map<String, String>> osVersionMap; |
| 45 | |
| 46 | @Inject |
| 47 | protected ParseOsFromVAppTemplateName(Map<OsFamily, Map<String, String>> osVersionMap) { |
| 48 | this.osVersionMap = checkNotNull(osVersionMap, "osVersionMap"); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public OperatingSystem apply(String from) { |
| 53 | OperatingSystem.Builder builder = OperatingSystem.builder(); |
| 54 | OsFamily osFamily = parseOsFamilyOrUnrecognized(checkNotNull(from, "vapp template name")); |
| 55 | builder.family(osFamily); |
| 56 | builder.description(from); |
| 57 | builder.is64Bit(from.indexOf("64") != -1); |
| 58 | Matcher matcher = OS_PATTERN.matcher(from); |
| 59 | if (matcher.find()) { |
| 60 | builder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(osFamily, matcher.group(3), osVersionMap)); |
| 61 | } |
| 62 | return builder.build(); |
| 63 | } |
| 64 | } |