| 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.vcloud.compute.util; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.filter; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.jclouds.cim.CIMPredicates; |
| 26 | import org.jclouds.cim.ResourceAllocationSettingData; |
| 27 | import org.jclouds.cim.ResourceAllocationSettingData.ResourceType; |
| 28 | import org.jclouds.compute.domain.CIMOperatingSystem; |
| 29 | import org.jclouds.compute.domain.OperatingSystem; |
| 30 | import org.jclouds.domain.Credentials; |
| 31 | import org.jclouds.vcloud.domain.NetworkConnection; |
| 32 | import org.jclouds.vcloud.domain.VApp; |
| 33 | import org.jclouds.vcloud.domain.VAppTemplate; |
| 34 | import org.jclouds.vcloud.domain.Vm; |
| 35 | import org.jclouds.vcloud.domain.ovf.VCloudNetworkAdapter; |
| 36 | |
| 37 | import com.google.common.collect.ImmutableSet; |
| 38 | import com.google.common.collect.Iterables; |
| 39 | import com.google.common.collect.ImmutableSet.Builder; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | public class VCloudComputeUtils { |
| 46 | public static OperatingSystem toComputeOs(VApp vApp, OperatingSystem defaultOs) { |
| 47 | CIMOperatingSystem cimOs = toComputeOs(vApp); |
| 48 | return cimOs != null ? cimOs : defaultOs; |
| 49 | } |
| 50 | |
| 51 | public static CIMOperatingSystem toComputeOs(VApp vApp) { |
| 52 | // TODO we need to change the design so that it doesn't assume single-vms |
| 53 | return vApp.getChildren().size() > 0 ? toComputeOs(Iterables.get(vApp.getChildren(), 0)) : null; |
| 54 | } |
| 55 | |
| 56 | public static CIMOperatingSystem toComputeOs(Vm vm) { |
| 57 | return CIMOperatingSystem.toComputeOs(vm.getOperatingSystemSection()); |
| 58 | } |
| 59 | |
| 60 | public static String getVirtualSystemIdentifierOfFirstVMIn(VApp vApp) { |
| 61 | return vApp.getChildren().size() > 0 ? getVirtualSystemIdentifierOf(Iterables.get(vApp.getChildren(), 0)) : null; |
| 62 | } |
| 63 | |
| 64 | public static String getVirtualSystemIdentifierOf(Vm vm) { |
| 65 | if (vm.getVirtualHardwareSection() != null && vm.getVirtualHardwareSection().getSystem() != null) |
| 66 | return vm.getVirtualHardwareSection().getSystem().getVirtualSystemIdentifier(); |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | public static Credentials getCredentialsFrom(VApp vApp) { |
| 71 | return vApp.getChildren().size() > 0 ? getCredentialsFrom(Iterables.get(vApp.getChildren(), 0)) : null; |
| 72 | } |
| 73 | |
| 74 | public static Credentials getCredentialsFrom(VAppTemplate vApp) { |
| 75 | return vApp.getChildren().size() > 0 ? getCredentialsFrom(Iterables.get(vApp.getChildren(), 0)) : null; |
| 76 | } |
| 77 | |
| 78 | public static Credentials getCredentialsFrom(Vm vm) { |
| 79 | String user = "root"; |
| 80 | if (vm.getOperatingSystemSection() != null && vm.getOperatingSystemSection().getDescription() != null |
| 81 | && vm.getOperatingSystemSection().getDescription().indexOf("Windows") >= 0) |
| 82 | user = "Administrator"; |
| 83 | String password = null; |
| 84 | if (vm.getGuestCustomizationSection() != null) |
| 85 | password = vm.getGuestCustomizationSection().getAdminPassword(); |
| 86 | return new Credentials(user, password); |
| 87 | } |
| 88 | |
| 89 | public static Set<String> getIpsFromVApp(VApp vApp) { |
| 90 | // TODO make this work with composite vApps |
| 91 | if (vApp.getChildren().size() == 0) |
| 92 | return ImmutableSet.of(); |
| 93 | Builder<String> ips = ImmutableSet.<String> builder(); |
| 94 | Vm vm = Iterables.get(vApp.getChildren(), 0); |
| 95 | // TODO: figure out how to differentiate public from private ip addresses |
| 96 | // assumption is that we'll do this from the network object, which may |
| 97 | // have |
| 98 | // enough data to tell whether or not it is a public network without |
| 99 | // string |
| 100 | // parsing. At worst, we could have properties set per cloud provider to |
| 101 | // declare the networks which are public, then check against these in |
| 102 | // networkconnection.getNetwork |
| 103 | if (vm.getNetworkConnectionSection() != null) { |
| 104 | for (NetworkConnection connection : vm.getNetworkConnectionSection().getConnections()) { |
| 105 | if (connection.getIpAddress() != null) |
| 106 | ips.add(connection.getIpAddress()); |
| 107 | if (connection.getExternalIpAddress() != null) |
| 108 | ips.add(connection.getExternalIpAddress()); |
| 109 | } |
| 110 | } else { |
| 111 | for (ResourceAllocationSettingData net : filter(vm.getVirtualHardwareSection().getItems(), |
| 112 | CIMPredicates.resourceTypeIn(ResourceType.ETHERNET_ADAPTER))) { |
| 113 | if (net instanceof VCloudNetworkAdapter) { |
| 114 | VCloudNetworkAdapter vNet = VCloudNetworkAdapter.class.cast(net); |
| 115 | if (vNet.getIpAddress() != null) |
| 116 | ips.add(vNet.getIpAddress()); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return ips.build(); |
| 121 | } |
| 122 | } |