Skip to content

Commit 7a36329

Browse files
authored
chore: Singletons in auto-generated resources (#3458)
* initial auditing_api and maintenance_window_api generation * fix unmarshal for empty JSON * improve maintenance_window_api * checkDestroy in manual resource tests * explicit version header * check apiResp is not nil * support FixedRequestBody * auditing except delete * support FixedRequestBody * comment in MakeApiError * comment Unmarshal * use same flow as in SDK * rename FixedRequestBody to StaticRequestBody * codegen tests * revert unneeded test
1 parent 67a303b commit 7a36329

File tree

23 files changed

+969
-25
lines changed

23 files changed

+969
-25
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,11 @@ jobs:
280280
- 'internal/provider/*.go'
281281
autogen:
282282
- 'internal/common/autogen/*.go'
283+
- 'internal/serviceapi/auditingapi/*.go'
283284
- 'internal/serviceapi/clusterapi/*.go'
284285
- 'internal/serviceapi/customdbroleapi/*.go'
285286
- 'internal/serviceapi/databaseuserapi/*.go'
287+
- 'internal/serviceapi/maintenancewindowapi/*.go'
286288
- 'internal/serviceapi/projectapi/*.go'
287289
- 'internal/serviceapi/pushbasedlogexportapi/*.go'
288290
- 'internal/serviceapi/resourcepolicyapi/*.go'
@@ -515,9 +517,11 @@ jobs:
515517
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }}
516518
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
517519
ACCTEST_PACKAGES: |
520+
./internal/serviceapi/auditingapi
518521
./internal/serviceapi/clusterapi
519522
./internal/serviceapi/customdbroleapi
520523
./internal/serviceapi/databaseuserapi
524+
./internal/serviceapi/maintenancewindowapi
521525
./internal/serviceapi/projectapi
522526
./internal/serviceapi/pushbasedlogexportapi
523527
./internal/serviceapi/resourcepolicyapi

internal/common/autogen/handle_operations.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,28 @@ func HandleUpdate(ctx context.Context, req HandleUpdateReq) {
145145
}
146146

147147
type HandleDeleteReq struct {
148-
Resp *resource.DeleteResponse
149-
Client *config.MongoDBClient
150-
State any
151-
CallParams *config.APICallParams
152-
Wait *WaitReq
148+
Resp *resource.DeleteResponse
149+
Client *config.MongoDBClient
150+
State any
151+
CallParams *config.APICallParams
152+
Wait *WaitReq
153+
StaticRequestBody string
153154
}
154155

