-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add new option to template which populates variables from sourc… (
#393) * Feat: add new option to template which populates variables from source code * added custom writer * update integration test
- Loading branch information
1 parent
4ee860f
commit f1a1a2b
Showing
14 changed files
with
403 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
package client | ||
|
||
import "encoding/json" | ||
|
||
func toParamsInterface(i interface{}) (map[string]interface{}, error) { | ||
b, err := json.Marshal(i) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var paramsInterface map[string]interface{} | ||
if err := json.Unmarshal(b, ¶msInterface); err != nil { | ||
return nil, err | ||
} | ||
|
||
return paramsInterface, 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,81 @@ | ||
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" | ||
) | ||
|
||
func dataSourceCodeVariables() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceCodeVariablesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"template_id": { | ||
Type: schema.TypeString, | ||
Description: "extracts source code terraform variables from the VCS configuration of this template", | ||
Required: true, | ||
}, | ||
|
||
"variables": { | ||
Type: schema.TypeList, | ||
Description: "a list of terraform variables extracted from the source code", | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "the name of the terraform variable", | ||
Computed: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Description: "the value of the terraform variable", | ||
Computed: true, | ||
}, | ||
"format": { | ||
Type: schema.TypeString, | ||
Description: "the format of the terraform variable (HCL or JSON)", | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceCodeVariablesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
templateId := d.Get("template_id").(string) | ||
|
||
template, err := apiClient.Template(templateId) | ||
if err != nil { | ||
return diag.Errorf("could not get template: %v", err) | ||
} | ||
|
||
payload := &client.VariablesFromRepositoryPayload{ | ||
BitbucketClientKey: template.BitbucketClientKey, | ||
GithubInstallationId: template.GithubInstallationId, | ||
Path: template.Path, | ||
Revision: template.Revision, | ||
TokenId: template.TokenId, | ||
Repository: template.Repository, | ||
} | ||
|
||
variables, err := apiClient.VariablesFromRepository(payload) | ||
if err != nil { | ||
return diag.Errorf("failed to extract variables from repository: %v", err) | ||
} | ||
|
||
if err := writeResourceDataSlice(variables, "variables", d); err != nil { | ||
return diag.Errorf("schema slice resource data serialization failed: %v", err) | ||
} | ||
|
||
d.SetId(templateId) | ||
|
||
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,105 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestSourceCodeVariablesDataSource(t *testing.T) { | ||
template := client.Template{ | ||
Id: "id0", | ||
Name: "template0", | ||
Description: "description0", | ||
Repository: "env0/repo", | ||
Path: "path/zero", | ||
Revision: "branch-zero", | ||
Type: "terraform", | ||
TokenId: "1", | ||
GitlabProjectId: 10, | ||
TerraformVersion: "0.12.24", | ||
} | ||
|
||
payload := &client.VariablesFromRepositoryPayload{ | ||
BitbucketClientKey: template.BitbucketClientKey, | ||
GithubInstallationId: template.GithubInstallationId, | ||
Path: template.Path, | ||
Revision: template.Revision, | ||
TokenId: template.TokenId, | ||
Repository: template.Repository, | ||
} | ||
|
||
var1 := client.ConfigurationVariable{ | ||
Id: "id0", | ||
Name: "name0", | ||
Description: "desc0", | ||
ScopeId: "scope0", | ||
Value: "value0", | ||
OrganizationId: "organization0", | ||
UserId: "user0", | ||
Schema: &client.ConfigurationVariableSchema{Type: "string", Format: client.HCL}, | ||
Regex: "regex", | ||
} | ||
|
||
var2 := client.ConfigurationVariable{ | ||
Id: "id1", | ||
Name: "name1", | ||
Description: "desc1", | ||
ScopeId: "scope1", | ||
Value: "value1", | ||
OrganizationId: "organization0", | ||
UserId: "user1", | ||
Schema: &client.ConfigurationVariableSchema{Type: "string", Format: client.JSON}, | ||
Regex: "regex", | ||
} | ||
|
||
vars := []client.ConfigurationVariable{var1, var2} | ||
|
||
resourceType := "env0_source_code_variables" | ||
resourceName := "test" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"template_id": template.Id}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "variables.0.name", var1.Name), | ||
resource.TestCheckResourceAttr(accessor, "variables.1.name", var2.Name), | ||
resource.TestCheckResourceAttr(accessor, "variables.0.value", var1.Value), | ||
resource.TestCheckResourceAttr(accessor, "variables.1.value", var2.Value), | ||
resource.TestCheckResourceAttr(accessor, "variables.0.format", string(var1.Schema.Format)), | ||
resource.TestCheckResourceAttr(accessor, "variables.1.format", string(var2.Schema.Format)), | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().Template(template.Id).AnyTimes().Return(template, nil) | ||
mock.EXPECT().VariablesFromRepository(payload).AnyTimes().Return(vars, nil) | ||
}) | ||
}) | ||
|
||
t.Run("API Call Error", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"template_id": template.Id}), | ||
ExpectError: regexp.MustCompile("error"), | ||
}, | ||
}, | ||
}, | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().Template(template.Id).AnyTimes().Return(template, nil) | ||
mock.EXPECT().VariablesFromRepository(payload).AnyTimes().Return(nil, errors.New("error")) | ||
}, | ||
) | ||
}) | ||
|
||
} |
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.