Skip to content

Commit

Permalink
Chore: Add tests to template resource (#105)
Browse files Browse the repository at this point in the history
* Add tests + change retry to not be a pointer + support ints in resource string

* Fix int in resource string builder

* Add test for type terraform/terragrunt

* DRY ssh key check

* Times 1 for delete mock

* Remove redundant ProviderFactories

* Do not expost test stuff to the package
  • Loading branch information
RLRabinowitz authored Jun 7, 2021
1 parent 4f5ee33 commit dc9b2b2
Show file tree
Hide file tree
Showing 4 changed files with 343 additions and 21 deletions.
6 changes: 3 additions & 3 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ type TemplateRetryOn struct {
}

type TemplateRetry struct {
OnDeploy *TemplateRetryOn `json:"onDeploy,omitempty"`
OnDestroy *TemplateRetryOn `json:"onDestroy,omitempty"`
OnDeploy TemplateRetryOn `json:"onDeploy,omitempty"`
OnDestroy TemplateRetryOn `json:"onDestroy,omitempty"`
}

type TemplateType string
Expand All @@ -97,7 +97,7 @@ type TemplateSshKey struct {
}

type TemplateCreatePayload struct {
Retry *TemplateRetry `json:"retry,omitempty"`
Retry TemplateRetry `json:"retry,omitempty"`
SshKeys []TemplateSshKey `json:"sshKeys,omitempty"`
Type TemplateType `json:"type"`
Description string `json:"description"`
Expand Down
4 changes: 2 additions & 2 deletions env0/data_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ func dataTemplateRead(ctx context.Context, d *schema.ResourceData, meta interfac
d.Set("project_ids", template.ProjectIds)
d.Set("terraform_version", template.TerraformVersion)
d.Set("ssh_keys", template.SshKeys)
if template.Retry.OnDeploy != nil {
if (template.Retry.OnDeploy != client.TemplateRetryOn{}) {
d.Set("retries_on_deploy", template.Retry.OnDeploy.Times)
d.Set("retry_on_deploy_only_when_matches_regex", template.Retry.OnDeploy.ErrorRegex)
} else {
d.Set("retries_on_deploy", 0)
d.Set("retry_on_deploy_only_when_matches_regex", "")
}
if template.Retry.OnDestroy != nil {
if (template.Retry.OnDestroy != client.TemplateRetryOn{}) {
d.Set("retries_on_destroy", template.Retry.OnDestroy.Times)
d.Set("retry_on_destroy_only_when_matches_regex", template.Retry.OnDestroy.ErrorRegex)
} else {
Expand Down
39 changes: 23 additions & 16 deletions env0/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,36 +130,43 @@ func templateCreatePayloadFromParameters(d *schema.ResourceData) (client.Templat
}
}
onDeployRetries, hasRetriesOnDeploy := d.GetOk("retries_on_deploy")
var onDeploy *client.TemplateRetryOn = nil
var onDestroy *client.TemplateRetryOn = nil
if hasRetriesOnDeploy {
if result.Retry == nil {
result.Retry = &client.TemplateRetry{}
}
result.Retry.OnDeploy = &client.TemplateRetryOn{
onDeploy = &client.TemplateRetryOn{
Times: onDeployRetries.(int),
}
}
if retryOnDeployOnlyIfMatchesRegex, ok := d.GetOk("retry_on_deploy_only_if_matches_regex"); ok {
if retryOnDeployOnlyIfMatchesRegex, ok := d.GetOk("retry_on_deploy_only_when_matches_regex"); ok {
if !hasRetriesOnDeploy {
return client.TemplateCreatePayload{}, diag.Errorf("may only specify 'retry_on_deploy_only_if_matches_regex'")
return client.TemplateCreatePayload{}, diag.Errorf("may only specify 'retry_on_deploy_only_when_matches_regex'")
}
result.Retry.OnDeploy.ErrorRegex = retryOnDeployOnlyIfMatchesRegex.(string)
onDeploy.ErrorRegex = retryOnDeployOnlyIfMatchesRegex.(string)
}

onDestroyRetries, hasRetriesOnDestroy := d.GetOk("retries_on_destroy")
if hasRetriesOnDestroy {
if result.Retry == nil {
result.Retry = &client.TemplateRetry{}
}
result.Retry.OnDestroy = &client.TemplateRetryOn{
onDestroy = &client.TemplateRetryOn{
Times: onDestroyRetries.(int),
}
}
if retryOnDestroyOnlyIfMatchesRegex, ok := d.GetOk("retry_on_destroy_only_if_matches_regex"); ok {
if retryOnDestroyOnlyIfMatchesRegex, ok := d.GetOk("retry_on_destroy_only_when_matches_regex"); ok {
if !hasRetriesOnDestroy {
return client.TemplateCreatePayload{}, diag.Errorf("may only specify 'retry_on_destroy_only_if_matches_regex'")
return client.TemplateCreatePayload{}, diag.Errorf("may only specify 'retry_on_destroy_only_when_matches_regex'")
}
result.Retry.OnDestroy.ErrorRegex = retryOnDestroyOnlyIfMatchesRegex.(string)
onDestroy.ErrorRegex = retryOnDestroyOnlyIfMatchesRegex.(string)
}

if onDeploy != nil || onDestroy != nil {
result.Retry = client.TemplateRetry{}
if onDeploy != nil {
result.Retry.OnDeploy = *onDeploy
}
if onDestroy != nil {
result.Retry.OnDestroy = *onDestroy
}
}

if terraformVersion, ok := d.GetOk("terraform_version"); ok {
result.TerraformVersion = terraformVersion.(string)
}
Expand Down Expand Up @@ -206,14 +213,14 @@ func resourceTemplateRead(ctx context.Context, d *schema.ResourceData, meta inte
}
d.Set("ssh_keys", rawSshKeys)

if template.Retry.OnDeploy != nil {
if (template.Retry.OnDeploy != client.TemplateRetryOn{}) {
d.Set("retries_on_deploy", template.Retry.OnDeploy.Times)
d.Set("retry_on_deploy_only_when_matches_regex", template.Retry.OnDeploy.ErrorRegex)
} else {
d.Set("retries_on_deploy", 0)
d.Set("retry_on_deploy_only_when_matches_regex", "")
}
if template.Retry.OnDestroy != nil {
if (template.Retry.OnDestroy != client.TemplateRetryOn{}) {
d.Set("retries_on_destroy", template.Retry.OnDestroy.Times)
d.Set("retry_on_destroy_only_when_matches_regex", template.Retry.OnDestroy.ErrorRegex)
} else {
Expand Down
Loading

0 comments on commit dc9b2b2

Please sign in to comment.