Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/clean/atlas/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *Cleaner) Clean(ctx context.Context, lifetime int) error {
for _, proj := range projects {
p := proj

fmt.Println(text.FgHiWhite.Sprintf("\tStarting deletion of project %s(%s) ...", p.GetName(), p.GetId()))
fmt.Println(text.FgHiWhite.Sprintf("\tStarting deletion of project %s(%s) (created at %v)...", p.GetName(), p.GetId(), p.Created))

if time.Since(p.Created) < time.Duration(lifetime)*time.Hour {
fmt.Println(text.FgYellow.Sprintf("\tProject %s(%s) skipped once created less than %d hour ago", p.GetName(), p.GetId(), lifetime))
Expand Down
24 changes: 18 additions & 6 deletions tools/clean/atlas/network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In atlas.go there are already constants with the supported cloud providers, please reuse them

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

)

func (c *Cleaner) listNetworkPeering(ctx context.Context, projectID string) []admin.BaseNetworkPeeringConnectionSettings {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See GetProjectDependencies() in projects.go and consider move the provider logic to there.
Reason is that Gov only support AWS and make a request for other provider results in an error

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
}

Expand Down
49 changes: 48 additions & 1 deletion tools/clean/provider/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does GCP require subnets to be remove VPCs with subnets? seems strange

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
}
Expand All @@ -39,7 +81,6 @@ func (gcp *GCP) DeleteVpc(ctx context.Context, vpcName string) error {
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down