155156
func HandleDelete(ctx context.Context, req HandleDeleteReq) {
156157
d := &req.Resp.Diagnostics
157-
if _, _, err := callAPIWithoutBody(ctx, req.Client, req.CallParams); err != nil {
158+
var err error
159+
if req.StaticRequestBody == "" {
160+
_, _, err = callAPIWithoutBody(ctx, req.Client, req.CallParams)
161+
} else {
162+
_, err = callAPIWithBody(ctx, req.Client, req.CallParams, []byte(req.StaticRequestBody))
163+
}
164+
if err != nil {
158165
addError(d, opDelete, errCallingAPI, err)
159166
return
160167
}
161-
if err := handleWaitDelete(ctx, req.Wait, req.Client); err != nil {
162-
addError(d, opDelete, errWaitingForChanges, err)
168+
if errWait := handleWaitDelete(ctx, req.Wait, req.Client); errWait != nil {
169+
addError(d, opDelete, errWaitingForChanges, errWait)
163170
}
164171
}
165172

internal/common/autogen/unmarshal.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
// Map is not supported yet, will be done in CLOUDP-312797.
1818
// Attributes that are in JSON but not in the model are ignored, no error is returned.
1919
func Unmarshal(raw []byte, model any) error {
20+
if isEmptyJSON(raw) {
21+
return nil // Some operations return an empty response body, in that case there is no need to update the model.
22+
}
2023
var objJSON map[string]any
2124
if err := json.Unmarshal(raw, &objJSON); err != nil {
2225
return err

internal/common/autogen/unmarshal_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ func TestUnmarshalZeroLenCollections(t *testing.T) {
433433
assert.Equal(t, modelExpected, model)
434434
}
435435

436+
func TestUnmarshalEmptyJSON(t *testing.T) {
437+
model := struct {
438+
Attr types.String `tfsdk:"attr"`
439+
}{
440+
Attr: types.StringValue("hello"),
441+
}
442+
require.NoError(t, autogen.Unmarshal([]byte(""), &model))
443+
require.NoError(t, autogen.Unmarshal(nil, &model))
444+
assert.Equal(t, types.StringValue("hello"), model.Attr)
445+
}
446+
436447
func TestUnmarshalErrors(t *testing.T) {
437448
testCases := map[string]struct {
438449
model any

internal/config/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ func (c *MongoDBClient) UntypedAPICall(ctx context.Context, params *APICallParam
289289
}
290290

291291
apiResp, err := untypedClient.CallAPI(apiReq)
292+
if err != nil || apiResp == nil {
293+
return apiResp, err
294+
}
292295

296+
// Returns a GenericOpenAPIError error if HTTP status code is not successful.
293297
if apiResp.StatusCode >= 300 {
294298
newErr := untypedClient.MakeApiError(apiResp, params.Method, localVarPath)
295299
return apiResp, newErr

internal/service/maintenancewindow/resource_maintenance_window_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestAccConfigRSMaintenanceWindow_basic(t *testing.T) {
4242
resource.ParallelTest(t, resource.TestCase{
4343
PreCheck: func() { acc.PreCheckBasic(t) },
4444
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
45+
CheckDestroy: checkDestroy,
4546
Steps: []resource.TestStep{
4647
{
4748
// testing hour_of_day set to 0 during creation phase does not return errors
@@ -80,6 +81,7 @@ func TestAccConfigRSMaintenanceWindow_emptyHourOfDay(t *testing.T) {
8081
resource.ParallelTest(t, resource.TestCase{
8182
PreCheck: func() { acc.PreCheckBasic(t) },
8283
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
84+
CheckDestroy: checkDestroy,
8385
Steps: []resource.TestStep{
8486
{
8587
Config: configBasic(orgID, projectName, dayOfWeek, nil, defaultProtectedHours),
@@ -100,6 +102,7 @@ func TestAccConfigRSMaintenanceWindow_autoDeferActivated(t *testing.T) {
100102
resource.ParallelTest(t, resource.TestCase{
101103
PreCheck: func() { acc.PreCheckBasic(t) },
102104
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
105+
CheckDestroy: checkDestroy,
103106
Steps: []resource.TestStep{
104107
{
105108
Config: configWithAutoDeferEnabled(orgID, projectName, dayOfWeek, hourOfDay),
@@ -135,6 +138,24 @@ func checkExists(resourceName string) resource.TestCheckFunc {
135138
}
136139
}
137140

141+
func checkDestroy(s *terraform.State) error {
142+
for _, rs := range s.RootModule().Resources {
143+
if rs.Type != "mongodbatlas_maintenance_window" {
144+
continue
145+
}
146+
projectID := rs.Primary.ID
147+
if projectID == "" {
148+
return fmt.Errorf("checkDestroy, no ID is set for: %s", resourceName)
149+
}
150+
maintenanceWindow, _, _ := acc.ConnV2().MaintenanceWindowsApi.GetMaintenanceWindow(context.Background(), projectID).Execute()
151+
// Check if it's back to default settings (day_of_week = 0 means it's been reset)
152+
if maintenanceWindow.GetDayOfWeek() != 0 {
153+
return fmt.Errorf("maintenance window for project (%s) was not properly reset to defaults", projectID)
154+
}
155+
}
156+
return nil
157+
}
158+
138159
func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
139160
return func(s *terraform.State) (string, error) {
140161
rs, ok := s.RootModule().Resources[resourceName]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package auditingapi_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
8+
)
9+
10+
func TestMain(m *testing.M) {
11+
cleanup := acc.SetupSharedResources()
12+
exitCode := m.Run()
13+
cleanup()
14+
os.Exit(exitCode)
15+
}

internal/serviceapi/auditingapi/resource.go

Lines changed: 142 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/serviceapi/auditingapi/resource_schema.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)