mirror of https://github.com/apache/jclouds.git
Improve image lookup when getting node info
This commit is contained in:
parent
6f23b89c69
commit
cec73089eb
|
@ -16,10 +16,9 @@
|
|||
*/
|
||||
package org.jclouds.azurecompute.arm.compute.functions;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
import static com.google.common.collect.Iterables.find;
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static com.google.common.collect.Iterables.tryFind;
|
||||
import static org.jclouds.azurecompute.arm.compute.AzureComputeServiceAdapter.GROUP_KEY;
|
||||
import static org.jclouds.azurecompute.arm.compute.extensions.AzureComputeImageExtension.CONTAINER_NAME;
|
||||
|
@ -38,6 +37,7 @@ import javax.inject.Inject;
|
|||
import javax.inject.Named;
|
||||
|
||||
import org.jclouds.azurecompute.arm.AzureComputeApi;
|
||||
import org.jclouds.azurecompute.arm.compute.functions.VirtualMachineToStatus.StatusAndBackendStatus;
|
||||
import org.jclouds.azurecompute.arm.domain.IdReference;
|
||||
import org.jclouds.azurecompute.arm.domain.IpConfiguration;
|
||||
import org.jclouds.azurecompute.arm.domain.NetworkInterfaceCard;
|
||||
|
@ -48,11 +48,6 @@ import org.jclouds.azurecompute.arm.domain.StorageProfile;
|
|||
import org.jclouds.azurecompute.arm.domain.StorageServiceKeys;
|
||||
import org.jclouds.azurecompute.arm.domain.VMImage;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachine;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineInstance;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineInstance.PowerState;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineProperties;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineProperties.ProvisioningState;
|
||||
import org.jclouds.azurecompute.arm.domain.Status;
|
||||
import org.jclouds.azurecompute.arm.functions.StorageProfileToStorageAccountName;
|
||||
import org.jclouds.azurecompute.arm.util.BlobHelper;
|
||||
import org.jclouds.collect.Memoized;
|
||||
|
@ -62,20 +57,17 @@ import org.jclouds.compute.domain.NodeMetadata;
|
|||
import org.jclouds.compute.domain.NodeMetadataBuilder;
|
||||
import org.jclouds.compute.functions.GroupNamingConvention;
|
||||
import org.jclouds.compute.reference.ComputeServiceConstants;
|
||||
import org.jclouds.compute.suppliers.ImageCacheSupplier;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.domain.Location;
|
||||
import org.jclouds.domain.LoginCredentials;
|
||||
import org.jclouds.logging.Logger;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
|
@ -85,62 +77,34 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
|
|||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
// When using the Deployment API to deploy an ARM template, the deployment
|
||||
// goes through
|
||||
// stages. Accepted -> Running -> Succeeded. Only when the deployment has
|
||||
// SUCCEEDED is
|
||||
// the resource deployed using the template actually ready.
|
||||
//
|
||||
// To get details about the resource(s) deployed via template, one needs to
|
||||
// query the
|
||||
// various resources after the deployment has "SUCCEEDED".
|
||||
private static final Function<VirtualMachineProperties.ProvisioningState, NodeMetadata.Status> PROVISIONINGSTATE_TO_NODESTATUS = Functions
|
||||
.forMap(
|
||||
ImmutableMap.<VirtualMachineProperties.ProvisioningState, NodeMetadata.Status> builder()
|
||||
.put(VirtualMachineProperties.ProvisioningState.ACCEPTED, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.READY, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.CREATING, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.RUNNING, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.UPDATING, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.DELETED, NodeMetadata.Status.TERMINATED)
|
||||
.put(VirtualMachineProperties.ProvisioningState.CANCELED, NodeMetadata.Status.TERMINATED)
|
||||
.put(VirtualMachineProperties.ProvisioningState.FAILED, NodeMetadata.Status.ERROR)
|
||||
.put(VirtualMachineProperties.ProvisioningState.UNRECOGNIZED, NodeMetadata.Status.UNRECOGNIZED)
|
||||
.build(), NodeMetadata.Status.UNRECOGNIZED);
|
||||
|
||||
private static final Function<PowerState, NodeMetadata.Status> POWERSTATE_TO_NODESTATUS = Functions
|
||||
.forMap(
|
||||
ImmutableMap.<PowerState, NodeMetadata.Status> builder()
|
||||
.put(PowerState.RUNNING, NodeMetadata.Status.RUNNING)
|
||||
.put(PowerState.STOPPED, NodeMetadata.Status.SUSPENDED)
|
||||
.put(PowerState.UNRECOGNIZED, NodeMetadata.Status.UNRECOGNIZED).build(),
|
||||
NodeMetadata.Status.UNRECOGNIZED);
|
||||
|
||||
private final AzureComputeApi api;
|
||||
private final GroupNamingConvention nodeNamingConvention;
|
||||
private final Supplier<Map<String, ? extends Image>> images;
|
||||
private final Supplier<Set<? extends Location>> locations;
|
||||
private final Supplier<Map<String, ? extends Hardware>> hardwares;
|
||||
private final Map<String, Credentials> credentialStore;
|
||||
private final Function<VMImage, Image> vmImageToImge;
|
||||
private final StorageProfileToStorageAccountName storageProfileToStorageAccountName;
|
||||
private final LoadingCache<String, ResourceGroup> resourceGroupMap;
|
||||
private final ImageCacheSupplier imageCache;
|
||||
private final VirtualMachineToStatus virtualMachineToStatus;
|
||||
|
||||
@Inject
|
||||
VirtualMachineToNodeMetadata(AzureComputeApi api, GroupNamingConvention.Factory namingConvention,
|
||||
Supplier<Map<String, ? extends Image>> images, Supplier<Map<String, ? extends Hardware>> hardwares,
|
||||
@Memoized Supplier<Set<? extends Location>> locations, Map<String, Credentials> credentialStore,
|
||||
Function<VMImage, Image> vmImageToImge, StorageProfileToStorageAccountName storageProfileToStorageAccountName,
|
||||
LoadingCache<String, ResourceGroup> resourceGroupMap) {
|
||||
Supplier<Map<String, ? extends Hardware>> hardwares, @Memoized Supplier<Set<? extends Location>> locations,
|
||||
Map<String, Credentials> credentialStore, Function<VMImage, Image> vmImageToImge,
|
||||
StorageProfileToStorageAccountName storageProfileToStorageAccountName,
|
||||
LoadingCache<String, ResourceGroup> resourceGroupMap, @Memoized Supplier<Set<? extends Image>> imageCache,
|
||||
VirtualMachineToStatus virtualMachineToStatus) {
|
||||
this.api = api;
|
||||
this.nodeNamingConvention = namingConvention.createWithoutPrefix();
|
||||
this.images = checkNotNull(images, "images cannot be null");
|
||||
this.locations = checkNotNull(locations, "locations cannot be null");
|
||||
this.hardwares = checkNotNull(hardwares, "hardwares cannot be null");
|
||||
this.credentialStore = credentialStore;
|
||||
this.locations = locations;
|
||||
this.hardwares = hardwares;
|
||||
this.vmImageToImge = vmImageToImge;
|
||||
this.storageProfileToStorageAccountName = storageProfileToStorageAccountName;
|
||||
this.resourceGroupMap = resourceGroupMap;
|
||||
this.virtualMachineToStatus = virtualMachineToStatus;
|
||||
checkArgument(imageCache instanceof ImageCacheSupplier,
|
||||
"This provider needs an instance of the ImageCacheSupplier");
|
||||
this.imageCache = (ImageCacheSupplier) imageCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -153,32 +117,9 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
|
|||
builder.name(virtualMachine.name());
|
||||
builder.hostname(virtualMachine.name());
|
||||
|
||||
ProvisioningState provisioningState = virtualMachine.properties().provisioningState();
|
||||
if (ProvisioningState.SUCCEEDED.equals(provisioningState)) {
|
||||
// If the provisioning succeeded, we need to query the *real* status of
|
||||
// the VM
|
||||
VirtualMachineInstance instanceDetails = api.getVirtualMachineApi(resourceGroup.name()).getInstanceDetails(
|
||||
virtualMachine.name());
|
||||
if (instanceDetails != null && instanceDetails.powerState() != null) {
|
||||
builder.status(POWERSTATE_TO_NODESTATUS.apply(instanceDetails.powerState()));
|
||||
builder.backendStatus(Joiner.on(',').join(
|
||||
transform(instanceDetails.statuses(), new Function<Status, String>() {
|
||||
@Override
|
||||
public String apply(Status input) {
|
||||
return input.code();
|
||||
}
|
||||
})));
|
||||
} else {
|
||||
builder.status(NodeMetadata.Status.PENDING);
|
||||
builder.backendStatus(provisioningState.name());
|
||||
}
|
||||
} else {
|
||||
builder.status(PROVISIONINGSTATE_TO_NODESTATUS.apply(provisioningState));
|
||||
builder.backendStatus(provisioningState.name());
|
||||
}
|
||||
|
||||
Credentials credentials = credentialStore.get("node#" + virtualMachine.name());
|
||||
builder.credentials(LoginCredentials.fromCredentials(credentials));
|
||||
StatusAndBackendStatus status = virtualMachineToStatus.apply(virtualMachine);
|
||||
builder.status(status.status());
|
||||
builder.backendStatus(status.backendStatus());
|
||||
|
||||
builder.publicAddresses(getPublicIpAddresses(virtualMachine.properties().networkProfile().networkInterfaces()));
|
||||
builder.privateAddresses(getPrivateIpAddresses(virtualMachine.properties().networkProfile().networkInterfaces()));
|
||||
|
@ -266,8 +207,8 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
|
|||
protected Optional<? extends Image> findImage(final StorageProfile storageProfile, String locatioName,
|
||||
String azureGroup) {
|
||||
if (storageProfile.imageReference() != null) {
|
||||
return Optional.fromNullable(images.get().get(
|
||||
encodeFieldsToUniqueId(false, locatioName, storageProfile.imageReference())));
|
||||
String imageId = encodeFieldsToUniqueId(false, locatioName, storageProfile.imageReference());
|
||||
return imageCache.get(imageId);
|
||||
} else {
|
||||
String storageAccountName = storageProfileToStorageAccountName.apply(storageProfile);
|
||||
StorageServiceKeys keys = api.getStorageAccountApi(azureGroup).getKeys(storageAccountName);
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jclouds.azurecompute.arm.compute.functions;
|
||||
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.azurecompute.arm.AzureComputeApi;
|
||||
import org.jclouds.azurecompute.arm.compute.functions.VirtualMachineToStatus.StatusAndBackendStatus;
|
||||
import org.jclouds.azurecompute.arm.domain.ResourceGroup;
|
||||
import org.jclouds.azurecompute.arm.domain.Status;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachine;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineInstance;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineInstance.PowerState;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineProperties;
|
||||
import org.jclouds.azurecompute.arm.domain.VirtualMachineProperties.ProvisioningState;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
@Singleton
|
||||
public class VirtualMachineToStatus implements Function<VirtualMachine, StatusAndBackendStatus> {
|
||||
|
||||
@AutoValue
|
||||
public abstract static class StatusAndBackendStatus {
|
||||
public abstract NodeMetadata.Status status();
|
||||
|
||||
public abstract String backendStatus();
|
||||
|
||||
public static StatusAndBackendStatus create(NodeMetadata.Status status, String backendStatus) {
|
||||
return new AutoValue_VirtualMachineToStatus_StatusAndBackendStatus(status, backendStatus);
|
||||
}
|
||||
}
|
||||
|
||||
// When using the Deployment API to deploy an ARM template, the deployment
|
||||
// goes through stages: Accepted -> Running -> Succeeded.
|
||||
// Only when the deployment has SUCCEEDED is the resource deployed using the
|
||||
// template actually ready.
|
||||
// To get details about the resource(s) deployed via template, one needs to
|
||||
// query the various resources after the deployment has SUCCEEDED.
|
||||
private static final Function<VirtualMachineProperties.ProvisioningState, NodeMetadata.Status> PROVISIONINGSTATE_TO_NODESTATUS = Functions
|
||||
.forMap(
|
||||
ImmutableMap.<VirtualMachineProperties.ProvisioningState, NodeMetadata.Status> builder()
|
||||
.put(VirtualMachineProperties.ProvisioningState.ACCEPTED, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.READY, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.CREATING, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.RUNNING, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.UPDATING, NodeMetadata.Status.PENDING)
|
||||
.put(VirtualMachineProperties.ProvisioningState.DELETED, NodeMetadata.Status.TERMINATED)
|
||||
.put(VirtualMachineProperties.ProvisioningState.CANCELED, NodeMetadata.Status.TERMINATED)
|
||||
.put(VirtualMachineProperties.ProvisioningState.FAILED, NodeMetadata.Status.ERROR)
|
||||
.put(VirtualMachineProperties.ProvisioningState.UNRECOGNIZED, NodeMetadata.Status.UNRECOGNIZED)
|
||||
.build(), NodeMetadata.Status.UNRECOGNIZED);
|
||||
|
||||
private static final Function<PowerState, NodeMetadata.Status> POWERSTATE_TO_NODESTATUS = Functions.forMap(
|
||||
ImmutableMap.<PowerState, NodeMetadata.Status> builder().put(PowerState.RUNNING, NodeMetadata.Status.RUNNING)
|
||||
.put(PowerState.STOPPED, NodeMetadata.Status.SUSPENDED)
|
||||
.put(PowerState.UNRECOGNIZED, NodeMetadata.Status.UNRECOGNIZED).build(),
|
||||
NodeMetadata.Status.UNRECOGNIZED);
|
||||
|
||||
private final AzureComputeApi api;
|
||||
private final LoadingCache<String, ResourceGroup> resourceGroupMap;
|
||||
|
||||
@Inject
|
||||
VirtualMachineToStatus(AzureComputeApi api, LoadingCache<String, ResourceGroup> resourceGroupMap) {
|
||||
this.api = api;
|
||||
this.resourceGroupMap = resourceGroupMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatusAndBackendStatus apply(VirtualMachine virtualMachine) {
|
||||
ResourceGroup resourceGroup = resourceGroupMap.getUnchecked(virtualMachine.location());
|
||||
ProvisioningState provisioningState = virtualMachine.properties().provisioningState();
|
||||
|
||||
NodeMetadata.Status status = PROVISIONINGSTATE_TO_NODESTATUS.apply(provisioningState);
|
||||
String backendStatus = provisioningState.name();
|
||||
|
||||
if (ProvisioningState.SUCCEEDED.equals(provisioningState)) {
|
||||
// If the provisioning succeeded, we need to query the *real* status of
|
||||
// the VM
|
||||
VirtualMachineInstance instanceDetails = api.getVirtualMachineApi(resourceGroup.name()).getInstanceDetails(
|
||||
virtualMachine.name());
|
||||
if (instanceDetails != null && instanceDetails.powerState() != null) {
|
||||
status = POWERSTATE_TO_NODESTATUS.apply(instanceDetails.powerState());
|
||||
backendStatus = Joiner.on(',').join(transform(instanceDetails.statuses(), new Function<Status, String>() {
|
||||
@Override
|
||||
public String apply(Status input) {
|
||||
return input.code();
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
status = NodeMetadata.Status.PENDING;
|
||||
}
|
||||
}
|
||||
|
||||
return StatusAndBackendStatus.create(status, backendStatus);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue