Skip to content

Commit

Permalink
Add supported resources and resource command to generate the list
Browse files Browse the repository at this point in the history
Former-commit-id: bd8da96
  • Loading branch information
MeNsaaH committed Jan 12, 2021
1 parent 699e157 commit 20759f8
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 25 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 23,14 @@ This tool is **HIGHLY DESTRUCTIVE** and can deletes all resources! This should b
- AWS: https://github.com/MeNsaaH/reka/issues/1
- GCP: https://github.com/MeNsaaH/reka/issues/2

Here is a list of all [supported resources](./supported-resources.md)

## Development
Copy `config/config.example.yaml` to `config/config.yaml` and make all necessary changes
```bash
cp config/config.example.yaml config/config.yaml
# One time run
go run main.go --config ../config/config.yaml

# TODO Web Dashboard
go run main.go web --config ../config/config.yaml
```

## Installation
Expand All @@ -50,9 48,5 @@ Copy `config/config.example.yaml` to `config/config.yaml` and make all necessary
cp config/config.example.yaml config/config.yaml
# Run reka using configuration. Stops stoppable resources, resume resumable resources and terminate
# resources dues for termination
reka --config ../config/config.yaml

# KABOOOOOM
# everything gone
reka nuke --config ../config/config.yaml
```
reka --config config/config.yaml
```
75 changes: 75 additions & 0 deletions cmd/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 1,75 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"log"
"os"
"text/template"

"github.com/mensaah/reka/provider/types"
"github.com/spf13/cobra"
)

var (
listResource bool
outputFormat string
)
var markdownTemplate = `
# Supported Resources
{{- range .Providers }}
## {{ .Name }}
| Resource | Destroyable| Stoppable|
| ---------|:----------:| --------:|
{{- range .Managers}}
| {{ .Name }} | true | {{ .IsStoppable }} |
{{- end}}
{{- end }}
`

// resourcesCmd represents the resources command
var resourcesCmd = &cobra.Command{
Use: "resources",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
type pr struct {
Providers []*types.Provider
}
p := pr{Providers: providers}
// Create a new template and parse the letter into it.
t := template.Must(template.New("markdown").Parse(markdownTemplate))

// Execute the template for each recipient.
err := t.Execute(os.Stdout, p)
if err != nil {
log.Println("executing template:", err)
}
},
}

func init() {
rootCmd.AddCommand(resourcesCmd)
resourcesCmd.Flags().StringVarP(&outputFormat, "output", "o", "markdown", "Set output type (default Markdown)")
}

func formatProviderDetails(p *types.Provider) {
}
12 changes: 6 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 27,9 @@ import (
"github.com/spf13/viper"

"github.com/mensaah/reka/config"
"github.com/mensaah/reka/provider"
"github.com/mensaah/reka/provider/aws"
"github.com/mensaah/reka/provider/gcp"
"github.com/mensaah/reka/provider/types"
"github.com/mensaah/reka/resource"
"github.com/mensaah/reka/rules"
"github.com/mensaah/reka/state"
Expand All @@ -38,7 38,7 @@ import (
var (
cfgFile string
cfg *config.Config
providers []*provider.Provider
providers []*types.Provider
backend state.Backender
activeState *state.State
)
Expand Down Expand Up @@ -130,11 130,11 @@ func initConfig() {
backend = state.InitBackend()
}

func initProviders() []*provider.Provider {
var providers []*provider.Provider
func initProviders() []*types.Provider {
var providers []*types.Provider
for _, p := range config.GetProviders() {
var (
provider *provider.Provider
provider *types.Provider
err error
)
switch p {
Expand All @@ -154,7 154,7 @@ func initProviders() []*provider.Provider {

// Refresh current status of resources from Providers
// TODO Reconcile state so that new resources are added to desired states and former resources removed
func refreshResources(providers []*provider.Provider) {
func refreshResources(providers []*types.Provider) {
// activeState is the current state stored in backend
activeState = backend.GetState()

Expand Down
6 changes: 3 additions & 3 deletions provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/mensaah/reka/config"
"github.com/mensaah/reka/provider"
"github.com/mensaah/reka/provider/types"
"github.com/mensaah/reka/resource"
)

Expand All @@ -28,9 28,9 @@ func NewResource(id, manager string) *resource.Resource {
}

// NewProvider : Creates a New AWS Provider
func NewProvider() (*provider.Provider, error) {
func NewProvider() (*types.Provider, error) {

aws := provider.Provider{}
aws := types.Provider{}
aws.Name = providerName

aws.SetLogger("logger.log")
Expand Down
2 changes: 1 addition & 1 deletion provider/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 124,7 @@ func destroyS3Buckets(cfg aws.Config, s3Buckets []*resource.Resource) error {
bucketsPerRegion[bucket.Region] = append(bucketsPerRegion[bucket.Region], bucket)
}

// TODO Use Goroutines
// TODO Use Goroutines and also destroy all objects in the bucket before executing destroy on bucket
for region, buckets := range bucketsPerRegion {
svc := s3.NewFromConfig(cfg, func(options *s3.Options) {
options.Region = region
Expand Down
6 changes: 3 additions & 3 deletions provider/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/mensaah/reka/config"
"github.com/mensaah/reka/provider"
"github.com/mensaah/reka/provider/types"
"github.com/mensaah/reka/resource"
)

Expand All @@ -28,9 28,9 @@ func NewResource(id, manager string) *resource.Resource {
}

// NewProvider : Creates a New AWS Provider
func NewProvider() (*provider.Provider, error) {
func NewProvider() (*types.Provider, error) {

gcp := provider.Provider{}
gcp := types.Provider{}
gcp.Name = providerName

gcp.SetLogger("logger.log")
Expand Down
1 change: 1 addition & 0 deletions provider/providers.go
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
package provider
2 changes: 1 addition & 1 deletion provider/provider.go → provider/types/provider.go
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
package provider
package types

import (
"fmt"
Expand Down
6 changes: 6 additions & 0 deletions resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 32,14 @@ func (mgr Manager) String() string {
return mgr.Name
}

func (mgr Manager) IsStoppable() bool {
return mgr.Stop != nil && mgr.Resume != nil
}

// Resource : The Provider Interface
// fields with `gorm:"-"` are ignored in database columns
// TODO create dependent Resource field. And ensure dependent resources are first destroyed before
// destruction of resource happens
type Resource struct {
// Add ID, CreatedAt, UpdatedAt and DeletedAt fields
gorm.Model `json:"-"`
Expand Down
4 changes: 2 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@ package state

import (
"github.com/mensaah/reka/config"
"github.com/mensaah/reka/provider"
"github.com/mensaah/reka/provider/types"
)

var backend Backender
Expand All @@ -14,7 14,7 @@ var cfg *config.Config
// s3: [ Resource1, Resource2 ]
// }
// }
type ProvidersState map[string]provider.Resources
type ProvidersState map[string]types.Resources

// State object: Represents a reka state consisting of current and desired state
// Desired state is used for resumption of resources. It stores the attributes of the resource to resume to
Expand Down
14 changes: 14 additions & 0 deletions supported-resources.md
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@

# Supported Resources
## aws
| Resource | Destroyable| Stoppable|
| ---------|:----------:| --------:|
| EBS | true | false |
| ec2 | true | true |
| eip | true | false |
| eks | true | true |
| s3 | true | false |
## gcp
| Resource | Destroyable| Stoppable|
| ---------|:----------:| --------:|
| CloudStorage | true | false |

0 comments on commit 20759f8

Please sign in to comment.