use of com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient in project cloudbreak by hortonworks.
the class AzureCredentialConnector method verify.
@Override
public CloudCredentialStatus verify(AuthenticatedContext authenticatedContext) {
try {
AzureClient client = authenticatedContext.getParameter(AzureClient.class);
client.getStorageAccounts().list();
} catch (RuntimeException e) {
LOGGER.error(e.getMessage(), e);
return new CloudCredentialStatus(authenticatedContext.getCloudCredential(), CredentialStatus.FAILED, e, e.getMessage());
}
return new CloudCredentialStatus(authenticatedContext.getCloudCredential(), CredentialStatus.VERIFIED);
}
use of com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient in project cloudbreak by hortonworks.
the class AzureInstanceConnector method stop.
@Override
public List<CloudVmInstanceStatus> stop(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
String stackName = armTemplateUtils.getStackName(ac.getCloudContext());
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
for (CloudInstance vm : vms) {
try {
AzureClient azureClient = ac.getParameter(AzureClient.class);
azureClient.deallocateVirtualMachine(stackName, vm.getInstanceId());
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
} catch (RuntimeException e) {
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.FAILED, e.getMessage()));
}
}
return statuses;
}
use of com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient in project cloudbreak by hortonworks.
the class AzureMetadataCollector method collect.
@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
CloudResource resource = azureUtils.getTemplateResource(resources);
List<CloudVmMetaDataStatus> results = new ArrayList<>();
List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
String resourceName = resource.getName();
Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> azureUtils.getPrivateInstanceId(resourceName, from.getGroupName(), Long.toString(from.getPrivateId())));
try {
for (Entry<String, InstanceTemplate> instance : templateMap.entrySet()) {
AzureClient azureClient = authenticatedContext.getParameter(AzureClient.class);
VirtualMachine vm = azureClient.getVirtualMachine(resourceName, instance.getKey());
String subnetId = vm.getPrimaryNetworkInterface().primaryIPConfiguration().subnetName();
String privateIp = null;
String publicIp = null;
Integer faultDomainCount = azureClient.getFaultDomainNumber(resourceName, vm.name());
String platform = authenticatedContext.getCloudContext().getPlatform().value();
String location = authenticatedContext.getCloudContext().getLocation().getRegion().value();
String hostgroupNm = instance.getValue().getGroupName();
StringBuilder localityIndicatorBuilder = new StringBuilder();
localityIndicatorBuilder.append(LOCALITY_SEPARATOR).append(platform).append(LOCALITY_SEPARATOR).append(location).append(LOCALITY_SEPARATOR).append(resourceName).append(LOCALITY_SEPARATOR).append(hostgroupNm).append(LOCALITY_SEPARATOR).append(faultDomainCount);
AzureUtils.removeBlankSpace(localityIndicatorBuilder);
List<String> networkInterfaceIdList = vm.networkInterfaceIds();
for (String networkInterfaceId : networkInterfaceIdList) {
NetworkInterface networkInterface = azureClient.getNetworkInterfaceById(networkInterfaceId);
privateIp = networkInterface.primaryPrivateIP();
PublicIPAddress publicIpAddress = networkInterface.primaryIPConfiguration().getPublicIPAddress();
List<LoadBalancerBackend> backends = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerBackends();
List<LoadBalancerInboundNatRule> inboundNatRules = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerInboundNatRules();
if (!backends.isEmpty() || !inboundNatRules.isEmpty()) {
publicIp = azureClient.getLoadBalancerIps(resource.getName(), azureUtils.getLoadBalancerId(resource.getName())).get(0);
}
if (publicIpAddress != null && publicIpAddress.ipAddress() != null) {
publicIp = publicIpAddress.ipAddress();
}
}
String instanceId = instance.getKey();
CloudInstanceMetaData md = new CloudInstanceMetaData(privateIp, publicIp, faultDomainCount == null ? null : localityIndicatorBuilder.toString());
InstanceTemplate template = templateMap.get(instanceId);
if (template != null) {
Map<String, Object> params = new HashMap<>(1);
params.put(CloudInstance.SUBNET_ID, subnetId);
// TODO use shhkey here
CloudInstance cloudInstance = new CloudInstance(instanceId, template, null, params);
CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
results.add(new CloudVmMetaDataStatus(status, md));
}
}
} catch (RuntimeException e) {
throw new CloudConnectorException(e.getMessage(), e);
}
return results;
}
use of com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient in project cloudbreak by hortonworks.
the class AzurePlatformResources method virtualMachines.
@Override
@Cacheable(cacheNames = "cloudResourceVmTypeCache", key = "#cloudCredential?.id + #region.getRegionName()")
public CloudVmTypes virtualMachines(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
AzureClient client = azureClientService.getClient(cloudCredential);
Set<VirtualMachineSize> vmTypes = client.getVmTypes(region.value());
Map<String, Set<VmType>> cloudVmResponses = new HashMap<>();
Map<String, VmType> defaultCloudVmResponses = new HashMap<>();
Set<VmType> types = new HashSet<>();
VmType defaultVmType = null;
for (VirtualMachineSize virtualMachineSize : vmTypes) {
float memoryInGB = virtualMachineSize.memoryInMB() / NO_MB_PER_GB;
VmTypeMetaBuilder builder = VmTypeMetaBuilder.builder().withCpuAndMemory(virtualMachineSize.numberOfCores(), memoryInGB);
for (VolumeParameterType volumeParameterType : values()) {
switch(volumeParameterType) {
case MAGNETIC:
builder.withMagneticConfig(volumeParameterConfig(MAGNETIC, virtualMachineSize));
break;
default:
break;
}
}
VmType vmType = VmType.vmTypeWithMeta(virtualMachineSize.name(), builder.create(), true);
types.add(vmType);
if (virtualMachineSize.name().equals(armVmDefault)) {
defaultVmType = vmType;
}
}
cloudVmResponses.put(region.value(), types);
defaultCloudVmResponses.put(region.value(), defaultVmType);
return new CloudVmTypes(cloudVmResponses, defaultCloudVmResponses);
}
use of com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient in project cloudbreak by hortonworks.
the class AzureResourceConnector method upscale.
@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
AzureClient client = authenticatedContext.getParameter(AzureClient.class);
AzureCredentialView azureCredentialView = new AzureCredentialView(authenticatedContext.getCloudCredential());
String stackName = azureUtils.getStackName(authenticatedContext.getCloudContext());
AzureStackView azureStackView = getAzureStack(azureCredentialView, authenticatedContext.getCloudContext(), stack, getNumberOfAvailableIPsInSubnets(client, stack.getNetwork()));
String customImageId = azureStorage.getCustomImageId(client, authenticatedContext, stack);
String template = azureTemplateBuilder.build(stackName, customImageId, azureCredentialView, azureStackView, authenticatedContext.getCloudContext(), stack);
String parameters = azureTemplateBuilder.buildParameters(authenticatedContext.getCloudCredential(), stack.getNetwork(), stack.getImage());
String resourceGroupName = azureUtils.getResourceGroupName(authenticatedContext.getCloudContext());
try {
String region = authenticatedContext.getCloudContext().getLocation().getRegion().value();
Map<String, AzureDiskType> storageAccounts = azureStackView.getStorageAccounts();
for (Entry<String, AzureDiskType> entry : storageAccounts.entrySet()) {
azureStorage.createStorage(client, entry.getKey(), entry.getValue(), resourceGroupName, region, azureStorage.isEncrytionNeeded(stack.getParameters()), stack.getTags());
}
Deployment templateDeployment = client.createTemplateDeployment(stackName, stackName, template, parameters);
LOGGER.info("created template deployment for upscale: {}", templateDeployment.exportTemplate().template());
CloudResource armTemplate = resources.stream().filter(r -> r.getType() == ResourceType.ARM_TEMPLATE).findFirst().orElseThrow(() -> new CloudConnectorException(String.format("Arm Template not found for: %s ", stackName)));
return Collections.singletonList(new CloudResourceStatus(armTemplate, ResourceStatus.IN_PROGRESS));
} catch (CloudException e) {
LOGGER.error("Upscale error, cloud exception happened: ", e);
if (e.body() != null && e.body().details() != null) {
String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", "));
throw new CloudConnectorException(String.format("Stack upscale failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details));
} else {
throw new CloudConnectorException(String.format("Stack upscale failed: '%s', please go to Azure Portal for detailed message", e));
}
} catch (Exception e) {
throw new CloudConnectorException(String.format("Could not upscale: %s ", stackName), e);
}
}
Aggregations