Search in sources :

Example 1 with NicSpec

use of com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec in project photon-model by vmware.

the class TestAWSSetupUtils method setUpTestVpc.

public static void setUpTestVpc(AmazonEC2AsyncClient client, Map<String, Object> awsTestContext, boolean isMock, String zoneId) {
    // If the pre-set VPC does not exist, get the test VPC for the given account and use it in the tests.
    if (!isMock && !vpcIdExists(client, AWS_DEFAULT_VPC_ID)) {
        String vpcId = createorGetVPCForAccount(client);
        awsTestContext.put(VPC_KEY, vpcId);
        Subnet subnet = createOrGetSubnet(client, AWS_DEFAULT_SUBNET_CIDR, vpcId, zoneId);
        awsTestContext.put(SUBNET_KEY, subnet.getSubnetId());
        String internetGatewayId = createOrGetInternetGatewayForGivenVPC(client, vpcId);
        awsTestContext.put(INTERNET_GATEWAY_KEY, internetGatewayId);
        SecurityGroup sg = createOrGetDefaultSecurityGroupForGivenVPC(client, vpcId);
        awsTestContext.put(SECURITY_GROUP_KEY, sg.getGroupId());
        awsTestContext.put(SECURITY_GROUP_NAME_KEY, sg.getGroupName());
        NetSpec network = new NetSpec(vpcId, vpcId, AWS_DEFAULT_VPC_CIDR);
        List<NetSpec> subnets = new ArrayList<>();
        subnets.add(new NetSpec(subnet.getSubnetId(), AWS_DEFAULT_SUBNET_NAME, subnet.getCidrBlock(), zoneId == null ? TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier : zoneId));
        NicSpec nicSpec = NicSpec.create().withSubnetSpec(subnets.get(0)).withDynamicIpAssignment();
        awsTestContext.put(NIC_SPECS_KEY, new AwsNicSpecs(network, Collections.singletonList(nicSpec)));
        return;
    }
    awsTestContext.put(VPC_KEY, AWS_DEFAULT_VPC_ID);
    awsTestContext.put(NIC_SPECS_KEY, SINGLE_NIC_SPEC);
    awsTestContext.put(SUBNET_KEY, AWS_DEFAULT_SUBNET_ID);
    awsTestContext.put(SECURITY_GROUP_KEY, AWS_DEFAULT_GROUP_ID);
    awsTestContext.put(SECURITY_GROUP_NAME_KEY, AWS_DEFAULT_GROUP_NAME);
}
Also used : NetSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NetSpec) ArrayList(java.util.ArrayList) Subnet(com.amazonaws.services.ec2.model.Subnet) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) NicSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec)

Example 2 with NicSpec

use of com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec in project photon-model by vmware.

the class TestAWSSetupUtils method createAWSNicStates.

/*
     * NOTE: It is highly recommended to keep this method in sync with its Azure counterpart:
     * AzureTestUtil.createDefaultNicStates
     */
