-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
1481cd2
commit 0f6031e
Showing
4 changed files
with
129 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |