use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class TestAzureEnumerationTask method getSubnetStates.
/**
* Get all SubnetStates within passed NetworkState. In other words, get all subnet states that
* refer the network state passed.
*/
// TODO : Duplicated from AWS TestUtils. Please advice where to put common test utils
public static List<SubnetState> getSubnetStates(VerificationHost host, NetworkState networkState) {
Query queryForReferrers = QueryUtils.queryForReferrers(networkState.documentSelfLink, SubnetState.class, SubnetState.FIELD_NAME_NETWORK_LINK);
QueryByPages<SubnetState> querySubnetStatesReferrers = new QueryByPages<>(host, queryForReferrers, SubnetState.class, networkState.tenantLinks, networkState.endpointLink);
DeferredResult<List<SubnetState>> subnetDR = querySubnetStatesReferrers.collectDocuments(Collectors.toList());
return waitToComplete(subnetDR);
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class TestAzureEnumerationTask method testEnumeration.
@Test
public void testEnumeration() throws Throwable {
this.storageDescription = createDefaultStorageAccountDescription(this.host, this.mockedStorageAccountName, computeHost, endpointState);
this.resourceGroupState = createDefaultResourceGroupState(this.host, this.mockedStorageAccountName, computeHost, endpointState, ResourceGroupStateType.AzureResourceGroup);
this.diskState = createDefaultDiskState(this.host, this.mockedStorageAccountName, this.mockedStorageAccountName, computeHost, endpointState);
// create an Azure VM compute resource (this also creates a disk and a storage account)
this.vmState = createDefaultVMResource(this.host, azureVMName, computeHost, endpointState, NIC_SPEC);
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskState provisionTask = new ProvisionComputeTaskState();
provisionTask.computeLink = this.vmState.documentSelfLink;
provisionTask.isMockRequest = this.isMock;
provisionTask.taskSubStage = SubStage.CREATING_HOST;
ProvisionComputeTaskState outTask = TestUtils.doPost(this.host, provisionTask, ProvisionComputeTaskState.class, UriUtils.buildUri(this.host, ProvisionComputeTaskService.FACTORY_LINK));
this.host.waitForFinishedTask(ProvisionComputeTaskState.class, outTask.documentSelfLink);
// Check resources have been created
// expected VM count = 2 (1 compute host instance + 1 vm compute state)
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 2, ComputeService.FACTORY_LINK, false);
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 1, StorageDescriptionService.FACTORY_LINK, false);
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 1, ResourceGroupService.FACTORY_LINK, false);
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 1, DiskService.FACTORY_LINK, false);
this.numberOfVMsToDelete++;
if (this.isMock) {
runEnumeration();
deleteVMs(this.host, this.vmState.documentSelfLink, this.isMock, 1);
this.vmState = null;
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 1, ComputeService.FACTORY_LINK, false);
deleteServiceDocument(this.host, this.storageDescription.documentSelfLink);
this.storageDescription = null;
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 0, StorageDescriptionService.FACTORY_LINK, true);
deleteServiceDocument(this.host, this.resourceGroupState.documentSelfLink);
this.resourceGroupState = null;
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 0, ResourceGroupService.FACTORY_LINK, false);
deleteServiceDocument(this.host, this.diskState.documentSelfLink);
this.diskState = null;
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, 0, DiskService.FACTORY_LINK, false);
return;
}
createStaleResource();
tagAzureResources();
// stale resources + 1 compute host instance + 1 vm compute state
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_VM_RESOURCES_COUNT + 2, ComputeService.FACTORY_LINK, false);
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_STORAGE_ACCOUNTS_COUNT + 1, StorageDescriptionService.FACTORY_LINK, false);
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_CONTAINERS_COUNT + STALE_RG_COUNT + 1, ResourceGroupService.FACTORY_LINK, false);
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_DISKS_COUNT + 1, DiskService.FACTORY_LINK, false);
// 1 network per each stale vm resource + 1 network for original vm compute state.
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_VM_RESOURCES_COUNT + 1, NetworkService.FACTORY_LINK, false);
// 1 subnet per network, 1 network per each stale vm resource + 1 subnet for the original
// compute state.
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_VM_RESOURCES_COUNT + 1, SubnetService.FACTORY_LINK, false);
// 1 network per each stale vm resource + 1 network for original vm compute state.
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_VM_RESOURCES_COUNT + 1, NetworkInterfaceService.FACTORY_LINK, false);
ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, STALE_SECURITY_GROUPS_COUNT, SecurityGroupService.FACTORY_LINK, false);
this.vmCount = getAzureVMCount(this.computeManagementClient);
this.host.log(Level.INFO, "Initial VM Count: %d", this.vmCount);
runEnumeration();
assertRemoteResources();
assertInternalTagResources();
assertStaleResources();
// VM count + 1 compute host instance
this.vmCount = this.vmCount + 1;
ServiceDocumentQueryResult result = ProvisioningUtils.queryDocumentsAndAssertExpectedCount(this.host, this.vmCount, ComputeService.FACTORY_LINK, false);
// validate type field for enumerated VMs
result.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), ComputeState.class)).filter(c -> !c.documentSelfLink.equals(computeHost.documentSelfLink));
// validate internal tags for enumerated VMs
TagService.TagState expectedInternalTypeTag = newTagState(TAG_KEY_TYPE, AzureConstants.AzureResourceType.azure_vm.toString(), EnumSet.of(SYSTEM), endpointState.tenantLinks);
result.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), ComputeState.class)).filter(c -> c.type.equals(ComputeType.VM_GUEST)).forEach(c -> {
assertNotNull("ComputeState tagLinks is NULL", c.tagLinks);
assertTrue(String.format("ComputeState doesn't contain tagLink: %s", expectedInternalTypeTag.documentSelfLink), c.tagLinks.contains(expectedInternalTypeTag.documentSelfLink));
});
// 1 network per each stale vm resource + 1 network for original vm compute state.
ServiceDocumentQueryResult networkResults = ProvisioningUtils.queryAllFactoryResources(this.host, NetworkService.FACTORY_LINK);
// validate internal tags for enumerated networks
TagService.TagState expectedNetworkInternalTypeTag = newTagState(TAG_KEY_TYPE, NETWORK_TAG_TYPE_VALUE, false, endpointState.tenantLinks);
networkResults.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), NetworkState.class)).forEach(c -> {
assertNotNull("NetworkState tagLinks is NULL", c.tagLinks);
assertTrue(String.format("NetworkState doesn't contain tagLink: %s", expectedNetworkInternalTypeTag.documentSelfLink), c.tagLinks.contains(expectedNetworkInternalTypeTag.documentSelfLink));
});
// 1 subnet per network, 1 network per each stale vm resource + 1 subnet for the original
// compute state.
ServiceDocumentQueryResult subnetResults = ProvisioningUtils.queryAllFactoryResources(this.host, SubnetService.FACTORY_LINK);
// validate internal tags for enumerated subnets
TagService.TagState expectedSubnetInternalTypeTag = newTagState(TAG_KEY_TYPE, SUBNET_TAG_TYPE_VALUE, false, endpointState.tenantLinks);
subnetResults.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), SubnetState.class)).forEach(c -> {
assertNotNull("SubnetState tagLinks is NULL", c.tagLinks);
assertTrue(String.format("SubnetState doesn't contain tagLink: %s", expectedSubnetInternalTypeTag.documentSelfLink), c.tagLinks.contains(expectedSubnetInternalTypeTag.documentSelfLink));
});
ServiceDocumentQueryResult nicResults = ProvisioningUtils.queryAllFactoryResources(this.host, NetworkInterfaceService.FACTORY_LINK);
// validate internal tags for enumerated network interfaces
TagService.TagState expectedNicInternalTypeTag = newTagState(TAG_KEY_TYPE, NETWORK_INTERFACE_TAG_TYPE_VALUE, false, endpointState.tenantLinks);
nicResults.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), NetworkInterfaceState.class)).filter(c -> c.regionId != null).forEach(c -> {
assertNotNull("NetworkInterfaceState tagLinks is NULL", c.tagLinks);
assertTrue(String.format("NetworkInterfaceState doesn't contain tagLink: %s", expectedNicInternalTypeTag.documentSelfLink), c.tagLinks.contains(expectedNicInternalTypeTag.documentSelfLink));
});
// validate internal tags for disks
ServiceDocumentQueryResult diskResults = ProvisioningUtils.queryAllFactoryResources(this.host, DiskService.FACTORY_LINK);
TagService.TagState expectedManagedDiskInternalTypeTag = newTagState(TAG_KEY_TYPE, azure_managed_disk.toString(), false, endpointState.tenantLinks);
diskResults.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), DiskState.class)).filter(d -> d.customProperties != null && d.customProperties.containsKey(AzureConstants.AZURE_MANAGED_DISK_TYPE)).forEach(c -> {
assertNotNull("tagLinks is null for disk", c.tagLinks);
assertTrue("disk doesn't contain correct internal tagLink", c.tagLinks.contains(expectedManagedDiskInternalTypeTag.documentSelfLink));
});
// validate environment name field for enumerated VMs
result.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), ComputeState.class)).forEach(c -> assertEquals(ComputeDescription.ENVIRONMENT_NAME_AZURE, c.environmentName));
// validate Security Group tagLinks
ServiceDocumentQueryResult securityGroupResults = ProvisioningUtils.queryAllFactoryResources(this.host, SecurityGroupService.FACTORY_LINK);
securityGroupResults.documents.entrySet().stream().map(e -> Utils.fromJson(e.getValue(), SecurityGroupService.SecurityGroupState.class)).forEach(c -> {
if (c.tagLinks != null) {
for (String tag : c.tagLinks) {
assertTrue(tag.startsWith(TagService.FACTORY_LINK));
}
}
});
for (Entry<String, Object> key : result.documents.entrySet()) {
ComputeState document = Utils.fromJson(key.getValue(), ComputeState.class);
if (!document.documentSelfLink.equals(computeHost.documentSelfLink) && !document.documentSelfLink.equals(this.vmState.documentSelfLink) && document.id.toLowerCase().contains(CUSTOM_DIAGNOSTIC_ENABLED_VM.toLowerCase())) {
this.enumeratedComputeLink = document.documentSelfLink;
break;
}
}
try {
// Test stats for the VM that was just enumerated from Azure.
this.host.log(Level.INFO, "Collecting stats for VM [%s]-[%s]", CUSTOM_DIAGNOSTIC_ENABLED_VM, this.enumeratedComputeLink);
this.host.setTimeoutSeconds(300);
if (this.enumeratedComputeLink != null) {
this.host.waitFor("Error waiting for VM stats", () -> {
try {
issueStatsRequest(this.enumeratedComputeLink, false);
} catch (Throwable t) {
return false;
}
return true;
});
}
// Test stats for the compute host.
this.host.log(Level.INFO, "Collecting stats for host [%s]", computeHost.documentSelfLink);
this.host.waitFor("Error waiting for host stats", () -> {
try {
issueStatsRequest(computeHost.documentSelfLink, true);
} catch (Throwable t) {
return false;
}
return true;
});
} catch (Throwable te) {
this.host.log(Level.SEVERE, te.getMessage());
}
// delete vm directly on azure
this.computeManagementClient.virtualMachines().beginDelete(azureVMName, azureVMName);
runEnumeration();
assertResourceDisassociated(this.host, ComputeService.FACTORY_LINK, azureVMName, true);
// clean up
this.vmState = null;
this.resourceManagementClient.resourceGroups().beginDelete(azureVMName);
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class AzureSubnetTaskServiceTest method createSubnetState.
private SubnetState createSubnetState(String id) throws Throwable {
SubnetState subnetState = new SubnetState();
subnetState.id = id;
subnetState.name = this.subnetName;
subnetState.lifecycleState = LifecycleState.PROVISIONING;
subnetState.subnetCIDR = AZURE_NON_EXISTING_SUBNET_CIDR;
subnetState.networkLink = this.networkState.documentSelfLink;
subnetState.instanceAdapterReference = UriUtils.buildUri(this.host, AzureSubnetService.SELF_LINK);
subnetState.endpointLink = endpointState.documentSelfLink;
subnetState.tenantLinks = endpointState.tenantLinks;
return postServiceSynchronously(SubnetService.FACTORY_LINK, subnetState, SubnetState.class);
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class AzureSubnetTaskServiceTest method testCreateSubnetDuplicatedCIDR.
@Test
public void testCreateSubnetDuplicatedCIDR() throws Throwable {
Assume.assumeFalse(this.isMock);
// Provision subnet in Azure.
SubnetState subnetState = createSubnetState(null);
kickOffSubnetProvision(InstanceRequestType.CREATE, subnetState, TaskStage.FINISHED);
subnetState.name += "new";
patchServiceSynchronously(subnetState.documentSelfLink, subnetState);
// Try to provision the same subnet second time.
ProvisionSubnetTaskState state = kickOffSubnetProvision(InstanceRequestType.CREATE, subnetState, TaskStage.FAILED);
assertTrue(state.taskInfo.failure.message.contains(STATUS_SUBNET_NOT_VALID));
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState 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;
}
Aggregations