Skip to content

Commit

Permalink
fix: random error related to client
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar Lopez committed Jul 27, 2021
1 parent 33e097e commit 1fb2418
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions mongodbatlas/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodbatlas
import (
"context"

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

digest "github.com/mongodb-forks/digest"
Expand All @@ -25,14 +26,14 @@ type MongoDBClient struct {
}

// NewClient func...
func (c *Config) NewClient() interface{} {
func (c *Config) NewClient(ctx context.Context) (interface{}, diag.Diagnostics) {
// setup a transport to handle digest
transport := digest.NewTransport(c.PublicKey, c.PrivateKey)

// initialize the client
client, err := transport.Client()
if err != nil {
return err
return nil, diag.FromErr(err)
}

client.Transport = logging.NewTransport("MongoDB Atlas", transport)
Expand All @@ -45,7 +46,7 @@ func (c *Config) NewClient() interface{} {
// Initialize the MongoDB Atlas API Client.
atlasClient, err := matlasClient.New(client, optsAtlas...)
if err != nil {
return err
return nil, diag.FromErr(err)
}

// Realm
Expand All @@ -54,9 +55,9 @@ func (c *Config) NewClient() interface{} {
optsRealm = append(optsRealm, realm.SetBaseURL(c.BaseURL))
}
authConfig := realmAuth.NewConfig(client)
token, err := authConfig.NewTokenFromCredentials(context.Background(), c.PublicKey, c.PrivateKey)
token, err := authConfig.NewTokenFromCredentials(ctx, c.PublicKey, c.PrivateKey)
if err != nil {
return err
return nil, diag.FromErr(err)
}

clientRealm := realmAuth.NewClient(realmAuth.BasicTokenSource(token))
Expand All @@ -65,13 +66,13 @@ func (c *Config) NewClient() interface{} {
// Initialize the MongoDB Realm API Client.
realmClient, err := realm.New(clientRealm, optsRealm...)
if err != nil {
return err
return nil, diag.FromErr(err)
}

clients := &MongoDBClient{
Atlas: atlasClient,
Realm: realmClient,
}

return clients
return clients, nil
}
2 changes: 1 addition & 1 deletion mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
config.BaseURL = baseURL.(string)
}

return config.NewClient(), nil
return config.NewClient(ctx)
}

func encodeStateID(values map[string]string) string {
Expand Down

0 comments on commit 1fb2418

Please sign in to comment.