-
Notifications
You must be signed in to change notification settings - Fork 107
CLOUDP-212108: Fix net peering cleanups #1265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,16 +8,28 @@ import ( | |
| "go.mongodb.org/atlas-sdk/v20231001002/admin" | ||
| ) | ||
|
|
||
| func (c *Cleaner) listNetworkPeering(ctx context.Context, projectID string) []admin.BaseNetworkPeeringConnectionSettings { | ||
| peers, _, err := c.client.NetworkPeeringApi. | ||
| ListPeeringConnections(ctx, projectID). | ||
| Execute() | ||
| if err != nil { | ||
| fmt.Println(text.FgRed.Sprintf("\tFailed to list networking peering for project %s: %s", projectID, err)) | ||
| var ( | ||
| SupportedProviders = []string{"AWS", "AZURE", "GCP"} | ||
| ) | ||
|
|
||
| func (c *Cleaner) listNetworkPeering(ctx context.Context, projectID string) []admin.BaseNetworkPeeringConnectionSettings { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See |
||
| peers := []admin.BaseNetworkPeeringConnectionSettings{} | ||
| for _, providerName := range SupportedProviders { | ||
| peers = append(peers, c.listNetworkPeeringForProvider(ctx, projectID, providerName)...) | ||
| } | ||
| if len(peers) == 0 { | ||
| return nil | ||
| } | ||
| return peers | ||
| } | ||
|
|
||
| func (c *Cleaner) listNetworkPeeringForProvider(ctx context.Context, projectID, providerName string) []admin.BaseNetworkPeeringConnectionSettings { | ||
| queryArgs := admin.ListPeeringConnectionsApiParams{GroupId: projectID, ProviderName: &providerName} | ||
| peers, _, err := c.client.NetworkPeeringApi.ListPeeringConnectionsWithParams(ctx, &queryArgs).Execute() | ||
| if err != nil { | ||
| fmt.Println(text.FgRed.Sprintf("\tFailed to list %s networking peering for project %s: %s", providerName, projectID, err)) | ||
| return []admin.BaseNetworkPeeringConnectionSettings{} | ||
| } | ||
| return peers.Results | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,18 +19,60 @@ type GCP struct { | |
| projectID string | ||
|
|
||
| networkClient *compute.NetworksClient | ||
| subnetworksClient *compute.SubnetworksClient | ||
| forwardRuleClient *compute.ForwardingRulesClient | ||
| addressClient *compute.AddressesClient | ||
| keyManagementClient *kms.KeyManagementClient | ||
| } | ||
|
|
||
| func (gcp *GCP) DeleteVpc(ctx context.Context, vpcName string) error { | ||
| vpcGetRequest := &computepb.GetNetworkRequest{ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does GCP require subnets to be remove VPCs with subnets? seems strange
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it failed on my test until I added this code. |
||
| Project: gcp.projectID, | ||
| Network: vpcName, | ||
| } | ||
| net, err := gcp.networkClient.Get(ctx, vpcGetRequest) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get VPC %q: %v", vpcName, err) | ||
| } | ||
| for _, subnetURL := range net.Subnetworks { | ||
| subnet, region := decodeSubnetURL(subnetURL) | ||
| if subnet == "" { | ||
| return fmt.Errorf("failed to decode subnet URL %q", subnetURL) | ||
| } | ||
| subnetDeleteRequest := &computepb.DeleteSubnetworkRequest{ | ||
| Project: gcp.projectID, | ||
| Subnetwork: subnet, | ||
| Region: region, | ||
| } | ||
| op, err := gcp.subnetworksClient.Delete(ctx, subnetDeleteRequest) | ||
| if err := waitOrFailOp(ctx, op, err); err != nil { | ||
| return fmt.Errorf("failed to delete subnet %q: %v", subnet, err) | ||
| } | ||
| } | ||
| vpcRequest := &computepb.DeleteNetworkRequest{ | ||
| Project: gcp.projectID, | ||
| Network: vpcName, | ||
| } | ||
|
|
||
| op, err := gcp.networkClient.Delete(ctx, vpcRequest) | ||
| if err := waitOrFailOp(ctx, op, err); err != nil { | ||
| return fmt.Errorf("failed to delete VPC %q: %v", vpcName, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func decodeSubnetURL(subnetURL string) (string, string) { | ||
| parts := strings.Split(subnetURL, "/") | ||
| if len(parts) < 11 { | ||
| return "", "" | ||
| } | ||
| region := parts[8] | ||
| subnet := parts[10] | ||
| return subnet, region | ||
| } | ||
|
|
||
| func waitOrFailOp(ctx context.Context, op *compute.Operation, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -39,7 +81,6 @@ func (gcp *GCP) DeleteVpc(ctx context.Context, vpcName string) error { | |
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -177,6 +218,11 @@ func NewGCPCleaner(ctx context.Context) (*GCP, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| subnetworksClient, err := compute.NewSubnetworksRESTClient(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| forwardRuleClient, err := compute.NewForwardingRulesRESTClient(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -195,6 +241,7 @@ func NewGCPCleaner(ctx context.Context) (*GCP, error) { | |
| return &GCP{ | ||
| projectID: projectID, | ||
| networkClient: networkClient, | ||
| subnetworksClient: subnetworksClient, | ||
| forwardRuleClient: forwardRuleClient, | ||
| addressClient: addressClient, | ||
| keyManagementClient: keyManagementClient, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
atlas.gothere are already constants with the supported cloud providers, please reuse themThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok