Search in sources :

Example 11 with AvailabilityZone

use of com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone in project cloudbreak by hortonworks.

the class GcpPlatformResources method virtualMachines.

@Override
@Cacheable(cacheNames = "cloudResourceVmTypeCache", key = "#cloudCredential?.id + #region.getRegionName()")
public CloudVmTypes virtualMachines(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    Map<String, Set<VmType>> cloudVmResponses = new HashMap<>();
    Map<String, VmType> defaultCloudVmResponses = new HashMap<>();
    try {
        Set<VmType> types = new HashSet<>();
        VmType defaultVmType = null;
        CloudRegions regions = regions(cloudCredential, region, filters);
        for (AvailabilityZone availabilityZone : regions.getCloudRegions().get(region)) {
            MachineTypeList machineTypeList = compute.machineTypes().list(projectId, availabilityZone.value()).execute();
            for (MachineType machineType : machineTypeList.getItems()) {
                VmTypeMeta vmTypeMeta = VmTypeMetaBuilder.builder().withCpuAndMemory(machineType.getGuestCpus(), machineType.getMemoryMb().floatValue() / THOUSAND).withMagneticConfig(TEN, machineType.getMaximumPersistentDisksSizeGb().intValue(), 1, machineType.getMaximumPersistentDisksSizeGb().intValue()).withSsdConfig(TEN, machineType.getMaximumPersistentDisksSizeGb().intValue(), 1, machineType.getMaximumPersistentDisks()).withMaximumPersistentDisksSizeGb(machineType.getMaximumPersistentDisksSizeGb().toString()).create();
                VmType vmType = VmType.vmTypeWithMeta(machineType.getName(), vmTypeMeta, true);
                types.add(vmType);
                if (machineType.getName().equals(gcpVmDefault)) {
                    defaultVmType = vmType;
                }
            }
            cloudVmResponses.put(availabilityZone.value(), types);
            defaultCloudVmResponses.put(availabilityZone.value(), defaultVmType);
        }
        return new CloudVmTypes(cloudVmResponses, defaultCloudVmResponses);
    } catch (Exception e) {
        return new CloudVmTypes(new HashMap<>(), new HashMap<>());
    }
}
Also used : VmTypeMeta(com.sequenceiq.cloudbreak.cloud.model.VmTypeMeta) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) MachineType(com.google.api.services.compute.model.MachineType) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) IOException(java.io.IOException) CloudVmTypes(com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes) Compute(com.google.api.services.compute.Compute) VmType(com.sequenceiq.cloudbreak.cloud.model.VmType) MachineTypeList(com.google.api.services.compute.model.MachineTypeList) HashSet(java.util.HashSet) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 12 with AvailabilityZone

use of com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone in project cloudbreak by hortonworks.

the class GcpPlatformResources method regions.

@Override
@Cacheable(cacheNames = "cloudResourceRegionCache", key = "#cloudCredential?.id")
public CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws Exception {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    Map<Region, List<AvailabilityZone>> regionListMap = new HashMap<>();
    Map<Region, String> displayNames = new HashMap<>();
    String defaultRegion = gcpZoneParameterDefault;
    RegionList regionList = compute.regions().list(projectId).execute();
    for (com.google.api.services.compute.model.Region gcpRegion : regionList.getItems()) {
        if (region == null || Strings.isNullOrEmpty(region.value()) || gcpRegion.getName().equals(region.value())) {
            List<AvailabilityZone> availabilityZones = new ArrayList<>();
            for (String s : gcpRegion.getZones()) {
                String[] split = s.split("/");
                if (split.length > 0) {
                    availabilityZones.add(AvailabilityZone.availabilityZone(split[split.length - 1]));
                }
            }
            regionListMap.put(region(gcpRegion.getName()), availabilityZones);
            displayNames.put(region(gcpRegion.getName()), displayName(gcpRegion.getName()));
        }
    }
    if (region != null && !Strings.isNullOrEmpty(region.value())) {
        defaultRegion = region.value();
    }
    return new CloudRegions(regionListMap, displayNames, defaultRegion);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) Compute(com.google.api.services.compute.Compute) Region(com.sequenceiq.cloudbreak.cloud.model.Region) ArrayList(java.util.ArrayList) NetworkList(com.google.api.services.compute.model.NetworkList) RegionList(com.google.api.services.compute.model.RegionList) List(java.util.List) MachineTypeList(com.google.api.services.compute.model.MachineTypeList) FirewallList(com.google.api.services.compute.model.FirewallList) RegionList(com.google.api.services.compute.model.RegionList) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 13 with AvailabilityZone

use of com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone in project cloudbreak by hortonworks.

the class HeatTemplateBuilder method build.

