-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: environment scheduling resource (#208)
- Loading branch information
Showing
14 changed files
with
332 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "env0_environment_scheduling Resource - terraform-provider-env0" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# env0_environment_scheduling (Resource) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "env0_environment" "example" { | ||
name = "Environment Name" | ||
} | ||
resource "env0_environment_schedling" "example" { | ||
environment_id = data.env0_environment.example.id | ||
deploy_cron = "5 * * * *" | ||
destroy_cron = "10 * * * *" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- **environment_id** (String) The environment's id | ||
|
||
### Optional | ||
|
||
- **deploy_cron** (String) Cron expression for scheduled deploy of the environment. Destroy and Deploy cron expressions must not be the same. | ||
- **destroy_cron** (String) Cron expression for scheduled destroy of the environment. Destroy and Deploy cron expressions must not be the same. | ||
- **id** (String) The ID of this resource. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
"github.com/adhocore/gronx" | ||
. "github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceEnvironmentScheduling() *schema.Resource { | ||
|
||
validateCronExpression := func(i interface{}, path cty.Path) diag.Diagnostics { | ||
expr := i.(string) | ||
parser := gronx.New() | ||
isValid := parser.IsValid(expr) | ||
|
||
if isValid != true { | ||
return diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "Invalid cron expression", | ||
AttributePath: path, | ||
}} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return &schema.Resource{ | ||
CreateContext: resourceEnvironmentSchedulingCreateOrUpdate, | ||
ReadContext: resourceEnvironmentSchedulingRead, | ||
UpdateContext: resourceEnvironmentSchedulingCreateOrUpdate, | ||
DeleteContext: resourceEnvironmentSchedulingDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"environment_id": { | ||
Type: schema.TypeString, | ||
Description: "The environment's id", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"destroy_cron": { | ||
Type: schema.TypeString, | ||
Description: "Cron expression for scheduled destroy of the environment. Destroy and Deploy cron expressions must not be the same.", | ||
AtLeastOneOf: []string{"destroy_cron", "deploy_cron"}, | ||
Optional: true, | ||
ValidateDiagFunc: validateCronExpression, | ||
}, | ||
"deploy_cron": { | ||
Type: schema.TypeString, | ||
Description: "Cron expression for scheduled deploy of the environment. Destroy and Deploy cron expressions must not be the same.", | ||
AtLeastOneOf: []string{"destroy_cron", "deploy_cron"}, | ||
Optional: true, | ||
ValidateDiagFunc: validateCronExpression, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceEnvironmentSchedulingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(ApiClientInterface) | ||
|
||
environmentId := d.Id() | ||
|
||
environmentScheduling, err := apiClient.EnvironmentScheduling(environmentId) | ||
|
||
if err != nil { | ||
return diag.Errorf("could not get environment scheduling: %v", err) | ||
} | ||
|
||
if environmentScheduling.Deploy != nil { | ||
d.Set("deploy_cron", environmentScheduling.Deploy.Cron) | ||
} else { | ||
d.Set("deploy_cron", "") | ||
} | ||
|
||
if environmentScheduling.Destroy != nil { | ||
d.Set("destroy_cron", environmentScheduling.Destroy.Cron) | ||
} else { | ||
d.Set("destroy_cron", "") | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceEnvironmentSchedulingCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(ApiClientInterface) | ||
|
||
environmentId := d.Get("environment_id").(string) | ||
deployCron := d.Get("deploy_cron").(string) | ||
destroyCron := d.Get("destroy_cron").(string) | ||
|
||
payload := EnvironmentScheduling{} | ||
|
||
if deployCron != "" { | ||
payload.Deploy = &EnvironmentSchedulingExpression{Cron: deployCron, Enabled: true} | ||
} | ||
|
||
if destroyCron != "" { | ||
payload.Destroy = &EnvironmentSchedulingExpression{Cron: destroyCron, Enabled: true} | ||
} | ||
|
||
_, err := apiClient.EnvironmentSchedulingUpdate(environmentId, payload) | ||
|
||
if err != nil { | ||
return diag.Errorf("could not create or update environment scheduling: %v", err) | ||
} | ||
|
||
d.SetId(environmentId) | ||
return nil | ||
} | ||
|
||
func resourceEnvironmentSchedulingDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(ApiClientInterface) | ||
|
||
environmentId := d.Id() | ||
|
||
err := apiClient.EnvironmentSchedulingDelete(environmentId) | ||
|
||
if err != nil { | ||
return diag.Errorf("could not delete environment scheduling: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package env0 | ||
|
||
import ( | ||
"fmt" | ||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestUnitEnvironmentSchedulingResource(t *testing.T) { | ||
environmentId := "environment0" | ||
resourceType := "env0_environment_scheduling" | ||
resourceName := "test" | ||
accessor := resourceAccessor(resourceType, resourceName) | ||
environmentScheduling := client.EnvironmentScheduling{ | ||
Deploy: &client.EnvironmentSchedulingExpression{Cron: "1 * * * *", Enabled: true}, | ||
Destroy: &client.EnvironmentSchedulingExpression{Cron: "2 * * * *", Enabled: true}, | ||
} | ||
|
||
cronExprKeys := []string{"deploy_cron", "destroy_cron"} | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"deploy_cron": environmentScheduling.Deploy.Cron, | ||
"destroy_cron": environmentScheduling.Destroy.Cron, | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "environment_id", environmentId), | ||
resource.TestCheckResourceAttr(accessor, "deploy_cron", environmentScheduling.Deploy.Cron), | ||
resource.TestCheckResourceAttr(accessor, "destroy_cron", environmentScheduling.Destroy.Cron), | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().EnvironmentSchedulingUpdate(environmentId, environmentScheduling).Times(1).Return(environmentScheduling, nil) | ||
mock.EXPECT().EnvironmentScheduling(environmentId).Times(1).Return(environmentScheduling, nil) | ||
mock.EXPECT().EnvironmentSchedulingDelete(environmentId).Times(1).Return(nil) | ||
}) | ||
}) | ||
|
||
for _, key := range cronExprKeys { | ||
t.Run(fmt.Sprintf("Failure due to invalid cron expression for %s", key), func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
key: "not_a_valid_cron", | ||
}), | ||
ExpectError: regexp.MustCompile("Invalid cron expression"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {}) | ||
|
||
}) | ||
} | ||
|
||
t.Run("Failure when both deploy_cron and destroy_cron attributes are missing", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
}), | ||
ExpectError: regexp.MustCompile("AtLeastOne"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {}) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
data "env0_environment" "example" { | ||
name = "Environment Name" | ||
} | ||
|
||
resource "env0_environment_schedling" "example" { | ||
environment_id = data.env0_environment.example.id | ||
deploy_cron = "5 * * * *" | ||
destroy_cron = "10 * * * *" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.