use of com.google.api.services.compute.Compute in project halyard by spinnaker.
the class GoogleBaseImageValidator method validate.
@Override
public void validate(ConfigProblemSetBuilder p, GoogleBaseImage n) {
String sourceImage = n.getVirtualizationSettings().getSourceImage();
String sourceImageFamily = n.getVirtualizationSettings().getSourceImageFamily();
if (StringUtils.isEmpty(sourceImage) && StringUtils.isEmpty(sourceImageFamily)) {
p.addProblem(Problem.Severity.ERROR, "Either source image or source image family must be specified for " + n.getBaseImage().getId() + ".");
}
if (!StringUtils.isEmpty(sourceImage)) {
int i = 0;
boolean[] foundSourceImageHolder = new boolean[1];
while (!foundSourceImageHolder[0] && i < credentialsList.size()) {
GoogleNamedAccountCredentials credentials = credentialsList.get(i);
List<String> imageProjects = Lists.newArrayList(credentials.getProject());
imageProjects.addAll(credentials.getImageProjects());
imageProjects.addAll(baseImageProjects);
Compute compute = credentials.getCompute();
BatchRequest imageListBatch = buildBatchRequest(compute);
JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
@Override
public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
// No need to look through these images if the requested image was already found.
if (!foundSourceImageHolder[0]) {
if (imageList.getItems() != null) {
foundSourceImageHolder[0] = imageList.getItems().stream().filter(image -> image.getName().equals(sourceImage)).findFirst().isPresent();
}
}
}
};
try {
for (String imageProject : imageProjects) {
compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
}
imageListBatch.execute();
} catch (IOException e) {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
i++;
}
if (!foundSourceImageHolder[0]) {
p.addProblem(Problem.Severity.ERROR, "Image " + sourceImage + " not found via any configured google account.");
}
}
if (!StringUtils.isEmpty(sourceImageFamily)) {
int i = 0;
boolean[] foundSourceImageFamilyHolder = new boolean[1];
while (!foundSourceImageFamilyHolder[0] && i < credentialsList.size()) {
GoogleNamedAccountCredentials credentials = credentialsList.get(i);
List<String> imageProjects = Lists.newArrayList(credentials.getProject());
imageProjects.addAll(credentials.getImageProjects());
imageProjects.addAll(baseImageProjects);
Compute compute = credentials.getCompute();
BatchRequest imageListBatch = buildBatchRequest(compute);
JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
@Override
public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
// No need to look through these images if the requested image family was already found.
if (!foundSourceImageFamilyHolder[0]) {
if (imageList.getItems() != null) {
foundSourceImageFamilyHolder[0] = imageList.getItems().stream().filter(image -> sourceImageFamily.equals(image.getFamily())).findFirst().isPresent();
}
}
}
};
try {
for (String imageProject : imageProjects) {
compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
}
imageListBatch.execute();
} catch (IOException e) {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
i++;
}
if (!foundSourceImageFamilyHolder[0]) {
p.addProblem(Problem.Severity.ERROR, "Image family " + sourceImageFamily + " not found via any configured google account.");
}
}
if (StringUtils.isEmpty(n.getBaseImage().getPackageType())) {
p.addProblem(Problem.Severity.ERROR, "Package type must be specified for " + n.getBaseImage().getId() + ".");
}
}
use of com.google.api.services.compute.Compute in project halyard by spinnaker.
the class GoogleCanaryAccountValidator method validate.
@Override
public void validate(ConfigProblemSetBuilder p, AbstractCanaryAccount n) {
super.validate(p, n);
GoogleCanaryAccount canaryAccount = (GoogleCanaryAccount) n;
DaemonTaskHandler.message("Validating " + n.getNodeName() + " with " + GoogleCanaryAccountValidator.class.getSimpleName());
GoogleNamedAccountCredentials credentials = canaryAccount.getNamedAccountCredentials(halyardVersion, p);
if (credentials == null) {
return;
}
try {
Compute compute = credentials.getCompute();
compute.projects().get(canaryAccount.getProject()).execute();
} catch (IOException e) {
p.addProblem(Severity.ERROR, "Failed to load project \"" + canaryAccount.getProject() + "\": " + e.getMessage() + ".");
}
}
use of com.google.api.services.compute.Compute 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<>());
}
}
use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.
the class GcpPlatformResources method securityGroups.
@Override
public CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws IOException {
Compute compute = GcpStackUtil.buildCompute(cloudCredential);
String projectId = GcpStackUtil.getProjectId(cloudCredential);
Map<String, Set<CloudSecurityGroup>> result = new HashMap<>();
if (compute != null) {
FirewallList firewallList = compute.firewalls().list(projectId).execute();
for (Firewall firewall : firewallList.getItems()) {
Map<String, Object> properties = new HashMap<>();
properties.put("network", getNetworkName(firewall));
CloudSecurityGroup cloudSecurityGroup = new CloudSecurityGroup(firewall.getName(), firewall.getName(), properties);
result.computeIfAbsent(region.value(), k -> new HashSet<>()).add(cloudSecurityGroup);
}
}
return new CloudSecurityGroups(result);
}
use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.
the class GcpPlatformResources method networks.
@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws Exception {
Compute compute = GcpStackUtil.buildCompute(cloudCredential);
String projectId = GcpStackUtil.getProjectId(cloudCredential);
Map<String, Set<CloudNetwork>> result = new HashMap<>();
Set<CloudNetwork> cloudNetworks = new HashSet<>();
if (compute != null) {
NetworkList networkList = compute.networks().list(projectId).execute();
List<Subnetwork> subnetworkList = compute.subnetworks().list(projectId, region.value()).execute().getItems();
for (Network network : networkList.getItems()) {
Map<String, Object> properties = new HashMap<>();
properties.put("gatewayIPv4", Strings.nullToEmpty(network.getGatewayIPv4()));
properties.put("description", Strings.nullToEmpty(network.getDescription()));
properties.put("IPv4Range", Strings.nullToEmpty(network.getIPv4Range()));
properties.put("creationTimestamp", Strings.nullToEmpty(network.getCreationTimestamp()));
Map<String, String> subnets = new HashMap<>();
if (subnetworkList != null && network.getSubnetworks() != null) {
for (Subnetwork subnetwork : subnetworkList) {
if (network.getSubnetworks().contains(subnetwork.getSelfLink())) {
subnets.put(subnetwork.getName(), subnetwork.getName());
}
}
}
CloudNetwork cloudNetwork = new CloudNetwork(network.getName(), network.getId().toString(), subnets, properties);
cloudNetworks.add(cloudNetwork);
}
result.put(region.value(), cloudNetworks);
}
return new CloudNetworks(result);
}
Aggregations