Skip to content

Commit 874bb7a

Browse files
oarbusiEspenAlbertlantoli
authored
chore: Supports Atlas Resource Policies (#2657)
* use SDK dev-preview * chore: Creates schema for the new `mongodbatlas_resource_policy` (#2548) * chore: Creates TF models & interfaces for new * fix: attribute computed-optional-required types * only include schema changes in PR * chore: copy solution for making build test from `CLOUDP-246459-ear-kms-dev` branch * fix: add missing OrgID to model * chore: add non_compliant and remove comment from generator_config.yml * fix: add plan modifier to `version` * chore: add markdown descriptions * chore: Creates schema for the new mongodbatlas_resource_policy data sources (#2563) * chore: data-source schemas for resource_policy * chore: copy solution for making build test from `CLOUDP-246459-ear-kms-dev` branch * chore: add markdown descriptions * chore: Adds `resource_policy` model conversions (#2575) * feat: sdk -> tf conversion test * chore: add add check for policy too * refactor: use stringPointerValue and avoid types.ObjectValueMust * refactor: use auto-generated version * chore: rename apiResp to input * test: add more test cases and support NewTFPoliciesModelToSDK * chore: nil check * fix: nil object pointers * refactor: use `diags *diag.Diagnostics` as argument instead of return value * refactor: continued simplifications to avoid *types.Object|List * refactor: return variable directly * refactor: another round * test: refactor test to create the TF model * test: add utility function for creating TF objects/lists for framework types * chore: remove leftover comment * refactor: use a []TFPolicy directly in the schema * chore: remove accidental AttributeTypes function * feat: Implements `resource_policy` (#2585) * feat: initial resource implementation * test: minor fixes * test: add a new client for RP organization * chore: add variables for dev to CI * test: use new variables in resource policy test * test: add the default main_test.go file * chore: fix accidental indentation * chore: add changelog file * test: fix use explicit provider config in the resource test * test: add test case for NewTFPoliciesModelToSDK * ci: remove rp_base_url and support qa environment * chore: remove base url from RP * fix: avoid using MONGODB_ATLAS_BASE_URL * test: remember to set the BaseURL * test: remove additional provider, instead map the inut/secret variables in GH action * fix: address PR comments * feat: Implements and tests resource_policy data-source (#2598) * feat: resource_policy data-source implementation and test * chore: remove unused data_source test * feat: resource_policies data-source implementation and test * doc: include changelog file * chore: minor fixes * perform review suggestions * chore: Merges master and use `name` as required (#2620) * chore: Supports detecting policy errors during terraform plan (#2621) * chore: support validating during plan * refactor: move AddJSONErrDiagnostics to separate function * chore: avoid POLICY_CANNOT_CONTAIN_A_DUPLICATE_NAME by using a random name when validating * address PR comments * test: Adds migration tests and more acceptance tests (#2622) * test: Adds migration test * refactor: includes name in error message * test: Add case for multiple nested policies * test: cleanup assertions and run sequentially to avoid assertion errors * chore: address PR comment * fix: link to correct upstream issue * doc: Adds example and registry docs for resource_policy (#2623) * doc: Adds initial example * doc: Adds registry documentation for resource_policy * doc: Apply doc review suggestions * chore: Uses name of resource policy instead of random value when validating (#2644) * fix resource policy sdk api change * Update .changelog/2585.txt Co-authored-by: Leo Antoli <[email protected]> * revert change --------- Co-authored-by: EspenAlbert <[email protected]> Co-authored-by: Espen Albert <[email protected]> Co-authored-by: Leo Antoli <[email protected]>
1 parent 134aece commit 874bb7a

33 files changed

+1931
-27
lines changed

.changelog/2585.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
resource/mongodbatlas_resource_policy
3+
```

.changelog/2598.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-datasource
2+
data-source/mongodbatlas_resource_policy
3+
```
4+
5+
```release-note:new-datasource
6+
data-source/mongodbatlas_resource_policies
7+
```

.github/workflows/acceptance-tests-runner.yml

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ on:
8080
mongodb_atlas_gov_base_url:
8181
type: string
8282
required: true
83+
mongodb_atlas_rp_org_id:
84+
type: string
85+
required: true
8386
mongodb_atlas_gov_project_owner_id:
8487
type: string
8588
required: true
@@ -134,6 +137,10 @@ on:
134137
required: true
135138
mongodb_atlas_gov_public_key:
136139
required: true
140+
mongodb_atlas_rp_private_key:
141+
required: true
142+
mongodb_atlas_rp_public_key:
143+
required: true
137144
azure_directory_id:
138145
required: true
139146
azure_resource_group_name:
@@ -215,6 +222,7 @@ jobs:
215222
network: ${{ steps.filter.outputs.network == 'true' || env.mustTrigger == 'true' }}
216223
project: ${{ steps.filter.outputs.project == 'true' || env.mustTrigger == 'true' }}
217224
push_based_log_export: ${{ steps.filter.outputs.push_based_log_export == 'true' || env.mustTrigger == 'true' }}
225+
resource_policy: ${{ steps.filter.outputs.resource_policy == 'true' || env.mustTrigger == 'true' }}
218226
search_deployment: ${{ steps.filter.outputs.search_deployment == 'true' || env.mustTrigger == 'true' }}
219227
search_index: ${{ steps.filter.outputs.search_index == 'true' || env.mustTrigger == 'true' }}
220228
serverless: ${{ steps.filter.outputs.serverless == 'true' || env.mustTrigger == 'true' }}
@@ -295,6 +303,8 @@ jobs:
295303
- 'internal/service/projectipaddresses/*.go'
296304
push_based_log_export:
297305
- 'internal/service/pushbasedlogexport/*.go'
306+
resource_policy:
307+
- 'internal/service/resourcepolicy/*.go'
298308
search_deployment:
299309
- 'internal/service/searchdeployment/*.go'
300310
search_index:
@@ -823,9 +833,9 @@ jobs:
823833
ACCTEST_PACKAGES: ./internal/service/pushbasedlogexport
824834
run: make testacc
825835

826-
search_deployment:
836+
resource_policy:
827837
needs: [ change-detection, get-provider-version ]
828-
if: ${{ needs.change-detection.outputs.search_deployment == 'true' || inputs.test_group == 'search_deployment' }}
838+
if: ${{ needs.change-detection.outputs.resource_policy == 'true' || inputs.test_group == 'resource_policy' }}
829839
runs-on: ubuntu-latest
830840
permissions: {}
831841
steps:
@@ -841,13 +851,18 @@ jobs:
841851
terraform_wrapper: false
842852
- name: Acceptance Tests
843853
env:
854+
MONGODB_ATLAS_ORG_ID: ${{ inputs.mongodb_atlas_rp_org_id }}
855+
MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.mongodb_atlas_rp_public_key }}
856+
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.mongodb_atlas_rp_private_key }}
844857
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
845-
ACCTEST_PACKAGES: ./internal/service/searchdeployment
858+
MONGODB_ATLAS_ENABLE_PREVIEW: "true"
859+
ACCTEST_PACKAGES: |
860+
./internal/service/resourcepolicy
846861
run: make testacc
847-
848-
search_index:
862+
863+
search_deployment:
849864
needs: [ change-detection, get-provider-version ]
850-
if: ${{ needs.change-detection.outputs.search_index == 'true' || inputs.test_group == 'search_index' }}
865+
if: ${{ needs.change-detection.outputs.search_deployment == 'true' || inputs.test_group == 'search_deployment' }}
851866
runs-on: ubuntu-latest
852867
permissions: {}
853868
steps:
@@ -860,16 +875,16 @@ jobs:
860875
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd
861876
with:
862877
terraform_version: ${{ inputs.terraform_version }}
863-
terraform_wrapper: false
878+
terraform_wrapper: false
864879
- name: Acceptance Tests
865880
env:
866881
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
867-
ACCTEST_PACKAGES: ./internal/service/searchindex
882+
ACCTEST_PACKAGES: ./internal/service/searchdeployment
868883
run: make testacc
869-
870-
serverless:
884+
885+
search_index:
871886
needs: [ change-detection, get-provider-version ]
872-
if: ${{ needs.change-detection.outputs.serverless == 'true' || inputs.test_group == 'serverless' }}
887+
if: ${{ needs.change-detection.outputs.search_index == 'true' || inputs.test_group == 'search_index' }}
873888
runs-on: ubuntu-latest
874889
permissions: {}
875890
steps:
@@ -885,18 +900,13 @@ jobs:
885900
terraform_wrapper: false
886901
- name: Acceptance Tests
887902
env:
888-
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }}
889-
AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key }}
890903
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
891-
ACCTEST_PACKAGES: |
892-
./internal/service/privatelinkendpointserverless
893-
./internal/service/privatelinkendpointserviceserverless
894-
./internal/service/serverlessinstance
904+
ACCTEST_PACKAGES: ./internal/service/searchindex
895905
run: make testacc
896906

