Search in sources :

Example 11 with NetworkInterfaceDescription

use of com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription in project photon-model by vmware.

the class AzureTestUtil method createDefaultNicStates.

/*
     * NOTE: It is highly recommended to keep this method in sync with its AWS counterpart:
     * TestAWSSetupUtils.createAWSNicStates
     */
public static List<NetworkInterfaceState> createDefaultNicStates(VerificationHost host, ComputeState computeHost, EndpointState endpointState, Set<String> networkRGLinks, Set<String> sgRGLinks, AzureNicSpecs azureNicSpecs, String azureVMName) throws Throwable {
    // Create network state.
    NetworkState networkState;
    {
        networkState = new NetworkState();
        networkState.id = azureNicSpecs.network.name;
        networkState.name = azureNicSpecs.network.name;
        networkState.subnetCIDR = azureNicSpecs.network.cidr;
        networkState.authCredentialsLink = endpointState.authCredentialsLink;
        networkState.endpointLink = endpointState.documentSelfLink;
        networkState.endpointLinks = new HashSet<>();
        networkState.endpointLinks.add(endpointState.documentSelfLink);
        networkState.tenantLinks = endpointState.tenantLinks;
        networkState.resourcePoolLink = computeHost.resourcePoolLink;
        networkState.groupLinks = networkRGLinks;
        networkState.regionId = azureNicSpecs.network.zoneId;
        networkState.instanceAdapterReference = UriUtils.buildUri(host, DEFAULT_INSTANCE_ADAPTER_REFERENCE);
        networkState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_vnet.name());
        networkState.computeHostLink = endpointState.computeHostLink;
        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 < azureNicSpecs.nicSpecs.size(); i++) {
        NicSpec nicSpec = azureNicSpecs.nicSpecs.get(i);
        // Create subnet state per NIC.
        SubnetState subnetState;
        {
            subnetState = new SubnetState();
            subnetState.id = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.name = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.subnetCIDR = azureNicSpecs.nicSpecs.get(i).subnetSpec.cidr;
            subnetState.zoneId = azureNicSpecs.nicSpecs.get(i).subnetSpec.zoneId;
            subnetState.networkLink = networkState.documentSelfLink;
            subnetState.endpointLink = endpointState.documentSelfLink;
            subnetState.endpointLinks = new HashSet<>();
            subnetState.endpointLinks.add(endpointState.documentSelfLink);
            subnetState.computeHostLink = endpointState.computeHostLink;
            subnetState.tenantLinks = endpointState.tenantLinks;
            subnetState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_subnet.name());
            subnetState = TestUtils.doPost(host, subnetState, SubnetState.class, UriUtils.buildUri(host, SubnetService.FACTORY_LINK));
        }
        // Create security group state
        SecurityGroupState securityGroupState;
        {
            securityGroupState = new SecurityGroupState();
            securityGroupState.name = AZURE_SECURITY_GROUP_NAME + "-" + azureVMName;
            securityGroupState.authCredentialsLink = endpointState.authCredentialsLink;
            securityGroupState.endpointLink = endpointState.documentSelfLink;
            securityGroupState.endpointLinks = new HashSet<>();
            securityGroupState.endpointLinks.add(endpointState.documentSelfLink);
            securityGroupState.computeHostLink = endpointState.computeHostLink;
            securityGroupState.tenantLinks = endpointState.tenantLinks;
            securityGroupState.groupLinks = sgRGLinks;
            securityGroupState.regionId = "regionId";
            securityGroupState.resourcePoolLink = "/link/to/rp";
            securityGroupState.instanceAdapterReference = new URI("http://instanceAdapterReference");
            {
                Rule ssh = new Rule();
                ssh.name = "ssh-in";
                ssh.protocol = "tcp";
                ssh.ipRangeCidr = "0.0.0.0/0";
                ssh.ports = "22";
                securityGroupState.ingress = Collections.singletonList(ssh);
            }
            {
                Rule out = new Rule();
                out.name = "out";
                out.protocol = "tcp";
                out.ipRangeCidr = "0.0.0.0/0";
                out.ports = SecurityGroupService.ALL_PORTS;
                securityGroupState.egress = Collections.singletonList(out);
            }
            securityGroupState = TestUtils.doPost(host, securityGroupState, SecurityGroupState.class, UriUtils.buildUri(host, SecurityGroupService.FACTORY_LINK));
        }
        // Create NIC description.
        NetworkInterfaceDescription nicDescription;
        {
            nicDescription = new NetworkInterfaceDescription();
            nicDescription.id = "nicDesc" + i;
            nicDescription.name = generateName("nicDesc" + i);
            nicDescription.deviceIndex = i;
            nicDescription.assignPublicIpAddress = azureNicSpecs.assignPublicIpAddress;
            nicDescription.tenantLinks = endpointState.tenantLinks;
            nicDescription.endpointLink = endpointState.documentSelfLink;
            nicDescription.endpointLinks = new HashSet<>();
            nicDescription.endpointLinks.add(endpointState.documentSelfLink);
            nicDescription.computeHostLink = endpointState.computeHostLink;
            nicDescription.assignment = nicSpec.getIpAssignment();
            // if staticIp is null, it will be assigned automatically by DHCP.
            nicDescription.address = nicSpec.ip();
            nicDescription = TestUtils.doPost(host, nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(host, NetworkInterfaceDescriptionService.FACTORY_LINK));
        }
        NetworkInterfaceState nicState = new NetworkInterfaceState();
        nicState.id = "nic" + i;
        nicState.name = generateName("nic" + i);
        nicState.deviceIndex = nicDescription.deviceIndex;
        nicState.networkInterfaceDescriptionLink = nicDescription.documentSelfLink;
        nicState.subnetLink = subnetState.documentSelfLink;
        nicState.networkLink = subnetState.networkLink;
        nicState.tenantLinks = endpointState.tenantLinks;
        nicState.endpointLink = endpointState.documentSelfLink;
        nicState.endpointLinks = new HashSet<>();
        nicState.endpointLinks.add(endpointState.documentSelfLink);
        nicState.computeHostLink = endpointState.computeHostLink;
        if (nicSpec.getIpAssignment() == IpAssignment.STATIC) {
            // There is a rule in:
            // \photon-model\photon-model\src\main\java\com\vmware\photon\controller\model\resources\NetworkInterfaceService.java::validateState()
            // // which will throws java.lang.IllegalArgumentException: both networkLink and IP
            // cannot be set
            nicState.networkLink = null;
        }
        if (i == 0) {
            // Attach security group only on the primary nic.
            nicState.securityGroupLinks = Collections.singletonList(securityGroupState.documentSelfLink);
        }
        nicState.tagLinks = Collections.singleton(TagsUtil.newTagState(TAG_KEY_TYPE, azure_net_interface.name(), false, endpointState.tenantLinks).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) URI(java.net.URI) NicSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) HashSet(java.util.HashSet)

Aggregations

NetworkInterfaceDescription (com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription)11 ArrayList (java.util.ArrayList)7 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)5 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)4 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)3 Operation (com.vmware.xenon.common.Operation)3 NetworkInterfaceStateWithDetails (com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails)2 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 URI (java.net.URI)2 Test (org.junit.Test)2 SubResource (com.microsoft.azure.SubResource)1 IPAllocationMethod (com.microsoft.azure.management.network.IPAllocationMethod)1 NetworkInterfaceIPConfigurationInner (com.microsoft.azure.management.network.implementation.NetworkInterfaceIPConfigurationInner)1 NetworkInterfaceInner (com.microsoft.azure.management.network.implementation.NetworkInterfaceInner)1 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)1 IAAS_API_ENABLED (com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED)1 ComputeInstanceRequest (com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest)1 InstanceRequestType (com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest.InstanceRequestType)1 ResourceRequest (com.vmware.photon.controller.model.adapterapi.ResourceRequest)1