
org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jclouds-shaded Show documentation
Show all versions of jclouds-shaded Show documentation
Provides a shaded jclouds with relocated guava and guice
The newest version!
/*
* 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.ec2.compute.functions;
import static shaded.com.google.common.base.Preconditions.checkNotNull;
import static shaded.com.google.common.base.Predicates.not;
import static shaded.com.google.common.base.Strings.emptyToNull;
import static shaded.com.google.common.collect.Iterables.filter;
import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromValuesOfEmptyString;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Resource;
import javax.inject.Singleton;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.ec2.compute.domain.RegionAndName;
import org.jclouds.ec2.domain.BlockDevice;
import org.jclouds.ec2.domain.InstanceState;
import org.jclouds.ec2.domain.RootDeviceType;
import org.jclouds.ec2.domain.RunningInstance;
import org.jclouds.logging.Logger;
import org.jclouds.util.InetAddresses2.IsPrivateIPAddress;
import shaded.com.google.common.annotations.VisibleForTesting;
import shaded.com.google.common.base.Function;
import shaded.com.google.common.base.Predicate;
import shaded.com.google.common.base.Supplier;
import shaded.com.google.common.cache.CacheLoader;
import shaded.com.google.common.cache.LoadingCache;
import shaded.com.google.common.collect.ImmutableSet;
import shaded.com.google.common.collect.ImmutableSet.Builder;
import shaded.com.google.common.collect.Iterables;
import shaded.com.google.common.collect.Lists;
import shaded.com.google.common.collect.Sets;
import shaded.com.google.common.util.concurrent.UncheckedExecutionException;
import shaded.com.google.inject.Inject;
@Singleton
public class RunningInstanceToNodeMetadata implements Function {
@Resource
protected Logger logger = Logger.NULL;
protected final Supplier> locations;
protected final Supplier> hardware;
protected final Supplier> imageMap;
protected final Map credentialStore;
protected final Map instanceToNodeStatus;
protected final GroupNamingConvention.Factory namingConvention;
@Inject
protected RunningInstanceToNodeMetadata(Map instanceToNodeStatus,
Map credentialStore, Supplier> imageMap,
@Memoized Supplier> locations, @Memoized Supplier> hardware,
GroupNamingConvention.Factory namingConvention) {
this.locations = checkNotNull(locations, "locations");
this.hardware = checkNotNull(hardware, "hardware");
this.imageMap = checkNotNull(imageMap, "imageMap");
this.instanceToNodeStatus = checkNotNull(instanceToNodeStatus, "instanceToNodeStatus");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.namingConvention = checkNotNull(namingConvention, "namingConvention");
}
@Override
public NodeMetadata apply(RunningInstance instance) {
if (instance == null || instance.getId() == null)
return null;
NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.name(instance.getTags().get("Name"));
addMetadataAndParseTagsFromValuesOfEmptyString(builder, instance.getTags());
builder.providerId(instance.getId());
builder.id(instance.getRegion() + "/" + instance.getId());
String group = getGroupForInstance(instance);
builder.group(group);
// standard convention from aws-ec2, which might not be re-used outside.
if (instance.getPrivateDnsName() != null)
builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", ""));
addCredentialsForInstance(builder, instance);
builder.status(instanceToNodeStatus.get(instance.getInstanceState()));
builder.backendStatus(instance.getRawState());
// collect all ip addresses into one bundle in case the api mistakenly put a private address
// into the public address field
Builder addressesBuilder = ImmutableSet.builder();
if (emptyToNull(instance.getIpAddress()) != null)
addressesBuilder.add(instance.getIpAddress());
if (emptyToNull(instance.getPrivateIpAddress()) != null)
addressesBuilder.add(instance.getPrivateIpAddress());
Set addresses = addressesBuilder.build();
builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
builder.hardware(parseHardware(instance));
Location location = getLocationForAvailabilityZoneOrRegion(instance);
builder.location(location);
builder.imageId(instance.getRegion() + "/" + instance.getImageId());
// extract the operating system from the image
RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId());
try {
Image image = imageMap.get().getUnchecked(regionAndName);
if (image != null)
builder.operatingSystem(image.getOperatingSystem());
} catch (CacheLoader.InvalidCacheLoadException e) {
logger.debug("image not found for %s: %s", regionAndName, e);
} catch (UncheckedExecutionException e) {
logger.debug("error getting image for %s: %s", regionAndName, e);
}
return builder.build();
}
protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
builder.credentials(LoginCredentials.fromCredentials(credentialStore.get("node#" + instance.getRegion() + "/"
+ instance.getId())));
}
protected Hardware parseHardware(final RunningInstance instance) {
Hardware hardware = getHardwareForInstance(instance);
if (hardware != null) {
hardware = HardwareBuilder.fromHardware(hardware).volumes(addEBS(instance, hardware.getVolumes())).build();
}
return hardware;
}
@VisibleForTesting
static List addEBS(final RunningInstance instance, Iterable volumes) {
Iterable ebsVolumes = Iterables.transform(instance.getEbsBlockDevices().entrySet(),
new Function, Volume>() {
@Override
public Volume apply(Entry from) {
return new VolumeImpl(from.getValue().getVolumeId(), Volume.Type.SAN, null, from.getKey(),
instance.getRootDeviceName() != null
&& instance.getRootDeviceName().equals(from.getKey()), true);
}
});
if (instance.getRootDeviceType() == RootDeviceType.EBS) {
volumes = Iterables.filter(volumes, new Predicate() {
@Override
public boolean apply(Volume input) {
return !input.isBootDevice();
}
});
}
return Lists.newArrayList(Iterables.concat(volumes, ebsVolumes));
}
@VisibleForTesting
String getGroupForInstance(final RunningInstance instance) {
String group = parseGroupFrom(instance, instance.getGroupNames());
if (group == null && instance.getKeyName() != null) {
// when not using a generated security group, e.g. in VPC, try from key:
group = parseGroupFrom(instance, Sets.newHashSet(instance.getKeyName()));
}
return group;
}
private String parseGroupFrom(final RunningInstance instance, final Set data) {
String group = null;
try {
Predicate containsAnyGroup = namingConvention.create().containsAnyGroup();
String encodedGroup = Iterables.getOnlyElement(Iterables.filter(data, containsAnyGroup));
group = namingConvention.create().extractGroup(encodedGroup);
} catch (NoSuchElementException e) {
logger.debug("no group parsed from %s's data: %s", instance.getId(), data);
} catch (IllegalArgumentException e) {
logger.debug("too many groups match naming convention; %s's data: %s", instance.getId(), data);
}
return group;
}
@VisibleForTesting
Hardware getHardwareForInstance(final RunningInstance instance) {
try {
return Iterables.find(hardware.get(), new Predicate() {
@Override
public boolean apply(Hardware input) {
return input.getId().equals(instance.getInstanceType());
}
});
} catch (NoSuchElementException e) {
logger.debug("couldn't match instance type %s in: %s", instance.getInstanceType(), hardware.get());
return null;
}
}
private Location getLocationForAvailabilityZoneOrRegion(final RunningInstance instance) {
Location location = findLocationWithId(instance.getAvailabilityZone());
if (location == null)
location = findLocationWithId(instance.getRegion());
return location;
}
private Location findLocationWithId(final String locationId) {
if (locationId == null)
return null;
try {
Location location = Iterables.find(locations.get(), new Predicate() {
@Override
public boolean apply(Location input) {
return input.getId().equals(locationId);
}
});
return location;
} catch (NoSuchElementException e) {
logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
return null;
}
}
}