Skip to content

Commit

Permalink
fix: Fixes creation of organization (#2462)
Browse files Browse the repository at this point in the history
* fix TerraformVersion interface conversion

* refactor organization resource

* add changelog entry

* PR comment
  • Loading branch information
oarbusi authored Jul 29, 2024
1 parent 12433b7 commit 80dcc2d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .changelog/2462.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/mongodbatlas_organization: Fixes a bug in organization resource creation where the provider crashed.
```
5 changes: 2 additions & 3 deletions internal/service/organization/data_source_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasOrganizationRead,
ReadContext: dataSourceRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -59,8 +59,7 @@ func DataSource() *schema.Resource {
}
}

func dataSourceMongoDBAtlasOrganizationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2
orgID := d.Get("org_id").(string)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccConfigDSOrganization_basic(t *testing.T) {
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasOrganizationConfigWithDS(orgID),
Config: configWithDS(orgID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(datasourceName, "name"),
resource.TestCheckResourceAttrSet(datasourceName, "id"),
Expand All @@ -31,7 +31,7 @@ func TestAccConfigDSOrganization_basic(t *testing.T) {
},
})
}
func testAccMongoDBAtlasOrganizationConfigWithDS(orgID string) string {
func configWithDS(orgID string) string {
config := fmt.Sprintf(`
data "mongodbatlas_organization" "test" {
Expand Down
5 changes: 2 additions & 3 deletions internal/service/organization/data_source_organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func PluralDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasOrganizationsRead,
ReadContext: pluralDataSourceRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -86,8 +86,7 @@ func PluralDataSource() *schema.Resource {
}
}

func dataSourceMongoDBAtlasOrganizationsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
func pluralDataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2

organizationOptions := &admin.ListOrganizationsApiParams{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccConfigDSOrganizations_basic(t *testing.T) {
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasOrganizationsConfigWithDS(),
Config: configWithPluralDS(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(datasourceName, "results.#"),
resource.TestCheckResourceAttrSet(datasourceName, "results.0.name"),
Expand All @@ -39,7 +39,7 @@ func TestAccConfigDSOrganizations_withPagination(t *testing.T) {
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasOrganizationsConfigWithPagination(2, 5),
Config: configWithPagination(2, 5),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(datasourceName, "results.#"),
),
Expand All @@ -48,14 +48,14 @@ func TestAccConfigDSOrganizations_withPagination(t *testing.T) {
})
}

func testAccMongoDBAtlasOrganizationsConfigWithDS() string {
func configWithPluralDS() string {
return `
data "mongodbatlas_organizations" "test" {
}
`
}

func testAccMongoDBAtlasOrganizationsConfigWithPagination(pageNum, itemPage int) string {
func configWithPagination(pageNum, itemPage int) string {
return fmt.Sprintf(`
data "mongodbatlas_organizations" "test" {
page_num = %d
Expand Down
28 changes: 14 additions & 14 deletions internal/service/organization/resource_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (

func Resource() *schema.Resource {
return &schema.Resource{
CreateContext: resourceMongoDBAtlasOrganizationCreate,
ReadContext: resourceMongoDBAtlasOrganizationRead,
UpdateContext: resourceMongoDBAtlasOrganizationUpdate,
DeleteContext: resourceMongoDBAtlasOrganizationDelete,
CreateContext: resourceCreate,
ReadContext: resourceRead,
UpdateContext: resourceUpdate,
DeleteContext: resourceDelete,
Importer: nil, // import is not supported. See CLOUDP-215155
Schema: map[string]*schema.Schema{
"org_owner_id": {
Expand Down Expand Up @@ -80,7 +80,7 @@ func Resource() *schema.Resource {
}
}

func resourceMongoDBAtlasOrganizationCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
if err := ValidateAPIKeyIsOrgOwner(conversion.ExpandStringList(d.Get("role_names").(*schema.Set).List())); err != nil {
return diag.FromErr(err)
}
Expand All @@ -104,7 +104,7 @@ func resourceMongoDBAtlasOrganizationCreate(ctx context.Context, d *schema.Resou
PublicKey: *organization.ApiKey.PublicKey,
PrivateKey: *organization.ApiKey.PrivateKey,
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
TerraformVersion: meta.(*config.Config).TerraformVersion,
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
}

clients, _ := cfg.NewClient(ctx)
Expand Down Expand Up @@ -136,16 +136,16 @@ func resourceMongoDBAtlasOrganizationCreate(ctx context.Context, d *schema.Resou
"org_id": organization.Organization.GetId(),
}))

return resourceMongoDBAtlasOrganizationRead(ctx, d, meta)
return resourceRead(ctx, d, meta)
}

func resourceMongoDBAtlasOrganizationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
cfg := config.Config{
PublicKey: d.Get("public_key").(string),
PrivateKey: d.Get("private_key").(string),
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
TerraformVersion: meta.(*config.Config).TerraformVersion,
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
}

clients, _ := cfg.NewClient(ctx)
Expand Down Expand Up @@ -189,13 +189,13 @@ func resourceMongoDBAtlasOrganizationRead(ctx context.Context, d *schema.Resourc
return nil
}

func resourceMongoDBAtlasOrganizationUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
cfg := config.Config{
PublicKey: d.Get("public_key").(string),
PrivateKey: d.Get("private_key").(string),
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
TerraformVersion: meta.(*config.Config).TerraformVersion,
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
}

clients, _ := cfg.NewClient(ctx)
Expand All @@ -218,16 +218,16 @@ func resourceMongoDBAtlasOrganizationUpdate(ctx context.Context, d *schema.Resou
}
}

return resourceMongoDBAtlasOrganizationRead(ctx, d, meta)
return resourceRead(ctx, d, meta)
}

func resourceMongoDBAtlasOrganizationDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
cfg := config.Config{
PublicKey: d.Get("public_key").(string),
PrivateKey: d.Get("private_key").(string),
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
TerraformVersion: meta.(*config.Config).TerraformVersion,
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
}

clients, _ := cfg.NewClient(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestMigConfigRSOrganization_Basic(t *testing.T) {
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, name, description, roleName),
Config: configBasic(orgOwnerID, name, description, roleName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttrSet(resourceName, "description"),
Expand All @@ -35,7 +35,7 @@ func TestMigConfigRSOrganization_Basic(t *testing.T) {
},
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, name, description, roleName),
Config: configBasic(orgOwnerID, name, description, roleName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
acc.DebugPlan(),
Expand Down
36 changes: 18 additions & 18 deletions internal/service/organization/resource_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func TestAccConfigRSOrganization_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasOrganizationDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, name, description, roleName),
Config: configBasic(orgOwnerID, name, description, roleName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMongoDBAtlasOrganizationExists(resourceName),
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttr(resourceName, "description", description),
resource.TestCheckResourceAttr(resourceName, "api_access_list_required", "false"),
Expand All @@ -47,9 +47,9 @@ func TestAccConfigRSOrganization_Basic(t *testing.T) {
),
},
{
Config: testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, updatedName, description, roleName),
Config: configBasic(orgOwnerID, updatedName, description, roleName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMongoDBAtlasOrganizationExists(resourceName),
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttr(resourceName, "description", description),
resource.TestCheckResourceAttr(resourceName, "api_access_list_required", "false"),
Expand All @@ -74,10 +74,10 @@ func TestAccConfigRSOrganization_BasicAccess(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasOrganizationDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, name, description, roleName),
Config: configBasic(orgOwnerID, name, description, roleName),
ExpectError: regexp.MustCompile("API Key must have the ORG_OWNER role"),
},
},
Expand Down Expand Up @@ -106,12 +106,12 @@ func TestAccConfigRSOrganization_Settings(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasOrganizationDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasOrganizationConfigWithSettings(orgOwnerID, name, description, roleName, settingsConfig),
Config: configWithSettings(orgOwnerID, name, description, roleName, settingsConfig),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMongoDBAtlasOrganizationExists(resourceName),
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttr(resourceName, "description", description),
resource.TestCheckResourceAttr(resourceName, "api_access_list_required", "false"),
Expand All @@ -120,9 +120,9 @@ func TestAccConfigRSOrganization_Settings(t *testing.T) {
),
},
{
Config: testAccMongoDBAtlasOrganizationConfigWithSettings(orgOwnerID, name, description, roleName, settingsConfigUpdated),
Config: configWithSettings(orgOwnerID, name, description, roleName, settingsConfigUpdated),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMongoDBAtlasOrganizationExists(resourceName),
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttr(resourceName, "description", description),
resource.TestCheckResourceAttr(resourceName, "api_access_list_required", "false"),
Expand All @@ -131,9 +131,9 @@ func TestAccConfigRSOrganization_Settings(t *testing.T) {
),
},
{
Config: testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, "org-name-updated", description, roleName),
Config: configBasic(orgOwnerID, "org-name-updated", description, roleName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMongoDBAtlasOrganizationExists(resourceName),
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttrSet(resourceName, "description"),
resource.TestCheckResourceAttr(resourceName, "description", description),
Expand All @@ -143,7 +143,7 @@ func TestAccConfigRSOrganization_Settings(t *testing.T) {
})
}

func testAccCheckMongoDBAtlasOrganizationExists(resourceName string) resource.TestCheckFunc {
func checkExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
Expand Down Expand Up @@ -174,7 +174,7 @@ func testAccCheckMongoDBAtlasOrganizationExists(resourceName string) resource.Te
}
}

func testAccCheckMongoDBAtlasOrganizationDestroy(s *terraform.State) error {
func checkDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "mongodbatlas_organization" {
continue
Expand All @@ -200,7 +200,7 @@ func testAccCheckMongoDBAtlasOrganizationDestroy(s *terraform.State) error {
return nil
}

func testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, name, description, roleNames string) string {
func configBasic(orgOwnerID, name, description, roleNames string) string {
return fmt.Sprintf(`
resource "mongodbatlas_organization" "test" {
org_owner_id = "%s"
Expand All @@ -211,7 +211,7 @@ func testAccMongoDBAtlasOrganizationConfigBasic(orgOwnerID, name, description, r
`, orgOwnerID, name, description, roleNames)
}

func testAccMongoDBAtlasOrganizationConfigWithSettings(orgOwnerID, name, description, roleNames, settingsConfig string) string {
func configWithSettings(orgOwnerID, name, description, roleNames, settingsConfig string) string {
return fmt.Sprintf(`
resource "mongodbatlas_organization" "test" {
org_owner_id = "%s"
Expand Down

0 comments on commit 80dcc2d

Please sign in to comment.