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

INTMDB-923: [Terraform] Add Autogenerated SDK to Terraform #1309

Merged
merged 9 commits into from
Jul 19, 2023
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/spf13/cast v1.5.1
github.com/zclconf/go-cty v1.13.2
go.mongodb.org/atlas v0.31.0
go.mongodb.org/atlas-sdk/v20230201002 v20230201002.0.0
go.mongodb.org/realm v0.1.0
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRK
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/atlas v0.31.0 h1:NgLqsNYm6wDYeDUO90etw1sl8T1U2DUKu36eUdnrFSI=
go.mongodb.org/atlas v0.31.0/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
go.mongodb.org/atlas-sdk/v20230201002 v20230201002.0.0 h1:ANYyIzbdxSdwwbragGKDuAg+3bmr940rbxCvZdIwnPk=
go.mongodb.org/atlas-sdk/v20230201002 v20230201002.0.0/go.mod h1:SG7r2T7R595Z/+x/miXGR89ZsinqXG/P6k0p+CSNBPY=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
48 changes: 38 additions & 10 deletions mongodbatlas/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ package mongodbatlas
import (
"context"
"errors"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/mongodb-forks/digest"
"github.com/mongodb/terraform-provider-mongodbatlas/version"
"github.com/spf13/cast"
atlasSDK "go.mongodb.org/atlas-sdk/v20230201002/admin"
matlasClient "go.mongodb.org/atlas/mongodbatlas"
realmAuth "go.mongodb.org/realm/auth"
"go.mongodb.org/realm/realm"
)

// Config struct ...
const ToolName = "terraform-provider-mongodbatlas"

var userAgent = fmt.Sprintf("%s/%s", ToolName, version.ProviderVersion)

// Config contains the configurations needed to use SDKs
type Config struct {
AssumeRole *AssumeRole
PublicKey string
Expand All @@ -23,14 +30,13 @@ type Config struct {
RealmBaseURL string
}

// MongoDBClient client
// MongoDBClient contains the mongodbatlas clients and configurations
type MongoDBClient struct {
Atlas *matlasClient.Client
Config *Config
Atlas *matlasClient.Client
AtlasV2 *atlasSDK.APIClient
Config *Config
}

var ua = "terraform-provider-mongodbatlas/" + version.ProviderVersion

// NewClient func...
func (c *Config) NewClient(ctx context.Context) (interface{}, diag.Diagnostics) {
// setup a transport to handle digest
Expand All @@ -44,7 +50,7 @@ func (c *Config) NewClient(ctx context.Context) (interface{}, diag.Diagnostics)

client.Transport = logging.NewTransport("MongoDB Atlas", transport)

optsAtlas := []matlasClient.ClientOpt{matlasClient.SetUserAgent(ua)}
optsAtlas := []matlasClient.ClientOpt{matlasClient.SetUserAgent(userAgent)}
if c.BaseURL != "" {
optsAtlas = append(optsAtlas, matlasClient.SetBaseURL(c.BaseURL))
}
Expand All @@ -55,21 +61,43 @@ func (c *Config) NewClient(ctx context.Context) (interface{}, diag.Diagnostics)
return nil, diag.FromErr(err)
}

sdkV2Client, err := c.newSDKV2Client(client)
if err != nil {
return nil, diag.FromErr(err)
}

clients := &MongoDBClient{
Atlas: atlasClient,
Config: c,
Atlas: atlasClient,
AtlasV2: sdkV2Client,
Config: c,
}

return clients, nil
}

func (c *Config) newSDKV2Client(client *http.Client) (*atlasSDK.APIClient, error) {
opts := []atlasSDK.ClientModifier{
atlasSDK.UseHTTPClient(client),
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
atlasSDK.UseUserAgent(userAgent),
atlasSDK.UseBaseURL(c.BaseURL),
atlasSDK.UseDebug(false)}

// Initialize the MongoDB Versioned Atlas Client.
sdkv2, err := atlasSDK.NewClient(opts...)
if err != nil {
return nil, err
}

return sdkv2, nil
}

func (c *MongoDBClient) GetRealmClient(ctx context.Context) (*realm.Client, error) {
// Realm
if c.Config.PublicKey == "" && c.Config.PrivateKey == "" {
return nil, errors.New("please set `public_key` and `private_key` in order to use the realm client")
}

optsRealm := []realm.ClientOpt{realm.SetUserAgent(ua)}
optsRealm := []realm.ClientOpt{realm.SetUserAgent(userAgent)}
if c.Config.BaseURL != "" && c.Config.RealmBaseURL != "" {
optsRealm = append(optsRealm, realm.SetBaseURL(c.Config.RealmBaseURL))
}
Expand Down