Skip to content

Commit

Permalink
Feat cloud cost credentials data - 218 (#317)
Browse files Browse the repository at this point in the history
Co-authored-by: Yaron Yarimi <yaronya@users.noreply.github.com>
  • Loading branch information
samuel-br and yaronya committed Apr 17, 2022
1 parent 60330de commit 5433e6a
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 21 deletions.
6 changes: 3 additions & 3 deletions client/cloud_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ type GoogleCostCredentialsCreatePayload struct {
Name string `json:"name"`
OrganizationId string `json:"organizationId"`
Type GcpCredentialsType `json:"type"`
Value GoogleCostCredentialsValeuPayload `json:"value"`
Value GoogleCostCredentialsValuePayload `json:"value"`
}

type GoogleCostCredentialsValeuPayload struct {
type GoogleCostCredentialsValuePayload struct {
TableId string `json:"tableid"`
Secret string `json:"secret"`
}
Expand All @@ -68,7 +68,7 @@ type GcpCredentialsValuePayload struct {
}

const (
GoogleCostCredentiassType GcpCredentialsType = "GCP_CREDENTIALS"
GoogleCostCredentialsType GcpCredentialsType = "GCP_CREDENTIALS"
AzureCostCredentialsType AzureCredentialsType = "AZURE_CREDENTIALS"
AwsCostCredentialsType AwsCredentialsType = "AWS_ASSUMED_ROLE"
AwsAssumedRoleCredentialsType AwsCredentialsType = "AWS_ASSUMED_ROLE_FOR_DEPLOYMENT"
Expand Down
2 changes: 1 addition & 1 deletion client/cloud_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = Describe("CloudCredentials", func() {
BeforeEach(func() {
mockOrganizationIdCall(organizationId)

payloadValue := GoogleCostCredentialsValeuPayload{
payloadValue := GoogleCostCredentialsValuePayload{
TableId: "table",
Secret: "secret",
}
Expand Down
96 changes: 96 additions & 0 deletions env0/data_cost_credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package env0

import (
"context"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataCostCredentials(credType string) *schema.Resource {
return &schema.Resource{
ReadContext: dataCostCredentialsRead(credType),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "the name of the credential",
Optional: true,
ExactlyOneOf: []string{"name", "id"},
},
"id": {
Type: schema.TypeString,
Description: "the id of the credential",
Optional: true,
ExactlyOneOf: []string{"name", "id"},
},
},
}
}

func dataCostCredentialsRead(credType string) func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var err diag.Diagnostics
var credentials *client.Credentials

id, ok := d.GetOk("id")
if ok {
credentials, err = getCostCredentialsById(id.(string), credType, meta)
if err != nil {
return err
}
} else {
name, ok := d.GetOk("name")
if !ok {
return diag.Errorf("Either 'name' or 'id' must be specified")
}
credentials, err = getCostCredentialsByName(name.(string), credType, meta)
if err != nil {
return err
}
}

errorWhenWriteData := writeResourceData(credentials, d)
if errorWhenWriteData != nil {
return diag.Errorf("Error: %v", errorWhenWriteData)
}

return nil
}
}

func getCostCredentialsByName(name interface{}, credType string, meta interface{}) (*client.Credentials, diag.Diagnostics) {
apiClient := meta.(client.ApiClientInterface)
credentialsList, err := apiClient.CloudCredentialsList()
if err != nil {
return &client.Credentials{}, diag.Errorf("Could not query Cost Credentials by name: %v", err)
}

credentialsByNameAndType := make([]client.Credentials, 0)
for _, candidate := range credentialsList {
if candidate.Name == name.(string) && candidate.Type == credType {
credentialsByNameAndType = append(credentialsByNameAndType, candidate)
}
}

if len(credentialsByNameAndType) > 1 {
return &client.Credentials{}, diag.Errorf("Found multiple Cost Credentials for name: %s", name)
}
if len(credentialsByNameAndType) == 0 {
return &client.Credentials{}, diag.Errorf("Could not find Cost Credentials with name: %s", name)
}
return &credentialsByNameAndType[0], nil
}

func getCostCredentialsById(id string, credType string, meta interface{}) (*client.Credentials, diag.Diagnostics) {
apiClient := meta.(client.ApiClientInterface)
credentials, err := apiClient.CloudCredentials(id)
if credentials.Type != credType {
return &client.Credentials{}, diag.Errorf("Found credentials which are not Cost Credentials: %v", credentials)
}
if err != nil {
return &client.Credentials{}, diag.Errorf("Could not query Cost Credentials: %v", err)
}
return &credentials, nil
}
Loading

0 comments on commit 5433e6a

Please sign in to comment.