Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add support for Environment Discovery API #802

Merged
merged 17 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:
ENV0_API_ENDPOINT: ${{ secrets.ENV0_API_ENDPOINT }}
ENV0_API_KEY: ${{ secrets.TF_PROVIDER_INTEGRATION_TEST_API_KEY }} # API Key for organization 'TF-provider-integration-tests' @ dev
ENV0_API_SECRET: ${{ secrets.TF_PROVIDER_INTEGRATION_TEST_API_SECRET }}
GO_VERSION: "1.20"
GO_VERSION: "1.21"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed an CVE and also updated the golang version.
This will now be considered a major release.

TERRAFORM_VERSION: 1.1.7

jobs:
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
integration-tests:
name: Integration Tests
runs-on: ubuntu-20.04
container: golang:1.20-alpine3.18
container: golang:1.21-alpine3.18
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - alpine newer than 3.18 do not support terraform (due to hashicorp licensing change). Consider switching to opentofu.

timeout-minutes: 20
steps:
- name: Install Terraform
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

env:
GO_VERSION: "1.20"
GO_VERSION: "1.21"

jobs:
generate-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 @@ -16,7 +16,7 @@ on:
- "v*.*.*"

env:
GO_VERSION: "1.20"
GO_VERSION: "1.21"

jobs:
goreleaser:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ resource "env0_template" "example" {

## Development Setup

> **Supported Go Version: 1.20**
> **Supported Go Version: 1.21**

### Build

Expand Down
3 changes: 3 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ type ApiClientInterface interface {
ProjectBudget(projectId string) (*ProjectBudget, error)
ProjectBudgetUpdate(projectId string, payload *ProjectBudgetUpdatePayload) (*ProjectBudget, error)
ProjectBudgetDelete(projectId string) error
EnableUpdateEnvironmentDiscovery(projectId string, payload *EnvironmentDiscoveryPutPayload) (*EnvironmentDiscoveryPayload, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename it to PutEnvironmentDiscovery, or just UpdateEnvironmentDiscovery

GetEnvironmentDiscovery(projectId string) (*EnvironmentDiscoveryPayload, error)
DeleteEnvironmentDiscovery(projectId string) error
}

func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface {
Expand Down
44 changes: 44 additions & 0 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions client/environment_discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package client

type EnvironmentDiscoveryPutPayload struct {
GlobPattern string `json:"globPattern"`
EnvironmentPlacement string `json:"environmentPlacement"`
WorkspaceNaming string `json:"workspaceNaming"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob,omitempty"`
Repository string `json:"repository"`
TerraformVersion string `json:"terraformVersion,omitempty"`
OpentofuVersion string `json:"opentofuVersion,omitempty"`
TerragruntVersion string `json:"terragruntVersion,omitempty"`
TerragruntTfBinary string `json:"terragruntTfBinary,omitempty"`
IsTerragruntRunAll bool `json:"is_terragrunt_run_all"`
Type string `json:"type"`
GitlabProjectId int `json:"gitlabProjectId,omitempty"`
TokenId string `json:"tokenId,omitempty"`
SshKeys []TemplateSshKey `json:"sshKeys,omitempty"`
GithubInstallationId int `json:"githubInstallationId,omitempty"`
BitbucketClientKey string `json:"bitbucketClientKey,omitempty"`
IsAzureDevops bool `json:"isAzureDevOps"`
Retry TemplateRetry `json:"retry"`
}

type EnvironmentDiscoveryPayload struct {
Id string `json:"id"`
GlobPattern string `json:"globPattern"`
EnvironmentPlacement string `json:"environmentPlacement"`
WorkspaceNaming string `json:"workspaceNaming"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob"`
Repository string `json:"repository"`
TerraformVersion string `json:"terraformVersion"`
OpentofuVersion string `json:"opentofuVersion"`
TerragruntVersion string `json:"terragruntVersion"`
TerragruntTfBinary string `json:"terragruntTfBinary" tfschema:",omitempty"`
IsTerragruntRunAll bool `json:"is_terragrunt_run_all"`
Type string `json:"type"`
GitlabProjectId int `json:"gitlabProjectId"`
TokenId string `json:"tokenId"`
SshKeys []TemplateSshKey `json:"sshKeys" tfschema:"-"`
GithubInstallationId int `json:"githubInstallationId"`
BitbucketClientKey string `json:"bitbucketClientKey"`
IsAzureDevops bool `json:"isAzureDevOps"`
Retry TemplateRetry `json:"retry" tfschema:"-"`
}

func (client *ApiClient) EnableUpdateEnvironmentDiscovery(projectId string, payload *EnvironmentDiscoveryPutPayload) (*EnvironmentDiscoveryPayload, error) {
var result EnvironmentDiscoveryPayload

if err := client.http.Put("/environment-discovery/projects/"+projectId, payload, &result); err != nil {
return nil, err
}

return &result, nil
}

func (client *ApiClient) GetEnvironmentDiscovery(projectId string) (*EnvironmentDiscoveryPayload, error) {
var result EnvironmentDiscoveryPayload

if err := client.http.Get("/environment-discovery/projects/"+projectId, nil, &result); err != nil {
return nil, err
}

return &result, nil
}

func (client *ApiClient) DeleteEnvironmentDiscovery(projectId string) error {
return client.http.Delete("/environment-discovery/projects/"+projectId, nil)
}
130 changes: 130 additions & 0 deletions client/environment_discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package client_test

import (
"errors"

. "github.com/env0/terraform-provider-env0/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
)

var _ = Describe("Environment Discovery", func() {
mockError := errors.New("error")

projectId := "pid"

mockEnvironmentDiscovery := EnvironmentDiscoveryPayload{
Id: "id",
GlobPattern: "**",
EnvironmentPlacement: "topProject",
WorkspaceNaming: "default",
AutoDeployByCustomGlob: "**",
Repository: "https://re.po",
TerraformVersion: "1.5.6",
Type: "terraform",
GithubInstallationId: 1000,
}

Describe("PUT", func() {
putPayload := EnvironmentDiscoveryPutPayload{
GlobPattern: mockEnvironmentDiscovery.GlobPattern,
EnvironmentPlacement: mockEnvironmentDiscovery.EnvironmentPlacement,
WorkspaceNaming: mockEnvironmentDiscovery.WorkspaceNaming,
AutoDeployByCustomGlob: mockEnvironmentDiscovery.AutoDeployByCustomGlob,
Repository: mockEnvironmentDiscovery.Repository,
TerraformVersion: mockEnvironmentDiscovery.TerraformVersion,
Type: mockEnvironmentDiscovery.Type,
GithubInstallationId: mockEnvironmentDiscovery.GithubInstallationId,
}

Describe("success", func() {
var ret *EnvironmentDiscoveryPayload

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Put("/environment-discovery/projects/"+projectId, &putPayload, gomock.Any()).
Do(func(path string, request interface{}, response *EnvironmentDiscoveryPayload) {
*response = mockEnvironmentDiscovery
}).Times(1)
ret, _ = apiClient.EnableUpdateEnvironmentDiscovery(projectId, &putPayload)
})

It("Should return environment discovery", func() {
Expect(*ret).To(Equal(mockEnvironmentDiscovery))
})
})

Describe("failure", func() {
var err error

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Put("/environment-discovery/projects/"+projectId, &putPayload, gomock.Any()).Return(mockError).Times(1)
_, err = apiClient.EnableUpdateEnvironmentDiscovery(projectId, &putPayload)
})

It("Should return error", func() {
Expect(err).To(Equal(mockError))
})
})
})

Describe("GET", func() {
Describe("success", func() {
var ret *EnvironmentDiscoveryPayload

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/environment-discovery/projects/"+projectId, nil, gomock.Any()).
Do(func(path string, request interface{}, response *EnvironmentDiscoveryPayload) {
*response = mockEnvironmentDiscovery
}).Times(1)
ret, _ = apiClient.GetEnvironmentDiscovery(projectId)
})

It("Should return environment discovery", func() {
Expect(*ret).To(Equal(mockEnvironmentDiscovery))
})
})

Describe("failure", func() {
var err error

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/environment-discovery/projects/"+projectId, nil, gomock.Any()).Return(mockError).Times(1)
_, err = apiClient.GetEnvironmentDiscovery(projectId)
})

It("Should return error", func() {
Expect(err).To(Equal(mockError))
})
})
})

