001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2019-2022 Andres Almiray.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.kordamp.gradle.plugin.oci.tasks.create
019
020 import com.oracle.bmc.model.BmcException
021 import com.oracle.bmc.objectstorage.ObjectStorage
022 import com.oracle.bmc.objectstorage.ObjectStorageClient
023 import com.oracle.bmc.objectstorage.model.Bucket
024 import com.oracle.bmc.objectstorage.model.CreateBucketDetails
025 import com.oracle.bmc.objectstorage.requests.CreateBucketRequest
026 import com.oracle.bmc.objectstorage.requests.GetBucketRequest
027 import groovy.transform.CompileStatic
028 import org.kordamp.gradle.plugin.oci.tasks.AbstractOCITask
029 import org.kordamp.gradle.plugin.oci.tasks.interfaces.OCITask
030 import org.kordamp.gradle.plugin.oci.tasks.traits.BucketNameAwareTrait
031 import org.kordamp.gradle.plugin.oci.tasks.traits.CompartmentIdAwareTrait
032 import org.kordamp.gradle.plugin.oci.tasks.traits.NamespaceNameAwareTrait
033 import org.kordamp.gradle.plugin.oci.tasks.traits.VerboseAwareTrait
034 import org.kordamp.jipsy.annotations.TypeProviderFor
035
036 import static org.kordamp.gradle.plugin.oci.tasks.printers.BucketPrinter.printBucket
037
038 /**
039 * @author Andres Almiray
040 * @since 0.3.0
041 */
042 @CompileStatic
043 @TypeProviderFor(OCITask)
044 class CreateBucketTask extends AbstractOCITask implements CompartmentIdAwareTrait,
045 NamespaceNameAwareTrait,
046 BucketNameAwareTrait,
047 VerboseAwareTrait {
048 static final String TASK_DESCRIPTION = 'Creates a Bucket.'
049
050 @Override
051 void doExecuteTask() {
052 validateCompartmentId()
053 validateNamespaceName()
054 validateBucketName()
055
056 ObjectStorageClient client = createObjectStorageClient()
057
058 maybeCreateBucket(this,
059 client,
060 getResolvedCompartmentId().get(),
061 getResolvedNamespaceName().get(),
062 getResolvedBucketName().get(),
063 getResolvedVerbose().get())
064 }
065
066 static Bucket maybeCreateBucket(OCITask owner,
067 ObjectStorage client,
068 String compartmentId,
069 String namespaceName,
070 String bucketName,
071 boolean verbose) {
072 // 1. Check if it exists
073 try {
074 List<GetBucketRequest.Fields> fields = new ArrayList<>(2)
075 fields.add(GetBucketRequest.Fields.ApproximateCount)
076 fields.add(GetBucketRequest.Fields.ApproximateSize)
077
078 Bucket bucket = client.getBucket(GetBucketRequest.builder()
079 .namespaceName(namespaceName)
080 .bucketName(bucketName)
081 .fields(fields)
082 .build())
083 .bucket
084 println("Bucket '${bucketName}' already exists.")
085 if (verbose) printBucket(owner, bucket, 0)
086 return bucket
087 } catch (BmcException e) {
088 // exception most likely means the bucket does not exist, continue
089 }
090
091 // 2. Create
092 println('Provisioning Bucket. This may take a while.')
093 Bucket bucket = client.createBucket(CreateBucketRequest.builder()
094 .namespaceName(namespaceName)
095 .createBucketDetails(CreateBucketDetails.builder()
096 .compartmentId(compartmentId)
097 .name(bucketName)
098 .build())
099 .build())
100 .bucket
101
102 println("Bucket '${bucketName}' has been provisioned.")
103 if (verbose) printBucket(owner, bucket, 0)
104 bucket
105 }
106 }
|