Skip to content

Commit

Permalink
Chore: add pre-commit hooks (#321)
Browse files Browse the repository at this point in the history
* Chore: add pre-commit hooks

* updated readme file

* add staticcheck and vet to pipeline
  • Loading branch information
TomerHeber committed Apr 7, 2022
1 parent 15db322 commit a5b9ad5
Show file tree
Hide file tree
Showing 50 changed files with 342 additions and 288 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ jobs:
- name: Go fmt
run: |
! go fmt ./... | read
- name: Go vet
run: |
! go vet ./... | read
- name: Go staticcheck
uses: dominikh/staticcheck-action@v1.2.0
with:
version: "2021.1.2"
install-go: false
- name: Go Test
run: go test -v ./...

Expand Down
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: trailing-whitespace
exclude: ^docs/|examples|tests/integration/
- id: end-of-file-fixer
exclude: ^docs/|examples/|tests/integration/
- id: check-yaml
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.0
hooks:
- id: go-fmt
- id: go-vet
- id: go-imports
- id: go-mod-tidy
- repo: https://github.com/TekWizely/pre-commit-golang
rev: v1.0.0-beta.5
hooks:
- id: go-staticcheck-mod
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ See [here](https://developer.env0.com/docs/api/YXBpOjY4Njc2-env0-api#creating-an
```terraform
variable "env0_api_key" {}
variable "env0_api_secret" {}
provider "env0" {
api_key = var.env0_api_key
api_secret = var.env0_api_secret
Expand All @@ -77,7 +77,7 @@ See [here](https://developer.env0.com/docs/api/YXBpOjY4Njc2-env0-api#creating-an
- Build - `./build.sh`
- Create the plugins folder - `mkdir -p ~/.terraform.d/plugins/terraform.env0.com/local/env0/6.6.6/darwin_amd64`
- Copy the built binary - `cp ./terraform-provider-env0 ~/.terraform.d/plugins/terraform.env0.com/local/env0/6.6.6/darwin_amd64` (Replace `darwin` with `linux` on Linux)
- Require the local provider in your `main.tf` -
- Require the local provider in your `main.tf` -
```
terraform {
required_providers {
Expand All @@ -89,10 +89,21 @@ terraform {
}
```

### Installing pre-commit hooks
For consistent coding style, install [pre-commit](https://pre-commit.com/#install) hooks.

```
go install golang.org/x/tools/...@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
pre-commit install
pre-commit install --hook-type pre-push
```


## Testing

### Integration tests
- The integration tests run against the real env0 API
- The integration tests run against the real env0 API
- Have `ENV0_API_KEY` and `ENV0_API_SECRET` environment variables defined.
- Also set `ENV0_API_ENDPOINT` if you want to run against a non-prod environment.
- Run `go run tests/harness.go` (from the project root folder) to run all the tests.
Expand All @@ -110,7 +121,7 @@ Afterwards, when cleanup is required, just set `DESTROY_MODE` to `DESTROY_ONLY`

#### Integration Test Prerequisites
- An env0 organization
- An API Key
- An API Key
- A Github.com integrated template name `Github Integrated Template` for https://github.com/env0/templates
- A Gitlab.com integrated template name `Gitlab Integrated Template` for https://gitlab.com/env0/gitlab-vcs-integration-tests

Expand Down
16 changes: 8 additions & 8 deletions client/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type ApiKeyCreatePayloadWith struct {
OrganizationId string `json:"organizationId"`
}

func (ac *ApiClient) ApiKeyCreate(payload ApiKeyCreatePayload) (*ApiKey, error) {
organizationId, err := ac.organizationId()
func (client *ApiClient) ApiKeyCreate(payload ApiKeyCreatePayload) (*ApiKey, error) {
organizationId, err := client.organizationId()
if err != nil {
return nil, err
}
Expand All @@ -33,25 +33,25 @@ func (ac *ApiClient) ApiKeyCreate(payload ApiKeyCreatePayload) (*ApiKey, error)
}

var result ApiKey
if err := ac.http.Post("/api-keys", payloadWith, &result); err != nil {
if err := client.http.Post("/api-keys", payloadWith, &result); err != nil {
return nil, err
}

return &result, nil
}

func (ac *ApiClient) ApiKeyDelete(id string) error {
return ac.http.Delete("/api-keys/" + id)
func (client *ApiClient) ApiKeyDelete(id string) error {
return client.http.Delete("/api-keys/" + id)
}

func (ac *ApiClient) ApiKeys() ([]ApiKey, error) {
organizationId, err := ac.organizationId()
func (client *ApiClient) ApiKeys() ([]ApiKey, error) {
organizationId, err := client.organizationId()
if err != nil {
return nil, err
}

var result []ApiKey
if err := ac.http.Get("/api-keys", map[string]string{"organizationId": organizationId}, &result); err != nil {
if err := client.http.Get("/api-keys", map[string]string{"organizationId": organizationId}, &result); err != nil {
return nil, err
}

Expand Down
38 changes: 19 additions & 19 deletions client/cloud_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ const (
AzureServicePrincipalCredentialsType AzureCredentialsType = "AZURE_SERVICE_PRINCIPAL_FOR_DEPLOYMENT"
)

func (self *ApiClient) CloudCredentials(id string) (Credentials, error) {
var credentials, err = self.CloudCredentialsList()
func (client *ApiClient) CloudCredentials(id string) (Credentials, error) {
var credentials, err = client.CloudCredentialsList()
if err != nil {
return Credentials{}, err
}
Expand All @@ -92,82 +92,82 @@ func (self *ApiClient) CloudCredentials(id string) (Credentials, error) {
return Credentials{}, fmt.Errorf("CloudCredentials: [%s] not found ", id)
}

func (self *ApiClient) CloudCredentialsList() ([]Credentials, error) {
organizationId, err := self.organizationId()
func (client *ApiClient) CloudCredentialsList() ([]Credentials, error) {
organizationId, err := client.organizationId()
if err != nil {
return []Credentials{}, err
}

var credentials []Credentials
err = self.http.Get("/credentials", map[string]string{"organizationId": organizationId}, &credentials)
err = client.http.Get("/credentials", map[string]string{"organizationId": organizationId}, &credentials)
if err != nil {
return []Credentials{}, err
}

return credentials, nil
}

func (self *ApiClient) AwsCredentialsCreate(request AwsCredentialsCreatePayload) (Credentials, error) {
organizationId, err := self.organizationId()
func (client *ApiClient) AwsCredentialsCreate(request AwsCredentialsCreatePayload) (Credentials, error) {
organizationId, err := client.organizationId()
if err != nil {
return Credentials{}, err
}

request.OrganizationId = organizationId

var result Credentials
err = self.http.Post("/credentials", request, &result)
err = client.http.Post("/credentials", request, &result)
if err != nil {
return Credentials{}, err
}
return result, nil
}

func (self *ApiClient) GcpCredentialsCreate(request GcpCredentialsCreatePayload) (Credentials, error) {
organizationId, err := self.organizationId()
func (client *ApiClient) GcpCredentialsCreate(request GcpCredentialsCreatePayload) (Credentials, error) {
organizationId, err := client.organizationId()
if err != nil {
return Credentials{}, err
}

request.OrganizationId = organizationId

var result Credentials
err = self.http.Post("/credentials", request, &result)
err = client.http.Post("/credentials", request, &result)
if err != nil {
return Credentials{}, err
}
return result, nil
}

func (self *ApiClient) AzureCredentialsCreate(request AzureCredentialsCreatePayload) (Credentials, error) {
organizationId, err := self.organizationId()
func (client *ApiClient) AzureCredentialsCreate(request AzureCredentialsCreatePayload) (Credentials, error) {
organizationId, err := client.organizationId()
if err != nil {
return Credentials{}, err
}

request.OrganizationId = organizationId

var result Credentials
err = self.http.Post("/credentials", request, &result)
err = client.http.Post("/credentials", request, &result)
if err != nil {
return Credentials{}, err
}
return result, nil
}

func (self *ApiClient) CloudCredentialsDelete(id string) error {
return self.http.Delete("/credentials/" + id)
func (client *ApiClient) CloudCredentialsDelete(id string) error {
return client.http.Delete("/credentials/" + id)
}

func (self *ApiClient) GoogleCostCredentialsCreate(request GoogleCostCredentialsCreatePayload) (Credentials, error) {
organizationId, err := self.organizationId()
func (client *ApiClient) GoogleCostCredentialsCreate(request GoogleCostCredentialsCreatePayload) (Credentials, error) {
organizationId, err := client.organizationId()
if err != nil {
return Credentials{}, err
}

request.OrganizationId = organizationId
var result Credentials
err = self.http.Post("/credentials", request, &result)
err = client.http.Post("/credentials", request, &result)
if err != nil {
return Credentials{}, err
}
Expand Down
12 changes: 6 additions & 6 deletions client/cloud_credentials_project_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ type CloudCredentialsProjectAssignment struct {
ProjectId string `json:"projectId"`
}

func (self *ApiClient) AssignCloudCredentialsToProject(projectId string, credentialId string) (CloudCredentialsProjectAssignment, error) {
func (client *ApiClient) AssignCloudCredentialsToProject(projectId string, credentialId string) (CloudCredentialsProjectAssignment, error) {
var result CloudCredentialsProjectAssignment

err := self.http.Put("/credentials/deployment/"+credentialId+"/project/"+projectId, nil, &result)
err := client.http.Put("/credentials/deployment/"+credentialId+"/project/"+projectId, nil, &result)
if err != nil {
return result, err
}
return result, nil
}

func (self *ApiClient) RemoveCloudCredentialsFromProject(projectId string, credentialId string) error {
return self.http.Delete("/credentials/deployment/" + credentialId + "/project/" + projectId)
func (client *ApiClient) RemoveCloudCredentialsFromProject(projectId string, credentialId string) error {
return client.http.Delete("/credentials/deployment/" + credentialId + "/project/" + projectId)
}

func (self *ApiClient) CloudCredentialIdsInProject(projectId string) ([]string, error) {
func (client *ApiClient) CloudCredentialIdsInProject(projectId string) ([]string, error) {
var result CloudCredentialIdsInProjectResponse
err := self.http.Get("/credentials/deployment/project/"+projectId, nil, &result)
err := client.http.Get("/credentials/deployment/project/"+projectId, nil, &result)

if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions client/cloud_credentials_project_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client_test

import (
"errors"

. "github.com/env0/terraform-provider-env0/client"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
Expand Down
32 changes: 16 additions & 16 deletions client/configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ type ConfigurationVariableUpdateParams struct {
Id string
}

func (self *ApiClient) ConfigurationVariablesById(id string) (ConfigurationVariable, error) {
func (client *ApiClient) ConfigurationVariablesById(id string) (ConfigurationVariable, error) {
var result ConfigurationVariable

err := self.http.Get("/configuration/"+id, nil, &result)
err := client.http.Get("/configuration/"+id, nil, &result)

if err != nil {
return ConfigurationVariable{}, err
}
return result, nil
}

func (self *ApiClient) ConfigurationVariablesByScope(scope Scope, scopeId string) ([]ConfigurationVariable, error) {
organizationId, err := self.organizationId()
func (client *ApiClient) ConfigurationVariablesByScope(scope Scope, scopeId string) ([]ConfigurationVariable, error) {
organizationId, err := client.organizationId()
if err != nil {
return nil, err
}
Expand All @@ -92,22 +92,22 @@ func (self *ApiClient) ConfigurationVariablesByScope(scope Scope, scopeId string
case scope == ScopeEnvironment:
params["environmentId"] = scopeId
case scope == ScopeDeployment:
return nil, errors.New("No api to fetch configuration variables by deployment")
return nil, errors.New("no api to fetch configuration variables by deployment")
case scope == ScopeDeploymentLog:
params["deploymentLogId"] = scopeId
}
err = self.http.Get("/configuration", params, &result)
err = client.http.Get("/configuration", params, &result)
if err != nil {
return []ConfigurationVariable{}, err
}
return result, nil
}

func (self *ApiClient) ConfigurationVariableCreate(params ConfigurationVariableCreateParams) (ConfigurationVariable, error) {
func (client *ApiClient) ConfigurationVariableCreate(params ConfigurationVariableCreateParams) (ConfigurationVariable, error) {
if params.Scope == ScopeDeploymentLog || params.Scope == ScopeDeployment {
return ConfigurationVariable{}, errors.New("Must not create variable on scope deployment / deploymentLog")
return ConfigurationVariable{}, errors.New("must not create variable on scope deployment / deploymentLog")
}
organizationId, err := self.organizationId()
organizationId, err := client.organizationId()
if err != nil {
return ConfigurationVariable{}, err
}
Expand All @@ -130,7 +130,7 @@ func (self *ApiClient) ConfigurationVariableCreate(params ConfigurationVariableC
request["schema"] = getSchema(params)

requestInArray := []map[string]interface{}{request}
err = self.http.Post("configuration", requestInArray, &result)
err = client.http.Post("configuration", requestInArray, &result)
if err != nil {
return ConfigurationVariable{}, err
}
Expand All @@ -150,16 +150,16 @@ func getSchema(params ConfigurationVariableCreateParams) map[string]interface{}
return schema
}

func (self *ApiClient) ConfigurationVariableDelete(id string) error {
return self.http.Delete("configuration/" + id)
func (client *ApiClient) ConfigurationVariableDelete(id string) error {
return client.http.Delete("configuration/" + id)
}

func (self *ApiClient) ConfigurationVariableUpdate(updateParams ConfigurationVariableUpdateParams) (ConfigurationVariable, error) {
func (client *ApiClient) ConfigurationVariableUpdate(updateParams ConfigurationVariableUpdateParams) (ConfigurationVariable, error) {
commonParams := updateParams.CommonParams
if commonParams.Scope == ScopeDeploymentLog || commonParams.Scope == ScopeDeployment {
return ConfigurationVariable{}, errors.New("Must not create variable on scope deployment / deploymentLog")
return ConfigurationVariable{}, errors.New("must not create variable on scope deployment / deploymentLog")
}
organizationId, err := self.organizationId()
organizationId, err := client.organizationId()
if err != nil {
return ConfigurationVariable{}, err
}
Expand All @@ -183,7 +183,7 @@ func (self *ApiClient) ConfigurationVariableUpdate(updateParams ConfigurationVar
request["schema"] = getSchema(updateParams.CommonParams)

requestInArray := []map[string]interface{}{request}
err = self.http.Post("/configuration", requestInArray, &result)
err = client.http.Post("/configuration", requestInArray, &result)
if err != nil {
return ConfigurationVariable{}, err
}
Expand Down
12 changes: 6 additions & 6 deletions client/cost_credentials_project_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ type CostCredentialProjectAssignment struct {
CredentialsType string `json:"credentialsType"`
}

func (self *ApiClient) AssignCostCredentialsToProject(projectId string, credentialId string) (CostCredentialProjectAssignment, error) {
func (client *ApiClient) AssignCostCredentialsToProject(projectId string, credentialId string) (CostCredentialProjectAssignment, error) {
var result CostCredentialProjectAssignment

err := self.http.Put("/costs/project/"+projectId+"/credentials", credentialId, &result)
err := client.http.Put("/costs/project/"+projectId+"/credentials", credentialId, &result)
if err != nil {
return result, err
}
return result, nil
}

func (self *ApiClient) RemoveCostCredentialsFromProject(projectId string, credentialId string) error {
return self.http.Delete("/costs/project/" + projectId + "/credentials/" + credentialId)
func (client *ApiClient) RemoveCostCredentialsFromProject(projectId string, credentialId string) error {
return client.http.Delete("/costs/project/" + projectId + "/credentials/" + credentialId)
}

func (self *ApiClient) CostCredentialIdsInProject(projectId string) ([]CostCredentialProjectAssignment, error) {
func (client *ApiClient) CostCredentialIdsInProject(projectId string) ([]CostCredentialProjectAssignment, error) {
var result []CostCredentialProjectAssignment
err := self.http.Get("/costs/project/"+projectId, nil, &result)
err := client.http.Get("/costs/project/"+projectId, nil, &result)

if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit a5b9ad5

Please sign in to comment.