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

Fix: using git token_id on Templates always assumes GitLab #929

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions .golangci.yaml
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is for a new linter... ignore unrelated changes... it's a lot of work so I'm slowly adding it to the PRS.

Copy link
Collaborator Author

@TomerHeber TomerHeber Aug 7, 2024

Choose a reason for hiding this comment

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

review only files related to the issue:

template.go
resource_template.go
resource_template_test.go

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
linters:
enable:
- errname
- errorlint
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- goconst
- gocritic
- misspell
- nilerr
- nilnil
- nlreturn
- perfsprint
- prealloc
- predeclared
- reassign
- sloglint
- spancheck
- testifylint
- unparam
- unused
- usestdlibvars
- wsl

linters-settings:
errorlint:
asserts: false
errcheck:
exclude-functions:
- (*github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.ResourceData).Set
4 changes: 2 additions & 2 deletions client/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Template struct {
IsAzureDevOps bool `json:"isAzureDevOps" tfschema:"is_azure_devops"`
IsHelmRepository bool `json:"isHelmRepository"`
HelmChartName string `json:"helmChartName" tfschema:",omitempty"`
IsGitLab bool `json:"isGitLab" tfschema:"is_gitlab"`
IsGitlab bool `json:"isGitLab"`
TerragruntTfBinary string `json:"terragruntTfBinary" tfschema:",omitempty"`
TokenName string `json:"tokenName" tfschema:",omitempty"`
GitlabProjectId int `json:"gitlabProjectId" tfschema:",omitempty"`
Expand All @@ -75,7 +75,7 @@ type TemplateCreatePayload struct {
Name string `json:"name"`
Repository string `json:"repository"`
Path string `json:"path,omitempty"`
IsGitLab bool `json:"isGitLab"`
IsGitlab bool `json:"isGitLab"`
TokenName string `json:"tokenName,omitempty"`
TokenId string `json:"tokenId,omitempty"`
GithubInstallationId int `json:"githubInstallationId,omitempty"`
Expand Down
12 changes: 5 additions & 7 deletions env0/cloud_configuration.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func getCloudConfigurationFromSchema(d *schema.ResourceData, provider string) (i
}

if err := readResourceData(configuration, d); err != nil {
return nil, fmt.Errorf("schema resource data deserialization failed: %v", err)
return nil, fmt.Errorf("schema resource data deserialization failed: %w", err)
}

return configuration, nil
Expand All @@ -47,6 +47,7 @@ func getCloudConfigurationByNameFromApi(apiClient client.ApiClientInterface, nam

func getCloudConfigurationFromApi(apiClient client.ApiClientInterface, id string) (*client.CloudAccount, error) {
var err error

var cloudAccount *client.CloudAccount

if _, parseErr := uuid.Parse(id); parseErr != nil {
Expand Down Expand Up @@ -108,9 +109,8 @@ func createCloudConfiguration(d *schema.ResourceData, meta interface{}, provider
return diag.Errorf("failed to create a cloud configuration: %v", err)
}

if err := d.Set("health", cloudAccount.Health); err != nil {
return diag.Errorf("failed to set health: %v", err)
}
d.Set("health", cloudAccount.Health)

d.SetId(cloudAccount.Id)

return nil
Expand Down Expand Up @@ -156,9 +156,7 @@ func updateCloudConfiguration(d *schema.ResourceData, meta interface{}, provider
return diag.Errorf("failed to update cloud configuration: %v", err)
}

if err := d.Set("health", cloudAccount.Health); err != nil {
return diag.Errorf("failed to set health: %v", err)
}
d.Set("health", cloudAccount.Health)

return nil
}
Expand Down
11 changes: 10 additions & 1 deletion env0/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func getCredentialsByName(name string, prefixList []string, meta interface{}) (c
}

var foundCredentials []client.Credentials

for _, credentials := range credentialsList {
if credentials.Name == name && credentials.HasPrefix(prefixList) {
foundCredentials = append(foundCredentials, credentials)
Expand All @@ -76,11 +77,13 @@ func getCredentialsByName(name string, prefixList []string, meta interface{}) (c

func getCredentialsById(id string, prefixList []string, meta interface{}) (client.Credentials, error) {
apiClient := meta.(client.ApiClientInterface)

credentials, err := apiClient.CloudCredentials(id)
if err != nil {
if _, ok := err.(*client.NotFoundError); ok {
return client.Credentials{}, errors.New("credentials not found")
}

return client.Credentials{}, err
}

Expand All @@ -93,11 +96,14 @@ func getCredentialsById(id string, prefixList []string, meta interface{}) (clien

func getCredentials(ctx context.Context, id string, prefixList []string, meta interface{}) (client.Credentials, error) {
_, err := uuid.Parse(id)

if err == nil {
tflog.Info(ctx, "Resolving credentials by id", map[string]interface{}{"id": id})

return getCredentialsById(id, prefixList, meta)
} else {
tflog.Info(ctx, "Resolving credentials by name", map[string]interface{}{"name": id})

return getCredentialsByName(id, prefixList, meta)
}
}
Expand All @@ -106,10 +112,12 @@ func resourceCredentialsDelete(ctx context.Context, d *schema.ResourceData, meta
apiClient := meta.(client.ApiClientInterface)

id := d.Id()

err := apiClient.CloudCredentialsDelete(id)
if err != nil {
return diag.Errorf("could not delete credentials: %v", err)
}

return nil
}

Expand Down Expand Up @@ -137,11 +145,12 @@ func resourceCredentialsImport(cloudType CloudType) schema.StateContextFunc {
if _, ok := err.(*client.NotFoundError); ok {
return nil, fmt.Errorf(string(cloudType)+" credentials resource with id %v not found", d.Id())
}

return nil, err
}

if err := writeResourceData(&credentials, d); err != nil {
return nil, fmt.Errorf("schema resource data serialization failed: %v", err)
return nil, fmt.Errorf("schema resource data serialization failed: %w", err)
}

return []*schema.ResourceData{d}, nil
Expand Down
4 changes: 1 addition & 3 deletions env0/data_agent_values.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func dataAgentValuesRead(ctx context.Context, d *schema.ResourceData, meta inter
return diag.Errorf("could not get agent values: %v", err)
}

if err := d.Set("values", values); err != nil {
return diag.FromErr(err)
}
d.Set("values", values)

d.SetId(agentKey)

Expand Down
3 changes: 3 additions & 0 deletions env0/data_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ func dataApiKey() *schema.Resource {

func dataApiKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var apiKey *client.ApiKey

var err error

id, ok := d.GetOk("id")
if ok {
apiKey, err = getApiKeyById(id.(string), meta)

if err != nil {
return diag.Errorf("could not read api key: %v", err)
}

if apiKey == nil {
return diag.Errorf("could not read api key: id %v not found", id)
}
Expand Down
4 changes: 1 addition & 3 deletions env0/data_cloud_credentials.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ func dataCloudCredentialsRead(ctx context.Context, d *schema.ResourceData, meta
data = append(data, credentials.Name)
}

if err := d.Set("names", data); err != nil {
return diag.FromErr(err)
}
d.Set("names", data)

// Not really needed. But required by Terraform SDK - https://github.com/hashicorp/terraform-plugin-sdk/issues/541
d.SetId("all_cloud_credential_names")
Expand Down
14 changes: 8 additions & 6 deletions env0/data_configuration_variable.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,10 @@ func dataConfigurationVariableRead(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("schema resource data serialization failed: %v", err)
}

if err := d.Set("enum", variable.Schema.Enum); err != nil {
return diag.FromErr(err)
}
d.Set("enum", variable.Schema.Enum)

if variable.Schema.Format != client.Text {
if err := d.Set("format", string(variable.Schema.Format)); err != nil {
return diag.FromErr(err)
}
d.Set("format", string(variable.Schema.Format))
}

return nil
Expand All @@ -151,22 +148,27 @@ func dataConfigurationVariableRead(ctx context.Context, d *schema.ResourceData,
func getScopeAndId(d *schema.ResourceData) (client.Scope, string) {
scope := client.ScopeGlobal
scopeId := ""

if projectId, ok := d.GetOk("project_id"); ok {
scope = client.ScopeProject
scopeId = projectId.(string)
}

if templateId, ok := d.GetOk("template_id"); ok {
scope = client.ScopeTemplate
scopeId = templateId.(string)
}

if environmentId, ok := d.GetOk("environment_id"); ok {
scope = client.ScopeEnvironment
scopeId = environmentId.(string)
}

if deploymentLogId, ok := d.GetOk("deployment_log_id"); ok {
scope = client.ScopeDeploymentLog
scopeId = deploymentLogId.(string)
}

return scope, scopeId
}

Expand Down
2 changes: 2 additions & 0 deletions env0/data_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func dataCredentials(cloudType CloudType) *schema.Resource {
func dataCredentialsRead(cloudType CloudType) schema.ReadContextFunc {
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var err error

var credentials client.Credentials

prefixList := credentialsTypeToPrefixList[cloudType]
Expand All @@ -47,6 +48,7 @@ func dataCredentialsRead(cloudType CloudType) schema.ReadContextFunc {
if !ok {
return diag.Errorf("either 'name' or 'id' must be specified")
}

credentials, err = getCredentialsByName(name.(string), prefixList, meta)
if err != nil {
return diag.Errorf("could not query %s credentials by name: %v", cloudType, err)
Expand Down
4 changes: 1 addition & 3 deletions env0/data_custom_roles.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func dataCustomRolesRead(ctx context.Context, d *schema.ResourceData, meta inter
}
}

if err := d.Set("names", data); err != nil {
return diag.FromErr(err)
}
d.Set("names", data)

// Not really needed. But required by Terraform SDK - https://github.com/hashicorp/terraform-plugin-sdk/issues/541
d.SetId("all_roles_names")
Expand Down
4 changes: 1 addition & 3 deletions env0/data_ip_ranges.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ func dataIpRangesRead(ctx context.Context, d *schema.ResourceData, meta interfac
"54.165.19.49/32",
}

if err := d.Set("ipv4", ipv4s); err != nil {
return diag.FromErr(err)
}
d.Set("ipv4", ipv4s)

d.SetId("ip_ranges")

Expand Down
4 changes: 1 addition & 3 deletions env0/data_notifications.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ func dataNotificationsRead(ctx context.Context, d *schema.ResourceData, meta int
names = append(names, notification.Name)
}

if err := d.Set("names", names); err != nil {
return diag.FromErr(err)
}
d.Set("names", names)

d.SetId("all_notification_names")

Expand Down
4 changes: 1 addition & 3 deletions env0/data_organization.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func dataOrganizationRead(ctx context.Context, d *schema.ResourceData, meta inte

oidcSub, err := apiClient.OidcSub()
if err == nil {
if err := d.Set("oidc_sub", oidcSub); err != nil {
return diag.FromErr(err)
}
d.Set("oidc_sub", oidcSub)
}

return nil
Expand Down
13 changes: 5 additions & 8 deletions env0/data_project.go
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have anything to do with the issue?

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(parentId string, projects []client.Project) ([]client.Project, error) {
func filterByParentProjectId(parentId string, projects []client.Project) []client.Project {
filteredProjects := make([]client.Project, 0)
for _, project := range projects {
if len(project.ParentProjectId) == 0 {
Expand All @@ -104,7 +104,7 @@ func filterByParentProjectId(parentId string, projects []client.Project) ([]clie
}
}

return filteredProjects, nil
return filteredProjects
}

func filterByParentProjectName(parentName string, projects []client.Project, meta interface{}) ([]client.Project, error) {
Expand All @@ -131,7 +131,7 @@ func getProjectByName(name string, parentId string, parentName string, meta inte
apiClient := meta.(client.ApiClientInterface)
projects, err := apiClient.Projects()
if err != nil {
return client.Project{}, fmt.Errorf("could not query project by name: %v", err)
return client.Project{}, fmt.Errorf("could not query project by name: %w", err)
}

projectsByName := make([]client.Project, 0)
Expand All @@ -142,10 +142,7 @@ func getProjectByName(name string, parentId string, parentName string, meta inte
}
if len(parentId) > 0 {
// Use parentId filter to reduce the results.
projectsByName, err = filterByParentProjectId(parentId, projectsByName)
if err != nil {
return client.Project{}, err
}
projectsByName = filterByParentProjectId(parentId, projectsByName)
} else if len(parentName) > 0 {
// Use parentName filter to reduce the results.
projectsByName, err = filterByParentProjectName(parentName, projectsByName, meta)
Expand All @@ -172,7 +169,7 @@ func getProjectById(id string, meta interface{}) (client.Project, error) {
if frerr, ok := err.(*http.FailedResponseError); ok && frerr.NotFound() {
return client.Project{}, fmt.Errorf("could not find a project with id: %s", id)
}
return client.Project{}, fmt.Errorf("could not query project: %v", err)
return client.Project{}, fmt.Errorf("could not query project: %w", err)
}
return project, nil
}
1 change: 1 addition & 0 deletions env0/data_project_cloud_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func dataProjectCloudCredentialsRead(ctx context.Context, d *schema.ResourceData
}

d.Set("ids", credentialIds)

d.SetId(projectId)

return nil
Expand Down
5 changes: 3 additions & 2 deletions env0/resource_approval_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/jinzhu/copier"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

Expand All @@ -33,7 +34,7 @@ func TestUnitApprovalPolicyResource(t *testing.T) {
}

var template client.Template
copier.Copy(&template, &approvalPolicy)
require.NoError(t, copier.Copy(&template, &approvalPolicy))
template.Type = string(ApprovalPolicy)

deletedTemplate := template
Expand All @@ -53,7 +54,7 @@ func TestUnitApprovalPolicyResource(t *testing.T) {
}

var updatedTemplate client.Template
copier.Copy(&updatedTemplate, &updatedApprovalPolicy)
require.NoError(t, copier.Copy(&updatedTemplate, &updatedApprovalPolicy))
updatedTemplate.Type = string(ApprovalPolicy)

createPayload := client.ApprovalPolicyCreatePayload{
Expand Down
Loading
Loading