Search in sources :

Example 1 with NicSpec

use of com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec in project photon-model by vmware.

the class TestAzureEnumerationTask method assertRemoteResources.

// Assert that remote resources are enumerated and exists locally.
private void assertRemoteResources() {
    assertResourceExists(this.host, NetworkService.FACTORY_LINK, NIC_SPEC.network.name, true);
    for (NicSpec nicSpec : NIC_SPEC.nicSpecs) {
        assertResourceExists(this.host, SubnetService.FACTORY_LINK, nicSpec.getSubnetSpec().name, true);
    }
    assertResourceExists(this.host, ResourceGroupService.FACTORY_LINK, azureVMName, true);
    assertResourceExists(this.host, SecurityGroupService.FACTORY_LINK, AZURE_SECURITY_GROUP_NAME + "-" + azureVMName, true);
    assertResourceExists(this.host, StorageDescriptionService.FACTORY_LINK, (azureVMName + "sa").replace("-", ""), true);
    assertDiskExist(this.host, DiskService.FACTORY_LINK, azureVMName + "-boot-disk", true);
    validateDiskInternalTag(this.host);
    // Tags
    final Map<String, String> expectedTags = new HashMap<>();
    expectedTags.put(NETWORK_TAG_KEY_PREFIX + azureVMName, NETWORK_TAG_VALUE);
    expectedTags.put(VM_TAG_KEY_PREFIX + azureVMName, VM_TAG_VALUE);
    expectedTags.put(SG_TAG_KEY_PREFIX + azureVMName, SG_TAG_VALUE);
    final List<String> keysToLowerCase = expectedTags.keySet().stream().map(String::toLowerCase).collect(Collectors.toList());
    Query query = Query.Builder.create().addKindFieldClause(TagState.class).addInClause(TagState.FIELD_NAME_KEY, keysToLowerCase).build();
    Map<String, Query.Occurance> origin = new HashMap<>();
    origin.put(DISCOVERED.toString(), Query.Occurance.MUST_OCCUR);
    origin.put(SYSTEM.toString(), Query.Occurance.MUST_NOT_OCCUR);
    origin.put(USER_DEFINED.toString(), Query.Occurance.MUST_NOT_OCCUR);
    Query externalQuery = createOriginTagQuery(Boolean.TRUE, origin);
    query.addBooleanClause(externalQuery);
    QueryStrategy<TagState> queryLocalTags = new QueryTop<>(getHost(), query, TagState.class, null).setMaxResultsLimit(expectedTags.size() + 1);
    List<TagState> tagStates = waitToComplete(queryLocalTags.collectDocuments(Collectors.toList()));
    this.host.log(Level.INFO, "external tag states discovered: " + tagStates.size());
    if (!AzureUtils.isAzureClientMock()) {
        assertEquals("TagStates were not discovered.", expectedTags.size(), tagStates.size());
    }
    for (TagState tag : tagStates) {
        assertEquals(expectedTags.get(tag.key), tag.value);
    }
}
Also used : Query(com.vmware.xenon.services.common.QueryTask.Query) PhotonModelUtils.createOriginTagQuery(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery) HashMap(java.util.HashMap) AzureTestUtil.randomString(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.randomString) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) NicSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec)

Example 2 with NicSpec

use of com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec in project photon-model by vmware.

the class AzureTestUtil method initializeNicSpecs.

public static AzureNicSpecs initializeNicSpecs(String prefix, boolean assignGateway, boolean assignPublicIpAddress, boolean assignPrivateIpAddress) {
    String networkName = (prefix != null ? prefix + "-" : "") + AZURE_NETWORK_NAME;
    NetSpec network = new NetSpec(networkName, AZURE_NETWORK_CIDR, AZURE_RESOURCE_GROUP_LOCATION);
    List<NetSpec> subnets = new ArrayList<>();
    for (int i = 0; i < AZURE_SUBNET_CIDR.length; i++) {
        String subnetName = (prefix != null ? prefix + "-" : "") + AZURE_SUBNET_NAME + i;
        subnets.add(new NetSpec(subnetName, AZURE_SUBNET_CIDR[i], AZURE_RESOURCE_GROUP_LOCATION));
    }
    GatewaySpec gateway = assignGateway ? new GatewaySpec(AZURE_GATEWAY_NAME, AZURE_GATEWAY_CIDR, AZURE_RESOURCE_GROUP_LOCATION, AZURE_GATEWAY_IP_CONFIGURATION_NAME, AZURE_GATEWAY_PUBLIC_IP_NAME, IPAllocationMethod.DYNAMIC, VirtualNetworkGatewaySkuName.STANDARD, VirtualNetworkGatewaySkuTier.STANDARD, VirtualNetworkGatewayType.VPN, VpnType.ROUTE_BASED) : null;
    List<NicSpec> nicSpecs = new ArrayList<>();
    for (int i = 0; i < subnets.size(); i++) {
        NicSpec nicSpec = null;
        if (i == 0 && assignPrivateIpAddress) {
            nicSpec = NicSpec.createStatic(subnets.get(i));
        } else {
            nicSpec = NicSpec.createDynamic(subnets.get(i));
        }
        nicSpecs.add(nicSpec);
    }
    return new AzureNicSpecs(network, gateway, nicSpecs, assignPublicIpAddress);
}
Also used : NetSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NetSpec) ArrayList(java.util.ArrayList) GatewaySpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.GatewaySpec) NicSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec)

Example 3 with NicSpec

use of com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec 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 4 with NicSpec

use of com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec in project photon-model by vmware.

the class TestAzureLongRunningEnumeration method assertRemoteResources.

/**
 * Assert that remote resources are enumerated and exist locally.
 */
private void assertRemoteResources() {
    for (int i = 0; i < numOfVMsToTest; i++) {
        assertResourceExists(this.host, NetworkService.FACTORY_LINK, nicSpecs.get(i).network.name, true);
        for (NicSpec nicSpec : nicSpecs.get(i).nicSpecs) {
            assertResourceExists(this.host, SubnetService.FACTORY_LINK, nicSpec.getSubnetSpec().name, true);
        }
        assertResourceExists(this.host, ResourceGroupService.FACTORY_LINK, azureVMNames.get(i), true);
        assertResourceExists(this.host, SecurityGroupService.FACTORY_LINK, AZURE_SECURITY_GROUP_NAME, true);
        assertResourceExists(this.host, StorageDescriptionService.FACTORY_LINK, (azureVMNames.get(i) + "sa").replace("-", ""), true);
        assertDiskExist(this.host, DiskService.FACTORY_LINK, azureVMNames.get(i) + "-boot-disk", true);
        validateDiskInternalTag(this.host);
        // Tags
        final Map<String, String> expectedTags = new HashMap<>();
        expectedTags.put(NETWORK_TAG_KEY_PREFIX + azureVMNames.get(i), NETWORK_TAG_VALUE);
        expectedTags.put(VM_TAG_KEY_PREFIX + azureVMNames.get(i), VM_TAG_VALUE);
        expectedTags.put(SG_TAG_KEY_PREFIX + azureVMNames.get(i), SG_TAG_VALUE);
        final List<String> keysToLowerCase = expectedTags.keySet().stream().map(String::toLowerCase).collect(Collectors.toList());
        Query query = Query.Builder.create().addKindFieldClause(TagState.class).addInClause(TagState.FIELD_NAME_KEY, keysToLowerCase).build();
        Map<String, Query.Occurance> origin = new HashMap<>();
        origin.put(DISCOVERED.toString(), Query.Occurance.MUST_OCCUR);
        origin.put(SYSTEM.toString(), Query.Occurance.MUST_NOT_OCCUR);
        origin.put(USER_DEFINED.toString(), Query.Occurance.MUST_NOT_OCCUR);
        Query externalQuery = createOriginTagQuery(Boolean.TRUE, origin);
        query.addBooleanClause(externalQuery);
        QueryStrategy<TagState> queryLocalTags = new QueryTop<>(this.host, query, TagState.class, null).setMaxResultsLimit(expectedTags.size() + 1);
        List<TagState> tagStates = waitToComplete(queryLocalTags.collectDocuments(Collectors.toList()));
        assertEquals("TagStates were not discovered.", expectedTags.size(), tagStates.size());
        for (TagState tag : tagStates) {
            assertEquals(expectedTags.get(tag.key), tag.value);
        }
    }
}
Also used : Query(com.vmware.xenon.services.common.QueryTask.Query) PhotonModelUtils.createOriginTagQuery(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) AzureTestUtil.randomString(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.randomString) NicSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)

Aggregations

NicSpec (com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec)4 AzureTestUtil.randomString (com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.randomString)2 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)2 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)2 PhotonModelUtils.createOriginTagQuery (com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery)2 Query (com.vmware.xenon.services.common.QueryTask.Query)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GatewaySpec (com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.GatewaySpec)1 NetSpec (com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NetSpec)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 Rule (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule)1 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1