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 Duration for AWS Assume Roles in env0_aws_crede… #711

Merged
merged 3 commits into from
Sep 27, 2023
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
1 change: 1 addition & 0 deletions client/cloud_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type AwsCredentialsCreatePayload struct {

type AwsCredentialsValuePayload struct {
RoleArn string `json:"roleArn" tfschema:"arn"`
Duration int `json:"duration,omitempty"`
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
}
Expand Down
6 changes: 4 additions & 2 deletions client/cloud_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ var _ = Describe("CloudCredentials", func() {
mockOrganizationIdCall(organizationId)

payloadValue := AwsCredentialsValuePayload{
RoleArn: "role",
RoleArn: "role",
Duration: 1,
sagilaufer1992 marked this conversation as resolved.
Show resolved Hide resolved
}

httpCall = mockHttpClient.EXPECT().
Expand Down Expand Up @@ -120,7 +121,8 @@ var _ = Describe("CloudCredentials", func() {
mockOrganizationIdCall(organizationId)

payloadValue := AwsCredentialsValuePayload{
RoleArn: "role",
RoleArn: "role",
Duration: 1,
}

httpCall = mockHttpClient.EXPECT().
Expand Down
6 changes: 6 additions & 0 deletions env0/resource_cost_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func resourceCostCredentials(providerName string) *schema.Resource {
Description: "the aws role arn",
Required: true,
},
"duration": {
Type: schema.TypeInt,
Description: "the session duration in seconds. If set must be one of the following: 3600 (1h), 7200 (2h), 14400 (4h), 18000 (5h default), 28800 (8h), 43200 (12h)",
Optional: true,
ValidateDiagFunc: NewIntInValidator([]int{3600, 7200, 14400, 18000, 28800, 43200}),
},
}
case AZURE:
return map[string]*schema.Schema{
Expand Down
38 changes: 32 additions & 6 deletions env0/resource_cost_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env0

import (
"regexp"
"strconv"
"testing"

"github.com/env0/terraform-provider-env0/client"
Expand All @@ -17,27 +18,37 @@ func TestUnitAwsCostCredentialsResource(t *testing.T) {
accessor := resourceAccessor(resourceType, resourceName)

awsCredentialResource := map[string]interface{}{
"name": "test",
"arn": "11111",
"name": "test",
"arn": "11111",
"duration": 7200,
}

updatedAwsCredentialResource := map[string]interface{}{
"name": "update",
"arn": "33333",
"name": "update",
"arn": "33333",
"duration": 3600,
}

invalidDurationAwsCredentialResource := map[string]interface{}{
"name": "update",
"arn": "33333",
"duration": 1234,
}

awsCredCreatePayload := client.AwsCredentialsCreatePayload{
Name: awsCredentialResource["name"].(string),
Value: client.AwsCredentialsValuePayload{
RoleArn: awsCredentialResource["arn"].(string),
RoleArn: awsCredentialResource["arn"].(string),
Duration: awsCredentialResource["duration"].(int),
},
Type: client.AwsCostCredentialsType,
}

updateAwsCredCreatePayload := client.AwsCredentialsCreatePayload{
Name: updatedAwsCredentialResource["name"].(string),
Value: client.AwsCredentialsValuePayload{
RoleArn: updatedAwsCredentialResource["arn"].(string),
RoleArn: updatedAwsCredentialResource["arn"].(string),
Duration: updatedAwsCredentialResource["duration"].(int),
},
Type: client.AwsCostCredentialsType,
}
Expand All @@ -64,6 +75,7 @@ func TestUnitAwsCostCredentialsResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "name", awsCredentialResource["name"].(string)),
resource.TestCheckResourceAttr(accessor, "arn", awsCredentialResource["arn"].(string)),
resource.TestCheckResourceAttr(accessor, "id", "id"),
resource.TestCheckResourceAttr(accessor, "duration", strconv.Itoa(awsCredentialResource["duration"].(int))),
),
},
},
Expand All @@ -77,6 +89,7 @@ func TestUnitAwsCostCredentialsResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "name", awsCredentialResource["name"].(string)),
resource.TestCheckResourceAttr(accessor, "arn", awsCredentialResource["arn"].(string)),
resource.TestCheckResourceAttr(accessor, "id", returnValues.Id),
resource.TestCheckResourceAttr(accessor, "duration", strconv.Itoa(awsCredentialResource["duration"].(int))),
),
},
{
Expand All @@ -85,6 +98,7 @@ func TestUnitAwsCostCredentialsResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "name", updatedAwsCredentialResource["name"].(string)),
resource.TestCheckResourceAttr(accessor, "arn", updatedAwsCredentialResource["arn"].(string)),
resource.TestCheckResourceAttr(accessor, "id", updateReturnValues.Id),
resource.TestCheckResourceAttr(accessor, "duration", strconv.Itoa(updatedAwsCredentialResource["duration"].(int))),
),
},
},
Expand Down Expand Up @@ -129,6 +143,18 @@ func TestUnitAwsCostCredentialsResource(t *testing.T) {
})
})

t.Run("throw error when don't enter duration valid values", func(t *testing.T) {
runUnitTest(t, resource.TestCase{
Steps: []resource.TestStep{
{
Config: resourceConfigCreate(resourceType, resourceName, invalidDurationAwsCredentialResource),
ExpectError: regexp.MustCompile("Error: must be one of"),
},
},
}, func(mock *client.MockApiClientInterface) {
})
})

}

func TestUnitAzureCostCredentialsResource(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions env0/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ func NewStringInValidator(allowedValues []string) schema.SchemaValidateDiagFunc
}
}

func NewIntInValidator(allowedValues []int) schema.SchemaValidateDiagFunc {
return func(i interface{}, p cty.Path) diag.Diagnostics {
value := i.(int)
for _, allowedValue := range allowedValues {
if value == allowedValue {
return nil
}
}

return diag.Errorf("must be one of: %s", fmt.Sprint(allowedValues))
}
}

func NewGreaterThanValidator(greaterThan int) schema.SchemaValidateDiagFunc {
return func(i interface{}, p cty.Path) diag.Diagnostics {
value := i.(int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ resource "env0_project" "project" {
}

resource "env0_aws_cost_credentials" "cost" {
name = "cost-${random_string.random.result}"
arn = "arn"
name = "cost-${random_string.random.result}"
arn = "arn"
duration = 3600
}

resource "env0_cost_credentials_project_assignment" "cost_project_assignment" {
Expand Down
Loading