@@ -2,6 +2,7 @@ package atlasproject
22
33import (
44 "context"
5+ "errors"
56
67 "go.mongodb.org/atlas/mongodbatlas"
78 "go.uber.org/zap"
@@ -39,24 +40,22 @@ func createOrDeletePEInAtlas(ctx *workflow.Context, projectID string, specPEs []
3940 return result
4041 }
4142
42- endpointsToCreate := set .Difference (specPEs , statusPEs )
43- endpointsToUpdate := set .Intersection (specPEs , statusPEs )
4443 endpointsToDelete := set .Difference (statusPEs , specPEs )
45-
46- log .Debugw ("Items to create" , "difference" , endpointsToCreate )
47- log .Debugw ("Items to update" , "difference" , endpointsToUpdate )
48- log .Debugw ("Items to delete" , "difference" , endpointsToDelete )
49-
44+ log .Debugw ("Private Endpoints to delete" , "difference" , endpointsToDelete )
5045 if result := deletePrivateEndpointsFromAtlas (ctx , projectID , endpointsToDelete ); ! result .IsOk () {
5146 return result
5247 }
5348
49+ endpointsToCreate := set .Difference (specPEs , statusPEs )
50+ log .Debugw ("Private Endpoints to create" , "difference" , endpointsToCreate )
5451 newConnections , err := createPeServiceInAtlas (ctx .Client , projectID , endpointsToCreate )
5552 if err != nil {
5653 log .Debugw ("Failed to create PE Service in Atlas" , "error" , err )
5754 }
5855 ctx .EnsureStatusOption (status .AtlasProjectAddPrivateEnpointsOption (convertAllToStatus (ctx , projectID , newConnections )))
5956
57+ endpointsToUpdate := set .Intersection (specPEs , statusPEs )
58+ log .Debugw ("Private Endpoints to update" , "difference" , endpointsToUpdate )
6059 if err = createPrivateEndpointInAtlas (ctx .Client , projectID , endpointsToUpdate , log ); err != nil {
6160 log .Debugw ("Failed to create PE Interface in Atlas" , "error" , err )
6261 }
@@ -212,8 +211,8 @@ func clearOutNotLinkedPEs(ctx *workflow.Context, projectID string, atlasConns []
212211 endpointsWithoutPair := []status.ProjectPrivateEndpoint {}
213212 endpointsAreDeleting := false
214213 for _ , atlasConn := range atlasConns {
215- if isDeleting (atlasConn .Status ) {
216- endpointsAreDeleting = true
214+ if ! isAvailable (atlasConn .Status ) {
215+ continue
217216 }
218217
219218 atlasPE := convertOneToStatus (ctx , projectID , atlasConn )
@@ -226,6 +225,7 @@ func clearOutNotLinkedPEs(ctx *workflow.Context, projectID string, atlasConns []
226225
227226 if ! found {
228227 endpointsWithoutPair = append (endpointsWithoutPair , atlasPE )
228+ endpointsAreDeleting = true
229229 }
230230 }
231231
@@ -271,65 +271,68 @@ func convertAllToStatus(ctx *workflow.Context, projectID string, peList []mongod
271271 return result
272272}
273273
274- func convertOneToStatus (ctx * workflow.Context , projectID string , endpoint mongodbatlas.PrivateEndpointConnection ) status.ProjectPrivateEndpoint {
274+ func convertOneToStatus (ctx * workflow.Context , projectID string , conn mongodbatlas.PrivateEndpointConnection ) status.ProjectPrivateEndpoint {
275275 pe := status.ProjectPrivateEndpoint {
276- ID : endpoint .ID ,
277- Provider : provider .ProviderName (endpoint .ProviderName ),
278- Region : endpoint .Region ,
276+ ID : conn .ID ,
277+ Provider : provider .ProviderName (conn .ProviderName ),
278+ Region : conn .Region ,
279279 }
280280
281281 switch pe .Provider {
282282 case provider .ProviderAWS :
283- pe .ServiceName = endpoint .EndpointServiceName
284- pe .ServiceResourceID = endpoint .ID
285- if len (endpoint .InterfaceEndpoints ) != 0 {
286- pe .InterfaceEndpointID = endpoint .InterfaceEndpoints [0 ]
283+ pe .ServiceName = conn .EndpointServiceName
284+ pe .ServiceResourceID = conn .ID
285+ if len (conn .InterfaceEndpoints ) != 0 {
286+ pe .InterfaceEndpointID = conn .InterfaceEndpoints [0 ]
287287 }
288288 case provider .ProviderAzure :
289- pe .ServiceName = endpoint .PrivateLinkServiceName
290- pe .ServiceResourceID = endpoint .PrivateLinkServiceResourceID
291- if len (endpoint .PrivateEndpoints ) != 0 {
292- pe .InterfaceEndpointID = endpoint .PrivateEndpoints [0 ]
289+ pe .ServiceName = conn .PrivateLinkServiceName
290+ pe .ServiceResourceID = conn .PrivateLinkServiceResourceID
291+ if len (conn .PrivateEndpoints ) != 0 {
292+ pe .InterfaceEndpointID = conn .PrivateEndpoints [0 ]
293293 }
294294 case provider .ProviderGCP :
295- pe .InterfaceEndpointID , pe .Endpoints = getGCPEndpointData (ctx , projectID , endpoint )
295+ pe .ServiceAttachmentNames = conn .ServiceAttachmentNames
296+ if len (conn .EndpointGroupNames ) != 0 {
297+ var err error
298+ pe .InterfaceEndpointID = conn .EndpointGroupNames [0 ]
299+ pe .Endpoints , err = getGCPInterfaceEndpoint (ctx , projectID , pe )
300+ if err != nil {
301+ ctx .Log .Warnw ("failed to get Interface Endpoint Data for GCP" , "err" , err , "pe" , pe )
302+ }
303+ }
296304 }
305+ ctx .Log .Debugw ("Converted Status" , "status" , pe , "connection" , conn )
306+
297307 return pe
298308}
299309
300- // getGCPEndpointData returns an InterfaceEndpointID and a list of GCP endpoints
301- func getGCPEndpointData (ctx * workflow.Context , projectID string , endpoint mongodbatlas. PrivateEndpointConnection ) (string , []status.GCPEndpoint ) {
310+ // getGCPInterfaceEndpoint returns an InterfaceEndpointID and a list of GCP endpoints
311+ func getGCPInterfaceEndpoint (ctx * workflow.Context , projectID string , endpoint status. ProjectPrivateEndpoint ) ([]status.GCPEndpoint , error ) {
302312 log := ctx .Log
303- serviceID := endpoint .ID
304- if len (endpoint .EndpointGroupNames ) == 0 {
305- return "" , nil
313+ if endpoint .InterfaceEndpointID == "" {
314+ return nil , errors .New ("InterfaceEndpointID is empty" )
306315 }
307- endpointID := endpoint .EndpointGroupNames [0 ]
308- interfaceEndpointConn , _ , err := ctx .Client .PrivateEndpoints .GetOnePrivateEndpoint (context .Background (), projectID , string (provider .ProviderGCP ), serviceID , endpointID )
316+ interfaceEndpointConn , _ , err := ctx .Client .PrivateEndpoints .GetOnePrivateEndpoint (context .Background (), projectID , string (provider .ProviderGCP ), endpoint .ID , endpoint .InterfaceEndpointID )
309317 if err != nil {
310- return endpointID , nil
318+ return nil , err
311319 }
312320
313- endpointConns := interfaceEndpointConn .Endpoints
314- listOfEndpoints := make ([]status.GCPEndpoint , 0 )
315- for _ , e := range endpointConns {
321+ interfaceConns := interfaceEndpointConn .Endpoints
322+ listOfInterfaces := make ([]status.GCPEndpoint , 0 )
323+ for _ , e := range interfaceConns {
316324 endpoint := status.GCPEndpoint {
317325 Status : e .Status ,
318326 EndpointName : e .EndpointName ,
319327 IPAddress : e .IPAddress ,
320328 }
321- listOfEndpoints = append (listOfEndpoints , endpoint )
329+ listOfInterfaces = append (listOfInterfaces , endpoint )
322330 }
331+ log .Debugw ("Result of getGCPEndpointData" , "endpoint.ID" , endpoint .ID , "listOfInterfaces" , listOfInterfaces )
323332
324- log .Debugw ("Result of getGCPEndpointData" , "endpointID" , endpointID , "listOfEndpoints" , listOfEndpoints )
325-
326- return endpointID , listOfEndpoints
333+ return listOfInterfaces , nil
327334}
328335
329336func isAvailable (status string ) bool {
330337 return status == "AVAILABLE"
331338}
332-
333- func isDeleting (status string ) bool {
334- return status == "DELETING"
335- }
0 commit comments