use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class AWSClientManager method getOrCreateCloudWatchClient.
/**
* Get or create a CloudWatch Client instance that will be used to get stats from AWS.
*
* Note: ARN-based credentials will not be accepted unless they have already been exchanged to
* AWS for session credentials. If unset, this method will throw a
* {@link UnsupportedOperationException} exception in this circumstance. To enable ARN-based
* credentials, migrate to {@link #getOrCreateCloudWatchClientAsync(AuthCredentialsServiceState,
* String, StatelessService, boolean)}.
*
* @param credentials The auth credentials to be used for the client creation
* @param regionId The region of the AWS client
* @param service The stateless service for which the operation is being performed.
* @param isMock Indicates if this a mock request
* @return
*/
public AmazonCloudWatchAsyncClient getOrCreateCloudWatchClient(AuthCredentialsServiceState credentials, String regionId, StatelessService service, boolean isMock, Consumer<Throwable> failConsumer) {
if (this.awsClientType != AwsClientType.CLOUD_WATCH) {
throw new UnsupportedOperationException("This client manager supports only AWS " + this.awsClientType + " clients.");
}
if (isArnCredentials(credentials) && !isSetCredentials(credentials)) {
throw new UnsupportedOperationException("For ARN-based credentials, exchange for session-based access key/secret key first before retrieving the client.");
}
String cacheKey = createCredentialRegionCacheKey(credentials, regionId);
if (isCloudWatchClientInvalid(cacheKey)) {
failConsumer.accept(new IllegalStateException("Invalid cloud watch client for key: " + cacheKey));
return null;
}
AmazonCloudWatchAsyncClient amazonCloudWatchClient = null;
try {
amazonCloudWatchClient = this.cloudWatchClientCache.computeIfAbsent(cacheKey, key -> {
AmazonCloudWatchAsyncClient client = AWSUtils.getStatsAsyncClient(credentials, regionId, getExecutor(), isMock);
client.describeAlarmsAsync(new AsyncHandler<DescribeAlarmsRequest, DescribeAlarmsResult>() {
@Override
public void onError(Exception exception) {
markCloudWatchClientInvalid(service, cacheKey);
}
@Override
public void onSuccess(DescribeAlarmsRequest request, DescribeAlarmsResult result) {
// noop
}
});
return client;
});
} catch (Throwable e) {
service.logSevere(e);
failConsumer.accept(e);
}
return amazonCloudWatchClient;
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class AWSClientManager method getOrCreateEC2Client.
/**
* Accesses the client cache to get the EC2 client for the given auth credentials and regionId.
* If a client is not found to exist, creates a new one and adds an entry in the cache for it.
*
* Note: ARN-based credentials will not be accepted unless they have already been exchanged to
* AWS for session credentials. If unset, this method will throw a
* {@link UnsupportedOperationException} exception in this circumstance. To enable ARN-based
* credentials, migrate to {@link #getOrCreateEC2ClientAsync(AuthCredentialsServiceState,
* String, StatelessService)}.
*
* @param credentials The auth credentials to be used for the client creation
* @param regionId The region of the AWS client
* @param service The stateless service making the request and for which the executor pool needs to be allocated.
* @return The AWSClient
*/
public AmazonEC2AsyncClient getOrCreateEC2Client(AuthCredentialsServiceState credentials, String regionId, StatelessService service, Consumer<Throwable> failConsumer) {
if (this.awsClientType != AwsClientType.EC2) {
throw new UnsupportedOperationException("This client manager supports only AWS " + this.awsClientType + " clients.");
}
if (isArnCredentials(credentials) && !isSetCredentials(credentials)) {
throw new UnsupportedOperationException("For ARN-based credentials, exchange for session-based access key/secret key first before retrieving the client.");
}
AmazonEC2AsyncClient amazonEC2Client = null;
String cacheKey = createCredentialRegionCacheKey(credentials, regionId);
try {
amazonEC2Client = this.ec2ClientCache.computeIfAbsent(cacheKey, key -> AWSUtils.getAsyncClient(credentials, regionId, getExecutor()));
} catch (Throwable e) {
service.logSevere(e);
failConsumer.accept(e);
}
return amazonEC2Client;
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class AWSRemoteCleanup method setUp.
@Before
public void setUp() {
CommandLineArgumentParser.parseFromProperties(this);
this.host.setTimeoutSeconds(600);
AuthCredentialsServiceState creds = new AuthCredentialsServiceState();
creds.privateKey = this.secretKey;
creds.privateKeyId = this.accessKey;
this.vpcTagsNotToBeDeleted.add(ENUMTEST_VPC_TAG);
for (Regions region : Regions.values()) {
try {
this.s3Clients.put(region.getName(), AWSUtils.getS3Client(creds, region.getName()));
} catch (Exception e) {
continue;
}
}
for (Regions region : Regions.values()) {
try {
this.ec2Clients.put(region.getName(), TestUtils.getEC2SynchronousClient(creds, region.getName()));
} catch (Exception e) {
continue;
}
}
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class AWSResetServiceTest method setUp.
@Before
public void setUp() throws Exception {
CommandLineArgumentParser.parseFromProperties(this);
setAwsClientMockInfo(this.isAwsClientMock, this.awsMockEndpointReference);
AuthCredentialsServiceState creds = new AuthCredentialsServiceState();
creds.privateKey = this.secretKey;
creds.privateKeyId = this.accessKey;
this.client = AWSUtils.getAsyncClient(creds, TestAWSSetupUtils.regionId, getExecutor());
this.awsTestContext = new HashMap<>();
setUpTestVpc(this.client, this.awsTestContext, this.isMock);
this.singleNicSpec = (AwsNicSpecs) this.awsTestContext.get(TestAWSSetupUtils.NIC_SPECS_KEY);
this.host = VerificationHost.create(0);
try {
this.host.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros(250));
this.host.start();
PhotonModelServices.startServices(this.host);
PhotonModelAdaptersRegistryAdapters.startServices(this.host);
PhotonModelMetricServices.startServices(this.host);
PhotonModelTaskServices.startServices(this.host);
AWSAdaptersTestUtils.startServicesSynchronously(this.host);
this.host.setTimeoutSeconds(1200);
this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
this.host.waitForServiceAvailable(PhotonModelMetricServices.LINKS);
this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
} catch (Throwable e) {
throw new Exception(e);
}
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class LongRunEndToEndStatsAggregationTest 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;
this.client = AWSUtils.getAsyncClient(creds, null, getExecutor());
try {
PhotonModelServices.startServices(this.host);
PhotonModelTaskServices.startServices(this.host);
PhotonModelMetricServices.startServices(this.host);
PhotonModelInMemoryServices.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);
this.host.waitForServiceAvailable(PhotonModelMetricServices.LINKS);
this.host.waitForServiceAvailable(PhotonModelInMemoryServices.LINKS);
} catch (Throwable e) {
this.host.log("Error starting up services for the test %s", e.getMessage());
throw new Exception(e);
}
this.nodeStatsUri = UriUtils.buildUri(this.host.getUri(), ServiceUriPaths.CORE_MANAGEMENT);
this.maxMemoryInMb = this.host.getState().systemInfo.maxMemoryByteCount / BYTES_TO_MB;
// create the compute host, resource pool and the VM state to be used in the test.
initResourcePoolAndComputeHost();
}
Aggregations