use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureInstanceService method resolveLatestVirtualMachineImage.
/**
* Get the LATEST VirtualMachineImage using publisher, offer and SKU.
*/
private DeferredResult<AzureInstanceContext> resolveLatestVirtualMachineImage(AzureInstanceContext ctx) {
ImageReferenceInner imageReference = ctx.imageSource.asImageReferenceInner();
if (AzureConstants.AZURE_URN_VERSION_LATEST.equalsIgnoreCase(imageReference.version())) {
String msg = String.format("Getting latest Azure image by %s:%s:%s", imageReference.publisher(), imageReference.offer(), imageReference.sku());
AzureDeferredResultServiceCallback<List<VirtualMachineImageResourceInner>> callback = new Default<>(ctx.service, msg);
getComputeManagementClientImpl(ctx).virtualMachineImages().listAsync(ctx.resourceGroup.location(), imageReference.publisher(), imageReference.offer(), imageReference.sku(), null, 1, AzureConstants.ORDER_BY_VM_IMAGE_RESOURCE_NAME_DESC, callback);
return callback.toDeferredResult().thenApply(imageResources -> {
if (imageResources == null || imageResources.isEmpty() || imageResources.get(0) == null) {
throw new IllegalStateException(String.format("No latest Azure image found by %s:%s:%s", imageReference.publisher(), imageReference.offer(), imageReference.sku()));
}
// Update 'latest'-version with actual version
imageReference.withVersion(imageResources.get(0).name());
return ctx;
});
}
return DeferredResult.completed(ctx);
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureInstanceService method createPublicIPs.
private void createPublicIPs(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
if (ctx.nics.isEmpty()) {
handleAllocation(ctx, nextStage);
return;
}
AzureNicContext nicCtx = ctx.getPrimaryNic();
// For now if not specified default to TRUE!
if (nicCtx.nicStateWithDesc.description.assignPublicIpAddress == null) {
nicCtx.nicStateWithDesc.description.assignPublicIpAddress = Boolean.TRUE;
}
if (nicCtx.nicStateWithDesc.description.assignPublicIpAddress == Boolean.FALSE) {
// Do nothing in this method -> proceed to next stage.
handleAllocation(ctx, nextStage);
return;
}
PublicIPAddressesInner azureClient = getNetworkManagementClientImpl(ctx).publicIPAddresses();
final PublicIPAddressInner publicIPAddress = newAzurePublicIPAddress(ctx, nicCtx);
final String publicIPName = ctx.vmName + "-pip";
final String publicIPRGName = ctx.resourceGroup.name();
String msg = "Creating Azure Public IP [" + publicIPName + "] for [" + ctx.vmName + "] VM";
AzureProvisioningCallback<PublicIPAddressInner> handler = new AzureProvisioningCallback<PublicIPAddressInner>(this, msg) {
@Override
protected DeferredResult<PublicIPAddressInner> consumeProvisioningSuccess(PublicIPAddressInner publicIP) {
nicCtx.publicIP = publicIP;
return DeferredResult.completed(publicIP);
}
@Override
protected String getProvisioningState(PublicIPAddressInner publicIP) {
return publicIP.provisioningState();
}
@Override
protected Runnable checkProvisioningStateCall(ServiceCallback<PublicIPAddressInner> checkProvisioningStateCallback) {
return () -> azureClient.getByResourceGroupAsync(publicIPRGName, publicIPName, null, /* expand */
checkProvisioningStateCallback);
}
};
azureClient.createOrUpdateAsync(publicIPRGName, publicIPName, publicIPAddress, handler);
handler.toDeferredResult().thenApply(ignore -> ctx).whenComplete(thenAllocation(ctx, nextStage, NETWORK_NAMESPACE));
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureInstanceService method createStorageAccountRG.
/**
* Init storage account name and resource group, using the following approach:
* <table border=1>
* <tr>
* <th>AZURE_STORAGE_ACCOUNT_NAME</th>
* <th>AZURE_STORAGE_ACCOUNT_RG_NAME</th>
* <th>Used Parameter</th>
* </tr>
* <tr>
* <td>provided</td>
* <td>provided</td>
* <td>SA name = AZURE_STORAGE_ACCOUNT_NAME<br>
* SA RG name = AZURE_STORAGE_ACCOUNT_RG_NAME</td>
* </tr>
* <tr>
* <td>provided</td>
* <td>not provided</td>
* <td>SA name = AZURE_STORAGE_ACCOUNT_NAME<br>
* SA RG name = AZURE_STORAGE_ACCOUNT_DEFAULT_RG_NAME</td>
* </tr>
* <tr>
* <td>not provided</td>
* <td>provided</td>
* <td>SA name = generated name<br>
* SA RG name = ctx.resourceGroup.getName()</td>
* </tr>
* <tr>
* <td>not provided</td>
* <td>not provided</td>
* <td>SA name = generated name<br>
* SA RG name = ctx.resourceGroup.getName()</td>
* </tr>
* </table>
*/
private DeferredResult<AzureInstanceContext> createStorageAccountRG(AzureInstanceContext ctx) {
// Either we are reusing an existing storage account or using managed disks.
if (ctx.reuseExistingStorageAccount() || ctx.useManagedDisks()) {
return DeferredResult.completed(ctx);
} else {
ctx.storageAccountName = ctx.bootDiskState.customProperties.get(AZURE_STORAGE_ACCOUNT_NAME);
}
ctx.storageAccountRGName = ctx.bootDiskState.customProperties.getOrDefault(AZURE_STORAGE_ACCOUNT_RG_NAME, AZURE_STORAGE_ACCOUNT_DEFAULT_RG_NAME);
if (ctx.storageAccountName == null) {
// In case SA is not provided in the request, use request VA resource group
ctx.storageAccountName = String.valueOf(System.currentTimeMillis()) + "st";
ctx.storageAccountRGName = ctx.resourceGroup.name();
return DeferredResult.completed(ctx);
}
String msg = "Create/Update SA Resource Group [" + ctx.storageAccountRGName + "] for [" + ctx.vmName + "] VM";
AzureDeferredResultServiceCallback<ResourceGroupInner> handler = new Default<>(this, msg);
// Use shared RG. In case not provided in the bootDisk properties, use the default one
final ResourceGroupInner sharedSARG = new ResourceGroupInner();
sharedSARG.withLocation(ctx.child.description.regionId);
getResourceManagementClientImpl(ctx).resourceGroups().createOrUpdateAsync(ctx.storageAccountRGName, sharedSARG, handler);
return handler.toDeferredResult().thenApply(ignore -> ctx);
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method processCreateUpdateNicRequest.
/**
* Processes request for creating and updating Network interface resources.
*/
private void processCreateUpdateNicRequest(NetworkInterfaceState nic, NetworkInterfaceInner remoteNic, EnumerationContext ctx, List<DeferredResult<NetworkInterfaceState>> ops, Map<String, String> subnetPerNicId, boolean isCreate) {
nic.name = remoteNic.name();
nic.subnetLink = subnetPerNicId.get(remoteNic.id());
NicMetadata nicMeta = new NicMetadata();
nicMeta.state = nic;
nicMeta.macAddress = remoteNic.macAddress();
// else will default to original ID for PATCH requests
if (isCreate) {
nic.id = remoteNic.id();
nic.endpointLink = ctx.request.endpointLink;
AdapterUtils.addToEndpointLinks(nic, ctx.request.endpointLink);
nic.tenantLinks = ctx.parentCompute.tenantLinks;
nic.regionId = remoteNic.location();
nic.computeHostLink = ctx.parentCompute.documentSelfLink;
} else {
if (StringUtils.isEmpty(nic.endpointLink)) {
nic.endpointLink = ctx.request.endpointLink;
}
nic.endpointLinks.add(ctx.request.endpointLink);
}
List<NetworkInterfaceIPConfigurationInner> ipConfigurations = remoteNic.ipConfigurations();
if (ipConfigurations == null || ipConfigurations.isEmpty()) {
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
return;
}
NetworkInterfaceIPConfigurationInner nicIPConf = ipConfigurations.get(0);
nic.address = nicIPConf.privateIPAddress();
if (nicIPConf.publicIPAddress() == null) {
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
return;
}
// IP address is not directly available in NetworkInterfaceIPConfigurationInner.
// It is available as a SubResource, We use the SubResource ID of IP address from
// NetworkInterfaceIPConfigurationInner to obtain the IP address.
Consumer<Throwable> failure = e -> {
logWarning("Error getting public IP address from Azure [endpointLink:%s], [Exception:%s]", ctx.request.endpointLink, e.getMessage());
handleError(ctx, e);
};
PhotonModelUtils.runInExecutor(this.executorService, () -> {
Azure azure = ctx.azureSdkClients.getAzureClient();
azure.publicIPAddresses().getByIdAsync(nicIPConf.publicIPAddress().id()).subscribe(injectOperationContext(new Action1<PublicIPAddress>() {
@Override
public void call(PublicIPAddress publicIPAddress) {
nicMeta.publicIp = publicIPAddress.ipAddress();
if (publicIPAddress.inner().dnsSettings() != null) {
nicMeta.publicDnsName = publicIPAddress.inner().dnsSettings().fqdn();
}
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
}
}));
}, failure);
}
Aggregations