package com.sequenceiq.cloudbreak.service.stack.resource.gcc.builders.instance;
import static com.sequenceiq.cloudbreak.service.stack.connector.azure.AzureStackUtil.ERROR;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.Disk;
import com.google.api.services.compute.model.Operation;
import com.google.common.base.Optional;
import com.sequenceiq.cloudbreak.controller.InternalServerException;
import com.sequenceiq.cloudbreak.controller.json.JsonHelper;
import com.sequenceiq.cloudbreak.domain.GccCredential;
import com.sequenceiq.cloudbreak.domain.GccTemplate;
import com.sequenceiq.cloudbreak.domain.Resource;
import com.sequenceiq.cloudbreak.domain.ResourceType;
import com.sequenceiq.cloudbreak.domain.Stack;
import com.sequenceiq.cloudbreak.repository.StackRepository;
import com.sequenceiq.cloudbreak.service.PollingService;
import com.sequenceiq.cloudbreak.service.stack.connector.gcc.GccDiskCheckerStatus;
import com.sequenceiq.cloudbreak.service.stack.connector.gcc.GccDiskReadyPollerObject;
import com.sequenceiq.cloudbreak.service.stack.connector.gcc.domain.GccImageType;
import com.sequenceiq.cloudbreak.service.stack.connector.gcc.GccRemoveCheckerStatus;
import com.sequenceiq.cloudbreak.service.stack.connector.gcc.GccRemoveReadyPollerObject;
import com.sequenceiq.cloudbreak.service.stack.resource.gcc.GccSimpleInstanceResourceBuilder;
import com.sequenceiq.cloudbreak.service.stack.resource.gcc.model.GccDeleteContextObject;
import com.sequenceiq.cloudbreak.service.stack.resource.gcc.model.GccDescribeContextObject;
import com.sequenceiq.cloudbreak.service.stack.resource.gcc.model.GccProvisionContextObject;
@Component
@Order(1)
public class GccDiskResourceBuilder extends GccSimpleInstanceResourceBuilder {
private static final long SIZE = 20L;
@Autowired
private StackRepository stackRepository;
@Autowired
private GccDiskCheckerStatus gccDiskCheckerStatus;
@Autowired
private PollingService<GccDiskReadyPollerObject> gccDiskReadyPollerObjectPollingService;
@Autowired
private GccRemoveCheckerStatus gccRemoveCheckerStatus;
@Autowired
private PollingService<GccRemoveReadyPollerObject> gccRemoveReadyPollerObjectPollingService;
@Autowired
private JsonHelper jsonHelper;
@Override
public List<Resource> create(GccProvisionContextObject po, int index, List<Resource> resources) throws Exception {
Stack stack = stackRepository.findById(po.getStackId());
GccTemplate template = (GccTemplate) stack.getTemplate();
String name = String.format("%s-%s", stack.getName(), index);
Disk disk = new Disk();
disk.setSizeGb(SIZE);
disk.setName(name);
disk.setKind(((GccTemplate) stack.getTemplate()).getGccRawDiskType().getUrl(po.getProjectId(), template.getGccZone()));
Compute.Disks.Insert insDisk = po.getCompute().disks().insert(po.getProjectId(), template.getGccZone().getValue(), disk);
insDisk.setSourceImage(GccImageType.DEBIAN_HACK.getAmbariUbuntu(po.getProjectId()));
insDisk.execute();
GccDiskReadyPollerObject gccDiskReady = new GccDiskReadyPollerObject(po.getCompute(), stack, name);
gccDiskReadyPollerObjectPollingService.pollWithTimeout(gccDiskCheckerStatus, gccDiskReady, POLLING_INTERVAL, MAX_POLLING_ATTEMPTS);
return Arrays.asList(new Resource(resourceType(), name, stack));
}
@Override
public Boolean delete(Resource resource, GccDeleteContextObject d) throws Exception {
Stack stack = stackRepository.findById(d.getStackId());
try {
GccTemplate gccTemplate = (GccTemplate) stack.getTemplate();
GccCredential gccCredential = (GccCredential) stack.getCredential();
Operation execute = d.getCompute().disks()
.delete(gccCredential.getProjectId(), gccTemplate.getGccZone().getValue(), resource.getResourceName()).execute();
Compute.ZoneOperations.Get zoneOperations = createZoneOperations(d.getCompute(), gccCredential, gccTemplate, execute);
Compute.GlobalOperations.Get globalOperations = createGlobalOperations(d.getCompute(), gccCredential, gccTemplate, execute);
GccRemoveReadyPollerObject gccRemoveReady =
new GccRemoveReadyPollerObject(zoneOperations, globalOperations, stack, resource.getResourceName());
gccRemoveReadyPollerObjectPollingService.pollWithTimeout(gccRemoveCheckerStatus, gccRemoveReady, POLLING_INTERVAL, MAX_POLLING_ATTEMPTS);
} catch (GoogleJsonResponseException ex) {
exceptionHandler(ex, resource.getResourceName(), stack);
} catch (IOException e) {
throw new InternalServerException(e.getMessage());
}
return true;
}
@Override
public Optional<String> describe(Resource resource, GccDescribeContextObject dco) throws Exception {
Stack stack = stackRepository.findById(dco.getStackId());
GccTemplate gccTemplate = (GccTemplate) stack.getTemplate();
GccCredential gccCredential = (GccCredential) stack.getCredential();
try {
Compute.Disks.Get getDisk =
dco.getCompute().disks().get(gccCredential.getProjectId(), gccTemplate.getGccZone().getValue(), resource.getResourceName());
return Optional.fromNullable(getDisk.execute().toPrettyString());
} catch (IOException e) {
return Optional.fromNullable(jsonHelper.createJsonFromString(String.format("{\"Disk\": {%s}}", ERROR)).toString());
}
}
@Override
public ResourceType resourceType() {
return ResourceType.GCC_DISK;
}
}