Skip to content

Commit

Permalink
INTMDB-454: New Data Source to GET Org Id (#973)
Browse files Browse the repository at this point in the history
* Add support for mongodbatlas_roles_org_id

* Add Documentation for roles_org_id

* Update go library

* Add mongodbatlas_roles_org_id to datasource and resource to provide org_id

* doc clean up / link to new API docs

* formatting fix + update links to new API docs

* formatting + update link to new API docs

* formatting

Co-authored-by: Zuhair Ahmed <[email protected]>
  • Loading branch information
martinstibbe and Zuhairahmed authored Dec 28, 2022
1 parent c78a274 commit e14172c
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/mwielbut/pointy v1.1.0
github.com/spf13/cast v1.5.0
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20210625132053-af2d5c0ad54f
go.mongodb.org/atlas v0.19.0
go.mongodb.org/atlas v0.20.1-0.20221216175724-08be1f503b9a
go.mongodb.org/realm v0.1.0
)

Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,12 @@ go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.etcd.io/etcd v0.0.0-20200513171258-e048e166ab9c/go.mod h1:xCI7ZzBfRuGgBXyXO6yfWfDmlWd35khcWpUa4L0xI/k=
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/atlas v0.18.1-0.20221109142841-f9f8ebe7b9b9 h1:9m35o4kyRYjwbsIb/lPrjxJ6afPpn9zwOF5i3SIY5Lg=
go.mongodb.org/atlas v0.18.1-0.20221109142841-f9f8ebe7b9b9/go.mod h1:PFk1IGhiGjFXHGVspOK7i1U2nnPjK8wAjYwQf6FoVf4=
go.mongodb.org/atlas v0.19.0 h1:gvezG9d0KsSDaExEdTtcGqZHRvvVazzuEcBUpBXxmlg=
go.mongodb.org/atlas v0.19.0/go.mod h1:PFk1IGhiGjFXHGVspOK7i1U2nnPjK8wAjYwQf6FoVf4=
go.mongodb.org/atlas v0.19.1-0.20221216041907-cf0875bb4b4b h1:frOL6IVpql4z1ApvVPZ2DaqkPp//Ez6LLOjB4r868BE=
go.mongodb.org/atlas v0.19.1-0.20221216041907-cf0875bb4b4b/go.mod h1:XTjsxWgoOSwaZrQUvhTEuwjymxnF0r12RPibZuW1Uts=
go.mongodb.org/atlas v0.19.1-0.20221216044137-9eeca171e6ce h1:7/vO1GEEQkCFgfBm5fNXh7P0smTuIk6ebC7nlnFKVHI=
go.mongodb.org/atlas v0.19.1-0.20221216044137-9eeca171e6ce/go.mod h1:XTjsxWgoOSwaZrQUvhTEuwjymxnF0r12RPibZuW1Uts=
go.mongodb.org/atlas v0.20.1-0.20221216175724-08be1f503b9a h1:bM1g5SMFRebki6zPratsKPXmtPPUxYEZkPLFp3BD0cI=
go.mongodb.org/atlas v0.20.1-0.20221216175724-08be1f503b9a/go.mod h1:XTjsxWgoOSwaZrQUvhTEuwjymxnF0r12RPibZuW1Uts=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.mozilla.org/mozlog v0.0.0-20170222151521-4bb13139d403/go.mod h1:jHoPAGnDrCy6kaI2tAze5Prf0Nr0w/oNkROt2lw3n3o=
Expand Down
51 changes: 51 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_org_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mongodbatlas

