Search in sources :

Example 1 with AWSNetworkClient

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);
    }
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) Subnet(com.amazonaws.services.ec2.model.Subnet)

Example 2 with AWSNetworkClient

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);
    }
}
Also used : AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) AmazonServiceException(com.amazonaws.AmazonServiceException) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before)

Example 3 with AWSNetworkClient

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);
}
Also used : AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) TagDescription(com.amazonaws.services.ec2.model.TagDescription) Test(org.junit.Test)

Example 4 with AWSNetworkClient

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;
}
Also used : AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 5 with AWSNetworkClient

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));
}
Also used : AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Aggregations

AWSNetworkClient (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient)6 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3 AmazonServiceException (com.amazonaws.AmazonServiceException)1 IpPermission (com.amazonaws.services.ec2.model.IpPermission)1 RouteTable (com.amazonaws.services.ec2.model.RouteTable)1 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)1 Subnet (com.amazonaws.services.ec2.model.Subnet)1 TagDescription (com.amazonaws.services.ec2.model.TagDescription)1 Vpc (com.amazonaws.services.ec2.model.Vpc)1 PhotonModelMetricServices (com.vmware.photon.controller.model.PhotonModelMetricServices)1 PhotonModelServices (com.vmware.photon.controller.model.PhotonModelServices)1 SecurityGroupInstanceRequest (com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest)1 EC2_LINUX_AMI (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.EC2_LINUX_AMI)1 TestAWSSetupUtils.deleteVMsUsingEC2Client (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsUsingEC2Client)1 TestAWSSetupUtils.provisionAWSVMWithEC2Client (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.provisionAWSVMWithEC2Client)1 TestUtils.getExecutor (com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor)1 AWSSecurityGroupClient (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient)1 PhotonModelAdaptersRegistryAdapters (com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters)1 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)1 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)1