Skip to content

Commit

Permalink
Feat: resource template has 'terraform' type default value - change t…
Browse files Browse the repository at this point in the history
…o 'opentofu' (#990)
  • Loading branch information
TomerHeber authored Dec 11, 2024
1 parent 8f43f97 commit d658cfa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
2 changes: 1 addition & 1 deletion env0/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema {
Type: schema.TypeString,
Description: fmt.Sprintf("template type (allowed values: %s)", strings.Join(allowedTemplateTypes, ", ")),
Optional: true,
Default: client.TERRAFORM,
Default: client.OPENTOFU,
ValidateDiagFunc: NewStringInValidator(allowedTemplateTypes),
},
"revision": {
Expand Down
88 changes: 54 additions & 34 deletions env0/resource_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func TestUnitTemplateResource(t *testing.T) {

const defaultVersion = "0.15.1"

const defaultType = "terraform"
const defaultType = client.OPENTOFU

const defaultOpentofuVersion = "1.6.0"

var resourceFullName = resourceAccessor(resourceType, resourceName)

Expand Down Expand Up @@ -814,13 +816,15 @@ func TestUnitTemplateResource(t *testing.T) {
Repository: template.Repository,
TerraformVersion: defaultVersion,
Type: string(defaultType),
OpentofuVersion: defaultOpentofuVersion,
}

basicTemplateResourceConfig := func(resourceType string, resourceName string, template client.Template) string {
return resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"name": template.Name,
"repository": template.Repository,
"terraform_version": defaultVersion,
"opentofu_version": defaultOpentofuVersion,
})
}

Expand All @@ -834,6 +838,7 @@ func TestUnitTemplateResource(t *testing.T) {
resource.TestCheckResourceAttr(resourceFullName, "repository", template.Repository),
resource.TestCheckResourceAttr(resourceFullName, "type", string(defaultType)),
resource.TestCheckResourceAttr(resourceFullName, "terraform_version", defaultVersion),
resource.TestCheckResourceAttr(resourceFullName, "opentofu_version", defaultOpentofuVersion),
),
},
},
Expand All @@ -842,10 +847,10 @@ func TestUnitTemplateResource(t *testing.T) {
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().Template(template.Id).AnyTimes().Return(templateWithDefaults, nil)
mock.EXPECT().TemplateCreate(client.TemplateCreatePayload{
Name: template.Name,
Repository: template.Repository,
Type: defaultType,
TerraformVersion: defaultVersion,
Name: template.Name,
Repository: template.Repository,
Type: defaultType,
OpentofuVersion: defaultOpentofuVersion,
}).Times(1).Return(template, nil)
mock.EXPECT().TemplateDelete(template.Id).Times(1).Return(nil)
})
Expand Down Expand Up @@ -898,6 +903,7 @@ func TestUnitTemplateResource(t *testing.T) {
TerraformVersion: defaultVersion,
Type: string(defaultType),
SshKeys: []client.TemplateSshKey{initialSshKey1, initialSshKey2},
OpentofuVersion: defaultOpentofuVersion,
}

updatedTemplate := client.Template{
Expand All @@ -907,6 +913,7 @@ func TestUnitTemplateResource(t *testing.T) {
TerraformVersion: defaultVersion,
Type: string(defaultType),
SshKeys: []client.TemplateSshKey{updatedSshKey1, updatedSshKey2},
OpentofuVersion: defaultOpentofuVersion,
}

sshKeyTemplateResourceConfig := func(name string, repository string, sshKey1 client.TemplateSshKey, sshKey2 client.TemplateSshKey) string {
Expand All @@ -916,14 +923,15 @@ func TestUnitTemplateResource(t *testing.T) {
repository = "%s"
terraform_version = "%s"
type = "%s"
opentofu_version = "%s"
ssh_keys = [{
id = "%s"
name = "%s"
}, {
id = "%s"
name = "%s"
}]
}`, name, repository, defaultVersion, string(defaultType), sshKey1.Id, sshKey1.Name, sshKey2.Id, sshKey2.Name)
}`, name, repository, defaultVersion, string(defaultType), defaultOpentofuVersion, sshKey1.Id, sshKey1.Name, sshKey2.Id, sshKey2.Name)
}