import (
"context"
"strings"

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

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

func dataSourceMongoDBAtlasOrgID() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasOrgIDRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

var (
err error
root *matlas.Root
)

options := &matlas.ListOptions{}
apiKeyOrgList, _, err := conn.Root.List(ctx, options)
if err != nil {
return diag.Errorf("error getting API Key's org assigned (%s): ", err)
}

if err := d.Set("org_id", apiKeyOrgList.APIKey.Roles[0].OrgID); err != nil {
return diag.Errorf(errorProjectSetting, `org_id`, root.APIKey.ID, err)
}

for _, role := range apiKeyOrgList.APIKey.Roles {
if strings.HasPrefix(role.RoleName, "ORG_") {
d.SetId(apiKeyOrgList.APIKey.Roles[0].OrgID)
}
}

return nil
}
43 changes: 43 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_org_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 TestAccConfigDSOrgID_basic(t *testing.T) {
var (
dataSourceName = "data.mongodbatlas_roles_org_id.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
name = fmt.Sprintf("test-acc-%[email protected]", acctest.RandString(10))
initialRole = []string{"ORG_OWNER"}
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasOrgInvitationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMongoDBAtlasOrgIDConfig(orgID, name, initialRole),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
),
},
},
})
}

func testAccDataSourceMongoDBAtlasOrgIDConfig(orgID, username string, roles []string) string {
return (`
data "mongodbatlas_roles_org_id" "test" {
}
output "org_id" {
value = data.mongodbatlas_roles_org_id.test.org_id
}`)
}
1 change: 1 addition & 0 deletions mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func getDataSourcesMap() map[string]*schema.Resource {
"mongodbatlas_custom_db_roles": dataSourceMongoDBAtlasCustomDBRoles(),
"mongodbatlas_database_user": dataSourceMongoDBAtlasDatabaseUser(),
"mongodbatlas_database_users": dataSourceMongoDBAtlasDatabaseUsers(),
"mongodbatlas_roles_org_id": dataSourceMongoDBAtlasOrgID(),
"mongodbatlas_project": dataSourceMongoDBAtlasProject(),
"mongodbatlas_projects": dataSourceMongoDBAtlasProjects(),
"mongodbatlas_cluster": dataSourceMongoDBAtlasCluster(),
Expand Down
11 changes: 7 additions & 4 deletions website/docs/d/project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ description: |-

### Using project_id attribute to query
```terraform
data "mongodbatlas_roles_org_id" "test" {
}
resource "mongodbatlas_project" "test" {
name = "project-name"
org_id = "<ORG_ID>"
org_id = data.mongodbatlas_roles_org_id.test.org_id
teams {
team_id = "5e0fa8c99ccf641c722fe645"
Expand Down Expand Up @@ -75,8 +78,8 @@ In addition to all arguments above, the following attributes are exported:

* `name` - The name of the project you want to create. (Cannot be changed via this Provider after creation.)
* `org_id` - The ID of the organization you want to create the project within.
*`cluster_count` - The number of Atlas clusters deployed in the project.
*`created` - The ISO-8601-formatted timestamp of when Atlas created the project.
* `cluster_count` - The number of Atlas clusters deployed in the project.
* `created` - The ISO-8601-formatted timestamp of when Atlas created the project.
* `teams.#.team_id` - The unique identifier of the team you want to associate with the project. The team and project must share the same parent organization.
* `teams.#.role_names` - Each string in the array represents a project role assigned to the team. Every user associated with the team inherits these roles.
The following are valid roles:
Expand Down Expand Up @@ -105,4 +108,4 @@ The following are valid roles:
* `region_usage_restrictions` - If GOV_REGIONS_ONLY the project can be used for government regions only, otherwise defaults to standard regions. For more information see [MongoDB Atlas for Government](https://www.mongodb.com/docs/atlas/government/api/#creating-a-project).


See [MongoDB Atlas API - Project](https://docs.atlas.mongodb.com/reference/api/project-get-one/) - [and MongoDB Atlas API - Teams](https://docs.atlas.mongodb.com/reference/api/project-get-teams/) Documentation for more information.
See [MongoDB Atlas API - Project](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Projects) - [and MongoDB Atlas API - Teams](https://docs.atlas.mongodb.com/reference/api/project-get-teams/) Documentation for more information.
11 changes: 7 additions & 4 deletions website/docs/d/projects.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ description: |-
## Example Usage

```terraform
data "mongodbatlas_roles_org_id" "test" {
}
resource "mongodbatlas_project" "test" {
name = "project-name"
org_id = "<ORG_ID>"
org_id = data.mongodbatlas_roles_org_id.test.org_id
teams {
team_id = "5e0fa8c99ccf641c722fe645"
Expand Down Expand Up @@ -55,8 +58,8 @@ data "mongodbatlas_projects" "test" {

* `name` - The name of the project you want to create. (Cannot be changed via this Provider after creation.)
* `org_id` - The ID of the organization you want to create the project within.
*`cluster_count` - The number of Atlas clusters deployed in the project.
*`created` - The ISO-8601-formatted timestamp of when Atlas created the project.
* `cluster_count` - The number of Atlas clusters deployed in the project.
* `created` - The ISO-8601-formatted timestamp of when Atlas created the project.
* `teams.#.team_id` - The unique identifier of the team you want to associate with the project. The team and project must share the same parent organization.
* `teams.#.role_names` - Each string in the array represents a project role assigned to the team. Every user associated with the team inherits these roles.
The following are valid roles:
Expand All @@ -83,4 +86,4 @@ The following are valid roles:
* `is_schema_advisor_enabled` - Flag that indicates whether to enable Schema Advisor for the project. If enabled, you receive customized recommendations to optimize your data model and enhance performance. Disable this setting to disable schema suggestions in the [Performance Advisor](https://www.mongodb.com/docs/atlas/performance-advisor/#std-label-performance-advisor) and the [Data Explorer](https://www.mongodb.com/docs/atlas/atlas-ui/#std-label-atlas-ui).
* `region_usage_restrictions` - If GOV_REGIONS_ONLY the project can be used for government regions only, otherwise defaults to standard regions. For more information see [MongoDB Atlas for Government](https://www.mongodb.com/docs/atlas/government/api/#creating-a-project).

See [MongoDB Atlas API - Projects](https://docs.atlas.mongodb.com/reference/api/project-get-all/) - [and MongoDB Atlas API - Teams](https://docs.atlas.mongodb.com/reference/api/project-get-teams/) Documentation for more information.
See [MongoDB Atlas API - Projects](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Projects) - [and MongoDB Atlas API - Teams](https://docs.atlas.mongodb.com/reference/api/project-get-teams/) Documentation for more information.
35 changes: 35 additions & 0 deletions website/docs/d/roles_org_id.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
layout: "mongodbatlas"
page_title: "MongoDB Atlas: roles_org_id"
sidebar_current: "docs-mongodbatlas-datasource-roles-org-id"
description: |-
Describes a Roles Org ID.
---

# Data Source: mongodbatlas_project

`mongodbatlas_project` describes a MongoDB Atlas Roles Org ID. This represents a Roles Org ID.

## Example Usage

### Using project_id attribute to query
```terraform
data "mongodbatlas_roles_org_id" "test" {
}
output "org_id" {
value = data.mongodbatlas_roles_org_id.test.org_id
}
```

## Argument Reference

* No parameters required

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `org_id` - The ID of the organization you want to retrieve associated to an API Key.

See [MongoDB Atlas API - Role Org ID](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Root/operation/getSystemStatus) - Documentation for more information.
7 changes: 5 additions & 2 deletions website/docs/r/project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ description: |-
## Example Usage

```terraform
data "mongodbatlas_roles_org_id" "test" {
}
resource "mongodbatlas_project" "test" {
name = "project-name"
org_id = "<ORG_ID>"
org_id = data.mongodbatlas_roles_org_id.test.org_id
project_owner_id = "<OWNER_ACCOUNT_ID>"
teams {
Expand Down Expand Up @@ -104,4 +107,4 @@ Project must be imported using project ID, e.g.
```
$ terraform import mongodbatlas_project.my_project 5d09d6a59ccf6445652a444a
```
For more information see: [MongoDB Atlas API Reference.](https://docs.atlas.mongodb.com/reference/api/projects/) - [and MongoDB Atlas API - Teams](https://docs.atlas.mongodb.com/reference/api/teams/) Documentation for more information.
For more information see: [MongoDB Atlas Admin API Projects](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Projects) and [MongoDB Atlas Admin API Teams](https://docs.atlas.mongodb.com/reference/api/teams/) Documentation for more information.

0 comments on commit e14172c

Please sign in to comment.