Skip to content

INTMDB-346: Feature add: add support for programmatic API keys #974

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

Merged
merged 17 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
69 changes: 69 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package mongodbatlas

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceMongoDBAtlasAPIKey() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasAPIKeyRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Required: true,
},
"api_key_id": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
},
"role_names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceMongoDBAtlasAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas

orgID := d.Get("org_id").(string)
apiKeyID := d.Get("api_key_id").(string)
apiKey, _, err := conn.APIKeys.Get(ctx, orgID, apiKeyID)
if err != nil {
return diag.FromErr(fmt.Errorf("error getting api key information: %s", err))
}

if err := d.Set("description", apiKey.Desc); err != nil {
return diag.FromErr(fmt.Errorf("error setting `description`: %s", err))
}

if err := d.Set("public_key", apiKey.PublicKey); err != nil {
return diag.FromErr(fmt.Errorf("error setting `public_key`: %s", err))
}

if err := d.Set("role_names", flattenOrgAPIKeyRoles(orgID, apiKey.Roles)); err != nil {
return diag.FromErr(fmt.Errorf("error setting `roles`: %s", err))
}

d.SetId(resource.UniqueId())

return nil
}
55 changes: 55 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package mongodbatlas

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccConfigDSAPIKey_basic(t *testing.T) {
resourceName := "mongodbatlas_api_key.test"
dataSourceName := "data.mongodbatlas_api_key.test"
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
description := fmt.Sprintf("test-acc-api_key-%s", acctest.RandString(5))
roleName := "ORG_MEMBER"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasNetworkPeeringDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAPIKeyConfig(orgID, description, roleName),
Check: resource.ComposeTestCheckFunc(
// Test for Resource
testAccCheckMongoDBAtlasAPIKeyExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttrSet(resourceName, "description"),
resource.TestCheckResourceAttr(resourceName, "org_id", orgID),
resource.TestCheckResourceAttr(resourceName, "description", description),
// Test for Data source
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "description"),
),
},
},
})
}

func testAccDSMongoDBAtlasAPIKeyConfig(orgID, apiKeyID, roleNames string) string {
return fmt.Sprintf(`
resource "mongodbatlas_api_key" "test" {
org_id = "%s"
description = "%s"
role_names = ["%s"]
}

data "mongodbatlas_api_key" "test" {
org_id = "${mongodbatlas_api_key.test.org_id}"
api_key_id = "${mongodbatlas_api_key.test.api_key_id}"
}
`, orgID, apiKeyID, roleNames)
}
83 changes: 83 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_api_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package mongodbatlas

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

matlas "go.mongodb.org/atlas/mongodbatlas"
)

func dataSourceMongoDBAtlasAPIKeys() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasAPIKeysRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Required: true,
},
"page_num": {
Type: schema.TypeInt,
Optional: true,
},
"items_per_page": {
Type: schema.TypeInt,
Optional: true,
},
"results": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
},
"api_key_id": {
Type: schema.TypeString,
Computed: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
},
"role_names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}

func dataSourceMongoDBAtlasAPIKeysRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas
options := &matlas.ListOptions{
PageNum: d.Get("page_num").(int),
ItemsPerPage: d.Get("items_per_page").(int),
}

orgID := d.Get("org_id").(string)

apiKeys, _, err := conn.APIKeys.List(ctx, orgID, options)
if err != nil {
return diag.FromErr(fmt.Errorf("error getting api keys information: %s", err))
}

if err := d.Set("results", flattenOrgAPIKeys(ctx, conn, orgID, apiKeys)); err != nil {
return diag.FromErr(fmt.Errorf("error setting `results`: %s", err))
}

d.SetId(resource.UniqueId())

return nil
}
56 changes: 56 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_api_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package mongodbatlas

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccConfigDSAPIKeys_basic(t *testing.T) {
resourceName := "mongodbatlas_api_key.test"
dataSourceName := "data.mongodbatlas_api_keys.test"
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
description := fmt.Sprintf("test-acc-api_key-%s", acctest.RandString(5))
roleName := "ORG_MEMBER"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasNetworkPeeringDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAPIKeysConfig(orgID, description, roleName),
Check: resource.ComposeTestCheckFunc(
// Test for Resource
testAccCheckMongoDBAtlasAPIKeyExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "org_id"),
resource.TestCheckResourceAttrSet(resourceName, "description"),

resource.TestCheckResourceAttr(resourceName, "org_id", orgID),
resource.TestCheckResourceAttr(resourceName, "description", description),

// Test for Data source
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "results.#"),
),
},
},
})
}

func testAccDSMongoDBAtlasAPIKeysConfig(orgID, description, roleNames string) string {
return fmt.Sprintf(`
resource "mongodbatlas_api_key" "test" {
org_id = "%s"
description = "%s"
role_names = ["%s"]
}

data "mongodbatlas_api_keys" "test" {
org_id = "${mongodbatlas_api_key.test.org_id}"
}
`, orgID, description, roleNames)
}
3 changes: 3 additions & 0 deletions mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func getDataSourcesMap() map[string]*schema.Resource {
"mongodbatlas_custom_db_roles": dataSourceMongoDBAtlasCustomDBRoles(),
"mongodbatlas_database_user": dataSourceMongoDBAtlasDatabaseUser(),
"mongodbatlas_database_users": dataSourceMongoDBAtlasDatabaseUsers(),
"mongodbatlas_api_key": dataSourceMongoDBAtlasAPIKey(),
"mongodbatlas_api_keys": dataSourceMongoDBAtlasAPIKeys(),
"mongodbatlas_project": dataSourceMongoDBAtlasProject(),
"mongodbatlas_projects": dataSourceMongoDBAtlasProjects(),
"mongodbatlas_cluster": dataSourceMongoDBAtlasCluster(),
Expand Down Expand Up @@ -154,6 +156,7 @@ func getDataSourcesMap() map[string]*schema.Resource {
func getResourcesMap() map[string]*schema.Resource {
resourcesMap := map[string]*schema.Resource{
"mongodbatlas_advanced_cluster": resourceMongoDBAtlasAdvancedCluster(),
"mongodbatlas_api_key": resourceMongoDBAtlasAPIKey(),
"mongodbatlas_custom_db_role": resourceMongoDBAtlasCustomDBRole(),
"mongodbatlas_database_user": resourceMongoDBAtlasDatabaseUser(),
"mongodbatlas_project": resourceMongoDBAtlasProject(),
Expand Down
Loading