Describe("DELETE", func() {
Describe("success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Delete("/environment-discovery/projects/"+projectId, nil).Times(1)
apiClient.DeleteEnvironmentDiscovery(projectId)
})

It("Should send DELETE request", func() {})
})

Describe("failure", func() {
var err error

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Delete("/environment-discovery/projects/"+projectId, nil).Return(mockError).Times(1)
err = apiClient.DeleteEnvironmentDiscovery(projectId)
})

It("Should return error", func() {
Expect(err).To(Equal(mockError))
})
})
})
})
8 changes: 4 additions & 4 deletions env0/data_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func dataProjectRead(ctx context.Context, d *schema.ResourceData, meta interface
return nil
}

func filterByParentProjectId(name string, parentId string, projects []client.Project) ([]client.Project, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated - but name is not used so removed it...

func filterByParentProjectId(parentId string, projects []client.Project) ([]client.Project, error) {
filteredProjects := make([]client.Project, 0)
for _, project := range projects {
if len(project.ParentProjectId) == 0 {
Expand All @@ -107,7 +107,7 @@ func filterByParentProjectId(name string, parentId string, projects []client.Pro
return filteredProjects, nil
}

func filterByParentProjectName(name string, parentName string, projects []client.Project, meta interface{}) ([]client.Project, error) {
func filterByParentProjectName(parentName string, projects []client.Project, meta interface{}) ([]client.Project, error) {
filteredProjects := make([]client.Project, 0)
for _, project := range projects {
if len(project.ParentProjectId) == 0 {
Expand Down Expand Up @@ -142,13 +142,13 @@ func getProjectByName(name string, parentId string, parentName string, meta inte
}
if len(parentId) > 0 {
// Use parentId filter to reduce the results.
projectsByName, err = filterByParentProjectId(name, parentId, projectsByName)
projectsByName, err = filterByParentProjectId(parentId, projectsByName)
if err != nil {
return client.Project{}, err
}
} else if len(parentName) > 0 {
// Use parentName filter to reduce the results.
projectsByName, err = filterByParentProjectName(name, parentName, projectsByName, meta)
projectsByName, err = filterByParentProjectName(parentName, projectsByName, meta)
if err != nil {
return client.Project{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions env0/data_project_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestPolicyDataSource(t *testing.T) {
resourceName := "test_policy"
accessor := dataSourceAccessor(resourceType, resourceName)

getValidTestCase := func(input map[string]interface{}) resource.TestCase {
getValidTestCase := func() resource.TestCase {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated - another unused variable.

return resource.TestCase{
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestPolicyDataSource(t *testing.T) {
}

t.Run("valid", func(t *testing.T) {
runUnitTest(t, getValidTestCase(map[string]interface{}{}), func(mock *client.MockApiClientInterface) {
runUnitTest(t, getValidTestCase(), func(mock *client.MockApiClientInterface) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused...

mock.EXPECT().Policy(policy.ProjectId).AnyTimes().Return(policy, nil)
})
})
Expand Down
1 change: 1 addition & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func Provider(version string) plugin.ProviderFunc {
"env0_approval_policy": resourceApprovalPolicy(),
"env0_approval_policy_assignment": resourceApprovalPolicyAssignment(),
"env0_project_budget": resourceProjectBudget(),
"env0_environment_discovery_configuration": resourceEnvironmentDiscoveryConfiguration(),
},
}

Expand Down
10 changes: 5 additions & 5 deletions env0/resource_cost_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ func TestUnitAzureCostCredentialsResource(t *testing.T) {

t.Run("validate missing arguments", func(t *testing.T) {
missingArgumentsTestCases := []resource.TestCase{
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}, "client_id"),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}, "client_secret"),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}, "subscription_id"),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}, "tenant_id"),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}, "name"),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused...

missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}),
missingArgumentTestCaseForCostCred(resourceType, resourceName, map[string]interface{}{}),
}
for _, testCase := range missingArgumentsTestCases {
tc := testCase
Expand Down
Loading
Loading