-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create / Read / Delete implementation and tests for `mongodbatl…
…as_employee_access_grant` (#2573) * main_test file * CRU operations * unit tests * basic acc test * basic mig test * more acc tests * adding expectedErrContains as per PR feedback * check err in test
- Loading branch information
Showing
8 changed files
with
328 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package employeeaccessgrant_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
cleanup := acc.SetupSharedResources() | ||
exitCode := m.Run() | ||
cleanup() | ||
os.Exit(exitCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package employeeaccessgrant | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | ||
"go.mongodb.org/atlas-sdk/v20240805003/admin" | ||
) | ||
|
||
func NewTFModel(projectID, clusterName string, apiResp *admin.EmployeeAccessGrant) *TFModel { | ||
return &TFModel{ | ||
ProjectID: types.StringValue(projectID), | ||
ClusterName: types.StringValue(clusterName), | ||
GrantType: types.StringValue(apiResp.GetGrantType()), | ||
ExpirationTime: types.StringValue(conversion.TimeToString(apiResp.GetExpirationTime())), | ||
} | ||
} | ||
|
||
func NewAtlasReq(tfModel *TFModel) (*admin.EmployeeAccessGrant, error) { | ||
expirationTimeStr := tfModel.ExpirationTime.ValueString() | ||
expirationTime, ok := conversion.StringToTime(expirationTimeStr) | ||
if !ok { | ||
return nil, fmt.Errorf("expiration_time format is incorrect: %s", expirationTimeStr) | ||
} | ||
return &admin.EmployeeAccessGrant{ | ||
GrantType: tfModel.GrantType.ValueString(), | ||
ExpirationTime: expirationTime, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package employeeaccessgrant_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/employeeaccessgrant" | ||
"github.com/stretchr/testify/assert" | ||
"go.mongodb.org/atlas-sdk/v20240805003/admin" | ||
) | ||
|
||
func TestNewTFModel(t *testing.T) { | ||
testCases := map[string]struct { | ||
apiResp *admin.EmployeeAccessGrant | ||
expectedTFModel *employeeaccessgrant.TFModel | ||
projectID string | ||
clusterName string | ||
}{ | ||
"valid": { | ||
projectID: "projectID", | ||
clusterName: "clusterName", | ||
apiResp: &admin.EmployeeAccessGrant{ | ||
GrantType: "grantType", | ||
ExpirationTime: time.Date(2024, 10, 13, 0, 0, 0, 0, time.UTC), | ||
}, | ||
expectedTFModel: &employeeaccessgrant.TFModel{ | ||
ProjectID: types.StringValue("projectID"), | ||
ClusterName: types.StringValue("clusterName"), | ||
GrantType: types.StringValue("grantType"), | ||
ExpirationTime: types.StringValue("2024-10-13T00:00:00Z"), | ||
}, | ||
}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
tfModel := employeeaccessgrant.NewTFModel(tc.projectID, tc.clusterName, tc.apiResp) | ||
assert.Equal(t, tc.expectedTFModel, tfModel) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewAtlasReq(t *testing.T) { | ||
testCases := map[string]struct { | ||
tfModel *employeeaccessgrant.TFModel | ||
expectedReq *admin.EmployeeAccessGrant | ||
expectedErrContains string | ||
}{ | ||
"valid": { | ||
tfModel: &employeeaccessgrant.TFModel{ | ||
ProjectID: types.StringValue("projectID"), | ||
ClusterName: types.StringValue("clusterName"), | ||
GrantType: types.StringValue("grantType"), | ||
ExpirationTime: types.StringValue("2024-10-13T00:00:00Z"), | ||
}, | ||
expectedReq: &admin.EmployeeAccessGrant{ | ||
GrantType: "grantType", | ||
ExpirationTime: time.Date(2024, 10, 13, 0, 0, 0, 0, time.UTC), | ||
}, | ||
}, | ||
"invalid expiration time": { | ||
tfModel: &employeeaccessgrant.TFModel{ | ||
ProjectID: types.StringValue("projectID"), | ||
ClusterName: types.StringValue("clusterName"), | ||
GrantType: types.StringValue("grantType"), | ||
ExpirationTime: types.StringValue("invalid_time"), | ||
}, | ||
expectedErrContains: "invalid_time", | ||
}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
req, err := employeeaccessgrant.NewAtlasReq(tc.tfModel) | ||
assert.Equal(t, tc.expectedErrContains == "", err == nil) | ||
if err == nil { | ||
assert.Equal(t, tc.expectedReq, req) | ||
} else { | ||
assert.Contains(t, err.Error(), tc.expectedErrContains) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
internal/service/employeeaccessgrant/resource_migration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package employeeaccessgrant_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" | ||
) | ||
|
||
func TestMigEmployeeAccessGrant_basic(t *testing.T) { | ||
mig.SkipIfVersionBelow(t, "1.20.0") // this feature was introduced in provider version 1.20.0 | ||
mig.CreateAndRunTestNonParallel(t, basicTestCase(t)) // does not run in parallel to reuse same execution project and cluster | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package employeeaccessgrant_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" | ||
) | ||
|
||
const ( | ||
resourceName = "mongodbatlas_employee_access_grant.test" | ||
dataSourceName = "data.mongodbatlas_employee_access_grant.test" | ||
grantType = "CLUSTER_INFRASTRUCTURE" | ||
expirationTime = "2025-08-01T12:00:00Z" | ||
grantTypeInvalid = "invalid_grant_type" | ||
expirationTimeInvalid = "invalid_time" | ||
) | ||
|
||
func TestAccEmployeeAccessGrant_basic(t *testing.T) { | ||
resource.Test(t, *basicTestCase(t)) | ||
} | ||
|
||
func basicTestCase(tb testing.TB) *resource.TestCase { | ||
tb.Helper() | ||
projectID, clusterName := acc.ClusterNameExecution(tb) | ||
return &resource.TestCase{ | ||
PreCheck: func() { acc.PreCheckBasic(tb) }, | ||
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, | ||
CheckDestroy: checkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configBasic(projectID, clusterName, grantType, expirationTime), | ||
Check: checkBasic(projectID, clusterName, grantType, expirationTime), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func TestAccEmployeeAccessGrant_invalidExpirationTime(t *testing.T) { | ||
projectID, clusterName := acc.ClusterNameExecution(t) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.PreCheckBasic(t) }, | ||
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, | ||
CheckDestroy: checkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configBasic(projectID, clusterName, grantType, expirationTimeInvalid), | ||
ExpectError: regexp.MustCompile("expiration_time format is incorrect.*" + expirationTimeInvalid), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccEmployeeAccessGrant_invalidGrantType(t *testing.T) { | ||
projectID, clusterName := acc.ClusterNameExecution(t) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.PreCheckBasic(t) }, | ||
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, | ||
CheckDestroy: checkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configBasic(projectID, clusterName, grantTypeInvalid, expirationTime), | ||
ExpectError: regexp.MustCompile("invalid enumeration value.*" + grantTypeInvalid), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func configBasic(projectID, clusterName, grantType, expirationTime string) string { | ||
return fmt.Sprintf(` | ||
resource "mongodbatlas_employee_access_grant" "test" { | ||
project_id = %[1]q | ||
cluster_name = %[2]q | ||
grant_type = %[3]q | ||
expiration_time = %[4]q | ||
} | ||
`, projectID, clusterName, grantType, expirationTime) | ||
} | ||
|
||
func checkBasic(projectID, clusterName, grantType, expirationTime string) resource.TestCheckFunc { | ||
checks := []resource.TestCheckFunc{checkExists(resourceName)} | ||
attrsMap := map[string]string{ | ||
"project_id": projectID, | ||
"cluster_name": clusterName, | ||
"grant_type": grantType, | ||
"expiration_time": expirationTime, | ||
} | ||
checks = acc.AddAttrChecks(resourceName, checks, attrsMap) | ||
return resource.ComposeAggregateTestCheckFunc(checks...) | ||
} | ||
|
||
func checkExists(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", resourceName) | ||
} | ||
if !exists(rs) { | ||
return fmt.Errorf("employee access grant (%s) does not exist", resourceName) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func checkDestroy(state *terraform.State) error { | ||
for _, rs := range state.RootModule().Resources { | ||
if rs.Type == "mongodbatlas_employee_access_grant" { | ||
if exists(rs) { | ||
return fmt.Errorf("employee access grant still exists") | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func exists(rs *terraform.ResourceState) bool { | ||
projectID := rs.Primary.Attributes["project_id"] | ||
clusterName := rs.Primary.Attributes["cluster_name"] | ||
cluster, _, _ := acc.ConnV2().ClustersApi.GetCluster(context.Background(), projectID, clusterName).Execute() | ||
resp, _ := cluster.GetMongoDBEmployeeAccessGrantOk() | ||
return resp != nil | ||
} |