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.ec2.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.NoSuchElementException; |
26 | import java.util.Set; |
27 | |
28 | import javax.annotation.Resource; |
29 | import javax.inject.Inject; |
30 | import javax.inject.Named; |
31 | import javax.inject.Singleton; |
32 | |
33 | import org.jclouds.collect.Memoized; |
34 | import org.jclouds.compute.domain.Image; |
35 | import org.jclouds.compute.domain.ImageBuilder; |
36 | import org.jclouds.compute.domain.OperatingSystem; |
37 | import org.jclouds.compute.domain.OsFamily; |
38 | import org.jclouds.compute.reference.ComputeServiceConstants; |
39 | import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy; |
40 | import org.jclouds.compute.util.ComputeServiceUtils; |
41 | import org.jclouds.domain.Location; |
42 | import org.jclouds.domain.LocationBuilder; |
43 | import org.jclouds.domain.LocationScope; |
44 | import org.jclouds.ec2.compute.strategy.ReviseParsedImage; |
45 | import org.jclouds.ec2.domain.Image.Architecture; |
46 | import org.jclouds.ec2.domain.Image.ImageType; |
47 | import org.jclouds.logging.Logger; |
48 | |
49 | import com.google.common.base.Function; |
50 | import com.google.common.base.Predicate; |
51 | import com.google.common.base.Supplier; |
52 | import com.google.common.collect.ImmutableMap; |
53 | import com.google.common.collect.Iterables; |
54 | |
55 | /** |
56 | * @author Adrian Cole |
57 | */ |
58 | @Singleton |
59 | public class EC2ImageParser implements Function<org.jclouds.ec2.domain.Image, Image> { |
60 | @Resource |
61 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
62 | protected Logger logger = Logger.NULL; |
63 | |
64 | private final PopulateDefaultLoginCredentialsForImageStrategy credentialProvider; |
65 | private final Supplier<Set<? extends Location>> locations; |
66 | private final Supplier<Location> defaultLocation; |
67 | private final Map<OsFamily, Map<String, String>> osVersionMap; |
68 | private final ReviseParsedImage reviseParsedImage; |
69 | |
70 | @Inject |
71 | public EC2ImageParser(PopulateDefaultLoginCredentialsForImageStrategy credentialProvider, |
72 | Map<OsFamily, Map<String, String>> osVersionMap, @Memoized Supplier<Set<? extends Location>> locations, |
73 | Supplier<Location> defaultLocation, ReviseParsedImage reviseParsedImage) { |
74 | this.credentialProvider = checkNotNull(credentialProvider, "credentialProvider"); |
75 | this.locations = checkNotNull(locations, "locations"); |
76 | this.defaultLocation = checkNotNull(defaultLocation, "defaultLocation"); |
77 | this.osVersionMap = checkNotNull(osVersionMap, "osVersionMap"); |
78 | this.reviseParsedImage = checkNotNull(reviseParsedImage, "reviseParsedImage"); |
79 | } |
80 | |
81 | @Override |
82 | public Image apply(final org.jclouds.ec2.domain.Image from) { |
83 | if (from.getImageType() != ImageType.MACHINE) { |
84 | return null; |
85 | } |
86 | ImageBuilder builder = new ImageBuilder(); |
87 | builder.providerId(from.getId()); |
88 | builder.id(from.getRegion() + "/" + from.getId()); |
89 | builder.name(from.getName()); |
90 | builder.description(from.getDescription() != null ? from.getDescription() : from.getImageLocation()); |
91 | builder.userMetadata(ImmutableMap.<String, String> builder().put("owner", from.getImageOwnerId()).put( |
92 | "rootDeviceType", from.getRootDeviceType().value()).put("virtualizationType", |
93 | from.getVirtualizationType().value()).put("hypervisor", from.getHypervisor().value()).build()); |
94 | |
95 | OperatingSystem.Builder osBuilder = OperatingSystem.builder(); |
96 | osBuilder.is64Bit(from.getArchitecture() == Architecture.X86_64); |
97 | OsFamily family = parseOsFamilyOrUnrecognized(from.getImageLocation()); |
98 | osBuilder.family(family); |
99 | osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, from.getImageLocation(), |
100 | osVersionMap)); |
101 | osBuilder.description(from.getImageLocation()); |
102 | osBuilder.arch(from.getVirtualizationType().value()); |
103 | |
104 | reviseParsedImage.reviseParsedImage(from, builder, family, osBuilder); |
105 | |
106 | builder.defaultCredentials(credentialProvider.apply(from)); |
107 | |
108 | try { |
109 | builder.location(Iterables.find(locations.get(), new Predicate<Location>() { |
110 | |
111 | @Override |
112 | public boolean apply(Location input) { |
113 | return input.getId().equals(from.getRegion()); |
114 | } |
115 | |
116 | })); |
117 | } catch (NoSuchElementException e) { |
118 | logger.error("unknown region %s for image %s; not in %s", from.getRegion(), from.getId(), locations); |
119 | builder.location(new LocationBuilder().scope(LocationScope.REGION).id(from.getRegion()).description( |
120 | from.getRegion()).parent(defaultLocation.get()).build()); |
121 | } |
122 | builder.operatingSystem(osBuilder.build()); |
123 | return builder.build(); |
124 | } |
125 | |
126 | } |