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.core.VirtualNetworkClient
021 import com.oracle.bmc.core.model.CreateSubnetDetails
022 import com.oracle.bmc.core.model.Subnet
023 import com.oracle.bmc.core.requests.CreateSubnetRequest
024 import com.oracle.bmc.core.requests.GetSubnetRequest
025 import com.oracle.bmc.core.requests.ListSubnetsRequest
026 import com.oracle.bmc.identity.IdentityClient
027 import com.oracle.bmc.identity.model.AvailabilityDomain
028 import groovy.transform.CompileStatic
029 import org.gradle.api.provider.Property
030 import org.gradle.api.tasks.Internal
031 import org.kordamp.gradle.plugin.oci.tasks.AbstractOCITask
032 import org.kordamp.gradle.plugin.oci.tasks.interfaces.OCITask
033 import org.kordamp.gradle.plugin.oci.tasks.traits.AvailabilityDomainAwareTrait
034 import org.kordamp.gradle.plugin.oci.tasks.traits.CompartmentIdAwareTrait
035 import org.kordamp.gradle.plugin.oci.tasks.traits.OptionalDnsLabelAwareTrait
036 import org.kordamp.gradle.plugin.oci.tasks.traits.SubnetNameAwareTrait
037 import org.kordamp.gradle.plugin.oci.tasks.traits.VcnIdAwareTrait
038 import org.kordamp.gradle.plugin.oci.tasks.traits.VerboseAwareTrait
039 import org.kordamp.gradle.plugin.oci.tasks.traits.WaitForCompletionAwareTrait
040 import org.kordamp.jipsy.annotations.TypeProviderFor
041
042 import static org.kordamp.gradle.plugin.oci.tasks.printers.SubnetPrinter.printSubnet
043
044 /**
045 * @author Andres Almiray
046 * @since 0.1.0
047 */
048 @CompileStatic
049 @TypeProviderFor(OCITask)
050 class CreateSubnetTask extends AbstractOCITask implements CompartmentIdAwareTrait,
051 AvailabilityDomainAwareTrait,
052 VcnIdAwareTrait,
053 SubnetNameAwareTrait,
054 OptionalDnsLabelAwareTrait,
055 WaitForCompletionAwareTrait,
056 VerboseAwareTrait {
057 static final String TASK_DESCRIPTION = 'Creates a Subnet.'
058
059 private final Property<String> createdSubnetId = project.objects.property(String)
060
061 @Internal
062 Property<String> getCreatedSubnetId() {
063 this.@createdSubnetId
064 }
065
066 @Override
067 void doExecuteTask() {
068 validateCompartmentId()
069 validateVcnId()
070 validateAvailabilityDomain()
071 validateSubnetName()
072 validateDnsLabel(getResolvedVcnId().get())
073
074 VirtualNetworkClient client = createVirtualNetworkClient()
075 IdentityClient identityClient = createIdentityClient()
076
077 AvailabilityDomain _availabilityDomain = validateAvailabilityDomain(identityClient, getResolvedCompartmentId().get())
078
079 Subnet subnet = maybeCreateSubnet(this,
080 client,
081 getResolvedCompartmentId().get(),
082 getResolvedVcnId().get(),
083 getResolvedDnsLabel().get(),
084 _availabilityDomain.name,
085 getResolvedSubnetName().get(),
086 '10.0.0.0/24',
087 getResolvedWaitForCompletion().get(),
088 getResolvedVerbose().get())
089 createdSubnetId.set(subnet.id)
090 }
091
092 static Subnet maybeCreateSubnet(OCITask owner,
093 VirtualNetworkClient client,
094 String compartmentId,
095 String vcnId,
096 String dnsLabel,
097 String availabilityDomain,
098 String subnetName,
099 String cidrBlock,
100 boolean waitForCompletion,
101 boolean verbose) {
102 // 1. Check if it exists
103 List<Subnet> subnets = client.listSubnets(ListSubnetsRequest.builder()
104 .compartmentId(compartmentId)
105 .vcnId(vcnId)
106 .displayName(subnetName)
107 .build())
108 .items
109
110 if (!subnets.empty) {
111 Subnet subnet = subnets[0]
112 println("Subnet '${subnetName}' already exists. id = ${owner.console.yellow(subnet.id)}")
113 if (verbose) printSubnet(owner, subnet, 0)
114 return subnets[0]
115 }
116
117 Subnet subnet = client.createSubnet(CreateSubnetRequest.builder()
118 .createSubnetDetails(CreateSubnetDetails.builder()
119 .compartmentId(compartmentId)
120 .vcnId(vcnId)
121 .availabilityDomain(availabilityDomain)
122 .displayName(subnetName)
123 .dnsLabel(dnsLabel)
124 .cidrBlock(cidrBlock)
125 .build())
126 .build())
127 .subnet
128
129 if (waitForCompletion) {
130 println("Waiting for Subnet to be ${owner.state('Available')}")
131 client.waiters.forSubnet(GetSubnetRequest.builder()
132 .subnetId(subnet.id)
133 .build(),
134 Subnet.LifecycleState.Available)
135 .execute()
136 }
137
138 println("Subnet '${subnetName}' has been provisioned. id = ${owner.console.yellow(subnet.id)}")
139 if (verbose) printSubnet(owner, subnet, 0)
140 subnet
141 }
142 }
|