CreateCompartmentTask.groovy
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.identity.IdentityClient
021 import com.oracle.bmc.identity.model.Compartment
022 import com.oracle.bmc.identity.model.CreateCompartmentDetails
023 import com.oracle.bmc.identity.requests.CreateCompartmentRequest
024 import com.oracle.bmc.identity.requests.GetCompartmentRequest
025 import com.oracle.bmc.identity.requests.ListCompartmentsRequest
026 import groovy.transform.CompileStatic
027 import org.gradle.api.provider.Property
028 import org.gradle.api.tasks.Internal
029 import org.kordamp.gradle.plugin.oci.tasks.AbstractOCITask
030 import org.kordamp.gradle.plugin.oci.tasks.interfaces.OCITask
031 import org.kordamp.gradle.plugin.oci.tasks.traits.CompartmentDescriptionAwareTrait
032 import org.kordamp.gradle.plugin.oci.tasks.traits.CompartmentIdAwareTrait
033 import org.kordamp.gradle.plugin.oci.tasks.traits.CompartmentNameAwareTrait
034 import org.kordamp.gradle.plugin.oci.tasks.traits.VerboseAwareTrait
035 import org.kordamp.gradle.plugin.oci.tasks.traits.WaitForCompletionAwareTrait
036 import org.kordamp.jipsy.annotations.TypeProviderFor
037 
038 import static org.kordamp.gradle.plugin.oci.tasks.printers.CompartmentPrinter.printCompartment
039 
040 /**
041  @author Andres Almiray
042  @since 0.1.0
043  */
044 @CompileStatic
045 @TypeProviderFor(OCITask)
046 class CreateCompartmentTask extends AbstractOCITask implements CompartmentIdAwareTrait,
047     CompartmentNameAwareTrait,
048     CompartmentDescriptionAwareTrait,
049     WaitForCompletionAwareTrait,
050     VerboseAwareTrait {
051     static final String TASK_DESCRIPTION = 'Creates a Compartment.'
052 
053     private final Property<String> createdCompartmentId = project.objects.property(String)
054 
055     @Internal
056     Property<String> getCreatedCompartmentId() {
057         this.@createdCompartmentId
058     }
059 
060     @Override
061     void doExecuteTask() {
062         validateCompartmentId()
063         validateCompartmentName()
064         validateCompartmentDescription()
065 
066         IdentityClient client = createIdentityClient()
067 
068         Compartment compartment = maybeCreateCompartment(this,
069             client,
070             getResolvedCompartmentId().get(),
071             getResolvedCompartmentName().get(),
072             getResolvedCompartmentDescription().get(),
073             getResolvedWaitForCompletion().get(),
074             getResolvedVerbose().get())
075         createdCompartmentId.set(compartment.id)
076     }
077 
078     static Compartment maybeCreateCompartment(OCITask owner,
079                                               IdentityClient client,
080                                               String parentCompartmentId,
081                                               String compartmentName,
082                                               String compartmentDescription,
083                                               boolean waitForCompletion,
084                                               boolean verbose) {
085         // 1. Check if it exists
086         List<Compartment> compartments = client.listCompartments(ListCompartmentsRequest.builder()
087             .compartmentId(parentCompartmentId)
088             .build()).items
089         Compartment compartment = compartments.find Compartment c -> c.name == compartmentName }
090 
091         if (compartment) {
092             println("Compartment '${compartmentName}' already exists. id = ${owner.console.yellow(compartment.id)}")
093             if (verboseprintCompartment(owner, compartment, 0)
094             return compartment
095         }
096         // 2. Create
097         println('Provisioning Compartment. This may take a while.')
098         compartment = client.createCompartment(CreateCompartmentRequest.builder()
099             .createCompartmentDetails(CreateCompartmentDetails.builder()
100                 .compartmentId(parentCompartmentId)
101                 .name(compartmentName)
102                 .description(compartmentDescription)
103                 .build())
104             .build())
105             .compartment
106 
107         if (waitForCompletion) {
108             println("Waiting for Compartment to be ${owner.state('Active')}")
109             client.waiters
110                 .forCompartment(GetCompartmentRequest.builder()
111                     .compartmentId(compartment.id)
112                     .build(),
113                     Compartment.LifecycleState.Active)
114                 .execute()
115         }
116 
117         println("Compartment '${compartmentName}' has been provisioned. id = ${owner.console.yellow(compartment.id)}")
118         if (verboseprintCompartment(owner, compartment, 0)
119         compartment
120     }
121 }