Skip to content

Commit

Permalink
Feat: module testing support (#789)
Browse files Browse the repository at this point in the history
* Feat: module testing support

* fix integration test

* fix integration test

* update to a new actions version
  • Loading branch information
TomerHeber authored Feb 8, 2024
1 parent 0dd94fa commit da23b7b
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 139 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "${{ env.GO_VERSION }}"
- name: Update generated docs
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "${{ env.GO_VERSION }}"
- name: Import GPG key
Expand Down
103 changes: 56 additions & 47 deletions client/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,49 @@ type ModuleSshKey struct {
}

type Module struct {
ModuleName string `json:"moduleName"`
ModuleProvider string `json:"moduleProvider"`
Repository string `json:"repository"`
Description string `json:"description"`
LogoUrl string `json:"logoUrl"`
TokenId string `json:"tokenId"`
TokenName string `json:"tokenName"`
GithubInstallationId *int `json:"githubInstallationId" tfschema:",omitempty"`
BitbucketClientKey *string `json:"bitbucketClientKey" tfschema:",omitempty"`
IsGitlab bool `json:"isGitLab"`
SshKeys []ModuleSshKey `json:"sshkeys"`
Type string `json:"type"`
Id string `json:"id"`
OrganizationId string `json:"organizationId"`
Author User `json:"author"`
AuthorId string `json:"authorId"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
IsDeleted bool `json:"isDeleted"`
Path string `json:"path"`
TagPrefix string `json:"tagPrefix"`
ModuleName string `json:"moduleName"`
ModuleProvider string `json:"moduleProvider"`
Repository string `json:"repository"`
Description string `json:"description"`
LogoUrl string `json:"logoUrl"`
TokenId string `json:"tokenId"`
TokenName string `json:"tokenName"`
GithubInstallationId *int `json:"githubInstallationId" tfschema:",omitempty"`
BitbucketClientKey *string `json:"bitbucketClientKey" tfschema:",omitempty"`
IsGitlab bool `json:"isGitLab"`
SshKeys []ModuleSshKey `json:"sshkeys"`
Type string `json:"type"`
Id string `json:"id"`
OrganizationId string `json:"organizationId"`
Author User `json:"author"`
AuthorId string `json:"authorId"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
IsDeleted bool `json:"isDeleted"`
Path string `json:"path"`
TagPrefix string `json:"tagPrefix"`
ModuleTestEnabled bool `json:"moduleTestEnabled"`
RunTestsOnPullRequest bool `json:"runTestsOnPullRequest"`
OpentofuVersion string `json:"opentofuVersion"`
}

type ModuleCreatePayload struct {
ModuleName string `json:"moduleName"`
ModuleProvider string `json:"moduleProvider"`
Repository string `json:"repository"`
Description string `json:"description,omitempty"`
LogoUrl string `json:"logoUrl,omitempty"`
TokenId string `json:"tokenId,omitempty"`
TokenName string `json:"tokenName,omitempty"`
GithubInstallationId *int `json:"githubInstallationId,omitempty"`
BitbucketClientKey string `json:"bitbucketClientKey,omitempty"`
IsGitlab *bool `json:"isGitLab,omitempty"`
SshKeys []ModuleSshKey `json:"sshkeys,omitempty"`
Path string `json:"path,omitempty"`
TagPrefix string `json:"tagPrefix,omitempty"`
ModuleName string `json:"moduleName"`
ModuleProvider string `json:"moduleProvider"`
Repository string `json:"repository"`
Description string `json:"description,omitempty"`
LogoUrl string `json:"logoUrl,omitempty"`
TokenId string `json:"tokenId,omitempty"`
TokenName string `json:"tokenName,omitempty"`
GithubInstallationId *int `json:"githubInstallationId,omitempty"`
BitbucketClientKey string `json:"bitbucketClientKey,omitempty"`
IsGitlab *bool `json:"isGitLab,omitempty"`
SshKeys []ModuleSshKey `json:"sshkeys,omitempty"`
Path string `json:"path,omitempty"`
TagPrefix string `json:"tagPrefix,omitempty"`
ModuleTestEnabled bool `json:"moduleTestEnabled"`
RunTestsOnPullRequest bool `json:"runTestsOnPullRequest"`
OpentofuVersion string `json:"opentofuVersion,omitempty"`
}

type ModuleCreatePayloadWith struct {
Expand All @@ -52,19 +58,22 @@ type ModuleCreatePayloadWith struct {
}

type ModuleUpdatePayload struct {
ModuleName string `json:"moduleName,omitempty"`
ModuleProvider string `json:"moduleProvider,omitempty"`
Repository string `json:"repository,omitempty"`
Description string `json:"description,omitempty"`
LogoUrl string `json:"logoUrl,omitempty"`
TokenId string `json:"tokenId"`
TokenName string `json:"tokenName"`
GithubInstallationId *int `json:"githubInstallationId"`
BitbucketClientKey string `json:"bitbucketClientKey"`
IsGitlab bool `json:"isGitLab"`
SshKeys []ModuleSshKey `json:"sshkeys"`
Path string `json:"path"`
TagPrefix string `json:"tagPrefix,omitempty"`
ModuleName string `json:"moduleName,omitempty"`
ModuleProvider string `json:"moduleProvider,omitempty"`
Repository string `json:"repository,omitempty"`
Description string `json:"description,omitempty"`
LogoUrl string `json:"logoUrl,omitempty"`
TokenId string `json:"tokenId"`
TokenName string `json:"tokenName"`
GithubInstallationId *int `json:"githubInstallationId"`
BitbucketClientKey string `json:"bitbucketClientKey"`
IsGitlab bool `json:"isGitLab"`
SshKeys []ModuleSshKey `json:"sshkeys"`
Path string `json:"path"`
TagPrefix string `json:"tagPrefix,omitempty"`
ModuleTestEnabled bool `json:"moduleTestEnabled"`
RunTestsOnPullRequest bool `json:"runTestsOnPullRequest"`
OpentofuVersion string `json:"opentofuVersion,omitempty"`
}

func (client *ApiClient) ModuleCreate(payload ModuleCreatePayload) (*Module, error) {
Expand Down
29 changes: 29 additions & 0 deletions env0/resource_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ func resourceModule() *schema.Resource {
Description: "a tag prefix for the module",
Optional: true,
},
"module_test_enabled": {
Type: schema.TypeBool,
Description: "set to 'true' to enable module test (defaults to 'false')",
Optional: true,
Default: false,
},
"run_tests_on_pull_request": {
Type: schema.TypeBool,
Description: "set to 'true' to run tests on pull request (defaults to 'false'). Can only be enabled if 'module_test_enabled' is enabled",
Optional: true,
Default: false,
RequiredWith: []string{"module_test_enabled"},
},
"opentofu_version": {
Type: schema.TypeString,
Description: "the opentofu version to use, Can only be set if 'module_test_enabled' is enabled",
Optional: true,
Default: "",
RequiredWith: []string{"module_test_enabled"},
ValidateDiagFunc: NewOpenTofuVersionValidator(),
},
},
}
}
Expand All @@ -104,6 +125,10 @@ func resourceModuleCreate(ctx context.Context, d *schema.ResourceData, meta inte
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

if !payload.ModuleTestEnabled && (payload.RunTestsOnPullRequest || payload.OpentofuVersion != "") {
return diag.Errorf("'run_tests_on_pull_request' and/or 'opentofu_version' may only be set if 'module_test_enabled' is enabled (set to 'true')")
}

module, err := apiClient.ModuleCreate(payload)
if err != nil {
return diag.Errorf("could not create module: %v", err)
Expand Down Expand Up @@ -137,6 +162,10 @@ func resourceModuleUpdate(ctx context.Context, d *schema.ResourceData, meta inte
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

if !payload.ModuleTestEnabled && (payload.RunTestsOnPullRequest || payload.OpentofuVersion != "") {
return diag.Errorf("'run_tests_on_pull_request' and/or 'opentofu_version' may only be set if 'module_test_enabled' is enabled (set to 'true')")
}

if _, err := apiClient.ModuleUpdate(d.Id(), payload); err != nil {
return diag.Errorf("could not update module: %v", err)
}
Expand Down
Loading

0 comments on commit da23b7b

Please sign in to comment.