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.apigateway.GatewayClient
021 import com.oracle.bmc.apigateway.model.CreateGatewayDetails
022 import com.oracle.bmc.apigateway.model.Gateway
023 import com.oracle.bmc.apigateway.model.Gateway.EndpointType
024 import com.oracle.bmc.apigateway.model.GatewaySummary
025 import com.oracle.bmc.apigateway.requests.CreateGatewayRequest
026 import com.oracle.bmc.apigateway.requests.GetGatewayRequest
027 import com.oracle.bmc.apigateway.requests.ListGatewaysRequest
028 import groovy.transform.CompileStatic
029 import org.gradle.api.provider.Property
030 import org.gradle.api.provider.Provider
031 import org.gradle.api.tasks.Input
032 import org.gradle.api.tasks.Internal
033 import org.gradle.api.tasks.options.Option
034 import org.gradle.api.tasks.options.OptionValues
035 import org.kordamp.gradle.plugin.oci.tasks.AbstractOCITask
036 import org.kordamp.gradle.plugin.oci.tasks.interfaces.OCITask
037 import org.kordamp.gradle.plugin.oci.tasks.traits.CompartmentIdAwareTrait
038 import org.kordamp.gradle.plugin.oci.tasks.traits.GatewayNameAwareTrait
039 import org.kordamp.gradle.plugin.oci.tasks.traits.SubnetIdAwareTrait
040 import org.kordamp.gradle.plugin.oci.tasks.traits.VerboseAwareTrait
041 import org.kordamp.gradle.plugin.oci.tasks.traits.WaitForCompletionAwareTrait
042 import org.kordamp.jipsy.annotations.TypeProviderFor
043
044 import static org.kordamp.gradle.PropertyUtils.resolveValue
045 import static org.kordamp.gradle.plugin.oci.tasks.printers.GatewayPrinter.printGateway
046 import static org.kordamp.gradle.util.StringUtils.isNotBlank
047
048 /**
049 * @author Andres Almiray
050 * @since 0.5.0
051 */
052 @CompileStatic
053 @TypeProviderFor(OCITask)
054 class CreateGatewayTask extends AbstractOCITask implements CompartmentIdAwareTrait,
055 SubnetIdAwareTrait,
056 GatewayNameAwareTrait,
057 WaitForCompletionAwareTrait,
058 VerboseAwareTrait {
059 static final String TASK_DESCRIPTION = 'Creates a Gateway.'
060 private final Property<String> createdGatewayId = project.objects.property(String)
061
062 @Internal
063 Property<String> getCreatedGatewayId() {
064 this.@createdGatewayId
065 }
066
067 @Internal
068 final Property<EndpointType> endpointType = project.objects.property(EndpointType)
069
070 @Option(option = 'endpoint-type', description = 'The endpointType to use (OPTIONAL).')
071 void setEndpointType(EndpointType endpointType) {
072 getEndpointType().set(endpointType)
073 }
074
075 @Input
076 Provider<EndpointType> getResolvedEndpointType() {
077 project.providers.provider {
078 String value = resolveValue('OCI_GATEWAY_ENDPOINT_TYPE', 'oci.gateway.endpoint.type', this, project.name)
079 isNotBlank(value) ? EndpointType.valueOf(value) : endpointType.getOrElse(EndpointType.Public)
080 }
081 }
082
083 @OptionValues("endpoint-type")
084 List<EndpointType> getAvailableEndpointTypes() {
085 return new ArrayList<EndpointType>(Arrays.asList(EndpointType.values()))
086 }
087
088 @Override
089 void doExecuteTask() {
090 validateCompartmentId()
091 validateSubnetId()
092 validateGatewayName()
093
094 GatewayClient client = createGatewayClient()
095
096 GatewaySummary gateway = maybeCreateGateway(this,
097 client,
098 getResolvedCompartmentId().get(),
099 getResolvedSubnetId().get(),
100 getResolvedGatewayName().get(),
101 getResolvedEndpointType().get(),
102 getResolvedWaitForCompletion().get(),
103 getResolvedVerbose().get())
104 createdGatewayId.set(gateway.id)
105 }
106
107 static GatewaySummary maybeCreateGateway(OCITask owner,
108 GatewayClient client,
109 String compartmentId,
110 String subnetId,
111 String gatewayName,
112 EndpointType endpointType,
113 boolean waitForCompletion,
114 boolean verbose) {
115 // 1. Check if it exists
116 List<GatewaySummary> gateways = client.listGateways(ListGatewaysRequest.builder()
117 .compartmentId(compartmentId)
118 .build())
119 .gatewayCollection
120 .items
121
122 GatewaySummary gatewaySummary = gateways.find { GatewaySummary ig -> ig.displayName == gatewayName }
123
124 if (gatewaySummary) {
125 println("Gateway '${gatewayName}' already exists. id = ${gatewaySummary.id}")
126 if (verbose) printGateway(owner, gatewaySummary, 0)
127 return gatewaySummary
128 }
129
130 if (!gateways.empty) {
131 gatewaySummary = gateways[0]
132 println("Gateway '${gatewaySummary.displayName}' exists. id = ${gatewaySummary.id}")
133 if (verbose) printGateway(owner, gatewaySummary, 0)
134 return gatewaySummary
135 }
136
137 Gateway gateway = client.createGateway(CreateGatewayRequest.builder()
138 .createGatewayDetails(CreateGatewayDetails.builder()
139 .compartmentId(compartmentId)
140 .subnetId(subnetId)
141 .displayName(gatewayName)
142 .endpointType(endpointType)
143 .build())
144 .build())
145 .gateway
146
147 if (waitForCompletion) {
148 println("Waiting for Gateway to be ${owner.state('Available')}")
149 client.waiters.forGateway(GetGatewayRequest.builder()
150 .gatewayId(gateway.id)
151 .build(),
152 Gateway.LifecycleState.Active)
153 .execute()
154 }
155
156 gateways = client.listGateways(ListGatewaysRequest.builder()
157 .compartmentId(compartmentId)
158 .displayName(gatewayName)
159 .build())
160 .gatewayCollection
161 .items
162
163 println("Gateway '${gatewayName}' has been provisioned. id = ${owner.console.yellow(gateway.id)}")
164 if (verbose) printGateway(owner, gateways[0], 0)
165 gateways[0]
166 }
167 }
|