Skip to content

Commit ffc9755

Browse files
oarbusimarcosuma
andauthored
doc: Example for migration of mongodbatlas_org_invitation resource and data source to the mongodbatlas_cloud_user_org_assignment resource (#3589)
* examples * Update examples/migrate_org_invitation_to_cloud_user_org_assignment/v2/README.md * Update examples/migrate_org_invitation_to_cloud_user_org_assignment/v2/README.md Co-authored-by: Marco Suma <[email protected]> * pr comments * fix from resource * reference example in migration guide --------- Co-authored-by: Marco Suma <[email protected]>
1 parent 5517239 commit ffc9755

File tree

17 files changed

+261
-0
lines changed

17 files changed

+261
-0
lines changed

docs/guides/org-invitation-to-cloud-user-org-assignment-migration-guide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ Then:
224224

225225
---
226226

227+
## Examples
228+
229+
For complete, working configurations that mirror the use-cases above, see the examples in the provider repository: [migrate_org_invitation_to_cloud_user_org_assignment](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/v2.0.0/examples/migrate_org_invitation_to_cloud_user_org_assignment). These include root-level setups for multiple approaches (e.g., moved blocks and imports) across different versions.
230+
231+
---
232+
227233
## Notes and tips
228234

229235
- Import formats:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Combined Example: Org Invitation → Cloud User Org Assignment
2+
3+
This combined example is organized into step subfolders (v1–v3):
4+
5+
- v1/: Initial state with:
6+
- a pending `mongodbatlas_org_invitation` (with `teams_ids`), and
7+
- an accepted (ACTIVE) user present in the org (no invitation in state).
8+
- v2/: Migration step showcasing both paths:
9+
- moved block for the pending invitation (module-friendly, recommended), and
10+
- import blocks for accepted (ACTIVE) users and team assignments.
11+
- v3/: Cleaned-up final configuration after v2 is applied:
12+
- remove the `mongodbatlas_org_invitation` resource,
13+
- remove moved and import blocks,
14+
- keep only `mongodbatlas_cloud_user_org_assignment` and `mongodbatlas_cloud_user_team_assignment`.
15+
16+
Navigate into each version folder to see the step-specific configuration.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# v1: Initial State
2+
3+
State:
4+
- `mongodbatlas_org_invitation` manages a pending user (with `teams_ids`).
5+
- An accepted (ACTIVE) user exists in the organization (no invitation in state), referenced via `data.mongodbatlas_organization`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
############################################################
2+
# v1: Initial State
3+
# - One pending invitation managed via mongodbatlas_org_invitation (with teams)
4+
# - One active user present in org (no invitation resource in state)
5+
############################################################
6+
7+
# Pending invitation (with teams)
8+
resource "mongodbatlas_org_invitation" "pending" {
9+
org_id = var.org_id
10+
username = var.pending_username
11+
roles = var.roles
12+
teams_ids = var.pending_team_ids
13+
}
14+
15+
# Active user is represented only for reference via data source
16+
data "mongodbatlas_organization" "org" {
17+
org_id = var.org_id
18+
}
19+
20+
locals {
21+
active_users = {
22+
for u in data.mongodbatlas_organization.org.users :
23+
u.username => u if u.org_membership_status == "ACTIVE" && u.username == var.active_username
24+
}
25+
}
26+
27+
output "active_users" {
28+
value = local.active_users
29+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "mongodbatlas" {
2+
public_key = var.public_key
3+
private_key = var.private_key
4+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "public_key" {
2+
type = string
3+
default = ""
4+
}
5+
variable "private_key" {
6+
type = string
7+
default = ""
8+
}
9+
10+
variable "org_id" { type = string }
11+
# Pending invite user
12+
variable "pending_username" { type = string }
13+
variable "roles" {
14+
type = set(string)
15+
default = ["ORG_MEMBER"]
16+
}
17+
# Teams for pending invite
18+
variable "pending_team_ids" {
19+
type = set(string)
20+
default = []
21+
}
22+
23+
# Active user already in org (no invitation resource remains in state)
24+
variable "active_username" { type = string }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.5.0"
3+
required_providers {
4+
mongodbatlas = {
5+
source = "mongodb/mongodbatlas"
6+
version = "~> 1.0"
7+
}
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# v2: Migrate using Moved and Import blocks
2+
3+
State:
4+
- Pending invitation → move state from `mongodbatlas_org_invitation` to `mongodbatlas_cloud_user_org_assignment` using a Terraform `moved` block (no recreate).
5+
- Accepted (ACTIVE) user → declare the resource and use `import` blocks to adopt the existing assignment (`org_id,user_id`).
6+
- Teams → manage memberships via `mongodbatlas_cloud_user_team_assignment`; import existing mappings (`org_id,team_id,user_id`).
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
############################################################
2+
# v2: Migration
3+
# - Pending invitation → cloud_user_org_assignment via moved block
4+
# - Demonstrate import path for ACTIVE users and team assignments
5+
############################################################
6+
7+
# New resource + moved block (recommended)
8+
resource "mongodbatlas_cloud_user_org_assignment" "pending" {
9+
org_id = var.org_id
10+
username = var.pending_username
11+
roles = { org_roles = var.roles }
12+
}
13+
14+
moved {
15+
from = mongodbatlas_org_invitation.pending
16+
to = mongodbatlas_cloud_user_org_assignment.pending
17+
}
18+
19+
# Import ACTIVE users discovered via data source
20+
data "mongodbatlas_organization" "org" {
21+
org_id = var.org_id
22+
}
23+
24+
locals {
25+
active_users = {
26+
for u in data.mongodbatlas_organization.org.users :
27+
u.id => u if u.org_membership_status == "ACTIVE" && u.username == var.active_username
28+
}
29+
}
30+
31+
resource "mongodbatlas_cloud_user_org_assignment" "active" {
32+
for_each = local.active_users
33+
34+
org_id = var.org_id
35+
username = each.value.username
36+
roles = { org_roles = each.value.roles[0].org_roles }
37+
}
38+
39+
import {
40+
for_each = local.active_users
41+
to = mongodbatlas_cloud_user_org_assignment.active[each.key]
42+
id = "${var.org_id}/${each.key}"
43+
}
44+
45+
# Team assignments for the pending user (after moved/import)
46+
resource "mongodbatlas_cloud_user_team_assignment" "teams" {
47+
for_each = var.pending_team_ids
48+
49+
org_id = var.org_id
50+
team_id = each.key
51+
user_id = mongodbatlas_cloud_user_org_assignment.pending.user_id
52+
}
53+
54+
import {
55+
for_each = var.pending_team_ids
56+
to = mongodbatlas_cloud_user_team_assignment.teams[each.key]
57+
id = "${var.org_id}/${each.key}/${var.pending_username}"
58+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "mongodbatlas" {
2+
public_key = var.public_key
3+
private_key = var.private_key
4+
}

0 commit comments

Comments
 (0)