use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class VSphereIOThreadPool method submit.
/**
* @see {@link #execute(URI, AuthCredentialsServiceState, ConnectionCallback)}
*
* @param sender
* @param adapterReference
* @param authLink
* where to look for the credentials
* @param callback
*/
public void submit(Service sender, URI adapterReference, String authLink, ConnectionCallback callback) {
URI authUri = createInventoryUri(this.host, authLink);
Operation op = Operation.createGet(authUri).setCompletion((o, e) -> {
if (e != null) {
ConnectionException failure = new ConnectionException("Cannot retrieve credentials from " + authLink, e);
callback.doInConnection(null, failure);
return;
}
AuthCredentialsServiceState auth = o.getBody(AuthCredentialsServiceState.class);
execute(adapterReference, auth, callback);
});
sender.sendRequest(op);
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class VSphereRegionEnumerationAdapterService method handlePost.
@Override
public void handlePost(Operation post) {
if (!post.hasBody()) {
post.fail(new IllegalArgumentException("body is required"));
return;
}
EndpointState request = post.getBody(EndpointState.class);
DeferredResult<AuthCredentialsServiceState> credentialsDr;
if (request.authCredentialsLink == null) {
credentialsDr = new DeferredResult<>();
credentialsDr.complete(new AuthCredentialsServiceState());
} else {
Operation getCredentials = Operation.createGet(createInventoryUri(this.getHost(), request.authCredentialsLink));
credentialsDr = sendWithDeferredResult(getCredentials, AuthCredentialsServiceState.class);
}
credentialsDr.whenComplete((AuthCredentialsServiceState creds, Throwable t) -> {
if (t != null) {
post.fail(t);
return;
}
VSphereIOThreadPoolAllocator.getPool(this).submit(() -> {
BasicConnection connection = new BasicConnection();
try {
EndpointAdapterUtils.Retriever retriever = EndpointAdapterUtils.Retriever.of(request.endpointProperties);
VSphereEndpointAdapterService.endpoint().accept(request, retriever);
VSphereEndpointAdapterService.credentials().accept(creds, retriever);
connection.setURI(URI.create("https://" + request.endpointProperties.get(HOST_NAME_KEY) + "/sdk"));
connection.setUsername(creds.privateKeyId);
connection.setPassword(EncryptionUtils.decrypt(creds.privateKey));
connection.setIgnoreSslErrors(true);
connection.connect();
DatacenterLister lister = new DatacenterLister(connection);
RegionEnumerationResponse res = new RegionEnumerationResponse();
res.regions = lister.listAllDatacenters().stream().map(dc -> new RegionEnumerationResponse.RegionInfo(DatacenterLister.prettifyPath(dc.path), VimUtils.convertMoRefToString(dc.object))).collect(Collectors.toList());
post.setBody(res);
post.complete();
} catch (Exception e) {
post.fail(e);
} finally {
connection.closeQuietly();
}
});
});
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class VSphereVMContext method populateVMContextThen.
/**
* Populates the given initial context and invoke the onSuccess handler when built. At every step,
* if failure occurs the VSphereVMContext's errorHandler is invoked to cleanup.
*
* @param ctx
* @param onSuccess
*/
protected static void populateVMContextThen(Service service, VSphereVMContext ctx, Consumer<VSphereVMContext> onSuccess) {
if (ctx.child == null) {
URI computeUri = UriUtils.extendUriWithQuery(ctx.resourceReference, UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
AdapterUtils.getServiceState(service, computeUri, op -> {
ctx.child = op.getBody(ComputeStateWithDescription.class);
populateVMContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
if (ctx.parent == null && ctx.child.parentLink != null) {
URI computeUri = UriUtils.extendUriWithQuery(UriUtils.buildUri(service.getHost(), ctx.child.parentLink), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
AdapterUtils.getServiceState(service, computeUri, op -> {
ctx.parent = op.getBody(ComputeStateWithDescription.class);
populateVMContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
if (ctx.parentAuth == null) {
if (IAAS_API_ENABLED) {
if (ctx.operation == null) {
ctx.fail(new IllegalArgumentException("Caller operation cannot be empty"));
return;
}
SessionUtil.retrieveExternalToken(service, ctx.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
if (throwable != null) {
ctx.errorHandler.accept(throwable);
return;
}
ctx.parentAuth = authCredentialsServiceState;
populateVMContextThen(service, ctx, onSuccess);
});
} else {
if (ctx.parent.description.authCredentialsLink == null) {
ctx.fail(new IllegalStateException("authCredentialsLink is not defined in resource " + ctx.parent.description.documentSelfLink));
return;
}
URI credUri = createInventoryUri(service.getHost(), ctx.parent.description.authCredentialsLink);
AdapterUtils.getServiceState(service, credUri, op -> {
ctx.parentAuth = op.getBody(AuthCredentialsServiceState.class);
populateVMContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
}
return;
}
// context populated, invoke handler
onSuccess.accept(ctx);
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class TestAWSEnumerationTask method initResourcePoolAndComputeHost.
/**
* Creates the state associated with the resource pool, compute host and the VM to be created.
*
* @throws Throwable
*/
private void initResourcePoolAndComputeHost() throws Throwable {
// Create a resource pool where the VM will be housed
ResourcePoolState resourcePool = createAWSResourcePool(this.host);
AuthCredentialsServiceState auth = createAWSAuthentication(this.host, this.accessKey, this.secretKey);
this.endpointState = createAWSEndpointState(this.host, auth.documentSelfLink, resourcePool.documentSelfLink);
// create a compute host for the AWS EC2 VM
this.computeHost = createAWSComputeHost(this.host, this.endpointState, null, /*zoneId*/
this.useAllRegions ? null : regionId, this.isAwsClientMock, this.awsMockEndpointReference, null);
this.endpointState.computeHostLink = this.computeHost.documentSelfLink;
this.host.waitForResponse(Operation.createPatch(this.host, this.endpointState.documentSelfLink).setBody(this.endpointState));
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class TestAWSEnumerationTask method setUp.
@Before
public void setUp() throws Throwable {
CommandLineArgumentParser.parseFromProperties(this);
setAwsClientMockInfo(this.isAwsClientMock, this.awsMockEndpointReference);
// create credentials
AuthCredentialsServiceState creds = new AuthCredentialsServiceState();
creds.privateKey = this.secretKey;
creds.privateKeyId = this.accessKey;
TestContext ec2WaitContext = new TestContext(1, Duration.ofSeconds(30L));
AWSUtils.getEc2AsyncClient(creds, TestAWSSetupUtils.regionId, getExecutor()).exceptionally(t -> {
ec2WaitContext.fail(t);
throw new CompletionException(t);
}).thenAccept(ec2Client -> {
this.client = ec2Client;
ec2WaitContext.complete();
});
ec2WaitContext.await();
TestContext s3WaitContext = new TestContext(1, Duration.ofSeconds(30L));
AWSUtils.getS3ClientAsync(creds, TestAWSSetupUtils.regionId, getExecutor()).exceptionally(t -> {
s3WaitContext.fail(t);
throw new CompletionException(t);
}).thenAccept(ec2Client -> {
this.s3Client = ec2Client;
s3WaitContext.complete();
});
s3WaitContext.await();
if (ENABLE_LOAD_BALANCER_ENUMERATION) {
TestContext lbWaitContext = new TestContext(1, Duration.ofSeconds(30L));
AWSUtils.getAwsLoadBalancingAsyncClient(creds, TestAWSSetupUtils.regionId, getExecutor()).exceptionally(t -> {
lbWaitContext.fail(t);
throw new CompletionException(t);
}).thenAccept(ec2Client -> {
this.lbClient = ec2Client;
lbWaitContext.complete();
});
lbWaitContext.await();
}
this.awsTestContext = new HashMap<>();
setUpTestVpc(this.client, this.awsTestContext, this.isMock);
this.vpcId = (String) this.awsTestContext.get(TestAWSSetupUtils.VPC_KEY);
this.subnetId = (String) this.awsTestContext.get(TestAWSSetupUtils.SUBNET_KEY);
this.securityGroupId = (String) this.awsTestContext.get(TestAWSSetupUtils.SECURITY_GROUP_KEY);
this.singleNicSpec = (AwsNicSpecs) this.awsTestContext.get(TestAWSSetupUtils.NIC_SPECS_KEY);
try {
PhotonModelServices.startServices(this.host);
PhotonModelMetricServices.startServices(this.host);
PhotonModelTaskServices.startServices(this.host);
PhotonModelAdaptersRegistryAdapters.startServices(this.host);
AWSAdaptersTestUtils.startServicesSynchronously(this.host);
this.host.setTimeoutSeconds(this.timeoutSeconds);
this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
} catch (Throwable e) {
this.host.log("Error starting up services for the test %s", e.getMessage());
throw new Exception(e);
}
// create the compute host, resource pool and the VM state to be used in the test.
initResourcePoolAndComputeHost();
}
Aggregations