use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient in project photon-model by vmware.
the class AWSNetworkService method handleStages.
private void handleStages(AWSNetworkContext context) {
try {
switch(context.stage) {
case NETWORK_TASK_STATE:
getNetworkTaskState(context, AWSNetworkStage.NETWORK_STATE);
break;
case NETWORK_STATE:
getNetworkState(context, AWSNetworkStage.CREDENTIALS);
break;
case CREDENTIALS:
getCredentials(context, AWSNetworkStage.AWS_CLIENT);
break;
case AWS_CLIENT:
this.clientManager.getOrCreateEC2ClientAsync(context.credentials, context.network.regionId, this).whenComplete((ec2Client, t) -> {
if (t != null) {
context.stage = AWSNetworkStage.FAILED;
context.error = t;
handleStages(context);
return;
}
context.client = new AWSNetworkClient(ec2Client);
if (context.networkRequest.requestType == NetworkInstanceRequest.InstanceRequestType.CREATE) {
context.stage = AWSNetworkStage.PROVISION_VPC;
} else {
context.stage = AWSNetworkStage.REMOVE_GATEWAY;
}
handleStages(context);
});
break;
case PROVISION_VPC:
String vpcID = context.client.createVPC(context.network.subnetCIDR);
updateNetworkProperties(AWS_VPC_ID, vpcID, context, AWSNetworkStage.PROVISION_SUBNET);
break;
case PROVISION_SUBNET:
Subnet subnet = context.client.createSubnet(context.network.subnetCIDR, getCustomProperty(context, AWS_VPC_ID));
createSubnetState(subnet, context, AWSNetworkStage.PROVISION_GATEWAY);
break;
case PROVISION_GATEWAY:
String gatewayID = context.client.createInternetGateway();
context.client.attachInternetGateway(getCustomProperty(context, AWS_VPC_ID), gatewayID);
updateNetworkProperties(AWS_GATEWAY_ID, gatewayID, context, AWSNetworkStage.PROVISION_ROUTE);
break;
case PROVISION_ROUTE:
RouteTable routeTable = context.client.getMainRouteTable(context.network.customProperties.get(AWS_VPC_ID));
context.client.createInternetRoute(getCustomProperty(context, AWS_GATEWAY_ID), routeTable.getRouteTableId(), ROUTE_DEST_ALL);
updateNetworkProperties(AWS_VPC_ROUTE_TABLE_ID, routeTable.getRouteTableId(), context, AWSNetworkStage.FINISHED);
break;
case REMOVE_GATEWAY:
context.client.detachInternetGateway(getCustomProperty(context, AWS_VPC_ID), getCustomProperty(context, AWS_GATEWAY_ID));
context.client.deleteInternetGateway(getCustomProperty(context, AWS_GATEWAY_ID));
updateNetworkProperties(AWS_GATEWAY_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.REMOVE_SUBNET);
break;
case REMOVE_SUBNET:
// Iterate SubnetStates (page-by-page) and delete AWS Subnet and SubnetState
deleteSubnetStates(context, AWSNetworkStage.REMOVE_ROUTE);
break;
case REMOVE_ROUTE:
// only need to update the document, the AWS artifact will be
// removed on VPC removal
updateNetworkProperties(AWS_VPC_ROUTE_TABLE_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.REMOVE_VPC);
break;
case REMOVE_VPC:
context.client.deleteVPC(getCustomProperty(context, AWS_VPC_ID));
updateNetworkProperties(AWS_VPC_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.FINISHED);
break;
case FAILED:
context.taskManager.patchTaskToFailure(context.error);
break;
case FINISHED:
context.taskManager.finishTask();
break;
default:
break;
}
} catch (Throwable error) {
// Same as FAILED stage
context.taskManager.patchTaskToFailure(error);
}
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient in project photon-model by vmware.
the class TestAWSNetworkService method setUp.
@Before
public void setUp() throws Exception {
CommandLineArgumentParser.parseFromProperties(this);
// ignore if any of the required properties are missing
org.junit.Assume.assumeTrue(TestUtils.isNull(this.secretKey, this.accessKey));
this.host = VerificationHost.create(0);
try {
this.host.start();
PhotonModelServices.startServices(this.host);
PhotonModelMetricServices.startServices(this.host);
PhotonModelTaskServices.startServices(this.host);
AWSNetworkService netSvc = new AWSNetworkService();
this.host.startService(Operation.createPost(UriUtils.buildUri(this.host, AWSNetworkService.class)), netSvc);
this.netClient = new AWSNetworkClient(TestUtils.getClient(this.accessKey, this.secretKey, regionId, false));
} catch (Throwable e) {
throw new Exception(e);
}
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient in project photon-model by vmware.
the class TestAWSUtils method testResourceNaming.
@Test
public void testResourceNaming() throws Throwable {
boolean tagFound = false;
AmazonEC2AsyncClient client = TestUtils.getClient(this.privateKeyId, this.privateKey, this.region, false);
// create something to name
AWSNetworkClient svc = new AWSNetworkClient(client);
String vpcID = svc.createVPC("10.20.0.0/16");
AWSUtils.tagResourcesWithName(client, TEST_NAME, vpcID);
List<TagDescription> tags = AWSUtils.getResourceTags(vpcID, client);
for (TagDescription tagDesc : tags) {
if (tagDesc.getKey().equalsIgnoreCase(AWS_TAG_NAME)) {
assertTrue(tagDesc.getValue().equalsIgnoreCase(TEST_NAME));
tagFound = true;
break;
}
}
// ensure we found the tag
assertTrue(tagFound);
svc.deleteVPC(vpcID);
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient in project photon-model by vmware.
the class AWSSubnetService method getAWSClient.
private DeferredResult<AWSSubnetContext> getAWSClient(AWSSubnetContext context) {
if (context.request.isMockRequest) {
return DeferredResult.completed(context);
}
DeferredResult<AWSSubnetContext> r = new DeferredResult<>();
this.clientManager.getOrCreateEC2ClientAsync(context.credentials, context.parentNetwork.regionId, this).whenComplete((client, t) -> {
if (t != null) {
r.fail(t);
return;
}
context.client = new AWSNetworkClient(this, client);
r.complete(context);
});
return r;
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient in project photon-model by vmware.
the class TestProvisionAWSNetwork method validateAWSArtifacts.
private void validateAWSArtifacts(String networkDescriptionLink, AuthCredentialsServiceState creds) throws Throwable {
NetworkState net = getNetworkState(networkDescriptionLink);
AmazonEC2AsyncClient client = AWSUtils.getAsyncClient(creds, regionId, getExecutor());
AWSNetworkClient netSVC = new AWSNetworkClient(client);
// if any artifact is not present then an error will be thrown
assertNotNull(netSVC.getVPC(net.customProperties.get(AWS_VPC_ID)));
assertNotNull(netSVC.getInternetGateway(net.customProperties.get(AWS_GATEWAY_ID)));
List<SubnetState> subnetStates = TestUtils.getSubnetStates(this.host, net);
assertEquals(1, subnetStates.size());
assertNotNull(netSVC.getSubnet(subnetStates.get(0).id));
}
Aggregations