Skip to content

Commit

Permalink
terraform-boot - Enable async processing of deploy and destroy API me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
WangLiNaruto committed Aug 24, 2023
1 parent bf23036 commit 2fc0dc8
Show file tree
Hide file tree
Showing 17 changed files with 680 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public TerraformResult destroy(

/**
* Method to async deploy resources requested in a workspace.
*
*/
@Tag(name = "Terraform", description = "APIs for running Terraform commands")
@Operation(description = "async deploy resources via Terraform")
Expand All @@ -130,7 +129,6 @@ public void asyncDeploy(

/**
* Method to async destroy resources requested in a workspace.
*
*/
@Tag(name = "Terraform", description = "APIs for running Terraform commands")
@Operation(description = "Async destroy the Terraform modules")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.terraform.boot.config;

import org.eclipse.xpanse.terraform.boot.models.enums.Csp;
import org.eclipse.xpanse.terraform.boot.models.exceptions.TerraformExecutorException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
* Version configuration classes for Terraform cloud providers.
*/
@Configuration
public class TerraformVersionProvider {

@Value("${terraform.provider.huaweicloud.version}")
private String terraformHuaweiCloudVersion;

@Value("${terraform.provider.flexibleengine.version}")
private String terraformFlexibleEngineVersion;

@Value("${terraform.provider.openstack.version}")
private String terraformOpenStackVersion;

@Value("${terraform.provider.aws.version}")
private String terraformAwsCloudVersion;

@Value("${terraform.provider.scs.version}")
private String terraformScsVersion;

/**
* get the Terraform version corresponding to the cloud provider.
*/
public String getTerraformVersionByCsp(Csp csp) {
if (csp == Csp.HUAWEI) {
return terraformHuaweiCloudVersion;
} else if (csp == Csp.FLEXIBLE_ENGINE) {
return terraformFlexibleEngineVersion;
} else if (csp == Csp.OPENSTACK) {
return terraformOpenStackVersion;
} else if (csp == Csp.AWS) {
return terraformAwsCloudVersion;
} else if (csp == Csp.SCS) {
return terraformScsVersion;
} else {
throw new TerraformExecutorException("Get Terraform Version,Csp does not exist");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.terraform.boot.models.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.commons.lang3.StringUtils;

/**
* Cloud service providers.
*/
public enum Csp {
HUAWEI("huawei"),
FLEXIBLE_ENGINE("flexibleEngine"),
OPENSTACK("openstack"),
ALICLOUD("alicloud"),
AWS("aws"),
AZURE("azure"),
GOOGLE("google"),
SCS("scs");

private final String value;

Csp(String value) {
this.value = value;
}

/**
* For CSP serialize.
*/
@JsonCreator
public static Csp getByValue(String name) {
for (Csp csp : values()) {
if (StringUtils.equalsIgnoreCase(csp.value, name)) {
return csp;
}
}
return null;
}

/**
* For CSP deserialize.
*/
@JsonValue
public String toValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.terraform.boot.models.enums;

import java.util.Objects;
import org.eclipse.xpanse.terraform.boot.models.exceptions.TerraformProviderNotFoundException;
import org.eclipse.xpanse.terraform.boot.models.providers.Aws;
import org.eclipse.xpanse.terraform.boot.models.providers.FlexibleEngine;
import org.eclipse.xpanse.terraform.boot.models.providers.Huawei;
import org.eclipse.xpanse.terraform.boot.models.providers.Openstack;
import org.eclipse.xpanse.terraform.boot.models.providers.Provider;
import org.eclipse.xpanse.terraform.boot.models.providers.Scs;

/**
* Terraform providers.
*/
public enum TerraformProviders {
AWS(Csp.AWS, new Aws()),
HUAWEI(Csp.HUAWEI, new Huawei()),
FLEXIBLE_ENGINE(Csp.FLEXIBLE_ENGINE, new FlexibleEngine()),
OPENSTACK(Csp.OPENSTACK, new Openstack()),
SCS(Csp.SCS, new Scs());

final Csp csp;

final Provider provider;

/**
* Constructor for TerraformProviders.
*
* @param csp the cloud service provider.
* @param provider the provider for the CSP.
*/
TerraformProviders(Csp csp, Provider provider) {
this.csp = csp;
this.provider = provider;
}

/**
* Get the provider by the Csp.
*
* @param csp the cloud service provider.
*/
public static Provider getProvider(Csp csp) {
for (TerraformProviders provider : values()) {
if (Objects.equals(csp, provider.csp)) {
return provider.provider;
}
}

throw new TerraformProviderNotFoundException("Cannot find provider " + csp + ".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public TerraformExecutorException(String message) {
super("TFExecutor Exception: " + message);
}

public TerraformExecutorException(String message, Throwable ex) {
super(message, ex);
}

public TerraformExecutorException(String message, String output) {
super("Executor Exception:" + message + System.lineSeparator() + output);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.terraform.boot.models.exceptions;

/**
* Exception thrown when Terraform Provider configuration for the requested Csp is not available.
*/
public class TerraformProviderNotFoundException extends RuntimeException {
public TerraformProviderNotFoundException(String message) {
super(message);
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.terraform.boot.models.providers;


import org.eclipse.xpanse.terraform.boot.models.enums.Csp;

/**
* Terraform provider information for AWS.
*/
public class Aws implements Provider {

public static final String PROVIDER = """
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "%s"
}
}
}
provider "aws" {
region = "%s"
}
""";

@Override
public String getProvider(String version, String region) {
return String.format(PROVIDER, version, region);
}

@Override
public Csp getCsp() {
return Csp.AWS;
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.terraform.boot.models.providers;

import org.eclipse.xpanse.terraform.boot.models.enums.Csp;

/**
* Terraform provider information for Huawei.
*/
public class FlexibleEngine implements Provider {

public static final String PROVIDER = """
terraform {
required_providers {
flexibleengine = {
source = "FlexibleEngineCloud/flexibleengine"
version = "%s"
}
}
}
provider "flexibleengine" {
region = "%s"
}
""";

@Override
public String getProvider(String version, String region) {
return String.format(PROVIDER, version, region);
}

@Override
public Csp getCsp() {
return Csp.FLEXIBLE_ENGINE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.terraform.boot.models.providers;

import org.eclipse.xpanse.terraform.boot.models.enums.Csp;

/**
* Terraform provider information for Huawei.
*/
public class Huawei implements Provider {

public static final String PROVIDER = """
terraform {
required_providers {
huaweicloud = {
source = "huaweicloud/huaweicloud"
version = "%s"
}
}
}
provider "huaweicloud" {
region = "%s"
}
""";

@Override
public String getProvider(String version, String region) {
return String.format(PROVIDER, version, region);
}

@Override
public Csp getCsp() {
return Csp.HUAWEI;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.terraform.boot.models.providers;

import org.eclipse.xpanse.terraform.boot.models.enums.Csp;

/**
* Terraform provider information for Openstack.
*/
public class Openstack implements Provider {

public static final String PROVIDER = """
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "%s"
}
}
}
provider "openstack" {
region = "%s"
}
""";

@Override
public String getProvider(String version, String region) {
return String.format(PROVIDER, version, region);
}

@Override
public Csp getCsp() {
return Csp.OPENSTACK;
}

}
Loading

0 comments on commit 2fc0dc8

Please sign in to comment.