http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeAvailable.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeAvailable.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeAvailable.java new file mode 100644 index 0000000..051531c --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeAvailable.java @@ -0,0 +1,55 @@ +/* + * 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.predicates; + +import javax.annotation.Resource; +import javax.inject.Singleton; + +import org.jclouds.ec2.domain.Volume; +import org.jclouds.ec2.features.ElasticBlockStoreApi; +import org.jclouds.logging.Logger; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import com.google.inject.Inject; + +/** + * + * Tests to see if a volume is completed. + */ +@Singleton +public class VolumeAvailable implements Predicate<Volume> { + + private final ElasticBlockStoreApi client; + @Resource + protected Logger logger = Logger.NULL; + + @Inject + public VolumeAvailable(ElasticBlockStoreApi client) { + this.client = client; + } + + public boolean apply(Volume volume) { + logger.trace("looking for status on volume %s", volume.getId()); + volume = Iterables.getOnlyElement(client.describeVolumesInRegion(volume.getRegion(), volume + .getId())); + logger.trace("%s: looking for status %s: currently: %s", volume, Volume.Status.AVAILABLE, + volume.getStatus()); + return volume.getStatus() == Volume.Status.AVAILABLE; + } + +}
http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeDetached.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeDetached.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeDetached.java new file mode 100644 index 0000000..55995bb --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/predicates/VolumeDetached.java @@ -0,0 +1,67 @@ +/* + * 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.predicates; + +import static com.google.common.collect.Iterables.getLast; + +import javax.annotation.Resource; + +import org.jclouds.ec2.domain.Attachment; +import org.jclouds.ec2.domain.Volume; +import org.jclouds.ec2.features.ElasticBlockStoreApi; +import org.jclouds.logging.Logger; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import com.google.inject.Inject; +import com.google.inject.Singleton; + +/** + * Tests to see if a volume is detached. + */ +@Singleton +public class VolumeDetached implements Predicate<Attachment> { + + private final ElasticBlockStoreApi client; + @Resource + protected Logger logger = Logger.NULL; + + @Inject + public VolumeDetached(ElasticBlockStoreApi client) { + this.client = client; + } + + public boolean apply(Attachment attachment) { + logger.trace("looking for volume %s", attachment.getVolumeId()); + Volume volume = Iterables.getOnlyElement(client.describeVolumesInRegion(attachment + .getRegion(), attachment.getVolumeId())); + + /*If attachment size is 0 volume is detached for sure.*/ + if (volume.getAttachments().size() == 0) { + return true; + } + + /* But if attachment size is > 0, then the attachment could be in any state. + * So we need to check if the status is DETACHED (return true) or not (return false). + */ + Attachment lastAttachment = getLast(volume.getAttachments()); + logger.trace("%s: looking for status %s: currently: %s", lastAttachment, + Attachment.Status.DETACHED, lastAttachment.getStatus()); + return lastAttachment.getStatus() == Attachment.Status.DETACHED; + } +} + http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/reference/EC2Constants.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/reference/EC2Constants.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/reference/EC2Constants.java new file mode 100644 index 0000000..a30c627 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/reference/EC2Constants.java @@ -0,0 +1,50 @@ +/* + * 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.reference; + + +/** + * Configuration properties and constants used in EC2 connections. + */ +public class EC2Constants { + /** + * Listing the universe of amis is extremely expensive. set this to a comma separated value of + * the ami owners you wish to use in {@link ComputeService} + */ + public static final String PROPERTY_EC2_AMI_OWNERS = "jclouds.ec2.ami-owners"; + + /** + * Eventual consistency delay for retrieving a security group after it is created (in ms) + */ + public static final String PROPERTY_EC2_TIMEOUT_SECURITYGROUP_PRESENT = "jclouds.ec2.timeout.securitygroup-present"; + + /** + * Whenever a node is created, automatically allocate and assign an elastic ip address, also + * deallocate when the node is destroyed. + */ + public static final String PROPERTY_EC2_AUTO_ALLOCATE_ELASTIC_IPS = "jclouds.ec2.auto-allocate-elastic-ips"; + + /** + * If this property is set to true(default), jclouds generate a name for each instance based on the group. ex. + * i-ef34ae2 becomes hadoop-ef34ae2. Note that this depends on {@link EC2Api#getTagApi} returning present. + */ + public static final String PROPERTY_EC2_GENERATE_INSTANCE_NAMES = "jclouds.ec2.generate-instance-names"; + + protected EC2Constants() { + throw new AssertionError("intentionally unimplemented"); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java new file mode 100644 index 0000000..f97e962 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java @@ -0,0 +1,92 @@ +/* + * 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.suppliers; + +import java.util.Map; +import java.util.Set; + +import javax.annotation.Resource; +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.ec2.EC2Api; +import org.jclouds.ec2.domain.AvailabilityZoneInfo; +import org.jclouds.ec2.features.AvailabilityZoneAndRegionApi; +import org.jclouds.http.HttpResponseException; +import org.jclouds.location.Region; +import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier; +import org.jclouds.logging.Logger; +import org.jclouds.util.Suppliers2; + +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; + +@Singleton +public class DescribeAvailabilityZonesInRegion implements RegionIdToZoneIdsSupplier { + @Resource + protected Logger logger = Logger.NULL; + + private final AvailabilityZoneAndRegionApi client; + private final Supplier<Set<String>> regions; + + @Inject + public DescribeAvailabilityZonesInRegion(EC2Api client, @Region Supplier<Set<String>> regions) { + this.client = client.getAvailabilityZoneAndRegionApi().get(); + this.regions = regions; + } + + @Override + public Map<String, Supplier<Set<String>>> get() { + Builder<String, Set<String>> map = ImmutableMap.builder(); + HttpResponseException exception = null; + // TODO: this should be parallel + for (String region : regions.get()) { + try { + ImmutableSet<String> zones = ImmutableSet.copyOf(Iterables.transform(client + .describeAvailabilityZonesInRegion(region), new Function<AvailabilityZoneInfo, String>() { + + @Override + public String apply(AvailabilityZoneInfo arg0) { + return arg0.getZone(); + } + + })); + if (zones.size() > 0) + map.put(region, zones); + } catch (HttpResponseException e) { + // TODO: this should be in retry handler, not here. + if (e.getMessage().contains("Unable to tunnel through proxy")) { + exception = e; + logger.error(e, "Could not describe availability zones in Region: %s", region); + } else { + throw e; + } + } + } + ImmutableMap<String, Set<String>> result = map.build(); + if (result.isEmpty() && exception != null) { + throw exception; + } + return Maps.transformValues(result, Suppliers2.<Set<String>> ofInstanceFunction()); + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForRegionURIs.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForRegionURIs.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForRegionURIs.java new file mode 100644 index 0000000..d8d7a51 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForRegionURIs.java @@ -0,0 +1,50 @@ +/* + * 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.suppliers; + +import java.net.URI; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.ec2.EC2Api; +import org.jclouds.ec2.features.AvailabilityZoneAndRegionApi; +import org.jclouds.location.Region; +import org.jclouds.location.suppliers.RegionIdToURISupplier; +import org.jclouds.util.Suppliers2; + +import com.google.common.base.Supplier; +import com.google.common.collect.Maps; + +@Singleton +public class DescribeRegionsForRegionURIs implements RegionIdToURISupplier { + private final AvailabilityZoneAndRegionApi client; + + @Inject + public DescribeRegionsForRegionURIs(EC2Api client) { + this.client = client.getAvailabilityZoneAndRegionApi().get(); + } + + @Singleton + @Region + @Override + public Map<String, Supplier<URI>> get() { + Map<String, URI> regionToUris = client.describeRegions(); + return Maps.transformValues(regionToUris, Suppliers2.<URI> ofInstanceFunction()); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/IpPermissions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/IpPermissions.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/IpPermissions.java new file mode 100644 index 0000000..99e2524 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/IpPermissions.java @@ -0,0 +1,195 @@ +/* + * 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.util; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; +import java.util.Map.Entry; + +import org.jclouds.net.domain.IpPermission; +import org.jclouds.net.domain.IpProtocol; +import org.jclouds.util.Maps2; + +import com.google.common.annotations.Beta; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; + +/** + * + * Shortcut to create ingress rules + */ +public class IpPermissions extends IpPermission { + + protected IpPermissions(IpProtocol ipProtocol, int fromPort, int toPort, + Multimap<String, String> userIdGroupPairs, Iterable<String> groupIds, Iterable<String> ipRanges) { + super(ipProtocol, fromPort, toPort, userIdGroupPairs, groupIds, userIdGroupPairs.size() == 0 ? ipRanges + : ImmutableSet.<String> of()); + } + + /** + * don't rely on this being here.. it will move + */ + @Beta + public static Multimap<String, String> buildFormParametersForIndex(final int index, IpPermission permission) { + Map<String, String> headers = Maps.newLinkedHashMap(); + headers.put("IpPermissions.%d.IpProtocol", permission.getIpProtocol().toString()); + headers.put("IpPermissions.%d.FromPort", permission.getFromPort() + ""); + headers.put("IpPermissions.%d.ToPort", permission.getToPort() + ""); + String prefix = "IpPermissions.%d.IpRanges."; + int i = 0; + for (String cidrIp : checkNotNull(permission.getCidrBlocks(), "cidrIps")) { + headers.put(prefix + i++ + ".CidrIp", cidrIp); + } + prefix = "IpPermissions.%d.Groups."; + i = 0; + for (String groupId : checkNotNull(permission.getGroupIds(), "groupIds")) { + headers.put(prefix + i++ + ".GroupId", groupId); + } + prefix = "IpPermissions.%d.Groups."; + i = 0; + for (Entry<String, String> tenantIdGroupNamePair : checkNotNull(permission.getTenantIdGroupNamePairs(), + "tenantIdGroupNamePairs").entries()) { + headers.put(prefix + i + ".UserId", tenantIdGroupNamePair.getKey()); + headers.put(prefix + i + ".GroupId", tenantIdGroupNamePair.getValue()); + i++; + } + prefix = "IpPermissions.%d.IpRanges."; + i = 0; + for (String cidrIp : checkNotNull(permission.getCidrBlocks(), "cidrIps")) { + headers.put(prefix + i++ + ".CidrIp", cidrIp); + } + return Multimaps.forMap(Maps2.transformKeys(headers, new Function<String, String>() { + + @Override + public String apply(String arg0) { + return String.format(arg0, index); + } + + })); + } + + public static ICMPTypeSelection permitICMP() { + return new ICMPTypeSelection(); + } + + public static ToSourceSelection permitAnyProtocol() { + return new ToSourceSelection(IpProtocol.ALL, 1, 65535); + } + + public static PortSelection permit(IpProtocol protocol) { + return new PortSelection(checkNotNull(protocol, "protocol")); + } + + public static class ICMPTypeSelection extends ToSourceSelection { + + ICMPTypeSelection() { + super(IpProtocol.ICMP, -1, -1); + } + + /** + * @param type ex. 8 for ECHO (i.e. Ping) + * @see <a href="http://www.iana.org/assignments/icmp-parameters/icmp-parameters.xml"> ICMP Types</a> + */ + public AndCodeSelection type(int type) { + return new AndCodeSelection(type); + } + } + + public static class AndCodeSelection extends ToSourceSelection { + AndCodeSelection(int type) { + super(IpProtocol.ICMP, type, -1); + } + + public ToSourceSelection andCode(int code) { + return new ToSourceSelection(getIpProtocol(), getFromPort(), code); + } + + } + + public static class PortSelection extends ToSourceSelection { + + PortSelection(IpProtocol ipProtocol) { + super(ipProtocol, ipProtocol == IpProtocol.ICMP ? -1 : 1, ipProtocol == IpProtocol.ICMP ? -1 : 65535); + } + + public ToPortSelection fromPort(int port) { + return new ToPortSelection(getIpProtocol(), port); + } + + public ToSourceSelection port(int port) { + return new ToSourceSelection(getIpProtocol(), port, port); + } + } + + public static class ToPortSelection extends ToSourceSelection { + + ToPortSelection(IpProtocol ipProtocol, int fromPort) { + super(ipProtocol, fromPort, ipProtocol == IpProtocol.ICMP ? -1 : 65535); + } + + public ToSourceSelection to(int port) { + return new ToSourceSelection(getIpProtocol(), getFromPort(), port); + } + } + + public static class ToVPCSourceSelection extends IpPermissions { + + protected ToVPCSourceSelection(IpProtocol ipProtocol, int fromPort, int toPort) { + super(ipProtocol, fromPort, toPort, ImmutableMultimap.<String, String> of(), ImmutableSet.<String> of(), + ImmutableSet.of("0.0.0.0/0")); + } + + public IpPermissions originatingFromSecurityGroupId(String groupId) { + return toVPCSecurityGroups(ImmutableSet.of(checkNotNull(groupId, "groupId"))); + } + + public IpPermissions toVPCSecurityGroups(Iterable<String> groupIds) { + return new IpPermissions(getIpProtocol(), getFromPort(), getToPort(), getTenantIdGroupNamePairs(), groupIds, + ImmutableSet.<String> of()); + } + } + + public static class ToSourceSelection extends ToVPCSourceSelection { + ToSourceSelection(IpProtocol ipProtocol, int fromPort, int toPort) { + super(ipProtocol, fromPort, toPort); + } + + public IpPermissions originatingFromCidrBlock(String cidrIp) { + return originatingFromCidrBlocks(ImmutableSet.of(checkNotNull(cidrIp, "cidrIp"))); + } + + public IpPermissions originatingFromCidrBlocks(Iterable<String> cidrIps) { + return new IpPermissions(getIpProtocol(), getFromPort(), getToPort(), + ImmutableMultimap.<String, String> of(), ImmutableSet.<String> of(), cidrIps); + } + + public IpPermissions originatingFromUserAndSecurityGroup(String userId, String groupName) { + return toEC2SecurityGroups(ImmutableMultimap.of(checkNotNull(userId, "userId"), + checkNotNull(groupName, "groupName"))); + } + + public IpPermissions toEC2SecurityGroups(Multimap<String, String> tenantIdGroupNamePairs) { + return new IpPermissions(getIpProtocol(), getFromPort(), getToPort(), tenantIdGroupNamePairs, getGroupIds(), + ImmutableSet.<String> of()); + } + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/SubnetFilterBuilder.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/SubnetFilterBuilder.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/SubnetFilterBuilder.java new file mode 100644 index 0000000..e1a23b3 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/SubnetFilterBuilder.java @@ -0,0 +1,249 @@ +/* + * 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.util; + +import java.util.Comparator; +import java.util.Map.Entry; + +import org.jclouds.ec2.domain.Subnet; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Multimap; + +/** + * You can specify filters so that the response includes information for only + * certain subnets. For example, you can use a filter to specify that you're + * interested in the subnets in the available state. You can specify multiple + * values for a filter. The response includes information for a subnet only if + * it matches at least one of the filter values that you specified. + * + * You can specify multiple filters; for example, specify subnets that are in a + * specific VPC and are in the available state. The response includes + * information for a subnet only if it matches all the filters that you + * specified. If there's no match, no special message is returned, the response + * is simply empty. + * + * <h4>Wildcards</h4> You can use wildcards with the filter values: {@code *} + * matches zero or more characters, and ? matches exactly one character. You can + * escape special characters using a backslash before the character. For + * example, a value of {@code \*amazon\?\\} searches for the literal string + * {@code *amazon?\}. + * + * @see SubnetApi + */ +public class SubnetFilterBuilder extends ImmutableMultimap.Builder<String, String> { + + private static final String AVAILABILITY_ZONE = "availability-zone"; + private static final String AVAILABLE_IP_ADDRESS_COUNT = "available-ip-address-count"; + private static final String CIDR = "cidr"; + private static final String STATE = "state"; + private static final String SUBNET_ID = "subnet-id"; + private static final String TAG_KEY = "tag-key"; + private static final String TAG_VALUE = "tag-value"; + private static final String TAG_ARBITRARY_BASE = "tag:"; + private static final String VPC_ID = "vpc-id"; + + public SubnetFilterBuilder availabilityZone(String availabilityZone) { + return put(AVAILABILITY_ZONE, availabilityZone); + } + + public SubnetFilterBuilder availabilityZones(String... availabilityZones) { + return putAll(AVAILABILITY_ZONE, availabilityZones); + } + + public SubnetFilterBuilder availabilityZones(Iterable<String> availabilityZones) { + return putAll(AVAILABILITY_ZONE, availabilityZones); + } + + public SubnetFilterBuilder anyAvailabilityZone() { + return putAll(AVAILABILITY_ZONE, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder availableIpAddressCount(String availableIpAddressCount) { + return put(AVAILABLE_IP_ADDRESS_COUNT, availableIpAddressCount); + } + + public SubnetFilterBuilder availableIpAddressCounts(String... availableIpAddressCounts) { + return putAll(AVAILABLE_IP_ADDRESS_COUNT, availableIpAddressCounts); + } + + public SubnetFilterBuilder availableIpAddressCounts(Iterable<String> availableIpAddressCounts) { + return putAll(AVAILABLE_IP_ADDRESS_COUNT, availableIpAddressCounts); + } + + public SubnetFilterBuilder anyAvailableIpAddressCount() { + return putAll(AVAILABLE_IP_ADDRESS_COUNT, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder cidr(String cidr) { + return put(CIDR, cidr); + } + + public SubnetFilterBuilder cidrs(String... cidrs) { + return putAll(CIDR, cidrs); + } + + public SubnetFilterBuilder cidrs(Iterable<String> cidrs) { + return putAll(CIDR, cidrs); + } + + public SubnetFilterBuilder anyCidr() { + return putAll(CIDR, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder state(String state) { + return put(STATE, state); + } + + public SubnetFilterBuilder states(String... states) { + return putAll(STATE, states); + } + + public SubnetFilterBuilder states(Iterable<String> states) { + return putAll(STATE, states); + } + + public SubnetFilterBuilder anyState() { + return putAll(STATE, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder available() { + return put(STATE, Subnet.State.AVAILABLE.value()); + } + + public SubnetFilterBuilder pending() { + return put(STATE, Subnet.State.PENDING.value()); + } + + public SubnetFilterBuilder subnetId(String subnetId) { + return put(SUBNET_ID, subnetId); + } + + public SubnetFilterBuilder subnetIds(String... subnetIds) { + return putAll(SUBNET_ID, subnetIds); + } + + public SubnetFilterBuilder subnetIds(Iterable<String> subnetIds) { + return putAll(SUBNET_ID, subnetIds); + } + + public SubnetFilterBuilder anySubnetId() { + return putAll(SUBNET_ID, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder tagKey(String tagKey) { + return put(TAG_KEY, tagKey); + } + + public SubnetFilterBuilder tagKeys(String... tagKeys) { + return putAll(TAG_KEY, tagKeys); + } + + public SubnetFilterBuilder tagKeys(Iterable<String> tagKeys) { + return putAll(TAG_KEY, tagKeys); + } + + public SubnetFilterBuilder anyTagKey() { + return putAll(TAG_KEY, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder tagValue(String tagValue) { + return put(TAG_VALUE, tagValue); + } + + public SubnetFilterBuilder tagValues(String... tagValues) { + return putAll(TAG_VALUE, tagValues); + } + + public SubnetFilterBuilder tagValues(Iterable<String> tagValues) { + return putAll(TAG_VALUE, tagValues); + } + + public SubnetFilterBuilder anyTagValue() { + return putAll(TAG_VALUE, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder vpcId(String vpcId) { + return put(VPC_ID, vpcId); + } + + public SubnetFilterBuilder vpcIds(String... vpcIds) { + return putAll(VPC_ID, vpcIds); + } + + public SubnetFilterBuilder vpcIds(Iterable<String> vpcIds) { + return putAll(VPC_ID, vpcIds); + } + + public SubnetFilterBuilder anyVpcId() { + return putAll(VPC_ID, ImmutableSet.<String> of()); + } + + public SubnetFilterBuilder arbitraryTag(String arbitraryTagKey, String arbitraryTagValue) { + return put(TAG_ARBITRARY_BASE + arbitraryTagKey, arbitraryTagValue); + } + + public SubnetFilterBuilder arbitraryTag(String arbitraryTagKey, String... arbitraryTagValues) { + return putAll(TAG_ARBITRARY_BASE + arbitraryTagKey, arbitraryTagValues); + } + + public SubnetFilterBuilder arbitraryTag(String arbitraryTagKey, Iterable<String> arbitraryTagValues) { + return putAll(TAG_ARBITRARY_BASE + arbitraryTagKey, arbitraryTagValues); + } + + + // to set correct return val in chain + + @Override + public SubnetFilterBuilder put(String key, String value) { + return SubnetFilterBuilder.class.cast(super.put(key, value)); + } + + @Override + public SubnetFilterBuilder put(Entry<? extends String, ? extends String> entry) { + return SubnetFilterBuilder.class.cast(super.put(entry)); + } + + @Override + public SubnetFilterBuilder putAll(String key, Iterable<? extends String> values) { + return SubnetFilterBuilder.class.cast(super.putAll(key, values)); + } + + @Override + public SubnetFilterBuilder putAll(String key, String... values) { + return SubnetFilterBuilder.class.cast(super.putAll(key, values)); + } + + @Override + public SubnetFilterBuilder putAll(Multimap<? extends String, ? extends String> multimap) { + return SubnetFilterBuilder.class.cast(super.putAll(multimap)); + } + + @Override + @Beta + public SubnetFilterBuilder orderKeysBy(Comparator<? super String> keyComparator) { + return SubnetFilterBuilder.class.cast(super.orderKeysBy(keyComparator)); + } + + @Override + @Beta + public SubnetFilterBuilder orderValuesBy(Comparator<? super String> valueComparator) { + return SubnetFilterBuilder.class.cast(super.orderValuesBy(valueComparator)); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/TagFilterBuilder.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/TagFilterBuilder.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/TagFilterBuilder.java new file mode 100644 index 0000000..f0b5715 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/TagFilterBuilder.java @@ -0,0 +1,222 @@ +/* + * 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.util; + +import java.util.Comparator; +import java.util.Map.Entry; + +import org.jclouds.ec2.domain.Tag; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Multimap; + +/** + * You can use filters to limit the results when describing tags. For example, + * you could get only the tags for a particular resource type. You can specify + * multiple values for a filter. A tag must match at least one of the specified + * values for it to be included in the results. + * + * You can specify multiple filters (for example, limit the results to a + * specific resource type, and get only tags with values that contain the string + * database). The result includes information for a particular tag only if it + * matches all your filters. If there's no match, no special message is + * returned; the response is simply empty. + * + * <h4>Wildcards</h4> You can use wildcards with the filter values: {@code *} + * matches zero or more characters, and ? matches exactly one character. You can + * escape special characters using a backslash before the character. For + * example, a value of {@code \*amazon\?\\} searches for the literal string + * {@code *amazon?\}. + * + * @see TagApi + */ +public class TagFilterBuilder extends ImmutableMultimap.Builder<String, String> { + + private static final String KEY = "key"; + private static final String VALUE = "value"; + private static final String RESOURCE_ID = "resource-id"; + private static final String RESOURCE_TYPE = "resource-type"; + + public TagFilterBuilder key(String key) { + return put(KEY, key); + } + + public TagFilterBuilder keys(String... keys) { + return putAll(KEY, keys); + } + + public TagFilterBuilder keys(Iterable<String> keys) { + return putAll(KEY, keys); + } + + public TagFilterBuilder anyKey() { + return putAll(KEY, ImmutableSet.<String> of()); + } + + public TagFilterBuilder value(String value) { + return put(VALUE, value); + } + + public TagFilterBuilder values(String... values) { + return putAll(VALUE, ImmutableSet.<String> copyOf(values)); + } + + public TagFilterBuilder values(Iterable<String> values) { + return putAll(VALUE, ImmutableSet.<String> copyOf(values)); + } + + public TagFilterBuilder anyValue() { + return putAll(VALUE, ImmutableSet.<String> of()); + } + + public TagFilterBuilder resourceId(String resourceId) { + return put(RESOURCE_ID, resourceId); + } + + public TagFilterBuilder resourceIds(String... resourceIds) { + return putAll(RESOURCE_ID, resourceIds); + } + + public TagFilterBuilder resourceIds(Iterable<String> resourceIds) { + return putAll(RESOURCE_ID, resourceIds); + } + + public TagFilterBuilder anyResourceId() { + return putAll(RESOURCE_ID, ImmutableSet.<String> of()); + } + + public TagFilterBuilder resourceType(String resourceType) { + return put(RESOURCE_TYPE, resourceType); + } + + public TagFilterBuilder resourceTypes(String... resourceTypes) { + return putAll(RESOURCE_TYPE, resourceTypes); + } + + public TagFilterBuilder resourceTypes(Iterable<String> resourceTypes) { + return putAll(RESOURCE_TYPE, resourceTypes); + } + + public TagFilterBuilder anyResourceType() { + return putAll(RESOURCE_TYPE, ImmutableSet.<String> of()); + } + + public TagFilterBuilder customerGateway() { + return put(RESOURCE_TYPE, Tag.ResourceType.CUSTOMER_GATEWAY); + } + + public TagFilterBuilder dhcpOptions() { + return put(RESOURCE_TYPE, Tag.ResourceType.DHCP_OPTIONS); + } + + public TagFilterBuilder image() { + return put(RESOURCE_TYPE, Tag.ResourceType.IMAGE); + } + + public TagFilterBuilder instance() { + return put(RESOURCE_TYPE, Tag.ResourceType.INSTANCE); + } + + public TagFilterBuilder internetGateway() { + return put(RESOURCE_TYPE, Tag.ResourceType.INTERNET_GATEWAY); + } + + public TagFilterBuilder networkAcl() { + return put(RESOURCE_TYPE, Tag.ResourceType.NETWORK_ACL); + } + + public TagFilterBuilder reservedInstance() { + return put(RESOURCE_TYPE, Tag.ResourceType.RESERVED_INSTANCES); + } + + public TagFilterBuilder routeTable() { + return put(RESOURCE_TYPE, Tag.ResourceType.ROUTE_TABLE); + } + + public TagFilterBuilder securityGroup() { + return put(RESOURCE_TYPE, Tag.ResourceType.SECURITY_GROUP); + } + + public TagFilterBuilder snapshot() { + return put(RESOURCE_TYPE, Tag.ResourceType.SNAPSHOT); + } + + public TagFilterBuilder instancesRequest() { + return put(RESOURCE_TYPE, Tag.ResourceType.SPOT_INSTANCES_REQUEST); + } + + public TagFilterBuilder subnet() { + return put(RESOURCE_TYPE, Tag.ResourceType.SUBNET); + } + + public TagFilterBuilder volume() { + return put(RESOURCE_TYPE, Tag.ResourceType.VOLUME); + } + + public TagFilterBuilder vpc() { + return put(RESOURCE_TYPE, Tag.ResourceType.VPC); + } + + public TagFilterBuilder vpnConnection() { + return put(RESOURCE_TYPE, Tag.ResourceType.VPN_CONNECTION); + } + + public TagFilterBuilder vpnGateway() { + return put(RESOURCE_TYPE, Tag.ResourceType.VPN_GATEWAY); + } + + // to set correct return val in chain + + @Override + public TagFilterBuilder put(String key, String value) { + return TagFilterBuilder.class.cast(super.put(key, value)); + } + + @Override + public TagFilterBuilder put(Entry<? extends String, ? extends String> entry) { + return TagFilterBuilder.class.cast(super.put(entry)); + } + + @Override + public TagFilterBuilder putAll(String key, Iterable<? extends String> values) { + return TagFilterBuilder.class.cast(super.putAll(key, values)); + } + + @Override + public TagFilterBuilder putAll(String key, String... values) { + return TagFilterBuilder.class.cast(super.putAll(key, values)); + } + + @Override + public TagFilterBuilder putAll(Multimap<? extends String, ? extends String> multimap) { + return TagFilterBuilder.class.cast(super.putAll(multimap)); + } + + @Override + @Beta + public TagFilterBuilder orderKeysBy(Comparator<? super String> keyComparator) { + return TagFilterBuilder.class.cast(super.orderKeysBy(keyComparator)); + } + + @Override + @Beta + public TagFilterBuilder orderValuesBy(Comparator<? super String> valueComparator) { + return TagFilterBuilder.class.cast(super.orderValuesBy(valueComparator)); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/Tags.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/Tags.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/Tags.java new file mode 100644 index 0000000..066f92c --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/util/Tags.java @@ -0,0 +1,97 @@ +/* + * 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.util; + +import static com.google.common.collect.Multimaps.index; + +import java.util.Map; + +import org.jclouds.ec2.domain.Tag; + +import com.google.common.base.Function; +import com.google.common.collect.Maps; + +public class Tags { + private Tags() { + } + + /** + * maps the input on {@link Tag#getResourceId()} with a value of a map of its {@link Tag#getKey()} to + * {@link Tag#getValue()} + */ + public static Map<String, Map<String, String>> resourceToTagsAsMap(Iterable<Tag> tags) { + return Maps.transformValues(index(tags, resourceIdFunction()).asMap(), + new Function<Iterable<Tag>, Map<String, String>>() { + @Override + public Map<String, String> apply(Iterable<Tag> in) { + return Maps.transformValues(Maps.uniqueIndex(in, keyFunction()), valueFunction()); + } + }); + } + + public static enum ValueFunction implements Function<Tag, String> { + INSTANCE; + @Override + public String apply(Tag in) { + return in.getValue().or(""); + } + + @Override + public String toString() { + return "getValue()"; + } + } + + public static Function<Tag, String> valueFunction() { + return ValueFunction.INSTANCE; + } + + public static enum KeyFunction implements Function<Tag, String> { + INSTANCE; + @Override + public String apply(Tag in) { + return in.getKey(); + } + + @Override + public String toString() { + return "getKey()"; + } + } + + public static Function<Tag, String> keyFunction() { + return KeyFunction.INSTANCE; + } + + public static enum ResourceIdFunction implements Function<Tag, String> { + INSTANCE; + @Override + public String apply(Tag in) { + return in.getResourceId(); + } + + @Override + public String toString() { + return "getResourceId()"; + } + } + + public static Function<Tag, String> resourceIdFunction() { + return ResourceIdFunction.INSTANCE; + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AllocateAddressResponseHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AllocateAddressResponseHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AllocateAddressResponseHandler.java new file mode 100644 index 0000000..ec50f44 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AllocateAddressResponseHandler.java @@ -0,0 +1,53 @@ +/* + * 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.xml; + +import javax.annotation.Resource; + +import org.jclouds.http.functions.ParseSax.HandlerWithResult; +import org.jclouds.logging.Logger; + +public class AllocateAddressResponseHandler extends HandlerWithResult<String> { + + @Resource + protected Logger logger = Logger.NULL; + + private String ipAddress; + private StringBuilder currentText = new StringBuilder(); + + protected String currentOrNull() { + String returnVal = currentText.toString().trim(); + return returnVal.equals("") ? null : returnVal; + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("publicIp")) { + ipAddress = currentOrNull(); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } + + @Override + public String getResult() { + return ipAddress; + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java new file mode 100644 index 0000000..af532fa --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java @@ -0,0 +1,82 @@ +/* + * 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.xml; + +import java.util.Date; + +import javax.annotation.Resource; +import javax.inject.Inject; + +import org.jclouds.aws.util.AWSUtils; +import org.jclouds.date.DateCodec; +import org.jclouds.date.DateCodecFactory; +import org.jclouds.ec2.domain.Attachment; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.location.Region; +import org.jclouds.logging.Logger; + +import com.google.common.base.Supplier; + +public class AttachmentHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Attachment> { + + @Resource + protected Logger logger = Logger.NULL; + + protected final DateCodec dateCodec; + protected final Supplier<String> defaultRegion; + + @Inject + AttachmentHandler(DateCodecFactory dateCodecFactory, @Region Supplier<String> defaultRegion) { + this.dateCodec = dateCodecFactory.iso8601(); + this.defaultRegion = defaultRegion; + } + + private StringBuilder currentText = new StringBuilder(); + private String volumeId; + private String instanceId; + private String device; + private Attachment.Status attachmentStatus; + private Date attachTime; + + public Attachment getResult() { + String region = AWSUtils.findRegionInArgsOrNull(getRequest()); + if (region == null) + region = defaultRegion.get(); + return new Attachment(region, volumeId, instanceId, device, attachmentStatus, attachTime); + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("volumeId")) { + volumeId = currentText.toString().trim(); + } else if (qName.equals("volumeId")) { + volumeId = currentText.toString().trim(); + } else if (qName.equals("status")) { + attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); + } else if (qName.equals("instanceId")) { + instanceId = currentText.toString().trim(); + } else if (qName.equals("device")) { + device = currentText.toString().trim(); + } else if (qName.equals("attachTime")) { + attachTime = dateCodec.toDate(currentText.toString().trim()); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java new file mode 100644 index 0000000..969d8ac --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java @@ -0,0 +1,213 @@ +/* + * 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.xml; + +import static org.jclouds.util.SaxUtils.currentOrNull; +import static org.jclouds.util.SaxUtils.equalsOrSuffix; + +import java.util.Date; +import java.util.Set; + +import javax.inject.Inject; + +import org.jclouds.aws.util.AWSUtils; +import org.jclouds.date.DateCodec; +import org.jclouds.date.DateCodecFactory; +import org.jclouds.ec2.domain.Attachment; +import org.jclouds.ec2.domain.BlockDevice; +import org.jclouds.ec2.domain.InstanceState; +import org.jclouds.ec2.domain.Reservation; +import org.jclouds.ec2.domain.RootDeviceType; +import org.jclouds.ec2.domain.RunningInstance; +import org.jclouds.ec2.domain.RunningInstance.Builder; +import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult; +import org.jclouds.location.Region; +import org.xml.sax.Attributes; + +import com.google.common.base.Supplier; +import com.google.common.collect.Sets; + +public abstract class BaseReservationHandler<T> extends HandlerForGeneratedRequestWithResult<T> { + + protected final DateCodec dateCodec; + protected final Supplier<String> defaultRegion; + + @Inject + public BaseReservationHandler(DateCodecFactory dateCodecFactory, @Region Supplier<String> defaultRegion) { + this.dateCodec = dateCodecFactory.iso8601(); + this.defaultRegion = defaultRegion; + } + + protected Builder<?> builder = newBuilder(); + + protected Builder<?> newBuilder() { + return RunningInstance.builder(); + } + + protected void inItem() { + if (endOfInstanceItem()) { + refineBuilderBeforeAddingInstance(); + instances.add(builder.build()); + builder = newBuilder(); + } + } + + protected StringBuilder currentText = new StringBuilder(); + protected int itemDepth; + protected boolean inInstancesSet; + protected boolean inProductCodes; + protected boolean inGroupSet; + + // attachments + private String volumeId; + private Attachment.Status attachmentStatus; + private Date attachTime; + private boolean deleteOnTermination; + private String deviceName; + + // reservation stuff + private Set<String> groupNames = Sets.newLinkedHashSet(); + private String ownerId; + private String requesterId; + private String reservationId; + + private Set<RunningInstance> instances = Sets.newLinkedHashSet(); + + + public void startElement(String uri, String name, String qName, Attributes attrs) { + if (equalsOrSuffix(qName, "item")) { + itemDepth++; + } else if (equalsOrSuffix(qName, "instancesSet")) { + inInstancesSet = true; + } else if (equalsOrSuffix(qName, "productCodes")) { + inProductCodes = true; + } else if (equalsOrSuffix(qName, "groupSet")) { + inGroupSet = true; + } + } + + public void endElement(String uri, String name, String qName) { + if (equalsOrSuffix(qName, "item")) { + inItem(); + itemDepth--; + } else if (equalsOrSuffix(qName, "instancesSet")) { + inInstancesSet = false; + } else if (equalsOrSuffix(qName, "productCodes")) { + inProductCodes = false; + } else if (equalsOrSuffix(qName, "groupSet")) { + inGroupSet = false; + } else if (equalsOrSuffix(qName, "groupId")) { + groupNames.add(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "ownerId")) { + ownerId = currentOrNull(currentText); + } else if (equalsOrSuffix(qName, "requesterId")) { + requesterId = currentOrNull(currentText); + } else if (equalsOrSuffix(qName, "reservationId")) { + reservationId = currentOrNull(currentText); + } else if (equalsOrSuffix(qName, "amiLaunchIndex")) { + builder.amiLaunchIndex(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "dnsName")) { + builder.dnsName(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "imageId")) { + builder.imageId(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "instanceId")) { + builder.instanceId(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "name")) { + String rawState = currentOrNull(currentText); + if (rawState != null) { + builder.rawState(rawState); + builder.instanceState(InstanceState.fromValue(rawState)); + } + } else if (equalsOrSuffix(qName, "instanceType")) { + builder.instanceType(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "ipAddress")) { + builder.ipAddress(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "kernelId")) { + builder.kernelId(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "keyName")) { + builder.keyName(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "launchTime")) { + builder.launchTime(dateCodec.toDate(currentOrNull(currentText))); + } else if (equalsOrSuffix(qName, "availabilityZone")) { + builder.availabilityZone(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "virtualizationType")) { + builder.virtualizationType(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "platform")) { + builder.platform(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "privateDnsName")) { + builder.privateDnsName(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "privateIpAddress")) { + builder.privateIpAddress(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "ramdiskId")) { + builder.ramdiskId(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "reason")) { + builder.reason(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "rootDeviceType")) { + builder.rootDeviceType(RootDeviceType.fromValue(currentOrNull(currentText))); + } else if (equalsOrSuffix(qName, "rootDeviceName")) { + builder.rootDeviceName(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "deviceName")) { + deviceName = currentOrNull(currentText); + } else if (equalsOrSuffix(qName, "volumeId")) { + volumeId = currentOrNull(currentText); + } else if (equalsOrSuffix(qName, "status")) { + attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); + } else if (equalsOrSuffix(qName, "attachTime")) { + attachTime = dateCodec.toDate(currentOrNull(currentText)); + } else if (equalsOrSuffix(qName, "deleteOnTermination")) { + deleteOnTermination = Boolean.parseBoolean(currentText.toString().trim()); + } else if (equalsOrSuffix(qName, "ebs")) { + builder.device(deviceName, new BlockDevice(volumeId, attachmentStatus, attachTime, deleteOnTermination)); + this.deviceName = null; + this.volumeId = null; + this.attachmentStatus = null; + this.attachTime = null; + this.deleteOnTermination = true; + } + currentText = new StringBuilder(); + } + + + protected void refineBuilderBeforeAddingInstance() { + String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; + builder.region((region == null) ? defaultRegion.get() : region); + builder.groupNames(groupNames); + } + + protected boolean endOfInstanceItem() { + return itemDepth <= 2 && inInstancesSet && !inProductCodes && !inGroupSet; + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } + + protected Reservation<? extends RunningInstance> newReservation() { + String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; + if (region == null) + region = defaultRegion.get(); + Reservation<? extends RunningInstance> info = new Reservation<RunningInstance>(region, groupNames, instances, + ownerId, requesterId, reservationId); + this.groupNames = Sets.newLinkedHashSet(); + this.instances = Sets.newLinkedHashSet(); + this.ownerId = null; + this.requesterId = null; + this.reservationId = null; + return info; + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BlockDeviceMappingHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BlockDeviceMappingHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BlockDeviceMappingHandler.java new file mode 100644 index 0000000..962a53e --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BlockDeviceMappingHandler.java @@ -0,0 +1,78 @@ +/* + * 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.xml; + +import java.util.Date; +import java.util.Map; + +import org.jclouds.date.DateCodec; +import org.jclouds.date.DateCodecFactory; +import org.jclouds.ec2.domain.Attachment; +import org.jclouds.ec2.domain.BlockDevice; +import org.jclouds.http.functions.ParseSax; + +import com.google.common.collect.Maps; +import com.google.inject.Inject; + +public class BlockDeviceMappingHandler extends + ParseSax.HandlerWithResult<Map<String, BlockDevice>> { + private StringBuilder currentText = new StringBuilder(); + + private Map<String, BlockDevice> ebsBlockDevices = Maps.newHashMap(); + private String deviceName; + private String volumeId; + private boolean deleteOnTermination = true;// correct default is true. + private Attachment.Status attachmentStatus; + private Date attachTime; + + protected final DateCodec dateCodec; + + @Inject + public BlockDeviceMappingHandler(DateCodecFactory dateCodecFactory) { + this.dateCodec = dateCodecFactory.iso8601(); + } + + public Map<String, BlockDevice> getResult() { + return ebsBlockDevices; + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("deviceName")) { + deviceName = currentText.toString().trim(); + } else if (qName.equals("volumeId")) { + volumeId = currentText.toString().trim(); + } else if (qName.equals("deleteOnTermination")) { + deleteOnTermination = Boolean.parseBoolean(currentText.toString().trim()); + } else if (qName.equals("status")) { + attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); + } else if (qName.equals("attachTime")) { + attachTime = dateCodec.toDate(currentText.toString().trim()); + } else if (qName.equals("item")) { + ebsBlockDevices.put(deviceName, new BlockDevice(volumeId, attachmentStatus, attachTime, deleteOnTermination)); + this.volumeId = null; + this.deviceName = null; + this.deleteOnTermination = true; + this.attachmentStatus = null; + this.attachTime = null; + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BooleanValueHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BooleanValueHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BooleanValueHandler.java new file mode 100644 index 0000000..b043864 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BooleanValueHandler.java @@ -0,0 +1,45 @@ +/* + * 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.xml; + +import org.jclouds.http.functions.ParseSax; + +/** + * + * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-DescribeInstanceAttribute.html" + * /> + */ +public class BooleanValueHandler extends ParseSax.HandlerWithResult<Boolean> { + + private StringBuilder currentText = new StringBuilder(); + private boolean value; + + public Boolean getResult() { + return value; + } + + public void endElement(String uri, String name, String qName) { + if (qName.equalsIgnoreCase("value")) { + this.value = Boolean.parseBoolean(currentText.toString().trim()); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java new file mode 100644 index 0000000..48419af --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java @@ -0,0 +1,107 @@ +/* + * 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.xml; + +import java.util.Date; + +import javax.inject.Inject; + +import org.jclouds.aws.util.AWSUtils; +import org.jclouds.date.DateCodec; +import org.jclouds.date.DateCodecFactory; +import org.jclouds.ec2.domain.BundleTask; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.location.Region; + +import com.google.common.base.Supplier; + +public class BundleTaskHandler extends ParseSax.HandlerForGeneratedRequestWithResult<BundleTask> { + protected final DateCodec dateCodec; + protected final Supplier<String> defaultRegion; + + @Inject + protected BundleTaskHandler(DateCodecFactory dateCodecFactory, @Region Supplier<String> defaultRegion) { + this.dateCodec = dateCodecFactory.iso8601(); + this.defaultRegion = defaultRegion; + } + + private StringBuilder currentText = new StringBuilder(); + + private String bundleId; + private String code; + private String message; + private String instanceId; + private int progress = 0; + private Date startTime; + private String state; + private String bucket; + private String prefix; + private Date updateTime; + + public BundleTask getResult() { + String region = AWSUtils.findRegionInArgsOrNull(getRequest()); + if (region == null) + region = defaultRegion.get(); + BundleTask.Error error = null; + if (code != null) + error = new BundleTask.Error(code, message); + BundleTask returnVal = new BundleTask(region, bundleId, error, instanceId, progress, startTime, + state, bucket, prefix, updateTime); + this.bundleId = null; + this.code = null; + this.message = null; + this.instanceId = null; + this.progress = 0; + this.startTime = null; + this.state = null; + this.bucket = null; + this.prefix = null; + this.updateTime = null; + return returnVal; + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("bundleId")) { + bundleId = currentText.toString().trim(); + } else if (qName.equals("code")) { + code = currentText.toString().trim(); + } else if (qName.equals("message")) { + message = currentText.toString().trim(); + } else if (qName.equals("instanceId")) { + instanceId = currentText.toString().trim(); + } else if (qName.equals("progress")) { + String temp = currentText.toString().trim(); + temp = temp.substring(0, temp.length() - 1); + progress = Integer.parseInt(temp); + } else if (qName.equals("startTime")) { + startTime = dateCodec.toDate(currentText.toString().trim()); + } else if (qName.equals("state")) { + state = currentText.toString().trim(); + } else if (qName.equals("bucket")) { + bucket = currentText.toString().trim(); + } else if (qName.equals("prefix")) { + prefix = currentText.toString().trim(); + } else if (qName.equals("updateTime")) { + updateTime = dateCodec.toDate(currentText.toString().trim()); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java new file mode 100644 index 0000000..af910a7 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java @@ -0,0 +1,207 @@ +/* + * 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.xml; + +import static com.google.common.base.Preconditions.checkNotNull; + +import javax.inject.Inject; +import java.util.Date; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import org.jclouds.aws.util.AWSUtils; +import org.jclouds.date.DateCodec; +import org.jclouds.date.DateCodecFactory; +import org.jclouds.ec2.domain.Attachment; +import org.jclouds.ec2.domain.Volume; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.location.Region; +import org.jclouds.location.Zone; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.xml.sax.Attributes; + +public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Volume> { + protected final DateCodec dateCodec; + protected final Supplier<String> defaultRegion; + protected final Supplier<Map<String, Supplier<Set<String>>>> regionToZonesSupplier; + protected final Supplier<Set<String>> zonesSupplier; + + @Inject + protected CreateVolumeResponseHandler(DateCodecFactory dateCodecFactory, @Region Supplier<String> defaultRegion, + @Zone Supplier<Map<String, Supplier<Set<String>>>> regionToZonesSupplier, + @Zone Supplier<Set<String>> zonesSupplier) { + this.dateCodec = dateCodecFactory.iso8601(); + this.defaultRegion = defaultRegion; + this.regionToZonesSupplier = regionToZonesSupplier; + this.zonesSupplier = zonesSupplier; + } + + protected StringBuilder currentText = new StringBuilder(); + + protected String id; + protected int size; + protected String snapshotId; + protected String availabilityZone; + protected Volume.Status volumeStatus; + protected Date createTime; + protected Set<Attachment> attachments = Sets.newLinkedHashSet(); + + protected String volumeId; + protected String instanceId; + protected String device; + protected Attachment.Status attachmentStatus; + protected Date attachTime; + protected String volumeType; + protected Integer iops; + protected boolean encrypted; + + protected boolean inAttachmentSet; + + protected String region; + + public Volume getResult() { + return newVolume(); + } + + public void startElement(String uri, String name, String qName, Attributes attrs) { + if (qName.equals("attachmentSet")) { + inAttachmentSet = true; + } + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("volumeId")) { + if (inAttachmentSet) { + volumeId = currentText.toString().trim(); + } else { + id = currentText.toString().trim(); + } + } else if (qName.equals("size")) { + size = Integer.parseInt(currentText.toString().trim()); + } else if (qName.equals("availabilityZone")) { + availabilityZone = currentText.toString().trim(); + } else if (qName.equals("volumeId")) { + if (inAttachmentSet) { + volumeId = currentText.toString().trim(); + } else { + id = currentText.toString().trim(); + } + } else if (qName.equals("status")) { + if (inAttachmentSet) { + attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); + } else { + volumeStatus = Volume.Status.fromValue(currentText.toString().trim()); + } + } else if (qName.equals("createTime")) { + createTime = dateCodec.toDate(currentText.toString().trim()); + } else if (qName.equals("attachmentSet")) { + inAttachmentSet = false; + } else if (qName.equals("instanceId")) { + instanceId = currentText.toString().trim(); + } else if (qName.equals("snapshotId")) { + snapshotId = currentText.toString().trim(); + if (snapshotId.equals("")) + snapshotId = null; + } else if (qName.equals("device")) { + device = currentText.toString().trim(); + } else if (qName.equals("attachTime")) { + attachTime = dateCodec.toDate(currentText.toString().trim()); + } else if (qName.equals("volumeType")) { + volumeType = currentText.toString().trim(); + if (volumeType.equals("")) + volumeType = null; + } else if (qName.equals("iops")) { + iops = Integer.parseInt(currentText.toString().trim()); + } else if (qName.equals("encrypted")) { + encrypted = Boolean.parseBoolean(currentText.toString().trim()); + } else if (qName.equals("item")) { + if (inAttachmentSet) { + attachments.add(new Attachment(region, volumeId, instanceId, device, attachmentStatus, attachTime)); + volumeId = null; + instanceId = null; + device = null; + attachmentStatus = null; + attachTime = null; + } + + } + currentText = new StringBuilder(); + } + + private Volume newVolume() { + Volume volume = new Volume(region, id, size, snapshotId, availabilityZone, volumeStatus, createTime, + volumeType, iops, encrypted, attachments); + id = null; + size = 0; + snapshotId = null; + availabilityZone = null; + volumeStatus = null; + createTime = null; + attachments = Sets.newLinkedHashSet(); + volumeType = null; + iops = null; + encrypted = false; + return volume; + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } + + @Override + public CreateVolumeResponseHandler setContext(HttpRequest request) { + super.setContext(request); + region = AWSUtils.findRegionInArgsOrNull(getRequest()); + if (region == null) { + Set<String> zones = zonesSupplier.get(); + String zone = findAvailabilityZoneInArgsOrNull(getRequest(), zones); + if (zone != null) { + Map<String, Set<String>> regionToZones = Maps.transformValues(regionToZonesSupplier.get(), Suppliers + .<Set<String>> supplierFunction()); + for (Entry<String, Set<String>> entry : regionToZones.entrySet()) { + if (entry.getValue().contains(zone)) { + region = entry.getKey(); + break; + } + + } + checkNotNull(regionToZones, String.format("zone %s not in %s", zone, regionToZones)); + } else { + region = defaultRegion.get(); + } + } + return this; + } + + public static String findAvailabilityZoneInArgsOrNull(GeneratedHttpRequest gRequest, Set<String> zones) { + for (Object arg : gRequest.getInvocation().getArgs()) { + if (arg instanceof String) { + String zone = (String) arg; + if (zones.contains(zone)) + return zone; + } + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java new file mode 100644 index 0000000..dd0a1e1 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java @@ -0,0 +1,76 @@ +/* + * 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.xml; + +import java.util.Set; + +import javax.annotation.Resource; +import javax.inject.Inject; + +import org.jclouds.aws.util.AWSUtils; +import org.jclouds.ec2.domain.PublicIpInstanceIdPair; +import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult; +import org.jclouds.location.Region; +import org.jclouds.logging.Logger; + +import com.google.common.base.Supplier; +import com.google.common.collect.Sets; + +public class DescribeAddressesResponseHandler extends + HandlerForGeneratedRequestWithResult<Set<PublicIpInstanceIdPair>> { + + @Resource + protected Logger logger = Logger.NULL; + private Set<PublicIpInstanceIdPair> pairs = Sets.newLinkedHashSet(); + private String ipAddress; + private StringBuilder currentText = new StringBuilder(); + @Inject + @Region + Supplier<String> defaultRegion; + private String instanceId; + + protected String currentOrNull() { + String returnVal = currentText.toString().trim(); + return returnVal.equals("") ? null : returnVal; + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("publicIp")) { + ipAddress = currentOrNull(); + } else if (qName.equals("instanceId")) { + instanceId = currentOrNull(); + } else if (qName.equals("item")) { + String region = AWSUtils.findRegionInArgsOrNull(getRequest()); + if (region == null) + region = defaultRegion.get(); + pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId)); + ipAddress = null; + instanceId = null; + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } + + @Override + public Set<PublicIpInstanceIdPair> getResult() { + return pairs; + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/397d9926/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java new file mode 100644 index 0000000..4039794 --- /dev/null +++ b/dependencies/jclouds/apis/ec2/1.8.0-stratos/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java @@ -0,0 +1,100 @@ +/* + * 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.xml; + +import java.util.Set; + +import javax.annotation.Resource; +import javax.inject.Inject; + +import org.jclouds.ec2.domain.AvailabilityZoneInfo; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.location.Region; +import org.jclouds.logging.Logger; +import org.xml.sax.Attributes; + +import com.google.common.base.Supplier; +import com.google.common.collect.Sets; + +public class DescribeAvailabilityZonesResponseHandler extends ParseSax.HandlerWithResult<Set<AvailabilityZoneInfo>> { + private StringBuilder currentText = new StringBuilder(); + private final Supplier<String> defaultRegion; + + private Set<AvailabilityZoneInfo> availabilityZones = Sets.newLinkedHashSet(); + private String zone; + @Resource + protected Logger logger = Logger.NULL; + private String region; + private String zoneState; + private boolean inMessageSet; + private Set<String> messages = Sets.newHashSet(); + + /** + * clones like {@code openstack-nova-ec2}, which don't support multiple regions don't return region in the XML + * output. + */ + @Inject + DescribeAvailabilityZonesResponseHandler(@Region Supplier<String> defaultRegion) { + this.defaultRegion = defaultRegion; + } + + @Override + public void startDocument() { + region = defaultRegion.get(); + } + + public Set<AvailabilityZoneInfo> getResult() { + return availabilityZones; + } + + public void startElement(String uri, String name, String qName, Attributes attrs) { + if (qName.equals("messageSet")) { + inMessageSet = true; + } + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("zoneName")) { + zone = currentText.toString().trim(); + } else if (qName.equals("regionName")) { + try { + region = currentText.toString().trim(); + } catch (IllegalArgumentException e) { + logger.warn(e, "unsupported region: %s", currentText.toString().trim()); + region = "UNKNOWN"; + } + } else if (qName.equals("zoneState")) { + zoneState = currentText.toString().trim(); + } else if (qName.equals("message")) { + messages.add(currentText.toString().trim()); + } else if (qName.equals("messageSet")) { + inMessageSet = false; + } else if (qName.equals("item") && !inMessageSet) { + availabilityZones.add(new AvailabilityZoneInfo(zone, zoneState, region, messages)); + this.zone = null; + this.region = defaultRegion.get(); + this.zoneState = null; + this.messages = Sets.newHashSet(); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } + +}
