Skip to content

Commit b075c56

Browse files
sivaram-mongodbsivaram-mongodbParthasarathyV
authored
feat: Update Federated Query Limit Resource (#1523)
Co-authored-by: sivaram-mongodb <sivaram@mongodb.com> Co-authored-by: ParthasarathyV <parthasarathy.varadhan@mongodb.com> Co-authored-by: ParthasarathyV <114770988+ParthasarathyV@users.noreply.github.com>
1 parent 2941cea commit b075c56

11 files changed

Lines changed: 101 additions & 21 deletions

File tree

cfn-resources/federated-query-limit/cmd/resource/resource.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"fmt"
2020
"net/http"
2121

22+
"go.mongodb.org/atlas-sdk/v20250312012/admin"
23+
2224
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler"
2325
"github.com/aws/aws-sdk-go-v2/aws"
2426
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
@@ -29,7 +31,6 @@ import (
2931
progress_events "github.com/mongodb/mongodbatlas-cloudformation-resources/util/progressevent"
3032
"github.com/mongodb/mongodbatlas-cloudformation-resources/util/validator"
3133
"github.com/spf13/cast"
32-
admin20231115014 "go.mongodb.org/atlas-sdk/v20231115014/admin"
3334
)
3435

3536
var CreateOrUpdateRequiredFields = []string{constants.ProjectID, constants.TenantName, constants.LimitName, constants.Value}
@@ -132,7 +133,7 @@ func Update(req handler.Request, prevModel *Model, currentModel *Model) (handler
132133
// Check already exists or not
133134
_, response, err := getFederatedQueryLimit(atlas, currentModel)
134135

135-
if err != nil && response.StatusCode == http.StatusNotFound {
136+
if err != nil && util.StatusNotFound(response) {
136137
return handler.ProgressEvent{
137138
OperationStatus: handler.Failed,
138139
Message: DoesntExists,
@@ -157,7 +158,7 @@ func Delete(req handler.Request, prevModel *Model, currentModel *Model) (handler
157158
if peErr != nil {
158159
return *peErr, nil
159160
}
160-
_, response, err := client.Atlas20231115014.DataFederationApi.DeleteOneDataFederationInstanceQueryLimit(
161+
response, err := client.AtlasSDK.DataFederationApi.DeleteDataFederationLimit(
161162
context.Background(),
162163
*currentModel.ProjectId,
163164
*currentModel.TenantName,
@@ -191,7 +192,7 @@ func List(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
191192
if peErr != nil {
192193
return *peErr, nil
193194
}
194-
listQueryLimitsAPIResult, response, err := client.Atlas20231115014.DataFederationApi.ReturnFederatedDatabaseQueryLimits(
195+
listQueryLimitsAPIResult, response, err := client.AtlasSDK.DataFederationApi.ListDataFederationLimits(
195196
context.Background(),
196197
*currentModel.ProjectId,
197198
*currentModel.TenantName,
@@ -220,7 +221,7 @@ func List(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
220221
func handleError(response *http.Response, method string, err error) (handler.ProgressEvent, error) {
221222
errMsg := fmt.Sprintf("%s error:%s", method, err.Error())
222223
_, _ = logger.Warn(errMsg)
223-
if response.StatusCode == http.StatusConflict {
224+
if util.StatusConflict(response) {
224225
return handler.ProgressEvent{
225226
OperationStatus: handler.Failed,
226227
Message: errMsg,
@@ -229,8 +230,8 @@ func handleError(response *http.Response, method string, err error) (handler.Pro
229230
return progress_events.GetFailedEventByResponse(errMsg, response), nil
230231
}
231232

232-
func getFederatedQueryLimit(client *util.MongoDBClient, currentModel *Model) (*admin20231115014.DataFederationTenantQueryLimit, *http.Response, error) {
233-
getQueryLimitAPIRequest := client.Atlas20231115014.DataFederationApi.ReturnFederatedDatabaseQueryLimit(
233+
func getFederatedQueryLimit(client *util.MongoDBClient, currentModel *Model) (*admin.DataFederationTenantQueryLimit, *http.Response, error) {
234+
getQueryLimitAPIRequest := client.AtlasSDK.DataFederationApi.GetDataFederationLimit(
234235
context.Background(),
235236
*currentModel.ProjectId,
236237
*currentModel.TenantName,
@@ -242,7 +243,7 @@ func getFederatedQueryLimit(client *util.MongoDBClient, currentModel *Model) (*a
242243

243244
func createOrUpdateQueryLimit(currentModel *Model, client *util.MongoDBClient, method string) (handler.ProgressEvent, error) {
244245
queryLimitInput := currentModel.setQueryLimit()
245-
queryLimit, response, err := client.Atlas20231115014.DataFederationApi.CreateOneDataFederationQueryLimit(
246+
queryLimit, response, err := client.AtlasSDK.DataFederationApi.SetDataFederationLimit(
246247
context.Background(),
247248
*currentModel.ProjectId,
248249
*currentModel.TenantName,
@@ -262,22 +263,24 @@ func createOrUpdateQueryLimit(currentModel *Model, client *util.MongoDBClient, m
262263
ResourceModel: currentModel}, nil
263264
}
264265

265-
func (model *Model) setQueryLimit() *admin20231115014.DataFederationTenantQueryLimit {
266-
queryLimit := &admin20231115014.DataFederationTenantQueryLimit{
266+
func (model *Model) setQueryLimit() *admin.DataFederationTenantQueryLimit {
267+
queryLimit := &admin.DataFederationTenantQueryLimit{
267268
OverrunPolicy: model.OverrunPolicy,
268269
}
269270
if model.Value != nil {
270271
queryLimit.Value = cast.ToInt64(*model.Value)
271272
}
272273
return queryLimit
273274
}
274-
func (model *Model) getQueryLimit(atlasQueryLimit *admin20231115014.DataFederationTenantQueryLimit) *Model {
275+
func (model *Model) getQueryLimit(atlasQueryLimit *admin.DataFederationTenantQueryLimit) *Model {
275276
if atlasQueryLimit == nil {
276277
return nil
277278
}
278279

279280
model.TenantName = atlasQueryLimit.TenantName
280-
model.OverrunPolicy = atlasQueryLimit.OverrunPolicy
281+
if atlasQueryLimit.OverrunPolicy != nil {
282+
model.OverrunPolicy = atlasQueryLimit.OverrunPolicy
283+
}
281284
model.LimitName = &atlasQueryLimit.Name
282285
if atlasQueryLimit.CurrentUsage != nil {
283286
currentUsage := cast.ToString(atlasQueryLimit.CurrentUsage)

cfn-resources/federated-query-limit/docs/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ _Update requires_: [Replacement](https://docs.aws.amazon.com/AWSCloudFormation/l
7777

7878
#### OverrunPolicy
7979

80-
Only used for Data Federation limits. Action to take when the usage limit is exceeded. If limit span is set to QUERY, this is ignored because MongoDB Cloud stops the query when it exceeds the usage limit. "enum" : [ "BLOCK", "BLOCK_AND_KILL" ]
80+
Action to take when the usage limit is exceeded. If limit span is set to QUERY, this is ignored because MongoDB Cloud stops the query when it exceeds the usage limit.
8181

8282
_Required_: No
8383

@@ -117,10 +117,6 @@ For more information about using the `Fn::GetAtt` intrinsic function, see [Fn::G
117117

118118
Amount that indicates the current usage of the limit.
119119

120-
#### DefaultLimit
121-
122-
Default value of the limit.
123-
124120
#### LastModifiedDate
125121

126122
Only used for Data Federation limits. Timestamp that indicates when this usage limit was last modified. This field uses the ISO 8601 timestamp format in UTC.

cfn-resources/federated-query-limit/mongodb-atlas-federatedquerylimit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"OverrunPolicy": {
4545
"type": "string",
46-
"description": "Only used for Data Federation limits. Action to take when the usage limit is exceeded. If limit span is set to QUERY, this is ignored because MongoDB Cloud stops the query when it exceeds the usage limit. \"enum\" : [ \"BLOCK\", \"BLOCK_AND_KILL\" ]"
46+
"description": "Action to take when the usage limit is exceeded. If limit span is set to QUERY, this is ignored because MongoDB Cloud stops the query when it exceeds the usage limit."
4747
},
4848
"Value": {
4949
"type": "string",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# MongoDB::Atlas::FederatedQueryLimit
2+
3+
## Prerequisites
4+
### Resources needed to run the manual QA
5+
All resources are created as part of `cfn-testing-helper.sh`:
6+
7+
- Atlas Project
8+
- Federated Database Instance (Tenant)
9+
10+
## Manual QA
11+
Please follow the steps in [TESTING.md](../../../TESTING.md).
12+
13+
14+
### Success criteria when testing the resource
15+
1. Ensure general [CFN resource success criteria](../../../TESTING.md#success-criteria-when-testing-the-resource) for this resource is met.
16+
17+
## Important Links
18+
- [API Documentation](https://www.mongodb.com/docs/api/doc/atlas-admin-api-v2/group/endpoint-data-federation)
19+
20+
## Unit Testing Locally
21+
22+
The local tests are integrated with the AWS `sam local` and `cfn invoke` tooling features:
23+
24+
```
25+
sam local start-lambda --skip-pull-image
26+
```
27+
then in another shell:
28+
```bash
29+
repo_root=$(git rev-parse --show-toplevel)
30+
source <(${repo_root}/quickstart-mongodb-atlas/scripts/export-mongocli-config.py)
31+
cd ${repo_root}/cfn-resources/federated-query-limit
32+
./test/cfn-test-create-inputs.sh YourProjectName YourTenantName > test.request.json
33+
echo "Sample request:"
34+
cat test.request.json
35+
cfn invoke resource CREATE test.request.json
36+
cfn invoke resource DELETE test.request.json
37+
cd -
38+
```
39+
40+
Both CREATE & DELETE tests must pass.

cfn-resources/federated-query-limit/test/cfn-test-create-inputs.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@ jq --arg projectId "${projectId}" \
5656
--arg profile "$profile" \
5757
'.Profile?|=$profile |.ProjectId?|=$projectId | .TenantName?|=$tenantName' \
5858
"$(dirname "$0")/inputs_1_update.template.json" >"inputs/inputs_1_update.json"
59+
60+
jq --arg projectId "${projectId}" \
61+
--arg tenantName "${tenantName}" \
62+
--arg profile "$profile" \
63+
'.Profile?|=$profile |.ProjectId?|=$projectId | .TenantName?|=$tenantName' \
64+
"$(dirname "$0")/inputs_2_create.template.json" >"inputs/inputs_2_create.json"
65+
66+
jq --arg projectId "${projectId}" \
67+
--arg tenantName "${tenantName}" \
68+
--arg profile "$profile" \
69+
'.Profile?|=$profile |.ProjectId?|=$projectId | .TenantName?|=$tenantName' \
70+
"$(dirname "$0")/inputs_2_update.template.json" >"inputs/inputs_2_update.json"

cfn-resources/federated-query-limit/test/inputs_1_create.template.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"ProjectId": "",
33
"TenantName":"",
44
"LimitName": "bytesProcessed.query",
5+
"OverrunPolicy": "BLOCK",
56
"Value": "2000000000",
67
"Profile": "default"
78
}

cfn-resources/federated-query-limit/test/inputs_1_update.template.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"ProjectId": "",
33
"TenantName":"",
44
"LimitName": "bytesProcessed.query",
5+
"OverrunPolicy": "BLOCK_AND_KILL",
56
"Value": "3000000000",
67
"Profile": "default"
78
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ProjectId": "",
3+
"TenantName":"",
4+
"LimitName": "bytesProcessed.query",
5+
"Value": "2000000000",
6+
"Profile": "default"
7+
}
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ProjectId": "",
3+
"TenantName":"",
4+
"LimitName": "bytesProcessed.query",
5+
"Value": "3000000000",
6+
"Profile": "default"
7+
}
8+

cfn-resources/util/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ const (
146146
SkipRoleValidation = "SkipRoleValidation"
147147
LimitName = "LimitName"
148148
Value = "Value"
149+
OverrunPolicy = "OverrunPolicy"
149150

150151
AlreadyExist = "Already Exist"
151152
EmptyString = ""

0 commit comments

Comments
 (0)