Skip to content

Commit

Permalink
Feat: module testing support
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Feb 5, 2024
1 parent 0dd94fa commit babd7bd
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 136 deletions.
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 babd7bd

Please sign in to comment.