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

chore: Migrates mongodbatlas_project_api_key to new auto-generated SDK #2437

Merged
merged 5 commits into from
Jul 19, 2024
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
21 changes: 10 additions & 11 deletions internal/service/projectapikey/data_source_project_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasProjectAPIKeyRead,
ReadContext: dataSourceRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -57,35 +57,34 @@ func DataSource() *schema.Resource {
}
}

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

projectID := d.Get("project_id").(string)
apiKeyID := d.Get("api_key_id").(string)
projectAPIKeys, _, err := conn.ProjectAPIKeys.List(ctx, projectID, nil)
projectAPIKeys, _, err := connV2.ProgrammaticAPIKeysApi.ListProjectApiKeys(ctx, projectID).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting api key information: %s", err))
}

for _, val := range projectAPIKeys {
if val.ID != apiKeyID {
for _, val := range projectAPIKeys.GetResults() {
if val.GetId() != apiKeyID {
continue
}

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

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

if err := d.Set("private_key", val.PrivateKey); err != nil {
if err := d.Set("private_key", val.GetPrivateKey()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `private_key`: %s", err))
}

if projectAssignments, err := newProjectAssignment(ctx, conn, apiKeyID); err == nil {
if projectAssignments, err := newProjectAssignment(ctx, connV2, apiKeyID); err == nil {
if err := d.Set("project_assignment", projectAssignments); err != nil {
return diag.Errorf(ErrorProjectSetting, `project_assignment`, projectID, err)
}
Expand Down
32 changes: 14 additions & 18 deletions internal/service/projectapikey/data_source_project_api_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"

matlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/atlas-sdk/v20240530002/admin"
)

func PluralDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasProjectAPIKeysRead,
ReadContext: pluralDataSourceRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -75,22 +74,19 @@ func PluralDataSource() *schema.Resource {
}
}

func dataSourceMongoDBAtlasProjectAPIKeysRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
conn := meta.(*config.MongoDBClient).Atlas
options := &matlas.ListOptions{
PageNum: d.Get("page_num").(int),
ItemsPerPage: d.Get("items_per_page").(int),
}
func pluralDataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
pageNum := d.Get("page_num").(int)
itemsPerPage := d.Get("items_per_page").(int)

projectID := d.Get("project_id").(string)

apiKeys, _, err := conn.ProjectAPIKeys.List(ctx, projectID, options)
apiKeys, _, err := connV2.ProgrammaticAPIKeysApi.ListProjectApiKeys(ctx, projectID).PageNum(pageNum).ItemsPerPage(itemsPerPage).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting api keys information: %s", err))
}

results, err := flattenProjectAPIKeys(ctx, conn, projectID, apiKeys)
results, err := flattenProjectAPIKeys(ctx, connV2, apiKeys.GetResults())
if err != nil {
diag.FromErr(fmt.Errorf("error setting `results`: %s", err))
}
Expand All @@ -104,7 +100,7 @@ func dataSourceMongoDBAtlasProjectAPIKeysRead(ctx context.Context, d *schema.Res
return nil
}

func flattenProjectAPIKeys(ctx context.Context, conn *matlas.Client, projectID string, apiKeys []matlas.APIKey) ([]map[string]any, error) {
func flattenProjectAPIKeys(ctx context.Context, connV2 *admin.APIClient, apiKeys []admin.ApiKeyUserDetails) ([]map[string]any, error) {
var results []map[string]any

if len(apiKeys) == 0 {
Expand All @@ -114,13 +110,13 @@ func flattenProjectAPIKeys(ctx context.Context, conn *matlas.Client, projectID s
results = make([]map[string]any, len(apiKeys))
for k, apiKey := range apiKeys {
results[k] = map[string]any{
"api_key_id": apiKey.ID,
"description": apiKey.Desc,
"public_key": apiKey.PublicKey,
"private_key": apiKey.PrivateKey,
"api_key_id": apiKey.GetId(),
"description": apiKey.GetDesc(),
"public_key": apiKey.GetPublicKey(),
"private_key": apiKey.GetPrivateKey(),
}

projectAssignment, err := newProjectAssignment(ctx, conn, apiKey.ID)
projectAssignment, err := newProjectAssignment(ctx, connV2, apiKey.GetId())
if err != nil {
return nil, err
}
Expand Down
Loading