897-
stream:
907+
serverless:
898908
needs: [ change-detection, get-provider-version ]
899-
if: ${{ needs.change-detection.outputs.stream == 'true' || inputs.test_group == 'stream' }}
909+
if: ${{ needs.change-detection.outputs.serverless == 'true' || inputs.test_group == 'serverless' }}
900910
runs-on: ubuntu-latest
901911
permissions: {}
902912
steps:
@@ -909,12 +919,38 @@ jobs:
909919
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd
910920
with:
911921
terraform_version: ${{ inputs.terraform_version }}
912-
terraform_wrapper: false
922+
terraform_wrapper: false
913923
- name: Acceptance Tests
914924
env:
925+
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }}
926+
AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key }}
915927
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
916928
ACCTEST_PACKAGES: |
917-
./internal/service/streamconnection
918-
./internal/service/streaminstance
919-
./internal/service/streamprocessor
929+
./internal/service/privatelinkendpointserverless
930+
./internal/service/privatelinkendpointserviceserverless
931+
./internal/service/serverlessinstance
920932
run: make testacc
933+
stream:
934+
needs: [ change-detection, get-provider-version ]
935+
if: ${{ needs.change-detection.outputs.stream == 'true' || inputs.test_group == 'stream' }}
936+
runs-on: ubuntu-latest
937+
permissions: {}
938+
steps:
939+
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
940+
with:
941+
ref: ${{ inputs.ref || github.ref }}
942+
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32
943+
with:
944+
go-version-file: 'go.mod'
945+
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd
946+
with:
947+
terraform_version: ${{ inputs.terraform_version }}
948+
terraform_wrapper: false
949+
- name: Acceptance Tests
950+
env:
951+
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
952+
ACCTEST_PACKAGES: |
953+
./internal/service/streamconnection
954+
./internal/service/streaminstance
955+
./internal/service/streamprocessor
956+
run: make testacc

