Skip to content

Commit e8944c5

Browse files
authored
feat: mcli integration (#536)
1 parent de6a15f commit e8944c5

File tree

3 files changed

+85
-71
lines changed

3 files changed

+85
-71
lines changed

mongodbatlas/config.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ package mongodbatlas
22

33
import (
44
"context"
5-
"fmt"
5+
"errors"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
9+
"github.com/mongodb-forks/digest"
910
"github.com/mongodb/terraform-provider-mongodbatlas/version"
1011
"github.com/spf13/cast"
11-
realmAuth "go.mongodb.org/realm/auth"
12-
13-
"github.com/mongodb-forks/digest"
1412
matlasClient "go.mongodb.org/atlas/mongodbatlas"
13+
realmAuth "go.mongodb.org/realm/auth"
1514
"go.mongodb.org/realm/realm"
1615
)
1716

1817
// Config struct ...
1918
type Config struct {
20-
PublicKey string
21-
PrivateKey string
22-
BaseURL string
19+
PublicKey string
20+
PrivateKey string
21+
BaseURL string
22+
RealmBaseURL string
2323
}
2424

2525
// MongoDBClient client
@@ -28,6 +28,8 @@ type MongoDBClient struct {
2828
Config *Config
2929
}
3030

31+
var ua = "terraform-provider-mongodbatlas/" + version.ProviderVersion
32+
3133
// NewClient func...
3234
func (c *Config) NewClient(ctx context.Context) (interface{}, diag.Diagnostics) {
3335
// setup a transport to handle digest
@@ -41,8 +43,8 @@ func (c *Config) NewClient(ctx context.Context) (interface{}, diag.Diagnostics)
4143

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

44-
optsAtlas := []matlasClient.ClientOpt{matlasClient.SetUserAgent("terraform-provider-mongodbatlas/" + version.ProviderVersion)}
45-
if len(c.BaseURL) > 0 {
46+
optsAtlas := []matlasClient.ClientOpt{matlasClient.SetUserAgent(ua)}
47+
if c.BaseURL != "" {
4648
optsAtlas = append(optsAtlas, matlasClient.SetBaseURL(c.BaseURL))
4749
}
4850

@@ -63,12 +65,12 @@ func (c *Config) NewClient(ctx context.Context) (interface{}, diag.Diagnostics)
6365
func (c *MongoDBClient) GetRealmClient(ctx context.Context) (*realm.Client, error) {
6466
// Realm
6567
if c.Config.PublicKey == "" && c.Config.PrivateKey == "" {
66-
return nil, fmt.Errorf("please set `public_key` and `private_key` in order to use the realm client")
68+
return nil, errors.New("please set `public_key` and `private_key` in order to use the realm client")
6769
}
6870

69-
optsRealm := []realm.ClientOpt{realm.SetUserAgent("terraform-provider-mongodbatlas/" + version.ProviderVersion)}
70-
if len(c.Config.BaseURL) > 0 {
71-
optsRealm = append(optsRealm, realm.SetBaseURL(c.Config.BaseURL))
71+
optsRealm := []realm.ClientOpt{realm.SetUserAgent(ua)}
72+
if c.Config.BaseURL != "" && c.Config.RealmBaseURL != "" {
73+
optsRealm = append(optsRealm, realm.SetBaseURL(c.Config.RealmBaseURL))
7274
}
7375
authConfig := realmAuth.NewConfig(nil)
7476
token, err := authConfig.NewTokenFromCredentials(ctx, c.Config.PublicKey, c.Config.PrivateKey)

mongodbatlas/provider.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mongodbatlas
22

33
import (
4-
"bytes"
54
"context"
65
"encoding/base64"
76
"fmt"
@@ -22,22 +21,38 @@ func Provider() *schema.Provider {
2221
return &schema.Provider{
2322
Schema: map[string]*schema.Schema{
2423
"public_key": {
25-
Type: schema.TypeString,
26-
Required: true,
27-
DefaultFunc: schema.EnvDefaultFunc("MONGODB_ATLAS_PUBLIC_KEY", ""),
24+
Type: schema.TypeString,
25+
Required: true,
26+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
27+
"MONGODB_ATLAS_PUBLIC_KEY",
28+
"MCLI_PUBLIC_API_KEY",
29+
}, ""),
2830
Description: "MongoDB Atlas Programmatic Public Key",
2931
},
3032
"private_key": {
31-
Type: schema.TypeString,
32-
Required: true,
33-
DefaultFunc: schema.EnvDefaultFunc("MONGODB_ATLAS_PRIVATE_KEY", ""),
33+
Type: schema.TypeString,
34+
Required: true,
35+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
36+
"MONGODB_ATLAS_PRIVATE_KEY",
37+
"MCLI_PRIVATE_API_KEY",
38+
}, ""),
3439
Description: "MongoDB Atlas Programmatic Private Key",
40+
Sensitive: true,
3541
},
3642
"base_url": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
46+
"MONGODB_ATLAS_BASE_URL",
47+
"MCLI_OPS_MANAGER_URL",
48+
}, ""),
49+
Description: "MongoDB Atlas Base URL",
50+
},
51+
"realm_base_url": {
3752
Type: schema.TypeString,
3853
Optional: true,
39-
DefaultFunc: schema.EnvDefaultFunc("MONGODB_ATLAS_BASE_URL", ""),
40-
Description: "MongoDB Atlas Base URL",
54+
DefaultFunc: schema.EnvDefaultFunc("MONGODB_REALM_BASE_URL", ""),
55+
Description: "MongoDB Realm Base URL",
4156
},
4257
},
4358

@@ -129,12 +144,10 @@ func Provider() *schema.Provider {
129144

130145
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
131146
config := Config{
132-
PublicKey: d.Get("public_key").(string),
133-
PrivateKey: d.Get("private_key").(string),
134-
}
135-
136-
if baseURL, ok := d.GetOk("base_url"); ok {
137-
config.BaseURL = baseURL.(string)
147+
PublicKey: d.Get("public_key").(string),
148+
PrivateKey: d.Get("private_key").(string),
149+
BaseURL: d.Get("base_url").(string),
150+
RealmBaseURL: d.Get("realm_base_url").(string),
138151
}
139152

140153
return config.NewClient(ctx)
@@ -302,14 +315,3 @@ func HashCodeString(s string) int {
302315
// v == MinInt
303316
return 0
304317
}
305-
306-
// HashCodeStrings hashes a list of strings to a unique hashcode.
307-
func HashCodeStrings(hashStrings []string) string {
308-
var buf bytes.Buffer
309-
310-
for _, s := range hashStrings {
311-
buf.WriteString(fmt.Sprintf("%s-", s))
312-
}
313-
314-
return fmt.Sprintf("%d", HashCodeString(buf.String()))
315-
}

website/docs/index.html.markdown

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ description: |-
88

99
# MongoDB Atlas Provider
1010

11-
The MongoDB Atlas provider is used to interact with the resources supported by [MongoDB Atlas](https://www.mongodb.com/cloud/atlas). The provider needs to be configured with the proper credentials before it can be used.
11+
You can use the MongoDB Atlas provider to interact with the resources supported by [MongoDB Atlas](https://www.mongodb.com/cloud/atlas).
12+
The provider needs to be configured with the proper credentials before it can be used.
1213

1314
Use the navigation to the left to read about the available provider resources and data sources.
1415

15-
You may want to consider pinning the [provider version](https://www.terraform.io/docs/configuration/providers.html#provider-versions) to ensure you have a chance to review and prepare for changes. Speaking of changes, see [CHANGELOG](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/CHANGELOG.md) for current version information.
16+
You may want to consider pinning the [provider version](https://www.terraform.io/docs/configuration/providers.html#provider-versions) to ensure you have a chance to review and prepare for changes.
17+
Speaking of changes, see [CHANGELOG](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/CHANGELOG.md) for current version information.
1618

1719
## Example Usage
1820

@@ -22,37 +24,25 @@ provider "mongodbatlas" {
2224
public_key = var.mongodbatlas_public_key
2325
private_key = var.mongodbatlas_private_key
2426
}
25-
26-
#Create the resources
27-
...
27+
# Create the resources
2828
```
2929

3030
## Configure Atlas Programmatic Access
3131

32-
In order to setup authentication with the MongoDB Atlas provider a programmatic API key must be generated for MongoDB Atlas with the appropriate permissions and IP access list entries. The [MongoDB Atlas documentation](https://docs.atlas.mongodb.com/tutorial/manage-programmatic-access/index.html) contains the most up-to-date instructions for creating and managing your key(s) and IP access. Be aware, not all API resources require an IP access list by default, but one can set Atlas to require IP access entries for all API resources, see the [organization settings documentation](https://docs.atlas.mongodb.com/tutorial/manage-organization-settings/#require-ip-access-list-for-public-api) for more info.
32+
In order to set up authentication with the MongoDB Atlas provider a programmatic API key must be generated for MongoDB Atlas with the appropriate permissions and IP access list entries.
33+
The [MongoDB Atlas documentation](https://docs.atlas.mongodb.com/tutorial/manage-programmatic-access/index.html) contains the most up-to-date instructions for creating and managing your key(s) and IP access.
34+
Be aware, not all API resources require an IP access list by default, but one can set Atlas to require IP access entries for all API resources, see the [organization settings documentation](https://docs.atlas.mongodb.com/tutorial/manage-organization-settings/#require-ip-access-list-for-public-api) for more info.
3335

3436
## Authenticate the Provider
3537

36-
The MongoDB Atlas provider offers a flexible means of providing credentials for authentication. The following methods are supported and explained below:
37-
38-
### Static credentials
39-
40-
Static credentials can be provided by adding the following attributes in-line in the MongoDB Atlas provider block, either directly or via input variable/local value:
41-
42-
Usage:
43-
44-
```hcl
45-
provider "mongodbatlas" {
46-
public_key = "atlas_public_api_key" #required
47-
private_key = "atlas_private_api_key" #required
48-
}
49-
```
50-
51-
~> *IMPORTANT* Hard-coding your MongoDB Atlas programmatic API key pair into a Terraform configuration is not recommended. Consider the risks, especially the inadvertent submission of a configuration file containing secrets to a public repository.
38+
The MongoDB Atlas provider offers a flexible means of providing credentials for authentication.
39+
You can use any the following methods:
5240

53-
### Environment variables
41+
### Environment Variables
5442

55-
You can also provide your credentials via the environment variables, MONGODB_ATLAS_PUBLIC_KEY and MONGODB_ATLAS_PRIVATE_KEY, for your public and private MongoDB Atlas programmatic API key pair respectively:
43+
You can also provide your credentials via the environment variables,
44+
`MONGODB_ATLAS_PUBLIC_KEY` and `MONGODB_ATLAS_PRIVATE_KEY`,
45+
for your public and private MongoDB Atlas programmatic API key pair respectively:
5646

5747
```hcl
5848
provider "mongodbatlas" {}
@@ -66,19 +56,36 @@ $ export MONGODB_ATLAS_PRIVATE_KEY="xxxx"
6656
$ terraform plan
6757
```
6858

59+
As an alternative to `MONGODB_ATLAS_PUBLIC_KEY` and `MONGODB_ATLAS_PRIVATE_KEY`
60+
if you are using [MongoDB CLI](https://docs.mongodb.com/mongocli/stable/)
61+
then `MCLI_PUBLIC_API_KEY` and `MCLI_PRIVATE_API_KEY` are also supported.
62+
63+
### Static Credentials
64+
65+
Static credentials can be provided by adding the following attributes in-line in the MongoDB Atlas provider block,
66+
either directly or via input variable/local value:
67+
68+
```hcl
69+
provider "mongodbatlas" {
70+
public_key = "atlas_public_api_key" #required
71+
private_key = "atlas_private_api_key" #required
72+
}
73+
```
74+
75+
~> *IMPORTANT* Hard-coding your MongoDB Atlas programmatic API key pair into a Terraform configuration is not recommended.
76+
Consider the risks, especially the inadvertent submission of a configuration file containing secrets to a public repository.
77+
6978
## Argument Reference
7079

71-
In addition to [generic `provider`
72-
arguments](https://www.terraform.io/docs/configuration/providers.html) (e.g.
73-
`alias` and `version`), the following arguments are supported in the MongoDB
74-
Atlas `provider` block:
80+
In addition to [generic `provider` arguments](https://www.terraform.io/docs/configuration/providers.html)
81+
(e.g. `alias` and `version`), the MongoDB Atlas `provider` supports the following arguments:
7582

7683
* `public_key` - (Optional) This is the public key of your MongoDB Atlas API key pair. It must be
77-
provided, but it can also be sourced from the `MONGODB_ATLAS_PUBLIC_KEY`
84+
provided, but it can also be sourced from the `MONGODB_ATLAS_PUBLIC_KEY` or `MCLI_PUBLIC_API_KEY`
7885
environment variable.
7986

8087
* `private_key` - (Optional) This is the private key of your MongoDB Atlas key pair. It must be
81-
provided, but it can also be sourced from the `MONGODB_ATLAS_PRIVATE_KEY`
88+
provided, but it can also be sourced from the `MONGODB_ATLAS_PRIVATE_KEY` or `MCLI_PRIVATE_API_KEY`
8289
environment variable.
8390

8491
For more information on configuring and managing programmatic API Keys see the [MongoDB Atlas Documentation](https://docs.atlas.mongodb.com/tutorial/manage-programmatic-access/index.html).
@@ -97,7 +104,10 @@ For more information on configuring and managing programmatic API Keys see the [
97104

98105
## Examples from MongoDB and the Community
99106

100-
We have [example configurations](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples) in our GitHub repo that will help both beginner and more advanced users.
107+
We have [example configurations](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples)
108+
in our GitHub repo that will help both beginner and more advanced users.
101109

102-
Have a good example you've created and want to share? Let us know the details via an [issue](https://github.com/mongodb/terraform-provider-mongodbatlas/issues) or submit a PR of your work to add it to the examples directory in our [GitHub repo](https://github.com/mongodb/terraform-provider-mongodbatlas/).
110+
Have a good example you've created and want to share?
111+
Let us know the details via an [issue](https://github.com/mongodb/terraform-provider-mongodbatlas/issues)
112+
or submit a PR of your work to add it to the `examples` directory in our [GitHub repo](https://github.com/mongodb/terraform-provider-mongodbatlas/).
103113

0 commit comments

Comments
 (0)