How access native provider API with Jclouds 1.7 - java

Using JClouds, up to version 1.6.x it was possible to access to the native EC2 provider API by using the following idiom:
AWSEC2Client ec2Client = AWSEC2Client.class.cast(context.getProviderSpecificContext().getApi());
Actually, I copied from the documentation page: http://jclouds.apache.org/guides/aws/
It turns out that in the latest release this method has been removed. Is there an alternative method/way to access to the provider specific features (security groups, key-pairs, etc)?

Unwrapping the API from the ComputeServiceContext
ComputeServiceContext context = ContextBuilder.newBuilder("aws-ec2")
.credentials("accessKey", "secretAccessKey")
.buildView(ComputeServiceContext.class);
ComputeService computeService = context.getComputeService();
AWSEC2Api ec2Api = context.unwrapApi(AWSEC2Api.class);
Building the API directly
AWSEC2Api ec2Api = ContextBuilder.newBuilder("aws-ec2")
.credentials("accessKey", "secretAccessKey")
.buildApi(AWSEC2Api.class);

Related

How do I set min and max tasks for auto scaling policy for ecs services using the SDK?

I want to utilize the AWS SDK to set and define min/max tasks for my auto scaling policy for my ECS service.
So I'm able to successfully modify my auto scaling group policy for my ECS containers instances using code.
UpdateAutoScalingGroupRequest request = new UpdateAutoScalingGroupRequest().withAutoScalingGroupName("helloWorld-ASG").withMinSize(1);
UpdateAutoScalingGroupResult response = client.updateAutoScalingGroup(request);
UpdateScalingPlanResult scalingResponse = scalingClient.updateScalingPlan(scalingRequest);
but how do I do this for the auto scaling policy for my ECS service?
What classes do I need to do this? Is it possible?
For ECS service auto scaling look at the AWSApplicationAutoScalingClient, PutScalingPolicyRequest and PutScalingPolicyResult classes, then depending on your preferred scaling policy you will need either the StepScalingPolicyConfiguration or TargetTrackingScalingPolicyConfiguration class.
See the following example taken from the AWS Java SDK docs:
AWSApplicationAutoScaling client = AWSApplicationAutoScalingClientBuilder.standard().build();
PutScalingPolicyRequest request = new PutScalingPolicyRequest()
.withPolicyName("web-app-cpu-gt-75")
.withServiceNamespace("ecs")
.withResourceId("service/default/web-app")
.withScalableDimension("ecs:service:DesiredCount")
.withPolicyType("StepScaling")
.withStepScalingPolicyConfiguration(
new StepScalingPolicyConfiguration().withAdjustmentType("PercentChangeInCapacity")
.withStepAdjustments(new StepAdjustment().withMetricIntervalLowerBound(0d).withScalingAdjustment(200)).withCooldown(60));
PutScalingPolicyResult response = client.putScalingPolicy(request);AWSApplicationAutoScaling client = AWSApplicationAutoScalingClientBuilder.standard().build();
This answer above was for version 1.x of the SDK for version 2.x of the SDK you would need something like this one:
https://sdk.amazonaws.com/java/api/latest/index.html?software/amazon/awssdk/services/applicationautoscaling/ApplicationAutoScalingClient.html

Unable to create grpc connection in 32bit windows machine

I am trying to access the list of Subscribers in my GCP Project using my java code and few libraries provided by GCP. This code works fine in my 64 bit Windows environment but not working in 32bit windows environment.
I have seen in a few documents, saying that netty is not supported on 32bit machines and we can build our own binaries if required.
https://netty.io/wiki/forked-tomcat-native.html#how-to-build
CredentialsProvider credentialsProvider =
FixedCredentialsProvider.create(
ServiceAccountCredentials.fromStream(new FileInputStream(JSONPath)));
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(SubscriptionAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider).build())) {
ListSubscriptionsRequest listSubscriptionsRequest =
ListSubscriptionsRequest.newBuilder()
.setProject(ProjectName.of(ProjectId).toString())
.build();
SubscriptionAdminClient.ListSubscriptionsPagedResponse response =
subscriptionAdminClient.listSubscriptions(listSubscriptionsRequest);
logger.log(Level.SEVERE,"response List: "+response.toString());
Iterable<Subscription> subscriptions = response.iterateAll();
for (Subscription subscription : subscriptions) {
if(subscription.getName().equals(SubscriptionId)){
return true;
}
}
[20:02:30:384]|[06-17-2019]|[io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts]|[INFO]|[36]: netty-tcnative unavailable (this may be normal)|
java.lang.IllegalArgumentException: Failed to load any of the given libraries: [netty_tcnative_windows_x86_32, netty_tcnative_x86_32, netty_tcnative] at io.grpc.netty.shaded.io.netty.util.internal.NativeLibraryLoader.loadFirstAvailable(NativeLibraryLoader.java:104)
at io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl.loadTcNative(OpenSsl.java:526)
at io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl.(OpenSsl.java:93)
at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.defaultSslProvider(GrpcSslContexts.java:244)
at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.configure(GrpcSslContexts.java:171)
at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.forClient(GrpcSslContexts.java:120)
at io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.buildTransportFactory(NettyChannelBuilder.java:385)
at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:435)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:254)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:165)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:157)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:157)
at com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub.create(GrpcSubscriberStub.java:260)
at com.google.cloud.pubsub.v1.stub.SubscriberStubSettings.createStub(SubscriberStubSettings.java:241)
at com.google.cloud.pubsub.v1.SubscriptionAdminClient.(SubscriptionAdminClient.java:177)
at com.google.cloud.pubsub.v1.SubscriptionAdminClient.create(SubscriptionAdminClient.java:158)
The grpc-java SECURITY.md describes your options:
Use Java 9+, which support ALPN without need for tcnative
For 32 bit Windows specifically, you can Conscrypt
The documentation also describes how to use Conscrypt. Namely to add a dependency on conscrypt-openjdk-uber and to register it as the default security provider:
import org.conscrypt.Conscrypt;
import java.security.Security;
...
// Somewhere in main()
Security.insertProviderAt(Conscrypt.newProvider(), 1);
You answered your own question... netty-tcnative-* does not support 32bit platforms so you will need to compile it yourself and include it in your class-path.

How enable force global bucket access in aws s3 sdk java 2.0?

Here is a link to the documentation for java 3 sdk version 1. Does version 2.0 has something similar or they removed such option?
Yes! It is possible in AWS SDK v2 to execute S3 operations on regions other than the one configured in the client.
In order to do this, set useArnRegionEnabled to true on the client.
An example of this using Scala is:
val s3Configuration = S3Configuration.builder.useArnRegionEnabled(true).build
val client = S3Client
.builder
.credentialsProvider({$foo})
.region(Region.EU_WEST_1)
.overrideConfiguration({$foo})
.serviceConfiguration(s3Configuration)
.build
Here is the documentation: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Configuration.Builder.html#useArnRegionEnabled-java.lang.Boolean-
Not supported per here
In version 1.x, services such as Amazon S3, Amazon SNS, and Amazon SQS allowed access to resources across Region boundaries. This is no longer allowed in version 2.x using the same client. If you need to access a resource in a different region, you must create a client in that region and retrieve the resource using the appropriate client.
This works for me when using java AWS SDK 2.16.98 and only requires the name of the bucket rather than the full arn.
private S3Client defaultClient;
private S3Client bucketSpecificClient;
private String bucketName = "my-bucket-in-some-region";
// this client seems to be able to look up the location of buckets from any region
defaultClient = S3Client.builder().endpointOverride(URI.create("https://s3.us-east-1.amazonaws.com")).region(Region.US_EAST_1).build();
public S3Client getClient() {
if (bucketSpecificClient == null) {
String bucketLocation = defaultClient.getBucketLocation(builder -> builder.bucket(this.bucketName)).locationConstraintAsString();
Region region = bucketLocation.trim().equals("") ? Region.US_EAST_1 : Region.of(bucketLocation);
bucketSpecificClient = S3Client.builder().region(region).build();
}
return bucketSpecificClient;
}
Now you can use bucketSpecificClient to perform operations on objects in the bucket my-bucket-in-some-region

