use of com.vmware.photon.controller.model.adapters.azure.AzureAsyncCallback in project photon-model by vmware.
the class AzureInstanceService method getStorageKeys.
/**
* Gets the storage keys from azure and patches the credential state.
*/
private void getStorageKeys(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
if (ctx.reuseExistingStorageAccount() || ctx.useManagedDisks()) {
// no need to get keys as no new storage description was created
handleAllocation(ctx, nextStage);
return;
}
StorageManagementClientImpl client = getStorageManagementClientImpl(ctx);
client.storageAccounts().listKeysAsync(ctx.storageAccountRGName, ctx.storageAccountName, new AzureAsyncCallback<StorageAccountListKeysResultInner>() {
@Override
public void onError(Throwable e) {
handleError(ctx, e);
}
@Override
public void onSuccess(StorageAccountListKeysResultInner result) {
logFine(() -> String.format("Retrieved the storage account keys for storage" + " account [%s]", ctx.storageAccountName));
AuthCredentialsServiceState storageAuth = new AuthCredentialsServiceState();
storageAuth.customProperties = new HashMap<>();
for (StorageAccountKey key : result.keys()) {
storageAuth.customProperties.put(getStorageAccountKeyName(storageAuth.customProperties), key.value());
}
storageAuth.tenantLinks = ctx.parent.tenantLinks;
Operation patchStorageDescriptionWithKeys = Operation.createPost(createInventoryUri(getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(storageAuth).setCompletion((o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
AuthCredentialsServiceState resultAuth = o.getBody(AuthCredentialsServiceState.class);
ctx.storageDescription.authCredentialsLink = resultAuth.documentSelfLink;
Operation patch = Operation.createPatch(UriUtils.buildUri(getHost(), ctx.storageDescription.documentSelfLink)).setBody(ctx.storageDescription).setCompletion(((completedOp, failure) -> {
if (failure != null) {
handleError(ctx, failure);
return;
}
logFine(() -> "Patched storage description.");
handleAllocation(ctx, nextStage);
}));
sendRequest(patch);
});
sendRequest(patchStorageDescriptionWithKeys);
}
});
}
use of com.vmware.photon.controller.model.adapters.azure.AzureAsyncCallback in project photon-model by vmware.
the class AzureInstanceService method getSubscriptionState.
private void getSubscriptionState(AzureInstanceContext ctx, String namespace, long retryExpiration) {
if (Utils.getNowMicrosUtc() > retryExpiration) {
String msg = String.format("Subscription for %s namespace did not reach %s state", namespace, PROVIDER_REGISTRED_STATE);
handleError(ctx, new RuntimeException(msg));
return;
}
ResourceManagementClientImpl client = getResourceManagementClientImpl(ctx);
getHost().schedule(() -> client.providers().getAsync(namespace, new AzureAsyncCallback<ProviderInner>() {
@Override
public void onError(Throwable e) {
handleError(ctx, e);
}
@Override
public void onSuccess(ProviderInner result) {
String registrationState = result.registrationState();
if (!PROVIDER_REGISTRED_STATE.equalsIgnoreCase(registrationState)) {
logInfo(() -> String.format("%s namespace registration in %s state", namespace, registrationState));
getSubscriptionState(ctx, namespace, retryExpiration);
return;
}
logFine(() -> String.format("Successfully registered namespace [%s]", result.namespace()));
handleAllocation(ctx);
}
}), RETRY_INTERVAL_SECONDS, TimeUnit.SECONDS);
}
use of com.vmware.photon.controller.model.adapters.azure.AzureAsyncCallback in project photon-model by vmware.
the class AzureInstanceService method createVM.
private void createVM(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
ComputeDescriptionService.ComputeDescription description = ctx.child.description;
Map<String, String> customProperties = description.customProperties;
if (customProperties == null) {
handleError(ctx, new IllegalStateException("Custom properties not specified"));
return;
}
// DiskService.DiskStateExpanded bootDisk = ctx.bootDiskState;
if (ctx.bootDiskState == null) {
handleError(ctx, new IllegalStateException("Azure bootDisk not specified"));
return;
}
String cloudConfig = null;
if (ctx.bootDiskState.bootConfig != null && ctx.bootDiskState.bootConfig.files.length > CLOUD_CONFIG_DEFAULT_FILE_INDEX) {
cloudConfig = ctx.bootDiskState.bootConfig.files[CLOUD_CONFIG_DEFAULT_FILE_INDEX].contents;
}
VirtualMachineInner request = new VirtualMachineInner();
request.withLocation(ctx.resourceGroup.location());
SubResource availabilitySetSubResource = new SubResource().withId(ctx.availabilitySet.id());
request.withAvailabilitySet(availabilitySetSubResource);
// Set OS profile.
OSProfile osProfile = new OSProfile();
osProfile.withComputerName(ctx.vmName);
if (ctx.childAuth != null) {
osProfile.withAdminUsername(ctx.childAuth.userEmail);
osProfile.withAdminPassword(EncryptionUtils.decrypt(ctx.childAuth.privateKey));
}
if (cloudConfig != null) {
try {
osProfile.withCustomData(Base64.getEncoder().encodeToString(cloudConfig.getBytes(Utils.CHARSET)));
} catch (UnsupportedEncodingException e) {
logWarning(() -> "Error encoding user data");
return;
}
}
request.withOsProfile(osProfile);
// Set hardware profile.
HardwareProfile hardwareProfile = new HardwareProfile();
hardwareProfile.withVmSize(description.instanceType != null ? VirtualMachineSizeTypes.fromString(description.instanceType) : VirtualMachineSizeTypes.BASIC_A0);
request.withHardwareProfile(hardwareProfile);
// Set storage profile.
// Create destination OS VHD
final OSDisk osDisk = newAzureOsDisk(ctx);
final StorageProfile storageProfile = new StorageProfile();
storageProfile.withOsDisk(osDisk);
List<DataDisk> dataDisks = new ArrayList<>();
List<Integer> LUNsOnImage = new ArrayList<>();
storageProfile.withImageReference(ctx.imageSource.asImageReferenceInner());
if (ctx.imageSource.type == ImageSource.Type.PRIVATE_IMAGE) {
// set LUNs of data disks present on the custom image.
final ImageState imageState = ctx.imageSource.asImageState();
if (imageState != null && imageState.diskConfigs != null) {
for (DiskConfiguration diskConfig : imageState.diskConfigs) {
if (diskConfig.properties != null && diskConfig.properties.containsKey(AzureConstants.AZURE_DISK_LUN)) {
DataDisk imageDataDisk = new DataDisk();
int lun = Integer.parseInt(diskConfig.properties.get(AzureConstants.AZURE_DISK_LUN));
LUNsOnImage.add(lun);
imageDataDisk.withLun(lun);
imageDataDisk.withCreateOption(DiskCreateOptionTypes.FROM_IMAGE);
dataDisks.add(imageDataDisk);
}
}
}
String dataDiskCaching = ctx.bootDiskState.customProperties.get(AZURE_DATA_DISK_CACHING);
if (dataDiskCaching != null) {
dataDisks.stream().forEach(dataDisk -> dataDisk.withCaching(CachingTypes.fromString(dataDiskCaching)));
}
String diskType = ctx.bootDiskState.customProperties.get(AZURE_MANAGED_DISK_TYPE);
if (diskType != null) {
ManagedDiskParametersInner managedDiskParams = new ManagedDiskParametersInner();
managedDiskParams.withStorageAccountType(StorageAccountTypes.fromString(diskType));
dataDisks.stream().forEach(dataDisk -> dataDisk.withManagedDisk(managedDiskParams));
}
}
// choose LUN greater than the one specified in case of custom image. Else start from zero.
int LUNForAdditionalDisk = LUNsOnImage.size() == 0 ? 0 : Collections.max(LUNsOnImage) + 1;
dataDisks.addAll(newAzureDataDisks(ctx, LUNForAdditionalDisk));
storageProfile.withDataDisks(dataDisks);
request.withStorageProfile(storageProfile);
// Set network profile {{
NetworkProfile networkProfile = new NetworkProfile();
networkProfile.withNetworkInterfaces(new ArrayList<>());
for (AzureNicContext nicCtx : ctx.nics) {
NetworkInterfaceReferenceInner nicRef = new NetworkInterfaceReferenceInner();
nicRef.withId(nicCtx.nic.id());
// NOTE: First NIC is marked as Primary.
nicRef.withPrimary(networkProfile.networkInterfaces().isEmpty());
networkProfile.networkInterfaces().add(nicRef);
}
request.withNetworkProfile(networkProfile);
logFine(() -> String.format("Creating virtual machine with name [%s]", ctx.vmName));
AzureAsyncCallback<VirtualMachineInner> callback = new AzureAsyncCallback<VirtualMachineInner>() {
@Override
public void onError(Throwable e) {
// exception and try again with a shorter name
if (isIncorrectNameLength(e)) {
request.osProfile().withComputerName(generateWindowsComputerName(ctx.vmName));
getComputeManagementClientImpl(ctx).virtualMachines().createOrUpdateAsync(ctx.resourceGroup.name(), ctx.vmName, request, this);
return;
}
handleCloudError(String.format("Provisioning VM %s: FAILED. Details:", ctx.vmName), ctx, COMPUTE_NAMESPACE, e);
}
// Cannot tell for sure, but these checks should be enough
private boolean isIncorrectNameLength(Throwable e) {
if (e instanceof CloudException) {
CloudException ce = (CloudException) e;
CloudError body = ce.body();
if (body != null) {
String code = body.code();
String target = body.target();
return INVALID_PARAMETER.equals(code) && COMPUTER_NAME.equals(target) && request.osProfile().computerName().length() > WINDOWS_COMPUTER_NAME_MAX_LENGTH && body.message().toLowerCase().contains("windows");
}
}
return false;
}
private String generateWindowsComputerName(String vmName) {
String computerName = vmName;
if (vmName.length() > WINDOWS_COMPUTER_NAME_MAX_LENGTH) {
// Take the first 12 and the last 3 chars of the generated VM name
computerName = vmName.substring(0, 12) + vmName.substring(vmName.length() - 3, vmName.length());
}
return computerName;
}
@Override
public void onSuccess(VirtualMachineInner result) {
logFine(() -> String.format("Successfully created vm [%s]", result.name()));
ctx.provisionedVm = result;
ComputeState cs = new ComputeState();
// Azure for some case changes the case of the vm id.
ctx.vmId = result.id().toLowerCase();
cs.id = ctx.vmId;
cs.type = ComputeType.VM_GUEST;
cs.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
cs.lifecycleState = LifecycleState.READY;
if (ctx.child.customProperties == null) {
cs.customProperties = new HashMap<>();
} else {
cs.customProperties = ctx.child.customProperties;
}
cs.customProperties.put(RESOURCE_GROUP_NAME, ctx.resourceGroup.name());
Operation.CompletionHandler completionHandler = (ox, exc) -> {
if (exc != null) {
handleError(ctx, exc);
return;
}
handleAllocation(ctx, nextStage);
};
sendRequest(Operation.createPatch(ctx.computeRequest.resourceReference).setBody(cs).setCompletion(completionHandler));
}
};
getComputeManagementClientImpl(ctx).virtualMachines().createOrUpdateAsync(ctx.resourceGroup.name(), ctx.vmName, request, callback);
}
use of com.vmware.photon.controller.model.adapters.azure.AzureAsyncCallback in project photon-model by vmware.
the class AzureComputeHostStorageStatsGatherer method getBlobUsedBytesAsync.
private void getBlobUsedBytesAsync(AzureStorageStatsDataHolder statsData, StorageMetricsStages next) {
Runnable getBlobsAsync = () -> {
String metricName = PhotonModelConstants.STORAGE_USED_BYTES;
List<ServiceStats.ServiceStat> statDatapoints = new ArrayList<>();
AtomicInteger accountsCount = new AtomicInteger(statsData.storageAccounts.size());
final List<Throwable> exs = new ArrayList<>();
for (Map.Entry<String, StorageAccount> account : statsData.storageAccounts.entrySet()) {
String resourceGroupName = getResourceGroupName(account.getValue().id);
statsData.azureClients.getAzureClient().storageAccounts().inner().listKeysAsync(resourceGroupName, account.getValue().name, new AzureAsyncCallback<StorageAccountListKeysResultInner>() {
@Override
public void onError(Throwable e) {
handleError(statsData, e);
}
@Override
public void onSuccess(StorageAccountListKeysResultInner result) {
logFine(() -> String.format("Retrieved the storage account keys for" + " storage account [%s].", account.getValue().name));
String storageConnectionString = String.format(STORAGE_CONNECTION_STRING, account.getValue().name, result.keys().get(0).value());
try {
CloudStorageAccount storageAccount = getAzureStorageClient(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
ResultContinuation nextContainerResults = null;
do {
ResultSegment<CloudBlobContainer> contSegment = blobClient.listContainersSegmented(null, ContainerListingDetails.NONE, QUERY_RESULT_LIMIT, nextContainerResults, null, null);
nextContainerResults = contSegment.getContinuationToken();
for (CloudBlobContainer container : contSegment.getResults()) {
ResultContinuation nextBlobResults = null;
do {
ResultSegment<ListBlobItem> blobsSegment = container.listBlobsSegmented(null, false, EnumSet.noneOf(BlobListingDetails.class), QUERY_RESULT_LIMIT, nextBlobResults, null, null);
nextBlobResults = blobsSegment.getContinuationToken();
for (ListBlobItem blobItem : blobsSegment.getResults()) {
if (blobItem instanceof CloudPageBlob) {
CloudPageBlob pageBlob = (CloudPageBlob) blobItem;
// TODO https://jira-hzn.eng.vmware.com/browse/VSYM-3445
try {
CloudBlob blobSnapshot = pageBlob.createSnapshot();
statsData.snapshots.add(blobSnapshot);
CloudPageBlob pageBlobSnapshot = (CloudPageBlob) blobSnapshot;
ArrayList<PageRange> pages = pageBlobSnapshot.downloadPageRanges();
// https://jira-hzn.eng.vmware.com/browse/VSYM-3355
for (PageRange pageRange : pages) {
statsData.utilizedBytes += pageRange.getEndOffset() - pageRange.getStartOffset();
}
} catch (StorageException e) {
logWarning(() -> String.format("Error getting blob size: [%s]", e.getMessage()));
}
}
}
} while (nextBlobResults != null);
}
} while (nextContainerResults != null);
} catch (Exception e) {
logWarning(() -> String.format("Exception while getting blob used bytes: %s", Utils.toString(e)));
exs.add(e);
} finally {
// in the Azure account
if (statsData.snapshots.size() > 0) {
synchronized (statsData.snapshots) {
Iterator<CloudBlob> snapshotIterator = statsData.snapshots.iterator();
while (snapshotIterator.hasNext()) {
try {
CloudBlob snapshot = snapshotIterator.next();
snapshot.deleteIfExists();
snapshotIterator.remove();
} catch (StorageException e) {
// Best effort to delete all the snapshots
logWarning(() -> String.format("Exception while deleting snapshot: %s", Utils.toString(e)));
}
}
}
}
}
// if all storage accounts were processed, create ServiceStat and finish
if (accountsCount.decrementAndGet() == 0) {
if (!exs.isEmpty()) {
handleError(statsData, exs.iterator().next());
return;
}
if (statsData.utilizedBytes != 0) {
ServiceStats.ServiceStat stat = new ServiceStats.ServiceStat();
stat.latestValue = statsData.utilizedBytes;
stat.sourceTimeMicrosUtc = TimeUnit.MILLISECONDS.toMicros(Utils.getNowMicrosUtc());
stat.unit = PhotonModelConstants.getUnitForMetric(metricName);
statDatapoints.add(stat);
}
statsData.statsResponse.statValues.put(metricName, statDatapoints);
if (statsData.statsResponse.statValues.size() == 1) {
statsData.statsResponse.computeLink = statsData.computeHostDesc.documentSelfLink;
}
statsData.stage = next;
handleStorageMetricDiscovery(statsData);
}
}
});
}
};
PhotonModelUtils.runInExecutor(this.executorService, getBlobsAsync, throwable -> handleError(statsData, throwable));
}
Aggregations