Skip to content

Commit

Permalink
Revert "Adding oauth stuff to only support access tokens"
Browse files Browse the repository at this point in the history
This reverts commit 1517a2a.
  • Loading branch information
OrKarstoft committed Sep 16, 2024
1 parent 5bae917 commit 3c5e84c
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 187 deletions.
13 changes: 2 additions & 11 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"encoding/json"
"fmt"
"log"
"os"

Expand All @@ -13,6 +12,7 @@ import (

// Command -
func Command() {

app := &cli.App{
Usage: "The GCP project cleanup tool with added radiation",
Version: "v0.1.0",
Expand Down Expand Up @@ -43,17 +43,9 @@ func Command() {
EnvVars: []string{"EXCLUSIONS_CONFIG"},
Aliases: []string{"ec"},
},
&cli.StringFlag{
Name: "gcpaccesstoken",
Usage: "GCP token for authentication",
EnvVars: []string{"GCP_ACCESS_TOKEN"},
},
},
Action: func(c *cli.Context) error {
if c.String("gcpaccesstoken") == "" {
return fmt.Errorf("GCP Access Token not provided")
}
token := config.ConvertStringToTokenSource(c.String("gcptoken"))

// Behaviour to delete all resource in parallel in one project at a time - will be made into loop / concurrenct project nuke if required
config := config.Config{
Project: c.String("project"),
Expand All @@ -63,7 +55,6 @@ func Command() {
Context: gcp.Ctx,
Zones: gcp.GetZones(gcp.Ctx, c.String("project")),
Regions: gcp.GetRegions(gcp.Ctx, c.String("project")),
GCPToken: token,
}

if c.String("exclusionsconfig") != "" {
Expand Down
7 changes: 0 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package config

import (
"context"

"golang.org/x/oauth2"
)

// Config -
Expand All @@ -16,7 +14,6 @@ type Config struct {
Context context.Context
DryRun bool
Exclusions Exclusions
GCPToken oauth2.TokenSource
}

type Exclusions struct {
Expand All @@ -38,7 +35,3 @@ type Exclusions struct {
GoogleComputeNetwork []string `json:"google_compute_network"`
IAMServiceAccount []string `json:"iam_service_account"`
}

func ConvertStringToTokenSource(token string) oauth2.TokenSource {
return oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
}
29 changes: 18 additions & 11 deletions gcp/bigquery_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/sync/syncmap"
"google.golang.org/api/bigquery/v2"
"google.golang.org/api/option"
)

type BigQueryDataset struct {
Expand All @@ -22,26 +21,30 @@ type BigQueryDataset struct {
DatasetIDs []string
}

func init() {

bigqueryService, err := bigquery.NewService(Ctx)
if err != nil {
log.Fatal(err)
}

bigqueryResource := BigQueryDataset{
serviceClient: bigqueryService,
}
register(&bigqueryResource)
}

func (c *BigQueryDataset) Name() string {
return "BigQueryDataset"
}

func (c *BigQueryDataset) ToSlice() (slice []string) {
return helpers.SortedSyncMapKeys(&c.resourceMap)

}

func (c *BigQueryDataset) Setup(config config.Config) {
c.base.config = config

bigqueryService, err := bigquery.NewService(Ctx, option.WithTokenSource(config.GCPToken))
if err != nil {
log.Fatal(err)
}

bigqueryResource := BigQueryDataset{
serviceClient: bigqueryService,
}
register(&bigqueryResource)
}

func (c *BigQueryDataset) List(refreshCache bool) []string {
Expand All @@ -57,7 +60,9 @@ func (c *BigQueryDataset) List(refreshCache bool) []string {
}

for _, dataset := range datasetList.Datasets {

c.resourceMap.Store(dataset.Id, dataset.DatasetReference.DatasetId)

}

return c.ToSlice()
Expand All @@ -69,6 +74,7 @@ func (c *BigQueryDataset) Dependencies() []string {
}

func (c *BigQueryDataset) Remove() error {

client, err := bq.NewClient(Ctx, c.base.config.Project)
if err != nil {
return fmt.Errorf("bigquery.NewClient: %v", err)
Expand Down Expand Up @@ -99,6 +105,7 @@ func (c *BigQueryDataset) Remove() error {
log.Printf("[Info] Resource currently being deleted %v [type: %v project: %v ] (%v seconds)", datasetID, c.Name(), c.base.config.Project, seconds)
tableList, _ := c.serviceClient.Tables.List(c.base.config.Project, datasetID).Context(Ctx).Do()
if tableList == nil {

deletedTables = true
}

Expand Down
22 changes: 13 additions & 9 deletions gcp/compute_disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/sync/syncmap"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)

// ComputeDisks -
Expand All @@ -22,6 +21,17 @@ type ComputeDisks struct {
resourceMap syncmap.Map
}

func init() {
computeService, err := compute.NewService(Ctx)
if err != nil {
log.Fatal(err)
}
computeResource := ComputeDisks{
serviceClient: computeService,
}
register(&computeResource)
}

// Name - Name of the resourceLister for ComputeDisks
func (c *ComputeDisks) Name() string {
return "ComputeDisks"
Expand All @@ -30,20 +40,13 @@ func (c *ComputeDisks) Name() string {
// ToSlice - Name of the resourceLister for ComputeDisks
func (c *ComputeDisks) ToSlice() (slice []string) {
return helpers.SortedSyncMapKeys(&c.resourceMap)

}

// Setup - populates the struct
func (c *ComputeDisks) Setup(config config.Config) {
c.base.config = config

computeService, err := compute.NewService(Ctx, option.WithTokenSource(config.GCPToken))
if err != nil {
log.Fatal(err)
}
computeResource := ComputeDisks{
serviceClient: computeService,
}
register(&computeResource)
}

// List - Returns a list of all ComputeDisks
Expand Down Expand Up @@ -82,6 +85,7 @@ func (c *ComputeDisks) Dependencies() []string {

// Remove -
func (c *ComputeDisks) Remove() error {

// Removal logic
errs, _ := errgroup.WithContext(c.base.config.Context)

Expand Down
22 changes: 13 additions & 9 deletions gcp/compute_firewalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/sync/syncmap"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)

// ComputeFirewalls -
Expand All @@ -22,6 +21,17 @@ type ComputeFirewalls struct {
resourceMap syncmap.Map
}

func init() {
computeService, err := compute.NewService(Ctx)
if err != nil {
log.Fatal(err)
}
computeResource := ComputeFirewalls{
serviceClient: computeService,
}
register(&computeResource)
}

// Name - Name of the resourceLister for ComputeFirewalls
func (c *ComputeFirewalls) Name() string {
return "ComputeFirewalls"
Expand All @@ -30,20 +40,13 @@ func (c *ComputeFirewalls) Name() string {
// ToSlice - Name of the resourceLister for ComputeFirewalls
func (c *ComputeFirewalls) ToSlice() (slice []string) {
return helpers.SortedSyncMapKeys(&c.resourceMap)

}

// Setup - populates the struct
func (c *ComputeFirewalls) Setup(config config.Config) {
c.base.config = config

computeService, err := compute.NewService(Ctx, option.WithTokenSource(config.GCPToken))
if err != nil {
log.Fatal(err)
}
computeResource := ComputeFirewalls{
serviceClient: computeService,
}
register(&computeResource)
}

// List - Returns a list of all ComputeFirewalls
Expand Down Expand Up @@ -76,6 +79,7 @@ func (c *ComputeFirewalls) Dependencies() []string {

// Remove -
func (c *ComputeFirewalls) Remove() error {

// Removal logic
errs, _ := errgroup.WithContext(c.base.config.Context)

Expand Down
22 changes: 13 additions & 9 deletions gcp/compute_instance_groups_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/sync/syncmap"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)

// ComputeInstanceGroupsRegion -
Expand All @@ -22,6 +21,17 @@ type ComputeInstanceGroupsRegion struct {
resourceMap syncmap.Map
}

func init() {
computeService, err := compute.NewService(Ctx)
if err != nil {
log.Fatal(err)
}
computeResource := ComputeInstanceGroupsRegion{
serviceClient: computeService,
}
register(&computeResource)
}

// Name - Name of the resourceLister for ComputeInstanceGroupsRegion
func (c *ComputeInstanceGroupsRegion) Name() string {
return "ComputeInstanceGroupsRegion"
Expand All @@ -30,20 +40,13 @@ func (c *ComputeInstanceGroupsRegion) Name() string {
// ToSlice - Name of the resourceLister for ComputeInstanceGroupsRegion
func (c *ComputeInstanceGroupsRegion) ToSlice() (slice []string) {
return helpers.SortedSyncMapKeys(&c.resourceMap)

}

// Setup - populates the struct
func (c *ComputeInstanceGroupsRegion) Setup(config config.Config) {
c.base.config = config

computeService, err := compute.NewService(Ctx, option.WithTokenSource(config.GCPToken))
if err != nil {
log.Fatal(err)
}
computeResource := ComputeInstanceGroupsRegion{
serviceClient: computeService,
}
register(&computeResource)
}

// List - Returns a list of all ComputeInstanceGroupsRegion
Expand Down Expand Up @@ -79,6 +82,7 @@ func (c *ComputeInstanceGroupsRegion) Dependencies() []string {

// Remove -
func (c *ComputeInstanceGroupsRegion) Remove() error {

// Removal logic
errs, _ := errgroup.WithContext(c.base.config.Context)

Expand Down
23 changes: 13 additions & 10 deletions gcp/compute_instance_groups_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/sync/syncmap"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)

// ComputeInstanceGroupsZone -
Expand All @@ -25,6 +24,17 @@ type ComputeInstanceGroupsZone struct {
resourceMap syncmap.Map
}

func init() {
computeService, err := compute.NewService(Ctx)
if err != nil {
log.Fatal(err)
}
computeResource := ComputeInstanceGroupsZone{
serviceClient: computeService,
}
register(&computeResource)
}

// Name - Name of the resourceLister for ComputeInstanceGroupsZone
func (c *ComputeInstanceGroupsZone) Name() string {
return "ComputeInstanceGroupsZone"
Expand All @@ -33,21 +43,13 @@ func (c *ComputeInstanceGroupsZone) Name() string {
// ToSlice - Name of the resourceLister for ComputeInstanceGroupsZone
func (c *ComputeInstanceGroupsZone) ToSlice() (slice []string) {
return helpers.SortedSyncMapKeys(&c.resourceMap)

}

// Setup - populates the struct
func (c *ComputeInstanceGroupsZone) Setup(config config.Config) {
c.base.config = config

computeService, err := compute.NewService(Ctx, option.WithTokenSource(config.GCPToken))
if err != nil {
log.Fatal(err)
}
computeResource := ComputeInstanceGroupsZone{
serviceClient: computeService,
}
register(&computeResource)

// Get the node pool list with some reflection rather than re-instantiating
a := ContainerGKEClusters{}
gkeResource := resourceMap[a.Name()]
Expand Down Expand Up @@ -95,6 +97,7 @@ func (c *ComputeInstanceGroupsZone) Dependencies() []string {

// Remove -
func (c *ComputeInstanceGroupsZone) Remove() error {

// Removal logic
errs, _ := errgroup.WithContext(c.base.config.Context)

Expand Down
Loading

0 comments on commit 3c5e84c

Please sign in to comment.