mirror of https://github.com/apache/jclouds.git
Merge pull request #750 from andrewgaul/predicates2
Introduce Predicates2.startsWith and endsWith
This commit is contained in:
commit
416a795796
|
@ -24,6 +24,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
|||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.util.Predicates2;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Function;
|
||||
|
@ -38,35 +39,16 @@ import com.google.common.base.Predicates;
|
|||
*/
|
||||
@Singleton
|
||||
public class EncodedRSAPublicKeyToBase64 implements Function<Object, String> {
|
||||
private static Predicate<Object> startsWith(String value) {
|
||||
return new ToStringStartsWith(value);
|
||||
}
|
||||
|
||||
private static final class ToStringStartsWith implements Predicate<Object> {
|
||||
private final String value;
|
||||
|
||||
private ToStringStartsWith(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(Object input) {
|
||||
return input.toString().startsWith(value);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "toStringStartsWith(" + value + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final Predicate<Object> ALLOWED_MARKERS = Predicates.or(startsWith("ssh-rsa"),
|
||||
startsWith("-----BEGIN CERTIFICATE-----"), startsWith("---- BEGIN SSH2 PUBLIC KEY ----"));
|
||||
private static final Predicate<String> ALLOWED_MARKERS = Predicates.or(
|
||||
Predicates2.startsWith("ssh-rsa"),
|
||||
Predicates2.startsWith("-----BEGIN CERTIFICATE-----"),
|
||||
Predicates2.startsWith("---- BEGIN SSH2 PUBLIC KEY ----"));
|
||||
|
||||
@Override
|
||||
public String apply(Object from) {
|
||||
checkNotNull(from, "input");
|
||||
checkArgument(ALLOWED_MARKERS.apply(from), "must be a ssh public key, conforming to %s ", ALLOWED_MARKERS);
|
||||
return CryptoStreams.base64(from.toString().getBytes(Charsets.UTF_8));
|
||||
String fromString = from.toString();
|
||||
checkArgument(ALLOWED_MARKERS.apply(fromString), "must be a ssh public key, conforming to %s ", ALLOWED_MARKERS);
|
||||
return CryptoStreams.base64(fromString.getBytes(Charsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
|||
import java.util.Set;
|
||||
|
||||
import org.jclouds.http.options.BaseHttpRequestOptions;
|
||||
import org.jclouds.util.Predicates2;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
@ -52,13 +53,7 @@ public class BaseEC2RequestOptions extends BaseHttpRequestOptions {
|
|||
|
||||
protected Set<String> getFormValuesWithKeysPrefixedBy(final String prefix) {
|
||||
Builder<String> values = ImmutableSet.builder();
|
||||
for (String key : Iterables.filter(formParameters.keySet(), new Predicate<String>() {
|
||||
|
||||
public boolean apply(String input) {
|
||||
return input.startsWith(prefix);
|
||||
}
|
||||
|
||||
})) {
|
||||
for (String key : Iterables.filter(formParameters.keySet(), Predicates2.startsWith(prefix))) {
|
||||
values.add(Iterables.get(formParameters.get(key), 0));
|
||||
}
|
||||
return values.build();
|
||||
|
|
|
@ -31,9 +31,11 @@ import javax.inject.Singleton;
|
|||
|
||||
import org.jclouds.location.Iso3166;
|
||||
import org.jclouds.location.suppliers.LocationIdToIso3166CodesSupplier;
|
||||
import org.jclouds.util.Predicates2;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
|
@ -63,14 +65,9 @@ public class LocationIdToIso3166CodesFromConfiguration implements LocationIdToIs
|
|||
@Iso3166
|
||||
@Override
|
||||
public Map<String, Supplier<Set<String>>> get() {
|
||||
Map<String, String> stringsBoundWithRegionOrZonePrefix = filterStringsBoundByName.apply(new Predicate<String>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(String input) {
|
||||
return (input.startsWith(PROPERTY_REGION) || input.startsWith(PROPERTY_ZONE));
|
||||
}
|
||||
|
||||
});
|
||||
Map<String, String> stringsBoundWithRegionOrZonePrefix = filterStringsBoundByName.apply(Predicates.<String>or(
|
||||
Predicates2.startsWith(PROPERTY_REGION),
|
||||
Predicates2.startsWith(PROPERTY_ZONE)));
|
||||
Builder<String, Supplier<Set<String>>> codes = ImmutableMap.builder();
|
||||
for (String key : ImmutableSet.of(PROPERTY_REGION, PROPERTY_ZONE)) {
|
||||
String regionOrZoneString = stringsBoundWithRegionOrZonePrefix.get(key + "s");
|
||||
|
|
|
@ -48,6 +48,7 @@ import org.jclouds.rest.internal.AsyncRestClientProxy;
|
|||
import org.jclouds.rest.internal.RestAnnotationProcessor;
|
||||
import org.jclouds.rest.internal.SeedAnnotationCache;
|
||||
import org.jclouds.util.Maps2;
|
||||
import org.jclouds.util.Predicates2;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
|
@ -111,15 +112,7 @@ public class RestModule extends AbstractModule {
|
|||
@Singleton
|
||||
@Named("TIMEOUTS")
|
||||
protected Map<String, Long> timeouts(Function<Predicate<String>, Map<String, String>> filterStringsBoundByName) {
|
||||
Map<String, String> stringBoundWithTimeoutPrefix = filterStringsBoundByName.apply(new Predicate<String>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(String input) {
|
||||
return input.startsWith(PROPERTY_TIMEOUTS_PREFIX);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Map<String, String> stringBoundWithTimeoutPrefix = filterStringsBoundByName.apply(Predicates2.startsWith(PROPERTY_TIMEOUTS_PREFIX));
|
||||
Map<String, Long> longsByName = Maps.transformValues(stringBoundWithTimeoutPrefix, new Function<String, Long>() {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds 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.util;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
public class Predicates2 {
|
||||
/** Returns a predicate that evaluates to true if the String being tested starts with a prefix. */
|
||||
public static Predicate<String> startsWith(final String prefix) {
|
||||
return new Predicate<String>() {
|
||||
@Override
|
||||
public boolean apply(final String input) {
|
||||
return input.startsWith(prefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "startsWith(" + prefix + ")";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** Returns a predicate that evaluates to true if the String being tested ends with a prefix. */
|
||||
public static Predicate<String> endsWith(final String suffix) {
|
||||
return new Predicate<String>() {
|
||||
@Override
|
||||
public boolean apply(final String input) {
|
||||
return input.endsWith(suffix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "endsWith(" + suffix + ")";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -51,10 +51,12 @@ import org.jclouds.location.Provider;
|
|||
import org.jclouds.nodepool.Backend;
|
||||
import org.jclouds.rest.annotations.ApiVersion;
|
||||
import org.jclouds.rest.annotations.BuildVersion;
|
||||
import org.jclouds.util.Predicates2;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
@ -96,14 +98,9 @@ public class BindBackendComputeService extends BindJcloudsModules {
|
|||
});
|
||||
}
|
||||
|
||||
private static final Predicate<String> keys = new Predicate<String>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(String input) {
|
||||
return !input.startsWith("jclouds.nodepool") && !input.startsWith("nodepool");
|
||||
}
|
||||
|
||||
};
|
||||
private static final Predicate<String> keys = Predicates.<String>and(
|
||||
Predicates.not(Predicates2.startsWith("jclouds.nodepool")),
|
||||
Predicates.not(Predicates2.startsWith("nodepool")));
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
|
|
|
@ -39,9 +39,11 @@ import org.jclouds.compute.functions.GroupNamingConvention;
|
|||
import org.jclouds.domain.Location;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.slicehost.domain.Slice;
|
||||
import org.jclouds.util.Predicates2;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
|
@ -109,22 +111,8 @@ public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> {
|
|||
builder.operatingSystem(parseOperatingSystem(from));
|
||||
builder.hardware(parseHardware(from));
|
||||
builder.status(sliceToNodeStatus.get(from.getStatus()));
|
||||
builder.publicAddresses(Iterables.filter(from.getAddresses(), new Predicate<String>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(String input) {
|
||||
return !input.startsWith("10.");
|
||||
}
|
||||
|
||||
}));
|
||||
builder.privateAddresses(Iterables.filter(from.getAddresses(), new Predicate<String>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(String input) {
|
||||
return input.startsWith("10.");
|
||||
}
|
||||
|
||||
}));
|
||||
builder.publicAddresses(Iterables.filter(from.getAddresses(), Predicates.not(Predicates2.startsWith("10."))));
|
||||
builder.privateAddresses(Iterables.filter(from.getAddresses(), Predicates2.startsWith("10.")));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -39,11 +39,13 @@ import org.jclouds.slicehost.domain.Image;
|
|||
import org.jclouds.slicehost.domain.Slice;
|
||||
import org.jclouds.ssh.SshClient;
|
||||
import org.jclouds.ssh.SshException;
|
||||
import org.jclouds.util.Predicates2;
|
||||
import org.testng.annotations.AfterTest;
|
||||
import org.testng.annotations.BeforeGroups;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.net.HostAndPort;
|
||||
import com.google.inject.Injector;
|
||||
|
@ -268,15 +270,7 @@ public class SlicehostClientLiveTest extends BaseComputeServiceContextLiveTest {
|
|||
}
|
||||
|
||||
private String getIp(Slice newDetails) {
|
||||
String ip = Iterables.find(newDetails.getAddresses(), new Predicate<String>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(String input) {
|
||||
return !input.startsWith("10.");
|
||||
}
|
||||
|
||||
});
|
||||
return ip;
|
||||
return Iterables.find(newDetails.getAddresses(), Predicates.not(Predicates2.startsWith("10.")));
|
||||
}
|
||||
|
||||
@Test(enabled = true, timeOut = 10 * 60 * 1000, dependsOnMethods = "testSliceDetails")
|
||||
|
|
Loading…
Reference in New Issue