public static List<NetworkInterfaceState> createAWSNicStates(VerificationHost host, ComputeState computeHost, EndpointState endpointState, String vmName, AwsNicSpecs nicSpecs, boolean addNewSecurityGroup, Map<String, Object> awsTestContext) throws Throwable {
    // Create network state.
    NetworkState networkState;
    {
        networkState = new NetworkState();
        networkState.id = nicSpecs.network.id;
        networkState.name = nicSpecs.network.name;
        networkState.subnetCIDR = nicSpecs.network.cidr;
        networkState.authCredentialsLink = endpointState.authCredentialsLink;
        networkState.resourcePoolLink = computeHost.resourcePoolLink;
        networkState.instanceAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_NETWORK_ADAPTER);
        networkState.regionId = regionId;
        networkState.endpointLink = endpointState.documentSelfLink;
        networkState.endpointLinks = new HashSet<String>();
        networkState.endpointLinks.add(endpointState.documentSelfLink);
        networkState.tenantLinks = endpointState.tenantLinks;
        networkState.computeHostLink = computeHost.documentSelfLink;
        networkState = TestUtils.doPost(host, networkState, NetworkState.class, UriUtils.buildUri(host, NetworkService.FACTORY_LINK));
    }
    // Create NIC states.
    List<NetworkInterfaceState> nics = new ArrayList<>();
    for (int i = 0; i < nicSpecs.nicSpecs.size(); i++) {
        // Create subnet state per NIC.
        SubnetState subnetState;
        {
            subnetState = new SubnetState();
            subnetState.id = nicSpecs.nicSpecs.get(i).subnetSpec.id;
            subnetState.name = nicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.subnetCIDR = nicSpecs.nicSpecs.get(i).subnetSpec.cidr;
            subnetState.zoneId = nicSpecs.nicSpecs.get(i).subnetSpec.zoneId;
            subnetState.networkLink = networkState.documentSelfLink;
            subnetState.regionId = regionId;
            subnetState.endpointLink = endpointState.documentSelfLink;
            subnetState.endpointLinks = new HashSet<String>();
            subnetState.endpointLinks.add(endpointState.documentSelfLink);
            subnetState.tenantLinks = endpointState.tenantLinks;
            subnetState = TestUtils.doPost(host, subnetState, SubnetState.class, UriUtils.buildUri(host, SubnetService.FACTORY_LINK));
        }
        // Create NIC description.
        NetworkInterfaceDescription nicDescription;
        NicSpec nicSpec = nicSpecs.nicSpecs.get(0);
        {
            nicDescription = new NetworkInterfaceDescription();
            nicDescription.id = "nicDesc" + i;
            nicDescription.name = "nicDesc" + i;
            nicDescription.deviceIndex = i;
            nicDescription.assignment = nicSpec.getIpAssignment();
            nicDescription.regionId = regionId;
            nicDescription.endpointLink = endpointState.documentSelfLink;
            nicDescription.endpointLinks = new HashSet<String>();
            nicDescription.endpointLinks.add(endpointState.documentSelfLink);
            nicDescription.tenantLinks = endpointState.tenantLinks;
            nicDescription = TestUtils.doPost(host, nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(host, NetworkInterfaceDescriptionService.FACTORY_LINK));
        }
        // Create security group state for an existing security group
        SecurityGroupState existingSecurityGroupState = createSecurityGroupState(host, computeHost, endpointState, true, awsTestContext);
        NetworkInterfaceState nicState = new NetworkInterfaceState();
        nicState.id = UUID.randomUUID().toString();
        nicState.name = vmName + "-nic-" + i;
        nicState.deviceIndex = nicDescription.deviceIndex;
        nicState.networkLink = networkState.documentSelfLink;
        nicState.subnetLink = subnetState.documentSelfLink;
        nicState.networkInterfaceDescriptionLink = nicDescription.documentSelfLink;
        nicState.regionId = regionId;
        nicState.endpointLink = endpointState.documentSelfLink;
        nicState.endpointLinks = new HashSet<String>();
        nicState.endpointLinks.add(endpointState.documentSelfLink);
        nicState.tenantLinks = endpointState.tenantLinks;
        nicState.securityGroupLinks = new ArrayList<>();
        nicState.securityGroupLinks.add(existingSecurityGroupState.documentSelfLink);
        if (addNewSecurityGroup) {
            // Create security group state for a new security group
            SecurityGroupState newSecurityGroupState = createSecurityGroupState(host, computeHost, endpointState, false, awsTestContext);
            nicState.securityGroupLinks.add(newSecurityGroupState.documentSelfLink);
        }
        nicState = TestUtils.doPost(host, nicState, NetworkInterfaceState.class, UriUtils.buildUri(host, NetworkInterfaceService.FACTORY_LINK));
        nics.add(nicState);
    }
    return nics;
}
Also used : NetworkInterfaceDescription(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ArrayList(java.util.ArrayList) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) NicSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) HashSet(java.util.HashSet)

Example 3 with NicSpec

use of com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec in project photon-model by vmware.

the class TestAWSSetupUtils method multiNicSpecs.

/**
 * Return two-NIC spec where first NIC should be assigned to 'secondary' subnet and second NIC
 * should be assigned to a randomly generated subnet that should be created.
 * <p>
 * For a matrix of maximum supported NICs per Instance type check:
 * http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html#AvailableIpPerENI
 *
 * @see {@value #instanceType} instance type
 */
public static AwsNicSpecs multiNicSpecs() throws UnknownHostException {
    String nextSubnetIp;
    {
        // 172.31.1000 0000.0000 0000
        final String startingSubnetIp = "172.31.128.0";
        Random r = new Random();
        // Convert IP as String -to-> byte[4] -to-> IP as integer
        int startingIpAsInt = ByteBuffer.wrap(InetAddress.getByName(startingSubnetIp).getAddress()).getInt();
        /*
             * The random subnet generated is between 172.31.1[000 0000.0000] 0000 and 172.31.1[111
             * 1111.1111] 0000. The last 4 zero bits are for the IPs within the subnet generated.
             */
        int nextSubnetIpAsInt = startingIpAsInt | (r.nextInt(1 << 11) << 4);
        // Convert IP as integer -to-> byte[4] -to-> IP as String
        nextSubnetIp = InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(nextSubnetIpAsInt).array()).getHostAddress();
    }
    NetSpec network = new NetSpec(AWS_DEFAULT_VPC_ID, AWS_DEFAULT_VPC_ID, AWS_DEFAULT_VPC_CIDR);
    // Configure with TWO NICS.
    List<NetSpec> subnets = new ArrayList<>();
    // 'secondary' subnet which is existing
    subnets.add(new NetSpec(AWS_SECONDARY_SUBNET_ID, AWS_SECONDARY_SUBNET_NAME, AWS_SECONDARY_SUBNET_CIDR, zoneId + avalabilityZoneIdentifier));
    // Random generated subnet with /28 mask to be created
    subnets.add(new NetSpec(null, "third", nextSubnetIp + "/28", zoneId + avalabilityZoneIdentifier));
    NicSpec nicSpec = NicSpec.create().withSubnetSpec(subnets.get(0)).withStaticIpAssignment();
    return new AwsNicSpecs(network, Collections.singletonList(nicSpec));
}
Also used : NetSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NetSpec) Random(java.util.Random) ArrayList(java.util.ArrayList) NicSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec)

Aggregations

NicSpec (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec)3 ArrayList (java.util.ArrayList)3 NetSpec (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NetSpec)2 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)1 Subnet (com.amazonaws.services.ec2.model.Subnet)1 NetworkInterfaceDescription (com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription)1 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)1 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)1 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)1 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1