|
| 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 | + |
| 20 | + "go.mongodb.org/atlas-sdk/v20250312013/admin" |
| 21 | + |
| 22 | + "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler" |
| 23 | + |
| 24 | + "github.com/mongodb/mongodbatlas-cloudformation-resources/util" |
| 25 | + "github.com/mongodb/mongodbatlas-cloudformation-resources/util/constants" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + stateDone = "DONE" |
| 30 | + stateFailed = "FAILED" |
| 31 | + stateDeleteRequested = "DELETE_REQUESTED" |
| 32 | + stateDeleting = "DELETING" |
| 33 | + stateDeleted = "DELETED" |
| 34 | + |
| 35 | + callbackDelaySeconds = 3 |
| 36 | + callbackKey = "callbackStreamPrivatelinkEndpoint" |
| 37 | +) |
| 38 | + |
| 39 | +var callbackContext = map[string]any{callbackKey: true} |
| 40 | + |
| 41 | +func isCallback(req *handler.Request) bool { |
| 42 | + _, found := req.CallbackContext[callbackKey] |
| 43 | + return found |
| 44 | +} |
| 45 | + |
| 46 | +func validateProgress(client *util.MongoDBClient, model *Model, isDelete bool) handler.ProgressEvent { |
| 47 | + ctx := context.Background() |
| 48 | + projectID := util.SafeString(model.ProjectId) |
| 49 | + connectionID := util.SafeString(model.Id) |
| 50 | + |
| 51 | + streamsPrivateLinkConnection, apiResp, err := client.AtlasSDK.StreamsApi.GetPrivateLinkConnection(ctx, projectID, connectionID).Execute() |
| 52 | + notFound := util.StatusNotFound(apiResp) |
| 53 | + |
| 54 | + if err != nil && !notFound { |
| 55 | + return handleError(apiResp, constants.READ, err) |
| 56 | + } |
| 57 | + |
| 58 | + state := stateDeleted |
| 59 | + if streamsPrivateLinkConnection != nil { |
| 60 | + state = streamsPrivateLinkConnection.GetState() |
| 61 | + } |
| 62 | + |
| 63 | + targetState := stateDone |
| 64 | + if isDelete { |
| 65 | + targetState = stateDeleted |
| 66 | + } |
| 67 | + |
| 68 | + if state == stateFailed { |
| 69 | + return handleFailedState(streamsPrivateLinkConnection) |
| 70 | + } |
| 71 | + |
| 72 | + if state != targetState { |
| 73 | + return inProgressEvent(model, streamsPrivateLinkConnection) |
| 74 | + } |
| 75 | + |
| 76 | + if isDelete { |
| 77 | + return handler.ProgressEvent{ |
| 78 | + OperationStatus: handler.Success, |
| 79 | + Message: constants.Complete, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + UpdateModel(model, streamsPrivateLinkConnection) |
| 84 | + return handler.ProgressEvent{ |
| 85 | + OperationStatus: handler.Success, |
| 86 | + Message: constants.Complete, |
| 87 | + ResourceModel: model, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func inProgressEvent(model *Model, apiResp *admin.StreamsPrivateLinkConnection) handler.ProgressEvent { |
| 92 | + UpdateModel(model, apiResp) |
| 93 | + return handler.ProgressEvent{ |
| 94 | + OperationStatus: handler.InProgress, |
| 95 | + Message: constants.Pending, |
| 96 | + ResourceModel: model, |
| 97 | + CallbackDelaySeconds: callbackDelaySeconds, |
| 98 | + CallbackContext: callbackContext, |
| 99 | + } |
| 100 | +} |
0 commit comments