|
| 1 | +// Copyright 2026 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package resource |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler" |
| 23 | + "github.com/mongodb/mongodbatlas-cloudformation-resources/util" |
| 24 | + "github.com/mongodb/mongodbatlas-cloudformation-resources/util/constants" |
| 25 | + "github.com/mongodb/mongodbatlas-cloudformation-resources/util/progressevent" |
| 26 | +) |
| 27 | + |
| 28 | +func HandleCreate(req *handler.Request, client *util.MongoDBClient, model *Model) handler.ProgressEvent { |
| 29 | + s3LogIntegrationReq := NewLogIntegrationCreateRequest(model) |
| 30 | + logIntegrationResp, resp, err := client.AtlasSDK.PushBasedLogExportApi.CreateGroupLogIntegration(context.Background(), *model.ProjectId, s3LogIntegrationReq).Execute() |
| 31 | + if err != nil { |
| 32 | + return handleError(resp, err, "Error creating log integration") |
| 33 | + } |
| 34 | + |
| 35 | + UpdateLogIntegrationModel(model, logIntegrationResp) |
| 36 | + return handler.ProgressEvent{ |
| 37 | + OperationStatus: handler.Success, |
| 38 | + Message: "Create complete", |
| 39 | + ResourceModel: model, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func HandleRead(req *handler.Request, client *util.MongoDBClient, model *Model) handler.ProgressEvent { |
| 44 | + logIntegrationResp, resp, err := client.AtlasSDK.PushBasedLogExportApi.GetGroupLogIntegration(context.Background(), *model.ProjectId, *model.IntegrationId).Execute() |
| 45 | + if err != nil { |
| 46 | + return handleError(resp, err, "Error reading log integration") |
| 47 | + } |
| 48 | + |
| 49 | + UpdateLogIntegrationModel(model, logIntegrationResp) |
| 50 | + return handler.ProgressEvent{ |
| 51 | + OperationStatus: handler.Success, |
| 52 | + Message: constants.ReadComplete, |
| 53 | + ResourceModel: model, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func HandleUpdate(req *handler.Request, client *util.MongoDBClient, model *Model) handler.ProgressEvent { |
| 58 | + logIntegrationReq := NewLogIntegrationUpdateRequest(model) |
| 59 | + logIntegrationResp, resp, err := client.AtlasSDK.PushBasedLogExportApi.UpdateGroupLogIntegration(context.Background(), *model.ProjectId, *model.IntegrationId, logIntegrationReq).Execute() |
| 60 | + if err != nil { |
| 61 | + return handleError(resp, err, "Error updating log integration") |
| 62 | + } |
| 63 | + |
| 64 | + UpdateLogIntegrationModel(model, logIntegrationResp) |
| 65 | + return handler.ProgressEvent{ |
| 66 | + OperationStatus: handler.Success, |
| 67 | + Message: "Update complete", |
| 68 | + ResourceModel: model, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func HandleDelete(req *handler.Request, client *util.MongoDBClient, model *Model) handler.ProgressEvent { |
| 73 | + resp, err := client.AtlasSDK.PushBasedLogExportApi.DeleteGroupLogIntegration(context.Background(), *model.ProjectId, *model.IntegrationId).Execute() |
| 74 | + if err != nil { |
| 75 | + return handleError(resp, err, "Error deleting log integration") |
| 76 | + } |
| 77 | + |
| 78 | + return handler.ProgressEvent{ |
| 79 | + OperationStatus: handler.Success, |
| 80 | + Message: "Delete complete", |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func HandleList(req *handler.Request, client *util.MongoDBClient, model *Model) handler.ProgressEvent { |
| 85 | + paginatedResp, resp, err := client.AtlasSDK.PushBasedLogExportApi.ListGroupLogIntegrations(context.Background(), *model.ProjectId).Execute() |
| 86 | + if err != nil { |
| 87 | + return handleError(resp, err, "Error listing log integrations") |
| 88 | + } |
| 89 | + |
| 90 | + var allModels []*Model |
| 91 | + results := paginatedResp.GetResults() |
| 92 | + for i := range results { |
| 93 | + modelItem := &Model{ |
| 94 | + ProjectId: model.ProjectId, |
| 95 | + Profile: model.Profile, |
| 96 | + } |
| 97 | + UpdateLogIntegrationModel(modelItem, &results[i]) |
| 98 | + allModels = append(allModels, modelItem) |
| 99 | + } |
| 100 | + |
| 101 | + return handler.ProgressEvent{ |
| 102 | + OperationStatus: handler.Success, |
| 103 | + Message: constants.Complete, |
| 104 | + ResourceModel: allModels, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func handleError(resp *http.Response, err error, message string) handler.ProgressEvent { |
| 109 | + errMsg := fmt.Sprintf("%s: %v", message, err) |
| 110 | + return progressevent.GetFailedEventByResponse(errMsg, resp) |
| 111 | +} |
0 commit comments