sshTemplateResourceCheck := func(resourceFullName string, template client.Template, sshKey1 client.TemplateSshKey, sshKey2 client.TemplateSshKey) resource.TestCheckFunc {
Expand Down Expand Up @@ -957,18 +965,18 @@ func TestUnitTemplateResource(t *testing.T) {
mock.EXPECT().Template(template.Id).Times(1).Return(updatedTemplate, nil), // 1 after update
)
mock.EXPECT().TemplateCreate(client.TemplateCreatePayload{
Name: template.Name,
Repository: template.Repository,
Type: defaultType,
TerraformVersion: defaultVersion,
SshKeys: template.SshKeys,
Name: template.Name,
Repository: template.Repository,
Type: defaultType,
SshKeys: template.SshKeys,
OpentofuVersion: defaultOpentofuVersion,
}).Times(1).Return(template, nil)
mock.EXPECT().TemplateUpdate(updatedTemplate.Id, client.TemplateCreatePayload{
Name: updatedTemplate.Name,
Repository: updatedTemplate.Repository,
Type: defaultType,
TerraformVersion: defaultVersion,
SshKeys: updatedTemplate.SshKeys,
Name: updatedTemplate.Name,
Repository: updatedTemplate.Repository,
Type: defaultType,
SshKeys: updatedTemplate.SshKeys,
OpentofuVersion: defaultOpentofuVersion,
}).Times(1).Return(updatedTemplate, nil)
mock.EXPECT().TemplateDelete(template.Id).Times(1).Return(nil)
})
Expand Down Expand Up @@ -1083,13 +1091,15 @@ func TestUnitTemplateResource(t *testing.T) {
Repository: template.Repository,
TerraformVersion: defaultVersion,
Type: string(defaultType),
OpentofuVersion: defaultOpentofuVersion,
}
templateWithDefaultsUpdate := client.Template{
Id: updateTemplate.Id,
Name: updateTemplate.Name,
Repository: updateTemplate.Repository,
TerraformVersion: defaultVersion,
Type: string(defaultType),
OpentofuVersion: defaultOpentofuVersion,
}

templateWithDrift := client.Template{
Expand All @@ -1099,13 +1109,15 @@ func TestUnitTemplateResource(t *testing.T) {
Repository: template.Repository,
TerraformVersion: defaultVersion,
Type: string(defaultType),
OpentofuVersion: defaultOpentofuVersion,
}

basicTemplateResourceConfig := func(resourceType string, resourceName string, template client.Template) string {
return resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"name": template.Name,
"repository": template.Repository,
"terraform_version": defaultVersion,
"opentofu_version": defaultOpentofuVersion,
})
}

