Skip to content

Commit

Permalink
fs: add provider and file resource
Browse files Browse the repository at this point in the history
This new provider will allow frieza to cleanup local resources like files and folders.
This should also ease core testing.

close #34
ref #35

Signed-off-by: Jérôme Jutteau <[email protected]>
  • Loading branch information
jerome-jutteau committed Jun 15, 2022
1 parent 1481cd2 commit 0f6031e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ Files: *.md docs/*
Copyright: 2019 Outscale SAS <[email protected]>
License: CC-BY-4.0

Files: cmd/* *.mod *.sum Makefile internal/common/*.go internal/providers/outscale_oapi/* docs/providers.sh internal/providers/provider_example/* internal/providers/s3/* .github/* .goreleaser.yml version .gitignore
Files: cmd/* *.mod *.sum Makefile internal/common/*.go internal/providers/outscale_oapi/* docs/providers.sh internal/providers/provider_example/* internal/providers/s3/* internal/providers/fs/* .github/* .goreleaser.yml version .gitignore
Copyright: 2021 Outscale SAS <[email protected]>
License: BSD-3-Clause
7 changes: 6 additions & 1 deletion cmd/frieza/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

. "github.com/outscale-dev/frieza/internal/common"
"github.com/outscale-dev/frieza/internal/providers/fs"
"github.com/outscale-dev/frieza/internal/providers/outscale_oapi"
"github.com/outscale-dev/frieza/internal/providers/s3"
"github.com/teris-io/cli"
Expand All @@ -17,18 +18,22 @@ func ProviderNew(profile Profile) (Provider, error) {
// return provider_example.New(profile.Config, Debug)
case s3.Name:
return s3.New(profile.Config, GlobalCliOptions.debug)
case fs.Name:
return fs.New(profile.Config, GlobalCliOptions.debug)
}
return nil, fmt.Errorf("Provider %s not found", profile.Provider)
return nil, fmt.Errorf("provider %s not found", profile.Provider)
}

var providersCli = []func() (string, cli.Command){
outscale_oapi.Cli,
//provider_example.Cli,
s3.Cli,
fs.Cli,
}

var providersTypes = map[string][]ObjectType{
outscale_oapi.Name: outscale_oapi.Types(),
//provider_example.Name: provider_example.Types(),
s3.Name: s3.Types(),
fs.Name: fs.Types(),
}
22 changes: 0 additions & 22 deletions docs/providers.md
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@
# Providers and supported objects

## s3
- object
- bucket

## outscale_oapi
- vm
- load_balancer
- nat_service
- security_group
- public_ip
- volume
- keypair
- route_table
- internet_service
- subnet
- net
- image
- snapshot
- vpn_connection
- virtual_gateway
- nic
122 changes: 122 additions & 0 deletions internal/providers/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package fs

import (
"errors"
"fmt"
"log"
"os"
"path"

. "github.com/outscale-dev/frieza/internal/common"
"github.com/teris-io/cli"
)

const Name = "fs"
const typeFile = "file"

type FileSystem struct {
Path string
}

func checkConfig(config ProviderConfig) error {
if len(config["path"]) == 0 {
return errors.New("path is needed")
}
return nil
}

func New(config ProviderConfig, debug bool) (*FileSystem, error) {
if err := checkConfig(config); err != nil {
return nil, err
}
return &FileSystem{
Path: config["path"],
}, nil
}

func Types() []ObjectType {
object_types := []ObjectType{typeFile}
return object_types
}

func Cli() (string, cli.Command) {
return Name, cli.NewCommand(Name, "create new file system profile").
WithOption(cli.NewOption("path", "folder path"))
}

func (provider *FileSystem) Name() string {
return Name
}

func (provider *FileSystem) Types() []ObjectType {
return Types()
}

func (provider *FileSystem) AuthTest() error {
// Will test if we can access the folder
if _, err := os.ReadDir(provider.Path); err != nil {
return fmt.Errorf("cannot move to directory: %s", err.Error())
}
return nil
}

func (provider *FileSystem) ReadObjects(typeName string) []Object {
switch typeName {
case typeFile:
return provider.readFiles()
}
return []Object{}
}

func (provider *FileSystem) DeleteObjects(typeName string, objects []Object) {
switch typeName {
case typeFile:
provider.deleteFiles(objects)
}
}

func (provider *FileSystem) StringObject(object string, typeName string) string {
return object
}

func (provider *FileSystem) readFiles() []Object {
files := make([]Object, 0)

if err := os.Chdir(provider.Path); err != nil {
log.Printf("cannot move to directory: %s", err.Error())
return files
}

folderStack := []string{"."}
for len(folderStack) > 0 {
dirPath := folderStack[len(folderStack)-1]
folderStack = folderStack[:len(folderStack)-1]
dir, err := os.ReadDir(dirPath)
if err != nil {
log.Printf("cannot read directory: %s", err.Error())
continue
}
for _, node := range dir {
nodePath := path.Join(dirPath, node.Name())
if node.IsDir() {
folderStack = append(folderStack, nodePath)
}
if node.Type().IsRegular() {
files = append(files, nodePath)
}
}
}
return files
}

func (provider *FileSystem) deleteFiles(files []Object) {
for _, relativeFilePath := range files {
filePath := path.Join(provider.Path, relativeFilePath)
log.Printf("Deleting file %s ... ", filePath)
if err := os.Remove(filePath); err != nil {
log.Printf("cannot remove file %s\n", err.Error())
continue
}
log.Println("OK")
}
}

0 comments on commit 0f6031e

Please sign in to comment.