public String build(ModelContext modelContext) {
    try {
        List<NovaInstanceView> novaInstances = new OpenStackGroupView(modelContext.stackName, modelContext.groups, modelContext.tags).getFlatNovaView();
        Map<String, Object> model = new HashMap<>();
        model.put("cb_stack_name", openStackUtil.adjustStackNameLength(modelContext.stackName));
        model.put("agents", novaInstances);
        model.put("core_user_data", formatUserData(modelContext.instanceUserData.getUserDataByType(InstanceGroupType.CORE)));
        model.put("gateway_user_data", formatUserData(modelContext.instanceUserData.getUserDataByType(InstanceGroupType.GATEWAY)));
        model.put("groups", modelContext.groups);
        model.put("existingNetwork", modelContext.existingNetwork);
        model.put("existingSubnet", modelContext.existingSubnet);
        model.put("network", modelContext.neutronNetworkView);
        model.putAll(defaultCostTaggingService.prepareAllTagsForTemplate());
        AvailabilityZone az = modelContext.location.getAvailabilityZone();
        if (az != null && az.value() != null) {
            model.put("availability_zone", az.value());
        }
        Template template = new Template(openStackHeatTemplatePath, modelContext.templateString, freemarkerConfiguration);
        String generatedTemplate = processTemplateIntoString(template, model);
        LOGGER.debug("Generated Heat template: {}", generatedTemplate);
        return generatedTemplate;
    } catch (IOException | TemplateException e) {
        throw new CloudConnectorException("Failed to process the OpenStack HeatTemplateBuilder", e);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) FreeMarkerTemplateUtils.processTemplateIntoString(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils.processTemplateIntoString) IOException(java.io.IOException) NovaInstanceView(com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView) Template(freemarker.template.Template) OpenStackGroupView(com.sequenceiq.cloudbreak.cloud.openstack.view.OpenStackGroupView)

Example 14 with AvailabilityZone

use of com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone in project cloudbreak by hortonworks.

the class OpenStackPlatformResourcesTest method testVirtualMachinesWhenTypesEmpty.

@Test
public void testVirtualMachinesWhenTypesEmpty() {
    CloudCredential cloudCredential = new CloudCredential(0L, "name");
    OSClient osClient = mock(OSClient.class);
    String regionName = "region1";
    Set<String> regionsFromOpenStack = Sets.newHashSet(regionName);
    List<AvailabilityZone> availabilityZones = newArrayList(new AvailabilityZone("zone1"));
    when(openStackClient.createOSClient(cloudCredential)).thenReturn(osClient);
    when(openStackClient.getRegion(cloudCredential)).thenReturn(regionsFromOpenStack);
    when(openStackClient.getZones(osClient, regionName)).thenReturn(availabilityZones);
    when(openStackClient.getFlavors(osClient)).thenReturn(emptyList());
    CloudVmTypes actual = underTest.virtualMachines(cloudCredential, null, null);
    verify(openStackClient, times(1)).getFlavors(osClient);
    Assert.assertEquals(0, actual.getCloudVmResponses().get("zone1").size());
    Assert.assertNull(actual.getDefaultCloudVmResponses().get(regionName));
}
Also used : CloudVmTypes(com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) OSClient(org.openstack4j.api.OSClient) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) Test(org.junit.Test)

Example 15 with AvailabilityZone

use of com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone in project cloudbreak by hortonworks.

the class OpenStackPlatformResources method publicIpPool.

@Override
public CloudIpPools publicIpPool(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    OSClient<?> osClient = openStackClient.createOSClient(cloudCredential);
    Map<String, Set<CloudIpPool>> cloudIpPools = new HashMap<>();
    CloudRegions regions = regions(cloudCredential, region, filters);
    for (Entry<Region, List<AvailabilityZone>> regionListEntry : regions.getCloudRegions().entrySet()) {
        Set<CloudIpPool> cloudGateWays = new HashSet<>();
        List<? extends Network> networks = getNetworks(osClient);
        List<? extends Network> networksWithExternalRouter = networks.stream().filter(Network::isRouterExternal).collect(Collectors.toList());
        for (Network network : networksWithExternalRouter) {
            CloudIpPool cloudIpPool = new CloudIpPool();
            cloudIpPool.setId(network.getId());
            cloudIpPool.setName(network.getName());
            cloudGateWays.add(cloudIpPool);
        }
        for (AvailabilityZone availabilityZone : regionListEntry.getValue()) {
            cloudIpPools.put(availabilityZone.value(), cloudGateWays);
        }
    }
    LOGGER.info("openstack public ip pool result: {}", cloudIpPools);
    return new CloudIpPools(cloudIpPools);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) CloudIpPool(com.sequenceiq.cloudbreak.cloud.model.CloudIpPool) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) CloudIpPools(com.sequenceiq.cloudbreak.cloud.model.CloudIpPools) Network(org.openstack4j.model.network.Network) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) Region(com.sequenceiq.cloudbreak.cloud.model.Region) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)17 HashMap (java.util.HashMap)13 List (java.util.List)10 Region (com.sequenceiq.cloudbreak.cloud.model.Region)9 CloudRegions (com.sequenceiq.cloudbreak.cloud.model.CloudRegions)8 HashSet (java.util.HashSet)7 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 Cacheable (org.springframework.cache.annotation.Cacheable)5 CloudVmTypes (com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes)4 Test (org.junit.Test)4 VmType (com.sequenceiq.cloudbreak.cloud.model.VmType)3 IOException (java.io.IOException)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2 Compute (com.google.api.services.compute.Compute)2 MachineTypeList (com.google.api.services.compute.model.MachineTypeList)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 CloudGateWay (com.sequenceiq.cloudbreak.cloud.model.CloudGateWay)2 CloudGateWays (com.sequenceiq.cloudbreak.cloud.model.CloudGateWays)2 Collections.singletonList (java.util.Collections.singletonList)2