@Override
public void setupProvisioning(Stack stack) {
MDCBuilder.buildMdcContext(stack);
try {
Storage storage = gccStackUtil.buildStorage((GccCredential) stack.getCredential(), stack);
Compute compute = gccStackUtil.buildCompute((GccCredential) stack.getCredential(), stack.getName());
GccTemplate template = (GccTemplate) stack.getTemplate();
GccCredential credential = (GccCredential) stack.getCredential();
ImageList list = compute.images().list(credential.getProjectId()).execute();
Long time = new Date().getTime();
if (!containsSpecificImage(list)) {
try {
Bucket bucket = new Bucket();
bucket.setName(credential.getProjectId() + time);
bucket.setStorageClass("STANDARD");
Storage.Buckets.Insert ins = storage.buckets().insert(credential.getProjectId(), bucket);
ins.execute();
} catch (GoogleJsonResponseException ex) {
if (ex.getStatusCode() != CONFLICT) {
throw ex;
}
}
Storage.Objects.Copy copy = storage.objects().copy(BUCKET_NAME, TAR_NAME, credential.getProjectId() + time, TAR_NAME,
new StorageObject());
copy.execute();
Image image = new Image();
image.setName(GccImageType.DEBIAN_HACK.getImageName());
Image.RawDisk rawDisk = new Image.RawDisk();
rawDisk.setSource(String.format("http://storage.googleapis.com/%s/%s", credential.getProjectId() + time, TAR_NAME));
image.setRawDisk(rawDisk);
Compute.Images.Insert ins1 = compute.images().insert(credential.getProjectId(), image);
ins1.execute();
}
} catch (IOException e) {
LOGGER.error(String.format("Error occurs on %s stack under the setup", stack.getId()), e);
throw new InternalServerException(e.getMessage());