Expand All @@ -1119,6 +1131,7 @@ func TestUnitTemplateResource(t *testing.T) {
resource.TestCheckResourceAttr(resourceFullName, "repository", template.Repository),
resource.TestCheckResourceAttr(resourceFullName, "type", string(defaultType)),
resource.TestCheckResourceAttr(resourceFullName, "terraform_version", defaultVersion),
resource.TestCheckResourceAttr(resourceFullName, "opentofu_version", defaultOpentofuVersion),
),
},
{
Expand All @@ -1129,24 +1142,25 @@ func TestUnitTemplateResource(t *testing.T) {
resource.TestCheckResourceAttr(resourceFullName, "repository", updateTemplate.Repository),
resource.TestCheckResourceAttr(resourceFullName, "type", string(defaultType)),
resource.TestCheckResourceAttr(resourceFullName, "terraform_version", defaultVersion),
resource.TestCheckResourceAttr(resourceFullName, "opentofu_version", defaultOpentofuVersion),
),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().TemplateCreate(client.TemplateCreatePayload{
Name: template.Name,
Repository: template.Repository,
Type: defaultType,
TerraformVersion: defaultVersion,
Name: template.Name,
Repository: template.Repository,
Type: defaultType,
OpentofuVersion: defaultOpentofuVersion,
}).Times(1).Return(template, nil)

mock.EXPECT().TemplateCreate(client.TemplateCreatePayload{
Name: updateTemplate.Name,
Repository: updateTemplate.Repository,
Type: defaultType,
TerraformVersion: defaultVersion,
Name: updateTemplate.Name,
Repository: updateTemplate.Repository,
Type: defaultType,
OpentofuVersion: defaultOpentofuVersion,
}).Times(1).Return(updateTemplate, nil)

gomock.InOrder(
Expand All @@ -1167,6 +1181,7 @@ func TestUnitTemplateResource(t *testing.T) {
Repository: "repo",
TerraformVersion: string(defaultVersion),
Type: string(defaultType),
OpentofuVersion: defaultOpentofuVersion,
}

updatedPathTemplate := client.Template{
Expand All @@ -1176,6 +1191,7 @@ func TestUnitTemplateResource(t *testing.T) {
Repository: "repo",
TerraformVersion: string(defaultVersion),
Type: string(defaultType),
OpentofuVersion: defaultOpentofuVersion,
}

testCase := resource.TestCase{
Expand All @@ -1186,6 +1202,7 @@ func TestUnitTemplateResource(t *testing.T) {
"path": "/" + pathTemplate.Path,
"repository": pathTemplate.Repository,
"terraform_version": pathTemplate.TerraformVersion,
"opentofu_version": defaultOpentofuVersion,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "id", pathTemplate.Id),
Expand All @@ -1194,6 +1211,7 @@ func TestUnitTemplateResource(t *testing.T) {
resource.TestCheckResourceAttr(resourceFullName, "type", pathTemplate.Type),
resource.TestCheckResourceAttr(resourceFullName, "terraform_version", pathTemplate.TerraformVersion),
resource.TestCheckResourceAttr(resourceFullName, "path", "/"+pathTemplate.Path),
resource.TestCheckResourceAttr(resourceFullName, "opentofu_version", defaultOpentofuVersion),
),
},
{
Expand All @@ -1202,6 +1220,7 @@ func TestUnitTemplateResource(t *testing.T) {
"path": "/" + updatedPathTemplate.Path,
"repository": updatedPathTemplate.Repository,
"terraform_version": updatedPathTemplate.TerraformVersion,
"opentofu_version": defaultOpentofuVersion,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "id", updatedPathTemplate.Id),
Expand All @@ -1210,6 +1229,7 @@ func TestUnitTemplateResource(t *testing.T) {
resource.TestCheckResourceAttr(resourceFullName, "type", updatedPathTemplate.Type),
resource.TestCheckResourceAttr(resourceFullName, "terraform_version", updatedPathTemplate.TerraformVersion),
resource.TestCheckResourceAttr(resourceFullName, "path", "/"+updatedPathTemplate.Path),
resource.TestCheckResourceAttr(resourceFullName, "opentofu_version", defaultOpentofuVersion),
),
},
},
Expand All @@ -1218,19 +1238,19 @@ func TestUnitTemplateResource(t *testing.T) {
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
gomock.InOrder(
mock.EXPECT().TemplateCreate(client.TemplateCreatePayload{
Path: "/" + pathTemplate.Path,
Name: pathTemplate.Name,
Type: pathTemplate.Type,
TerraformVersion: pathTemplate.TerraformVersion,
Repository: pathTemplate.Repository,
Path: "/" + pathTemplate.Path,
Name: pathTemplate.Name,
Type: pathTemplate.Type,
Repository: pathTemplate.Repository,
OpentofuVersion: defaultOpentofuVersion,
}).Times(1).Return(pathTemplate, nil),
mock.EXPECT().Template(pathTemplate.Id).Times(2).Return(pathTemplate, nil),
mock.EXPECT().TemplateUpdate(updatedPathTemplate.Id, client.TemplateCreatePayload{
Path: "/" + updatedPathTemplate.Path,
Name: updatedPathTemplate.Name,
Type: updatedPathTemplate.Type,
TerraformVersion: updatedPathTemplate.TerraformVersion,
Repository: updatedPathTemplate.Repository,
Path: "/" + updatedPathTemplate.Path,
Name: updatedPathTemplate.Name,
Type: updatedPathTemplate.Type,
Repository: updatedPathTemplate.Repository,
OpentofuVersion: defaultOpentofuVersion,
}).Times(1).Return(updatedPathTemplate, nil),
mock.EXPECT().Template(pathTemplate.Id).Times(1).Return(updatedPathTemplate, nil),
mock.EXPECT().TemplateDelete(pathTemplate.Id).Times(1).Return(nil),
Expand Down

0 comments on commit d658cfa

Please sign in to comment.