Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feat_cloud_cost_credentials-#218
  • Loading branch information
samuel-br committed Mar 20, 2022
2 parents 8c7b00f + adcf90b commit 9bb4679
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 49 deletions.
91 changes: 91 additions & 0 deletions .github/env0_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Docs

on:
push:
branches:
- main

env:
GO_VERSION: 1.16
Expand All @@ -14,6 +16,8 @@ jobs:
-
name: Checkout
uses: actions/checkout@v2
with:
token: ${{ secrets.ENV0_BOT_PAT }}

- name: Unshallow
run: git fetch --prune --unshallow
Expand Down
63 changes: 35 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# terraform-provider-env0
<a href="https://env0.com">
<img src=".github/env0_logo.svg" alt="env0 logo" title="env0" align="right" height="40" />
</a>

Terraform provider to interact with env0
# Terraform Provider for env0

Available in the [Terraform Registry](https://registry.terraform.io/providers/env0/env0/latest)
[![Go Report Card](https://goreportcard.com/badge/github.com/env0/terraform-provider-env0)](https://goreportcard.com/report/github.com/env0/terraform-provider-env0)

The full list of supported resources is available [here](https://registry.terraform.io/providers/env0/env0/latest/docs).
- [Documentation](https://registry.terraform.io/providers/env0/env0/latest/docs)
- [Usage Examples](https://github.com/env0/terraform-provider-env0/tree/main/examples)

## Example usage
## Quick Start

```terraform
terraform {
required_providers {
env0 = {
source = "env0/env0"
version = "~> 0.0.13"
}
}
}
provider "env0" {}
data "env0_project" "default_project" {
name = "Default Organization Project"
name = "My First Project"
}
resource "env0_template" "example" {
Expand All @@ -40,31 +42,34 @@ resource "env0_configuration_variable" "in_a_template" {

## Authentication

First, generate an `api_key` and `api_secret` from the organization settings page.
1. Generate an `api_key` and `api_secret` from the Organization Settings page.
See [here](https://developer.env0.com/docs/api/YXBpOjY4Njc2-env0-api#creating-an-api-key).

These can be provided by one of two methods. First method consists of setting `ENV0_API_KEY` and `ENV0_API_SECRET` environment variables, and just declaring the provider with no parameters:
2. These can be provided by one of two methods:

```terraform
provider "env0" {}
```
1. Set `ENV0_API_KEY` and `ENV0_API_SECRET` environment variables, and just declaring the provider with no parameters:

The second method would be to specify these fields as parameters to the provider:
```terraform
provider "env0" {}
```

```terraform
variable "env0_api_key" {}
variable "env0_api_secret" {}
2. Specify these fields as parameters to the provider:

provider "env0" {
api_key = var.env0_api_key
api_secret = var.env0_api_secret
}
```
```terraform
variable "env0_api_key" {}
variable "env0_api_secret" {}
provider "env0" {
api_key = var.env0_api_key
api_secret = var.env0_api_secret
}
```

## Development Setup

## Dev setup
> **Supported Go Version: 1.16**
**make sure you run with go version 1.16**
### Build
### Build
- Use the `./build.sh` script.
- The output binary is called `terraform-provider-env0`

Expand Down Expand Up @@ -149,7 +154,9 @@ go generate ./...
- Please add an example to `examples/<resources or data-sources>/env0_<name>` dir and make sure it is added to the docs.

## Release
To release a version to the [Terraform Public Registry](https://registry.terraform.io/providers/env0/env0/latest?pollNotifications=true) -
1. Create and push a tag **locally**, in semver format - `git tag v0.0.9 && git push origin --tags`
2. New release with binaries **will be automatically generated** by the GitHub action defined in `.github/workflows/release.yml`.
3. The Registry will automatically pick up on the new version.
To release a version to the [Terraform Public Registry](https://registry.terraform.io/providers/env0/env0/latest?pollNotifications=true):
1. Validate that all status checks are ✅ on `main` branch (specifically that docs generation is complete)
2. Pull from remote first - `git pull origin main`
3. Create and push a tag **locally**, in semver format - `git tag v0.0.9 && git push origin --tags`
4. New release with binaries **will be automatically generated** by the GitHub action defined in `.github/workflows/release.yml`.
5. The Registry will automatically pick up on the new version.
1 change: 1 addition & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type ApiClientInterface interface {
TeamProjectAssignmentDelete(assignmentId string) error
TeamProjectAssignments(projectId string) ([]TeamProjectAssignment, error)
Environments() ([]Environment, error)
ProjectEnvironments(projectId string) ([]Environment, error)
Environment(id string) (Environment, error)
EnvironmentCreate(payload EnvironmentCreate) (Environment, error)
EnvironmentDestroy(id string) (Environment, error)
Expand Down
15 changes: 15 additions & 0 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ func (self *ApiClient) Environments() ([]Environment, error) {
return result, nil
}

func (self *ApiClient) ProjectEnvironments(projectId string) ([]Environment, error) {

var result []Environment
err := self.http.Get("/environments", map[string]string{"projectId": projectId}, &result)

if err != nil {
return []Environment{}, err
}
return result, nil
}

func (self *ApiClient) Environment(id string) (Environment, error) {
var result Environment
err := self.http.Get("/environments/"+id, nil, &result)
Expand Down
3 changes: 3 additions & 0 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type EnvironmentCreate struct {
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob,omitempty"`
ConfigurationChanges *ConfigurationChanges `json:"configurationChanges,omitempty"`
TTL *TTL `json:"ttl,omitempty"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
}

type DeployRequest struct {
Expand Down Expand Up @@ -90,6 +91,7 @@ type EnvironmentUpdate struct {
PullRequestPlanDeployments *bool `json:"pullRequestPlanDeployments,omitempty"`
AutoDeployOnPathChangesOnly *bool `json:"autoDeployOnPathChangesOnly,omitempty"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob,omitempty"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
}

type EnvironmentDeployResponse struct {
Expand Down Expand Up @@ -296,6 +298,7 @@ type Environment struct {
LatestDeploymentLogId string `json:"latestDeploymentLogId"`
LatestDeploymentLog DeploymentLog `json:"latestDeploymentLog"`
IsArchived bool `json:"isArchived"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
}

type DeploymentLog struct {
Expand Down
1 change: 1 addition & 0 deletions docs/resources/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ resource "env0_environment" "example" {
- **id** (String) the environment's id
- **revision** (String) the revision the environment is to be run against
- **run_plan_on_pull_requests** (Boolean) should run terraform plan on pull requests creations
- **terragrunt_working_directory** (String) The working directory path to be used by a Terragrunt template. If left empty '/' is used.
- **ttl** (String) the date the environment should be destroyed at (iso format). omitting this attribute will result in infinite ttl.
- **workspace** (String) the terraform workspace of the environment

Expand Down
1 change: 1 addition & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ resource "env0_project" "example" {
### Optional

- **description** (String) description of the project
- **force_destroy** (Boolean) Destroy the project even when environments exist

### Read-Only

Expand Down
10 changes: 10 additions & 0 deletions docs/resources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ resource "env0_template" "example" {
ssh_keys = [data.env0_ssh_key.my_key]
}
resource "env0_template" "example_terragrunt" {
name = "example - Terragrunt"
description = "Example template with Terragrunt version"
repository = "https://github.com/env0/templates"
path = "terragrunt/misc/null-resource"
ssh_keys = [data.env0_ssh_key.my_key]
type = "terragrunt"
terragrunt_version = "0.35.0"
}
resource "env0_template_project_assignment" "assignment" {
template_id = env0_template.example.id
project_id = data.env0_project.default_project.id
Expand Down
13 changes: 12 additions & 1 deletion env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func resourceEnvironment() *schema.Resource {
Description: "destroy safegurad",
Optional: true,
},
"terragrunt_working_directory": {
Type: schema.TypeString,
Description: "The working directory path to be used by a Terragrunt template. If left empty '/' is used.",
Optional: true,
},
"configuration": {
Type: schema.TypeList,
Description: "terraform and environment variables for the environment",
Expand Down Expand Up @@ -183,6 +188,7 @@ func setEnvironmentSchema(d *schema.ResourceData, environment client.Environment
d.Set("workspace", environment.WorkspaceName)
d.Set("auto_deploy_by_custom_glob", environment.AutoDeployByCustomGlob)
d.Set("ttl", environment.LifespanEndAt)
d.Set("terragrunt_working_directory", environment.TerragruntWorkingDirectory)
if environment.LatestDeploymentLog != (client.DeploymentLog{}) {
d.Set("template_id", environment.LatestDeploymentLog.BlueprintId)
d.Set("revision", environment.LatestDeploymentLog.BlueprintRevision)
Expand Down Expand Up @@ -296,7 +302,7 @@ func shouldDeploy(d *schema.ResourceData) bool {
}

func shouldUpdate(d *schema.ResourceData) bool {
return d.HasChanges("name", "approve_plan_automatically", "deploy_on_push", "run_plan_on_pull_requests", "auto_deploy_by_custom_glob", "auto_deploy_on_path_changes_only")
return d.HasChanges("name", "approve_plan_automatically", "deploy_on_push", "run_plan_on_pull_requests", "auto_deploy_by_custom_glob", "auto_deploy_on_path_changes_only", "terragrunt_working_directory")
}

func shouldUpdateTTL(d *schema.ResourceData) bool {
Expand Down Expand Up @@ -367,6 +373,10 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac
payload.WorkspaceName = workspace.(string)
}

if terragruntWorkingDirectory, ok := d.GetOk("terragrunt_working_directory"); ok {
payload.TerragruntWorkingDirectory = terragruntWorkingDirectory.(string)
}

continuousDeployment := d.Get("deploy_on_push").(bool)
if d.HasChange("deploy_on_push") {
payload.ContinuousDeployment = &continuousDeployment
Expand Down Expand Up @@ -442,6 +452,7 @@ func getUpdatePayload(d *schema.ResourceData) (client.EnvironmentUpdate, diag.Di
autoDeployOnPathChangesOnly := d.Get("auto_deploy_on_path_changes_only").(bool)
payload.AutoDeployOnPathChangesOnly = &autoDeployOnPathChangesOnly
payload.AutoDeployByCustomGlob = d.Get("auto_deploy_by_custom_glob").(string)
payload.TerragruntWorkingDirectory = d.Get("terragrunt_working_directory").(string)

err := assertDeploymentTriggers(payload.AutoDeployByCustomGlob, continuousDeployment, pullRequestPlanDeployments, autoDeployOnPathChangesOnly)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions env0/resource_environment_scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env0

import (
"context"

. "github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down
Loading

0 comments on commit 9bb4679

Please sign in to comment.