use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createResourceGroupStateObject.
private ResourceGroupState createResourceGroupStateObject(StorageEnumContext context, CloudBlobContainer container, String storageLink, ResourceGroupState oldResourceGroupState) {
ResourceGroupState resourceGroupState = new ResourceGroupState();
resourceGroupState.id = canonizeId(container.getUri().toString());
resourceGroupState.name = container.getName();
resourceGroupState.computeHostLink = context.parentCompute.documentSelfLink;
resourceGroupState.tenantLinks = context.parentCompute.tenantLinks;
if (storageLink != null) {
resourceGroupState.groupLinks = new HashSet<>();
resourceGroupState.groupLinks.add(storageLink);
}
resourceGroupState.customProperties = new HashMap<>();
if (context.request.endpointLink != null) {
resourceGroupState.customProperties.put(CUSTOM_PROP_ENDPOINT_LINK, context.request.endpointLink);
AdapterUtils.addToEndpointLinks(resourceGroupState, context.request.endpointLink);
resourceGroupState.endpointLink = context.request.endpointLink;
}
resourceGroupState.customProperties.put(AZURE_STORAGE_TYPE, AZURE_STORAGE_CONTAINERS);
resourceGroupState.customProperties.put(AZURE_STORAGE_CONTAINER_LEASE_LAST_MODIFIED, container.getProperties().getLastModified().toString());
resourceGroupState.customProperties.put(AZURE_STORAGE_CONTAINER_LEASE_STATE, container.getProperties().getLeaseState().toString());
resourceGroupState.customProperties.put(AZURE_STORAGE_CONTAINER_LEASE_STATUS, container.getProperties().getLeaseStatus().toString());
resourceGroupState.customProperties.put(ComputeProperties.RESOURCE_TYPE_KEY, ResourceGroupStateType.AzureStorageContainer.name());
resourceGroupState.customProperties.put(COMPUTE_HOST_LINK_PROP_NAME, context.parentCompute.documentSelfLink);
if (oldResourceGroupState != null) {
resourceGroupState.documentSelfLink = oldResourceGroupState.documentSelfLink;
}
return resourceGroupState;
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createResourceGroupStateHelper.
private DeferredResult<Operation> createResourceGroupStateHelper(StorageEnumContext context, CloudBlobContainer container, ResourceGroupState oldResourceGroup) {
// Associate resource group with storage account
String storageAcctName = getStorageAccountNameFromUri(container.getStorageUri().getPrimaryUri().getHost());
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(StorageDescription.class).addFieldClause(StorageDescription.FIELD_NAME_NAME, storageAcctName);
QueryTop<StorageDescription> queryLocalStates = new QueryTop<>(getHost(), qBuilder.build(), StorageDescription.class, context.parentCompute.tenantLinks, // endpointLink
null, context.parentCompute.documentSelfLink).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectLinks(Collectors.toSet()).thenCompose(sdls -> {
logFine(() -> String.format("Found %d matching storage descriptions", sdls.size()));
// the storage account names are unique so we should only get 1 result back
if (sdls.isEmpty()) {
return DeferredResult.completed((Operation) null);
}
String storageDescSelfLink = sdls.iterator().next();
if (sdls.size() > 1) {
logWarning(() -> String.format("Found multiple instances of the same" + " storage description %s", storageAcctName));
}
ResourceGroupState resourceGroupState = createResourceGroupStateObject(context, container, storageDescSelfLink, oldResourceGroup);
if (oldResourceGroup != null) {
return updateResourceGroupState(context, resourceGroupState);
} else {
return createResourceGroupState(context, resourceGroupState);
}
});
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class AzureTestUtil method createDefaultVMResource.
public static ComputeState createDefaultVMResource(VerificationHost host, String azureVMName, ComputeState computeHost, EndpointState endpointState, AzureNicSpecs nicSpecs, String networkRGLink, ImageSource imageSource, int numberOfAdditionalDisks, ComputeDescription azureVMDesc, String azureRgName) throws Throwable {
if (azureRgName == null) {
azureRgName = azureVMName;
}
final ResourceGroupState defaultVmRG = createDefaultResourceGroupState(host, azureRgName, computeHost, endpointState, ResourceGroupStateType.AzureResourceGroup);
final String defaultVmRGLink = defaultVmRG.documentSelfLink;
if (networkRGLink == null) {
// The RG where the VM is deployed is also used as RG for the Network!
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(host, AZURE_STORAGE_CONTAINER_RG_NAME, computeHost, endpointState, ResourceGroupStateType.AzureStorageContainer);
final Set<String> networkRGLinks = new HashSet<>();
networkRGLinks.add(networkRGLink);
networkRGLinks.add(azureStorageContainerRG.documentSelfLink);
final Set<String> sgRGLinks = new HashSet<>();
sgRGLinks.add(sgRGLink);
sgRGLinks.add(azureStorageContainerRG.documentSelfLink);
if (azureVMDesc == null) {
azureVMDesc = buildComputeDescription(host, computeHost, endpointState, null);
}
List<String> vmDisks = new ArrayList<>();
DiskState rootDisk = new DiskState();
rootDisk.name = azureVMName + "-boot-disk";
rootDisk.id = UUID.randomUUID().toString();
rootDisk.documentSelfLink = rootDisk.id;
rootDisk.type = DiskType.HDD;
// Custom OSDisk size of 32 GBs
rootDisk.capacityMBytes = AZURE_CUSTOM_OSDISK_SIZE;
if (imageSource.type == Type.PRIVATE_IMAGE || imageSource.type == Type.PUBLIC_IMAGE) {
rootDisk.imageLink = imageSource.asImageState().documentSelfLink;
} else if (imageSource.type == Type.IMAGE_REFERENCE) {
rootDisk.sourceImageReference = URI.create(imageSource.asRef());
}
rootDisk.bootOrder = 1;
rootDisk.endpointLink = endpointState.documentSelfLink;
rootDisk.endpointLinks = new HashSet<>();
rootDisk.endpointLinks.add(endpointState.documentSelfLink);
rootDisk.computeHostLink = computeHost.documentSelfLink;
rootDisk.tenantLinks = endpointState.tenantLinks;
rootDisk.storageType = AZURE_STORAGE_DISKS;
rootDisk.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_vhd.name());
rootDisk.customProperties = new HashMap<>();
rootDisk.customProperties.put(AZURE_OSDISK_CACHING, DEFAULT_OS_DISK_CACHING.name());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_NAME, (azureVMName + "sa").replaceAll("[_-]", "").toLowerCase());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_RG_NAME, azureRgName);
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_TYPE, AZURE_STORAGE_ACCOUNT_TYPE);
rootDisk = TestUtils.doPost(host, rootDisk, DiskState.class, UriUtils.buildUri(host, DiskService.FACTORY_LINK));
vmDisks.add(rootDisk.documentSelfLink);
// create additional disks
vmDisks.addAll(createAdditionalDisks(host, azureVMName, endpointState, numberOfAdditionalDisks, VMResourceSpec.PersistentDisks.NONE, false));
// Create NICs
List<String> nicLinks = createDefaultNicStates(host, computeHost, endpointState, networkRGLinks, sgRGLinks, nicSpecs, 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 = azureVMName;
computeState.parentLink = computeHost.documentSelfLink;
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
computeState.descriptionLink = azureVMDesc.documentSelfLink;
computeState.resourcePoolLink = computeHost.resourcePoolLink;
computeState.diskLinks = vmDisks;
computeState.networkInterfaceLinks = nicLinks;
computeState.customProperties = Collections.singletonMap(RESOURCE_GROUP_NAME, azureRgName);
computeState.groupLinks = Collections.singleton(defaultVmRGLink);
computeState.endpointLink = endpointState.documentSelfLink;
computeState.endpointLinks = new HashSet<>();
computeState.endpointLinks.add(endpointState.documentSelfLink);
computeState.computeHostLink = computeHost.documentSelfLink;
computeState.tenantLinks = endpointState.tenantLinks;
computeState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, AzureConstants.AzureResourceType.azure_vm.toString());
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
return TestUtils.doPost(host, computeState, ComputeState.class, UriUtils.buildUri(host, ComputeService.FACTORY_LINK));
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class GCPTestUtil method createDefaultResourceGroup.
/**
* Create a resource group for a GCP project.
* @param host The test host service.
* @param projectId The GCP project ID.
* @return The default resource group.
* @throws Throwable The exception during creating compute host.
*/
public static ResourceGroupState createDefaultResourceGroup(VerificationHost host, String projectId) throws Throwable {
ResourceGroupState resourceGroup = new ResourceGroupState();
resourceGroup.name = projectId;
return TestUtils.doPost(host, resourceGroup, ResourceGroupState.class, UriUtils.buildUri(host, ResourceGroupService.FACTORY_LINK));
}
use of com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState in project photon-model by vmware.
the class DiskContext method populateContextThen.
/**
* Populates the given initial context and invoke the onSuccess handler when built. At every
* step, if failure occurs the DiskContext's errorHandler is invoked to cleanup.
*/
public static void populateContextThen(Service service, DiskContext ctx, Consumer<DiskContext> onSuccess) {
// Step 1: Get disk details
if (ctx.diskState == null) {
URI diskUri = createInventoryUri(service.getHost(), DiskService.DiskStateExpanded.buildUri(ctx.diskReference));
AdapterUtils.getServiceState(service, diskUri, op -> {
ctx.diskState = op.getBody(DiskService.DiskStateExpanded.class);
EnumSet<DiskService.DiskType> notSupportedTypes = EnumSet.of(DiskService.DiskType.SSD, DiskService.DiskType.NETWORK);
if (notSupportedTypes.contains(ctx.diskState.type)) {
ctx.fail(new IllegalStateException(String.format("Not supported disk type %s.", ctx.diskState.type)));
return;
}
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
// the disk.
if (ctx.datastoreName == null && ctx.diskInstanceRequest.requestType == DiskInstanceRequest.DiskRequestType.CREATE) {
if (ctx.diskState.storageDescription != null) {
ctx.datastoreName = ctx.diskState.storageDescription.id;
populateContextThen(service, ctx, onSuccess);
} else if (ctx.diskState.resourceGroupStates != null && !ctx.diskState.resourceGroupStates.isEmpty()) {
// There will always be only one resource group state existing for a disk
ResourceGroupState resource = ctx.diskState.resourceGroupStates.iterator().next();
ClientUtils.getDatastoresForProfile(service, resource.documentSelfLink, ctx.diskState.endpointLink, ctx.diskState.tenantLinks, ctx.errorHandler, (result) -> {
if (result.documents != null && result.documents.size() > 0) {
// pick the first datastore and proceed.
StorageDescription dsStorageDesc = Utils.fromJson(result.documents.values().iterator().next(), StorageDescription.class);
ctx.datastoreName = dsStorageDesc.id;
ctx.diskState.storageDescriptionLink = dsStorageDesc.documentSelfLink;
} else {
// Since no result found default to the available datastore.
ctx.datastoreName = "";
}
populateContextThen(service, ctx, onSuccess);
});
} else if (CustomProperties.of(ctx.diskState).getString(CustomProperties.DISK_DATASTORE_NAME) != null) {
ctx.datastoreName = CustomProperties.of(ctx.diskState).getString(CustomProperties.DISK_DATASTORE_NAME);
populateContextThen(service, ctx, onSuccess);
} else {
// Mark empty so that it can fall back to any available datastore from the system.
ctx.datastoreName = "";
populateContextThen(service, ctx, onSuccess);
}
return;
}
// Step 3: Get Credentials
if (ctx.vSphereCredentials == null) {
if (IAAS_API_ENABLED) {
if (ctx.operation == null) {
ctx.fail(new IllegalArgumentException("Caller operation cannot be empty"));
return;
}
SessionUtil.retrieveExternalToken(service, ctx.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
if (throwable != null) {
ctx.errorHandler.accept(throwable);
return;
}
ctx.vSphereCredentials = authCredentialsServiceState;
populateContextThen(service, ctx, onSuccess);
});
} else {
if (ctx.diskState.authCredentialsLink == null || ctx.diskState.authCredentialsLink.isEmpty()) {
ctx.fail(new IllegalArgumentException("Auth credentials cannot be empty"));
return;
}
URI credUri = createInventoryUri(service.getHost(), ctx.diskState.authCredentialsLink);
AdapterUtils.getServiceState(service, credUri, op -> {
ctx.vSphereCredentials = op.getBody(AuthCredentialsServiceState.class);
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
}
return;
}
// Step 4: Get the endpoint compute link
if (ctx.endpointComputeLink == null) {
URI endpointUri = createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), ctx.diskState.endpointLink));
AdapterUtils.getServiceState(service, endpointUri, op -> {
EndpointService.EndpointState endpointState = op.getBody(EndpointService.EndpointState.class);
ctx.endpointComputeLink = endpointState.computeLink;
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
// Step 5: Get the adapter reference to from the endpoint compute link
if (ctx.adapterManagementReference == null) {
URI computeUri = createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), ctx.endpointComputeLink));
AdapterUtils.getServiceState(service, computeUri, op -> {
ComputeService.ComputeState computeState = op.getBody(ComputeService.ComputeState.class);
ctx.adapterManagementReference = computeState.adapterManagementReference;
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
// Step 6: Obtain reference to the datacenter moref.
if (ctx.datacenterMoRef == null) {
try {
ctx.datacenterMoRef = VimUtils.convertStringToMoRef(ctx.diskState.regionId);
} catch (IllegalArgumentException ex) {
ctx.fail(ex);
return;
}
}
onSuccess.accept(ctx);
}
Aggregations