Search in sources :

Example 46 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class AzureSubnetTaskServiceTest method testCreateSubnet.

@Test
public void testCreateSubnet() throws Throwable {
    SubnetState subnetState = createSubnetState(this.subnetName);
    kickOffSubnetProvision(InstanceRequestType.CREATE, subnetState, TaskStage.FINISHED);
    subnetState = getServiceSynchronously(subnetState.documentSelfLink, SubnetState.class);
    assertNotNull(subnetState.id);
    assertNotEquals(subnetState.id, this.subnetName);
    assertEquals(LifecycleState.READY, subnetState.lifecycleState);
    if (!this.isMock) {
        // Verify that the subnet was deleted.
        SubnetInner subnetResponse = this.subnetsClient.get(this.rgName, this.vNetName, this.subnetName, null);
        assertEquals(this.subnetName, subnetResponse.name());
    }
}
Also used : SubnetInner(com.microsoft.azure.management.network.implementation.SubnetInner) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 47 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class AzureSubnetTaskServiceTest method testDeleteSubnet.

@Test
public void testDeleteSubnet() throws Throwable {
    SubnetInner azureSubnet = createAzureSubnet();
    SubnetState subnetState = createSubnetState(azureSubnet.id());
    kickOffSubnetProvision(InstanceRequestType.DELETE, subnetState, TaskStage.FINISHED);
    if (!this.isMock) {
        // Verify that the subnet was deleted.
        SubnetInner subnetInner = this.subnetsClient.get(this.rgName, this.vNetName, this.subnetName, null);
        if (subnetInner != null) {
            fail("Subnet should not exist in Azure.");
        }
    }
}
Also used : SubnetInner(com.microsoft.azure.management.network.implementation.SubnetInner) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 48 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState 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)

Example 49 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method addNewInterfaceState.

/**
 * Add new interface state to compute network interface links
 * @param service
 * @param enumerationProgress Enumeration progress
 * @param state Compute state
 * @param id The id of distributed virtual port or opaque network
 * @param mode Either using distributed virtual port or opaque network
 * @param docType The document class type
 * @param type The type
 * @param ipAddress The primary ip address of this interface
 */
static <T> Operation addNewInterfaceState(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, ComputeState state, String id, InterfaceStateMode mode, Class<? extends ServiceDocument> docType, Class<T> type, String ipAddress) {
    String fieldKey;
    String fieldValue;
    switch(mode) {
        case INTERFACE_STATE_WITH_DISTRIBUTED_VIRTUAL_PORT:
            {
                fieldKey = CustomProperties.MOREF;
                fieldValue = TYPE_PORTGROUP + ":" + id;
                break;
            }
        case INTERFACE_STATE_WITH_OPAQUE_NETWORK:
            {
                fieldKey = NsxProperties.OPAQUE_NET_ID;
                fieldValue = id;
                break;
            }
        default:
            {
                service.logFine(() -> String.format("invalid mode when creating compute state with " + "network interface links: [%s]", mode));
                return null;
            }
    }
    Operation operation = queryByPortGroupIdOrByOpaqueNetworkId(service, enumerationProgress, fieldKey, fieldValue, docType).setCompletion((o, e) -> {
        if (e != null) {
            service.logWarning(() -> String.format("Error processing queryByPortGroupIdOrByOpaqueNetworkId for id: [%s]," + "error: [%s]", fieldValue, e.toString()));
            return;
        }
        QueryTask task = o.getBody(QueryTask.class);
        if (task.results != null && !task.results.documentLinks.isEmpty()) {
            T netState = VsphereEnumerationHelper.convertOnlyResultToDocument(task.results, type);
            NetworkInterfaceState iface = null;
            if (netState instanceof SubnetState) {
                SubnetState subnetState = (SubnetState) netState;
                iface = createNewInterfaceState(service, id, subnetState.networkLink, subnetState.documentSelfLink, ipAddress);
            } else if (netState instanceof NetworkState) {
                NetworkState networkState = (NetworkState) netState;
                iface = createNewInterfaceState(service, id, null, networkState.documentSelfLink, ipAddress);
            }
            if (iface != null) {
                state.networkInterfaceLinks.add(iface.documentSelfLink);
            }
        } else {
            service.logFine(() -> String.format("Will not add nic with id: [%s]", fieldValue));
        }
    });
    return operation;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Operation(com.vmware.xenon.common.Operation) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 50 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method processFoundNetwork.

public static void processFoundNetwork(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, NetworkOverlay net, MoRefKeyedMap<NetworkOverlay> allNetworks) {
    if (net.getParentSwitch() != null) {
        // portgroup: create subnet
        QueryTask task = queryForSubnet(enumerationProgress, net, net.getParentSwitch());
        withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
            if (result.documentLinks.isEmpty()) {
                createNewSubnet(service, enumerationProgress, net, allNetworks.get(net.getParentSwitch()).getDvsUuid());
            } else {
                SubnetState oldDocument = convertOnlyResultToDocument(result, SubnetState.class);
                updateSubnet(service, oldDocument, enumerationProgress, net, true);
            }
        });
    } else {
        // DVS or opaque network
        QueryTask task = queryForNetwork(enumerationProgress, net);
        withTaskResults(service, task, result -> {
            if (result.documentLinks.isEmpty()) {
                createNewNetwork(service, enumerationProgress, net);
            } else {
                NetworkState oldDocument = convertOnlyResultToDocument(result, NetworkState.class);
                updateNetwork(service, oldDocument, enumerationProgress, net);
            }
        });
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Aggregations

SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)61 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)22 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)16 Operation (com.vmware.xenon.common.Operation)14 Query (com.vmware.xenon.services.common.QueryTask.Query)12 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)11 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)11 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)11 URI (java.net.URI)11 DeferredResult (com.vmware.xenon.common.DeferredResult)10 HashMap (java.util.HashMap)10 List (java.util.List)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 StatelessService (com.vmware.xenon.common.StatelessService)9 SubnetService (com.vmware.photon.controller.model.resources.SubnetService)8 UriUtils (com.vmware.xenon.common.UriUtils)8 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)7 NetworkService (com.vmware.photon.controller.model.resources.NetworkService)7 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)7