Reindex elasticsearch 2.3.3 when using Java NodeClient

With the deprecation of SearchTye.SCAN and the newly Reindex API we want to migrate our elasticsearch cluster and clients from 2.1.1 to 2.3.3.
We use java and the appropiate libraries to access elasticsearch. To access the cluster we use the TransportClient, for embedded Unittests we use the NodeClient.
Unfortunatly the Reindex API is provided as plugin, which the NodeClient seems to be unable to deal with.
So the question is how to use the NodeClient with the Reindex-Plugin?
I already tried exposing the protected NodeClient constructor to pass the ReindexPlugin class as argument without success.
Using the NodeClient to start an embedded ElasticSearch and using the TransportClient with the added ReindexPlugin didn't worked either. All I get here is an Exception: ActionNotFoundTransportException[No handler for action [indices:data/write/reindex]]
Dependencies of interest:
org.elasticsearch:elasticsearch:2.3.3
org.elasticsearch.module:reindex:2.3.3
org.apache.lucene:lucene-expressions:5.5.1
org.codehaus.groovy:groovy:2.4.6
Starting the NodeClient:
Settings.Builder settings = Settings.settingsBuilder();
settings.put("path.data", "/some/path/data");
settings.put("path.home", "/some/path/home");
//settings.put("plugin.types", ReindexPlugin.class.getName()); > No effect
settings.put("http.port", 9299);
settings.put("transport.tcp.port", 9399);
node = NodeBuilder.nodeBuilder()
.clusterName("testcluster")
.settings(settings)
.local(true)
.node();
// also tested with local(false), then no transport port is available, resulting in NoNodeAvailableException
Using TransportClient to access the Node:
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "testcluster")
.put("discovery.zen.ping.multicast.enabled", false)
.build();
InetSocketTransportAddress[] addresses = new InetSocketTransportAddress[]
{new InetSocketTransportAddress(new InetSocketAddress("localhost", 9399))};
client = TransportClient.builder()
.settings(settings)
.addPlugin(ReindexPlugin.class)
.build()
.addTransportAddresses(addresses);
Main part of triggering reindex:
ReindexRequestBuilder builder = ReindexAction.INSTANCE.newRequestBuilder(getClient())
.source(indexFrom)
.destination(indexTo)
.refresh(true);
I was able to solve this, by combining both approaches described above.
So creating the NodeClient involves overriding the Node:
class ExposedNode extends Node {
public ExposedNode(Environment tmpEnv, Version version, Collection<Class<? extends Plugin>> classpathPlugins) {
super(tmpEnv, version, classpathPlugins);
}
}
And using it when starting the NodeClient:
Settings.Builder settings = Settings.settingsBuilder();
settings.put("path.data", "/some/path/data");
settings.put("path.home", "/some/path/home");
settings.put("http.port", 9299);
settings.put("transport.tcp.port", 9399);
// Construct Node without NodeBuilder
List<Class<? extends Plugin>> classpathPlugins = ImmutableList.of(ReindexPlugin.class);
settings.put("node.local", false);
settings.put("cluster.name", "testcluster");
Settings preparedSettings = settings.build();
node = new ExposedNode(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), Version.CURRENT, classpathPlugins);
node.start();
After that you can use the TransportClient which adds the ReindexPlugin, as described in the question.
Nevertheless this is a dirty hack, which may break in a future release, and shows how poorly Elasticsearch supports plugin development imo.

Check-and-set in Couchbase Java SDK 2.x?

In older version of Couchbase Java SDK there were several check-and-set (CAS) methods for implementing optimistic locking. But what is corresponding API in a newer version of SDK (>= 2.0)?
Initial code:
JsonDocument doc = bucket.get("myKey");
Long casValue = doc.cas();
// some method to set new value for "myKey" only if CAS value
// has not been changed
As you already saw, the CAS value is embedded in the document. Most methods in the API will take that into account if the CAS is not 0, for example replace(Document) will throw a CASMismatchException if the given document has a different CAS value than what is on the server.

Categories

Resources