| 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.cloudsigma.compute.functions; |
| 20 | |
| 21 | import javax.inject.Inject; |
| 22 | import javax.inject.Singleton; |
| 23 | |
| 24 | import org.jclouds.cloudsigma.domain.DriveInfo; |
| 25 | import org.jclouds.compute.domain.Image; |
| 26 | import org.jclouds.compute.domain.ImageBuilder; |
| 27 | import org.jclouds.compute.domain.OperatingSystem; |
| 28 | import org.jclouds.compute.domain.OsFamilyVersion64Bit; |
| 29 | import org.jclouds.compute.domain.OperatingSystem.Builder; |
| 30 | import org.jclouds.domain.Credentials; |
| 31 | import org.jclouds.domain.Location; |
| 32 | |
| 33 | import com.google.common.base.Function; |
| 34 | import com.google.common.base.Supplier; |
| 35 | import com.google.common.collect.ImmutableMap; |
| 36 | |
| 37 | /** |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | @Singleton |
| 41 | public class PreinstalledDiskToImage implements Function<DriveInfo, Image> { |
| 42 | private final Supplier<Location> locationSupplier; |
| 43 | private final Function<String, OsFamilyVersion64Bit> imageParser; |
| 44 | |
| 45 | @Inject |
| 46 | public PreinstalledDiskToImage(Supplier<Location> locationSupplier, |
| 47 | Function<String, OsFamilyVersion64Bit> imageParser) { |
| 48 | this.locationSupplier = locationSupplier; |
| 49 | this.imageParser = imageParser; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Image apply(DriveInfo drive) { |
| 54 | if (drive.getName() == null) |
| 55 | return null; |
| 56 | String description = drive.getDescription() != null ? drive.getDescription() : drive.getName(); |
| 57 | Builder builder = OperatingSystem.builder(); |
| 58 | OsFamilyVersion64Bit parsed = imageParser.apply(drive.getName()); |
| 59 | builder.name(drive.getName()).description(description) |
| 60 | .is64Bit(drive.getBits() != null ? drive.getBits() == 64 : parsed.is64Bit).version(parsed.version) |
| 61 | .family(parsed.family); |
| 62 | return new ImageBuilder().ids(drive.getUuid()).adminPassword("cloudsigma").userMetadata( |
| 63 | ImmutableMap.<String, String> of("size", drive.getSize() / 1024 / 1024 / 1024 + "")).defaultCredentials( |
| 64 | new Credentials("cloudsigma", "cloudsigma")).location(locationSupplier.get()).name(drive.getName()) |
| 65 | .description(description).operatingSystem(builder.build()).version("").build(); |
| 66 | } |
| 67 | } |