use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method queryResourceGroupStates.
/**
* Query resource group states stored in the local document store based on the retrieved azure
* virtual networks.
*/
private void queryResourceGroupStates(NetworkEnumContext context, NetworkEnumStages next) {
List<String> resourceGroupIds = context.virtualNetworks.values().stream().map(vNet -> AzureUtils.getResourceGroupId(vNet.id)).collect(Collectors.toList());
Query.Builder qBuilder = Builder.create().addKindFieldClause(ResourceGroupState.class).addInClause(ResourceGroupState.FIELD_NAME_ID, resourceGroupIds).addCompositeFieldClause(ResourceGroupState.FIELD_NAME_CUSTOM_PROPERTIES, ComputeProperties.RESOURCE_TYPE_KEY, ResourceGroupStateType.AzureResourceGroup.name());
QueryByPages<ResourceGroupState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), ResourceGroupState.class, context.parentCompute.tenantLinks, null, /* endpoint */
context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
queryLocalStates.queryDocuments(rg -> context.resourceGroupStates.put(rg.id, rg.documentSelfLink)).whenComplete(thenHandleSubStage(context, next));
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method disassociateResourceGroupStates.
/*
* Disassociate local storage containers that no longer exist in Azure
*
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the storage accounts discovered as part of get storage
* accounts call.
*
* A disassociate on a resource group state is invoked only if it meets two criteria: -
* Timestamp
* older than current enumeration cycle. - Storage container is not present on Azure.
*/
private void disassociateResourceGroupStates(StorageEnumContext context, StorageEnumStages next) {
Query.Builder qBuilder = newResourceGroupQueryBuilder(context).addRangeClause(ResourceGroupState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(context.enumerationStartTimeInMicros));
QueryByPages<ResourceGroupState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), ResourceGroupState.class, context.parentCompute.tenantLinks, null, /* endpointLink */
context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
List<DeferredResult<Operation>> ops = new ArrayList<>();
queryLocalStates.queryDocuments(rg -> {
// repository it means nothing has changed about it.
if (context.containerIds.contains(canonizeId(rg.id))) {
return;
}
// Deleting the localResourceState is done by disassociating the
// endpointLink from the localResourceState. If the localResourceState
// isn't associated with any other endpointLink, it should be eventually
// deleted by the groomer task
Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, context.request.endpointLink, rg);
if (disassociateOp == null) {
return;
}
// NOTE: The original Op is set with completion that must be executed.
// Since sendWithDeferredResult is used we must manually call it, otherwise it's
// just ignored.
CompletionHandler disassociateOpCompletion = disassociateOp.getCompletion();
DeferredResult<Operation> disassociateDR = sendWithDeferredResult(disassociateOp).whenComplete(disassociateOpCompletion::handle).whenComplete((o, e) -> {
final String message = "Disassociate stale %s state";
if (e != null) {
logWarning(message + ": FAILED with %s", rg.documentSelfLink, Utils.toString(e));
} else {
log(Level.FINEST, message + ": SUCCESS", rg.documentSelfLink);
}
});
ops.add(disassociateDR);
}).thenCompose(r -> DeferredResult.allOf(ops)).whenComplete((r, e) -> {
logFine(() -> "Finished disassociation of storage containers for Azure");
context.subStage = next;
handleSubStage(context);
});
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createDiskStateHelper.
private DeferredResult<Operation> createDiskStateHelper(StorageEnumContext context, ListBlobItem blob, DiskState oldDiskState) {
try {
// Associate blob with the resource group state (container) it belongs to
String containerId = blob.getContainer().getUri().toString();
Query.Builder qBuilder = newResourceGroupQueryBuilder(context).addFieldClause(ResourceGroupState.FIELD_NAME_ID, containerId);
QueryTop<ResourceGroupState> queryLocalStates = new QueryTop<>(getHost(), qBuilder.build(), ResourceGroupState.class, context.parentCompute.tenantLinks, null, /* endpointLink */
context.parentCompute.documentSelfLink).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectLinks(Collectors.toSet()).thenCompose(rgLinks -> {
logFine(() -> String.format("Found %d matching resource groups", rgLinks.size()));
String containerLink = null;
if (rgLinks.size() > 0) {
containerLink = rgLinks.iterator().next();
if (rgLinks.size() > 1) {
logWarning(() -> String.format("Found multiple instances of the same" + " resource group %s", containerId));
}
if (oldDiskState != null) {
if (rgLinks.contains(oldDiskState.storageDescriptionLink)) {
containerLink = oldDiskState.storageDescriptionLink;
}
}
}
DiskState diskState = createDiskStateObject(context, blob, containerLink, oldDiskState);
if (oldDiskState != null) {
return updateDiskState(context, diskState);
} else {
return createDiskState(context, diskState);
}
});
} catch (URISyntaxException e) {
logWarning(() -> String.format("Could not set storage description link for blob: %s", blob.getUri().toString()));
} catch (StorageException e) {
logWarning(() -> String.format("Could not set storage description link for disk blob: %s", blob.getUri().toString()));
}
return DeferredResult.completed(null);
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class ResourceGroomerTaskService method populateEndpointLinksByDocumentLinks.
/**
* Helper for creating corresponding object and parsing the response for given documentKind
* to store selfLink and endpointLink.
*/
private static void populateEndpointLinksByDocumentLinks(Map<String, Object> documents, Map<String, Set<String>> endpointLinksByDocumentLinks, Map<String, String> endpointLinkByDocumentLinks) {
for (Object document : documents.values()) {
ServiceDocument doc = Utils.fromJson(document, ServiceDocument.class);
Set<String> endpointLinks = new HashSet<>();
if (doc.documentKind.equals(COMPUTE_STATE_DOCUMENT_KIND)) {
ComputeState state = Utils.fromJson(document, ComputeState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(DISK_STATE_DOCUMENT_KIND)) {
DiskState state = Utils.fromJson(document, DiskState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(COMPUTE_DESCRIPTION_DOCUMENT_KIND)) {
ComputeDescription state = Utils.fromJson(document, ComputeDescription.class);
// only deleting discovered resources
if (!(state.customProperties != null && (ResourceEnumerationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK)) || EndpointAllocationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK))))) {
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_STATE_DOCUMENT_KIND)) {
NetworkState state = Utils.fromJson(document, NetworkState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_INTERFACE_STATE_DOCUMENT_KIND)) {
NetworkInterfaceState state = Utils.fromJson(document, NetworkInterfaceState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SECURITY_GROUP_STATE_DOCUMENT_KIND)) {
SecurityGroupState state = Utils.fromJson(document, SecurityGroupState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SUBNET_STATE_DOCUMENT_KIND)) {
SubnetState state = Utils.fromJson(document, SubnetState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(LOAD_BALANCER_DOCUMENT_KIND)) {
LoadBalancerState state = Utils.fromJson(document, LoadBalancerState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(STORAGE_DESCRIPTION_DOCUMENT_KIND)) {
StorageDescription state = Utils.fromJson(document, StorageDescription.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(RESOURCE_GROUP_DOCUMENT_KIND)) {
ResourceGroupState state = Utils.fromJson(document, ResourceGroupState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceGroupService.PROPERTY_NAME_IS_USER_CREATED)) {
continue;
}
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(IMAGE_STATE_KIND)) {
ImageState state = Utils.fromJson(document, ImageState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(ROUTER_STATE_KIND)) {
RouterState state = Utils.fromJson(document, RouterState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(AUTH_CREDENTIALS_SERVICE_STATE_KIND)) {
AuthCredentialsServiceState state = Utils.fromJson(document, AuthCredentialsServiceState.class);
if (state.customProperties != null && state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK) != null) {
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK));
} else {
endpointLinkByDocumentLinks.put(state.documentSelfLink, EMPTY_STRING);
}
}
}
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class AzureTestUtil method createVMResourceFromSpec.
/**
* Separate method to create VM from given spec
*/
public static ComputeState createVMResourceFromSpec(VMResourceSpec spec) throws Throwable {
final String defaultVmRGName = spec.azureVmName;
// TODO Modify createDefaultResourceGroupState() to have only spec parameter passed
final ResourceGroupState defaultVmRG = createDefaultResourceGroupState(spec.host, defaultVmRGName, spec.computeHost, spec.endpointState, ResourceGroupStateType.AzureResourceGroup);
final String defaultVmRGLink = defaultVmRG.documentSelfLink;
if (spec.networkRGLink == null) {
// The RG where the VM is deployed is also used as RG for the Network!
spec.networkRGLink = defaultVmRGLink;
}
// The RG where the VM is deployed is also used as RG for the SecurityGroup!
final String sgRGLink = defaultVmRGLink;
// Create resource group with a different type. It should be filtered out.
ResourceGroupState azureStorageContainerRG = createDefaultResourceGroupState(spec.host, AZURE_STORAGE_CONTAINER_RG_NAME, spec.computeHost, spec.endpointState, ResourceGroupStateType.AzureStorageContainer);
final Set<String> networkRGLinks = new HashSet<>();
networkRGLinks.add(spec.networkRGLink);
networkRGLinks.add(azureStorageContainerRG.documentSelfLink);
final Set<String> sgRGLinks = new HashSet<>();
sgRGLinks.add(sgRGLink);
sgRGLinks.add(azureStorageContainerRG.documentSelfLink);
AuthCredentialsServiceState azureVMAuth = new AuthCredentialsServiceState();
azureVMAuth.userEmail = AZURE_ADMIN_USERNAME;
azureVMAuth.privateKey = AZURE_ADMIN_PASSWORD;
azureVMAuth = TestUtils.doPost(spec.host, azureVMAuth, AuthCredentialsServiceState.class, UriUtils.buildUri(spec.host, AuthCredentialsService.FACTORY_LINK));
// Create a VM desc
ComputeDescription azureVMDesc = new ComputeDescription();
azureVMDesc.id = UUID.randomUUID().toString();
azureVMDesc.documentSelfLink = azureVMDesc.id;
azureVMDesc.name = azureVMDesc.id;
azureVMDesc.regionId = AZURE_RESOURCE_GROUP_LOCATION;
azureVMDesc.authCredentialsLink = azureVMAuth.documentSelfLink;
azureVMDesc.tenantLinks = spec.endpointState.tenantLinks;
azureVMDesc.endpointLink = spec.endpointState.documentSelfLink;
azureVMDesc.endpointLinks = new HashSet<>();
azureVMDesc.endpointLinks.add(spec.endpointState.documentSelfLink);
azureVMDesc.computeHostLink = spec.endpointState.computeHostLink;
azureVMDesc.instanceType = AZURE_VM_SIZE;
azureVMDesc.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
azureVMDesc.customProperties = new HashMap<>();
// set the create service to the azure instance service
azureVMDesc.instanceAdapterReference = UriUtils.buildUri(spec.host, AzureUriPaths.AZURE_INSTANCE_ADAPTER);
azureVMDesc.powerAdapterReference = UriUtils.buildUri(spec.host, AzureUriPaths.AZURE_POWER_ADAPTER);
azureVMDesc = TestUtils.doPost(spec.host, azureVMDesc, ComputeDescription.class, UriUtils.buildUri(spec.host, ComputeDescriptionService.FACTORY_LINK));
DiskState rootDisk = new DiskState();
rootDisk.name = spec.azureVmName + "-boot-disk";
rootDisk.id = UUID.randomUUID().toString();
rootDisk.documentSelfLink = rootDisk.id;
rootDisk.type = DiskType.HDD;
rootDisk.storageType = AZURE_STORAGE_DISKS;
// Custom OSDisk size of 32 GBs
rootDisk.capacityMBytes = AZURE_CUSTOM_OSDISK_SIZE;
rootDisk.bootOrder = 1;
rootDisk.endpointLink = spec.endpointState.documentSelfLink;
rootDisk.endpointLinks = new HashSet<>();
rootDisk.endpointLinks.add(spec.endpointState.documentSelfLink);
rootDisk.computeHostLink = spec.endpointState.computeHostLink;
rootDisk.tenantLinks = spec.endpointState.tenantLinks;
if (spec.isManagedDisk) {
rootDisk.tagLinks = createTagStateSet(spec.host, spec.endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_managed_disk.name());
} else {
rootDisk.tagLinks = createTagStateSet(spec.host, spec.endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_vhd.name());
}
rootDisk.customProperties = new HashMap<>();
rootDisk.customProperties.put(AZURE_OSDISK_CACHING, DEFAULT_OS_DISK_CACHING.name());
if (spec.imageSource.type == Type.PRIVATE_IMAGE) {
if (spec.isManagedDisk) {
rootDisk.imageLink = spec.imageSource.asImageState().documentSelfLink;
rootDisk.customProperties.put(AzureConstants.AZURE_MANAGED_DISK_TYPE, SkuName.STANDARD_LRS.toString());
}
} else if (spec.imageSource.type == Type.PUBLIC_IMAGE) {
if (spec.isManagedDisk) {
rootDisk.imageLink = spec.imageSource.asImageState().documentSelfLink;
rootDisk.customProperties.put(AzureConstants.AZURE_MANAGED_DISK_TYPE, SkuName.STANDARD_LRS.toString());
} else {
rootDisk.imageLink = spec.imageSource.asImageState().documentSelfLink;
if (spec.storageAccountName == null || spec.resourceGroupForStorageAccount == null) {
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_NAME, (spec.azureVmName + "sa").replaceAll("[_-]", "").toLowerCase());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_RG_NAME, defaultVmRGName);
} else {
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_NAME, spec.storageAccountName);
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_RG_NAME, spec.resourceGroupForStorageAccount);
}
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_TYPE, AZURE_STORAGE_ACCOUNT_TYPE);
}
} else if (spec.imageSource.type == Type.IMAGE_REFERENCE) {
rootDisk.sourceImageReference = URI.create(spec.imageSource.asRef());
}
rootDisk = TestUtils.doPost(spec.host, rootDisk, DiskState.class, UriUtils.buildUri(spec.host, DiskService.FACTORY_LINK));
List<String> vmDisks = new ArrayList<>();
vmDisks.add(rootDisk.documentSelfLink);
// create additional disks
if (spec.numberOfAdditionalDisks > 0) {
// TODO Need to modify createAdditionalDisks() to have only spec passed as parameter
vmDisks.addAll(createAdditionalDisks(spec.host, spec.azureVmName, spec.endpointState, spec.numberOfAdditionalDisks, spec.persistentDisks, spec.isManagedDisk));
}
// Add external existing data disks (if present) to the list for attaching
if (null != spec.externalDiskLinks && spec.externalDiskLinks.size() > 0) {
vmDisks.addAll(spec.externalDiskLinks);
}
// Create NICs
List<String> nicLinks = createDefaultNicStates(spec.host, spec.computeHost, spec.endpointState, networkRGLinks, sgRGLinks, spec.nicSpecs, spec.azureVmName).stream().map(nic -> nic.documentSelfLink).collect(Collectors.toList());
// Finally create the compute resource state to provision using all constructs above.
ComputeState computeState = new ComputeState();
computeState.id = UUID.randomUUID().toString();
computeState.name = spec.azureVmName;
computeState.parentLink = spec.computeHost.documentSelfLink;
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
computeState.descriptionLink = azureVMDesc.documentSelfLink;
computeState.resourcePoolLink = spec.computeHost.resourcePoolLink;
computeState.diskLinks = vmDisks;
computeState.networkInterfaceLinks = nicLinks;
computeState.customProperties = Collections.singletonMap(RESOURCE_GROUP_NAME, defaultVmRGName);
computeState.groupLinks = Collections.singleton(defaultVmRGLink);
computeState.endpointLink = spec.endpointState.documentSelfLink;
computeState.endpointLinks = new HashSet<>();
computeState.endpointLinks.add(spec.endpointState.documentSelfLink);
computeState.computeHostLink = spec.endpointState.computeHostLink;
computeState.tenantLinks = spec.endpointState.tenantLinks;
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
return TestUtils.doPost(spec.host, computeState, ComputeState.class, UriUtils.buildUri(spec.host, ComputeService.FACTORY_LINK));
}
Aggregations