Spring Boot Starter for Terraform
Перейти к файлу
microsoft-github-policy-service[bot] 5e3f7a6ac0
Auto merge mandatory file pr
This pr is auto merged as it contains a mandatory file and is opened for more than 10 days.
2022-11-28 19:11:23 +00:00
terraform-client TerraformClient.java Fixed indentation (#3) 2018-04-19 15:30:21 -07:00
terraform-spring-boot-autoconfigure Add required project description. 2018-04-13 16:34:52 -07:00
terraform-spring-boot-samples Upgrade to version 1.0.0. 2018-04-09 14:09:27 -07:00
terraform-spring-boot-starter Add required project description. 2018-04-13 16:34:52 -07:00
.gitignore Introduce Azure Terraform Client Library. 2018-03-01 01:28:12 +08:00
LICENSE Initial commit 2018-02-02 16:24:54 -08:00
README.md Update README (#5) 2021-10-12 17:19:07 -07:00
SECURITY.md Microsoft mandatory file 2022-08-15 21:15:26 +00:00
pom.xml Upgrade to version 1.0.0. 2018-04-09 14:09:27 -07:00

README.md

Terraform Spring Boot

Introduction

This repository is for Spring Boot Starters of Terraform client.

How to use it

There are two ways you could use this library. One way is to directly use the TerraformClient class which wraps the terraform executable on your local machine; and the other way is to integrate it into a Spring boot application using annotations.

Client library

Simply add the following dependency to your project's pom.xml will enable you to use the TerraformClient class.

<dependency>
    <groupId>com.microsoft.terraform</groupId>
    <artifactId>terraform-client</artifactId>
    <version>1.0.0</version>
</dependency>

And now you are able to provision terraform resources in your Java application. Make sure you have already put a terraform file storage.tf under /some/local/path/ folder; and then use the Java code snippet below to invoke terraform executable operate on the resources defined in storage.tf. In this example, we also assume that you are provisioning Azure specific resource, which means you need to set some Azure related credentials.

TerraformOptions options = new TerraformOptions();
options.setArmSubscriptionId("<Azure Subscription ID>");
options.setArmClientId("<Azure Client ID>");
options.setArmClientSecret("<Azure Client Secret>");
options.setArmTenantId("<Azure Tenant ID>");

try (TerraformClient client = new TerraformClient(options)) {
    client.setOutputListener(System.out::println);
    client.setErrorListener(System.err::println);

    client.setWorkingDirectory("/some/local/path/");
    client.plan().get();
    client.apply().get();
}

Spring boot

Let's still use the terraform file storage.tf under /some/local/path/ folder to provision Azure resources in this example. Rather than create the TerraformClient by ourselves, we let the spring boot framework to wire it for us. First add the following dependency to your pom.xml:

<dependency>
    <groupId>com.microsoft.terraform</groupId>
    <artifactId>terraform-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

And now let's also introduce the Azure credentials in application.properties:

terraform.armSubscriptionId=<Azure Subscription ID>
terraform.armClientId=<Azure Client ID>
terraform.armClientSecret=<Azure Client Secret>
terraform.armTenantId=<Azure Tenant ID>

The final step is to let the Spring framework wire up everything in your spring boot application:

@SpringBootApplication
public class SpringStarterSampleApp implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SpringStarterSampleApp.class, args);
    }

    @Autowired
    private TerraformClient terraform;

    @Override
    public void run(String... args) throws Exception {
        try {
            this.terraform.setOutputListener(System.out::println);
            this.terraform.setErrorListener(System.err::println);

            this.terraform.setWorkingDirectory("/some/local/path/");
            this.terraform.plan().get();
            this.terraform.apply().get();
        } finally {
            this.terraform.close();
        }
    }
}

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.