use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.
the class GcpAttachedDiskResourceBuilder method build.
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) throws Exception {
CloudInstance instance = group.getReferenceInstanceConfiguration();
InstanceTemplate template = instance.getTemplate();
Volume volume = template.getVolumes().get(0);
List<CloudResource> resources = new ArrayList<>();
List<CloudResource> syncedResources = Collections.synchronizedList(resources);
String projectId = context.getProjectId();
Location location = context.getLocation();
Compute compute = context.getCompute();
Collection<Future<Void>> futures = new ArrayList<>();
for (CloudResource cloudResource : buildableResource) {
Disk disk = createDisk(volume, projectId, location.getAvailabilityZone(), cloudResource.getName(), tags);
Future<Void> submit = intermediateBuilderExecutor.submit(() -> {
Insert insDisk = compute.disks().insert(projectId, location.getAvailabilityZone().value(), disk);
try {
Operation operation = insDisk.execute();
syncedResources.add(createOperationAwareCloudResource(cloudResource, operation));
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), cloudResource.getName());
}
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), cloudResource.getName());
}
return null;
});
futures.add(submit);
}
for (Future<Void> future : futures) {
future.get();
}
return resources;
}
use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.
the class GcpDiskResourceBuilder method build.
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResources, Map<String, String> tags) throws Exception {
String projectId = context.getProjectId();
Location location = context.getLocation();
Disk disk = new Disk();
disk.setSizeGb(DEFAULT_ROOT_DISK_SIZE);
disk.setName(buildableResources.get(0).getName());
disk.setKind(GcpDiskType.HDD.getUrl(projectId, location.getAvailabilityZone()));
Map<String, String> customTags = new HashMap<>();
customTags.putAll(tags);
customTags.putAll(defaultCostTaggingService.prepareDiskTagging());
disk.setLabels(customTags);
Insert insDisk = context.getCompute().disks().insert(projectId, location.getAvailabilityZone().value(), disk);
insDisk.setSourceImage(GcpStackUtil.getAmbariImage(projectId, image.getImageName()));
try {
Operation operation = insDisk.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResources.get(0).getName());
}
return Collections.singletonList(createOperationAwareCloudResource(buildableResources.get(0), operation));
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), buildableResources.get(0).getName());
}
}
use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilder method stopStart.
private CloudVmInstanceStatus stopStart(GcpContext context, AuthenticatedContext auth, CloudInstance instance, boolean stopRequest) {
String projectId = GcpStackUtil.getProjectId(auth.getCloudCredential());
String availabilityZone = context.getLocation().getAvailabilityZone().value();
Compute compute = context.getCompute();
String instanceId = instance.getInstanceId();
try {
Get get = compute.instances().get(projectId, availabilityZone, instanceId);
String state = stopRequest ? "RUNNING" : "TERMINATED";
if (state.equals(get.execute().getStatus())) {
Operation operation = stopRequest ? compute.instances().stop(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute() : compute.instances().start(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute();
CloudInstance operationAwareCloudInstance = createOperationAwareCloudInstance(instance, operation);
return checkInstances(context, auth, Collections.singletonList(operationAwareCloudInstance)).get(0);
} else {
LOGGER.info("Instance {} is not in {} state - won't start it.", state, instanceId);
return null;
}
} catch (IOException e) {
throw new GcpResourceException(String.format("An error occurred while stopping the vm '%s'", instanceId), e);
}
}
use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.
the class GcpSubnetResourceBuilder method build.
@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
if (isNewNetworkAndSubnet(network) || isNewSubnetInExistingNetwork(network)) {
Compute compute = context.getCompute();
String projectId = context.getProjectId();
Subnetwork gcpSubnet = new Subnetwork();
gcpSubnet.setName(resource.getName());
gcpSubnet.setIpCidrRange(network.getSubnet().getCidr());
String networkName = context.getStringParameter(GcpNetworkResourceBuilder.NETWORK_NAME);
gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, networkName));
Insert snInsert = compute.subnetworks().insert(projectId, auth.getCloudContext().getLocation().getRegion().value(), gcpSubnet);
try {
Operation operation = snInsert.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
}
context.putParameter(SUBNET_NAME, resource.getName());
return createOperationAwareCloudResource(resource, operation);
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
}
}
context.putParameter(SUBNET_NAME, resource.getName());
return new Builder().cloudResource(resource).persistent(false).build();
}
use of com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException in project cloudbreak by hortonworks.
the class GcpReservedIpResourceBuilder method build.
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) throws Exception {
List<CloudResource> result = buildableResource;
if (!buildableResource.isEmpty()) {
CloudResource resource = buildableResource.get(0);
String projectId = context.getProjectId();
String region = context.getLocation().getRegion().value();
Address address = new Address();
address.setName(resource.getName());
Map<String, String> customTags = new HashMap<>();
customTags.putAll(tags);
customTags.putAll(defaultCostTaggingService.prepareIpTagging());
address.setLabels(customTags);
Insert networkInsert = context.getCompute().addresses().insert(projectId, region, address);
try {
Operation operation = networkInsert.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
}
result = Collections.singletonList(createOperationAwareCloudResource(resource, operation));
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
}
}
return result;
}
Aggregations