.github/workflows/acceptance-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ jobs:
5757
mongodb_atlas_private_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_QA || secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV }}
5858
mongodb_atlas_gov_public_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_GOV_PUBLIC_KEY_QA || secrets.MONGODB_ATLAS_GOV_PUBLIC_KEY_DEV }}
5959
mongodb_atlas_gov_private_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_GOV_PRIVATE_KEY_QA || secrets.MONGODB_ATLAS_GOV_PRIVATE_KEY_DEV }}
60+
mongodb_atlas_rp_public_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_RP_PUBLIC_KEY_QA || secrets.MONGODB_ATLAS_RP_PUBLIC_KEY_DEV }}
61+
mongodb_atlas_rp_private_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_RP_PRIVATE_KEY_QA || secrets.MONGODB_ATLAS_RP_PRIVATE_KEY_DEV }}
6062
ca_cert: ${{ secrets.CA_CERT }}
6163
aws_account_id: ${{ secrets.AWS_ACCOUNT_ID }}
6264
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
@@ -113,3 +115,4 @@ jobs:
113115
mongodb_atlas_project_ear_pe_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_PROJECT_EAR_PE_ID_QA || vars.MONGODB_ATLAS_PROJECT_EAR_PE_ID_DEV }}
114116
mongodb_atlas_enable_preview: ${{ vars.MONGODB_ATLAS_ENABLE_PREVIEW }}
115117
azure_private_endpoint_region: ${{ vars.AZURE_PRIVATE_ENDPOINT_REGION }}
118+
mongodb_atlas_rp_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_RP_ORG_ID_QA || vars.MONGODB_ATLAS_RP_ORG_ID_DEV }}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Data Source: mongodbatlas_resource_policies
2+
3+
`mongodbatlas_resource_policies` returns all resource policies in an organization.
4+
5+
-> **NOTE**: Resource Policies are currently in Public Preview. To use this feature, you must take the following actions:
6+
1. Enable the `Atlas Resource Policies` Beta Feature in your organization (contact MongoDB Support).
7+
2. Enable the [Preview Features](https://github.com/mongodb/terraform-provider-mongodbatlas?tab=readme-ov-file#preview-features) when running `terraform` commands.
8+
9+
## Example Usages
10+
```terraform
11+
resource "mongodbatlas_resource_policy" "project_ip_access_list" {
12+
org_id = var.org_id
13+
name = "forbid-access-from-anywhere"
14+
15+
policies = [
16+
{
17+
body = <<EOF
18+
forbid (
19+
principal,
20+
action == cloud::Action::"project.edit",
21+
resource
22+
)
23+
when {
24+
context.project.ipAccessList.contains(ip("0.0.0.0/0"))
25+
};
26+
EOF
27+
},
28+
]
29+
}
30+
31+
resource "mongodbatlas_resource_policy" "cloud_provider" {
32+
org_id = var.org_id
33+
name = "forbid-cloud-provider"
34+
policies = [
35+
{
36+
body = templatefile("${path.module}/cloud-provider.cedar", {
37+
CLOUD_PROVIDER = "azure"
38+
})
39+
},
40+
{
41+
body = templatefile("${path.module}/cloud-provider.cedar", {
42+
CLOUD_PROVIDER = "aws"
43+
})
44+
},
45+
]
46+
}
47+
48+
data "cedar_policyset" "cloud_region" {
49+
policy {
50+
any_principal = true
51+
effect = "forbid"
52+
action = {
53+
type = " cloud::Action"
54+
id = "cluster.createEdit"
55+
}
56+
any_resource = true
57+
when {
58+
text = "context.cluster.regions.contains(cloud::region::\"gcp:us-east1\")"
59+
}
60+
}
61+
}
62+
63+
resource "mongodbatlas_resource_policy" "cloud_region" {
64+
org_id = var.org_id
65+
name = "forbid-cloud-region"
66+
policies = [
67+
{
68+
body = data.cedar_policyset.cloud_region.text
69+
},
70+
]
71+
}
72+
73+
74+
data "mongodbatlas_resource_policy" "project_ip_access_list" {
75+
org_id = mongodbatlas_resource_policy.project_ip_access_list.org_id
76+
id = mongodbatlas_resource_policy.project_ip_access_list.id
77+
}
78+
79+
data "mongodbatlas_resource_policies" "this" {
80+
org_id = data.mongodbatlas_resource_policy.project_ip_access_list.org_id
81+
82+
depends_on = [mongodbatlas_resource_policy.project_ip_access_list, mongodbatlas_resource_policy.cloud_provider, mongodbatlas_resource_policy.cloud_region]
83+
}
84+
85+
86+
output "policy_ids" {
87+
value = { for policy in data.mongodbatlas_resource_policies.this.resource_policies : policy.name => policy.id }
88+
}
89+
```
90+
91+
<!-- schema generated by tfplugindocs -->
92+
## Schema
93+
94+
### Required
95+
96+
- `org_id` (String) Unique 24-hexadecimal digit string that identifies the organization that contains your projects. Use the [/orgs](#tag/Organizations/operation/listOrganizations) endpoint to retrieve all organizations to which the authenticated user has access.
97+
98+
### Read-Only
99+
100+
- `resource_policies` (Attributes List) (see [below for nested schema](#nestedatt--resource_policies))
101+
102+
<a id="nestedatt--resource_policies"></a>
103+
### Nested Schema for `resource_policies`
104+
105+
Read-Only:
106+
107+
- `created_by_user` (Attributes) The user that last updated the Atlas resource policy. (see [below for nested schema](#nestedatt--resource_policies--created_by_user))
108+
- `created_date` (String) Date and time in UTC when the Atlas resource policy was created.
109+
- `id` (String) Unique 24-hexadecimal digit string that identifies an Atlas resource policy.
110+
- `last_updated_by_user` (Attributes) The user that last updated the Atlas resource policy. (see [below for nested schema](#nestedatt--resource_policies--last_updated_by_user))
111+
- `last_updated_date` (String) Date and time in UTC when the Atlas resource policy was last updated.
112+
- `name` (String) Human-readable label that describes the Atlas resource policy.
113+
- `org_id` (String) Unique 24-hexadecimal digit string that identifies the organization that contains your projects. Use the [/orgs](#tag/Organizations/operation/listOrganizations) endpoint to retrieve all organizations to which the authenticated user has access.
114+
- `policies` (Attributes List) List of policies that make up the Atlas resource policy. (see [below for nested schema](#nestedatt--resource_policies--policies))
115+
- `version` (String) A string that identifies the version of the Atlas resource policy.
116+
117+
<a id="nestedatt--resource_policies--created_by_user"></a>
118+
### Nested Schema for `resource_policies.created_by_user`
119+
120+
Read-Only:
121+
122+
- `id` (String) Unique 24-hexadecimal character string that identifies a user.
123+
- `name` (String) Human-readable label that describes a user.
124+
125+
126+
<a id="nestedatt--resource_policies--last_updated_by_user"></a>
127+
### Nested Schema for `resource_policies.last_updated_by_user`
128+
129+
Read-Only:
130+
131+
- `id` (String) Unique 24-hexadecimal character string that identifies a user.
132+
- `name` (String) Human-readable label that describes a user.
133+
134+
135+
<a id="nestedatt--resource_policies--policies"></a>
136+
### Nested Schema for `resource_policies.policies`
137+
138+
Read-Only:
139+
140+
- `body` (String) A string that defines the permissions for the policy. The syntax used is the Cedar Policy language.
141+
- `id` (String) Unique 24-hexadecimal character string that identifies the policy.
142+

0 commit comments

Comments
 (0)