diff --git a/server-go/collections/action.go b/server-go/collections/action.go deleted file mode 100644 index 0224625c..00000000 --- a/server-go/collections/action.go +++ /dev/null @@ -1 +0,0 @@ -package collections diff --git a/server-go/collections/repository.go b/server-go/collections/repository.go index 0224625c..c9928eb1 100644 --- a/server-go/collections/repository.go +++ b/server-go/collections/repository.go @@ -1 +1,32 @@ package collections + +import ( + "encoding/json" + "errors" + "fmt" + "github.com/riotkit-org/backup-repository/config" + "github.com/sirupsen/logrus" +) + +const KindCollection = "backupcollections" + +type collectionRepository struct { + config config.ConfigurationProvider +} + +// getById Returns a `kind: BackupCollection` object by it's `metadata.name` +func (c *collectionRepository) getById(id string) (*Collection, error) { + doc, retrieveErr := c.config.GetSingleDocument(KindCollection, id) + result := Collection{} + + if retrieveErr != nil { + return &result, errors.New(fmt.Sprintf("error retrieving result: %v", retrieveErr)) + } + + if err := json.Unmarshal([]byte(doc), &result); err != nil { + logrus.Debugln(doc) + return &Collection{}, errors.New(fmt.Sprintf("cannot unmarshal response fron Kubernetes to get collection of id=%v", id)) + } + + return &result, nil +} diff --git a/server-go/collections/service.go b/server-go/collections/service.go new file mode 100644 index 00000000..14ed45a0 --- /dev/null +++ b/server-go/collections/service.go @@ -0,0 +1,19 @@ +package collections + +import "github.com/riotkit-org/backup-repository/config" + +type Service struct { + repository collectionRepository +} + +func (s *Service) GetCollectionById(id string) (*Collection, error) { + return s.repository.getById(id) +} + +func NewService(config config.ConfigurationProvider) Service { + return Service{ + repository: collectionRepository{ + config: config, + }, + } +} diff --git a/server-go/core/ctx.go b/server-go/core/ctx.go index 12be3068..62cb7f54 100644 --- a/server-go/core/ctx.go +++ b/server-go/core/ctx.go @@ -1,6 +1,7 @@ package core import ( + "github.com/riotkit-org/backup-repository/collections" "github.com/riotkit-org/backup-repository/config" "github.com/riotkit-org/backup-repository/security" "github.com/riotkit-org/backup-repository/users" @@ -10,5 +11,6 @@ type ApplicationContainer struct { Config *config.ConfigurationProvider Users *users.Service GrantedAccesses *security.Service + Collections *collections.Service JwtSecretKey string } diff --git a/server-go/http/collection.go b/server-go/http/collection.go index d02cfda6..6f98dbdd 100644 --- a/server-go/http/collection.go +++ b/server-go/http/collection.go @@ -1 +1,28 @@ package http + +import ( + "errors" + "github.com/gin-gonic/gin" + "github.com/riotkit-org/backup-repository/core" +) + +func addUploadRoute(r *gin.RouterGroup, ctx *core.ApplicationContainer) { + r.POST("/repository/collection/:collectionId/version", func(c *gin.Context) { + // todo: check if collection exists + // todo: check if backup window is OK + // todo: check if rotation strategy allows uploading + // todo: deactivate token if temporary token is used + // todo: handle upload + // todo: check uploaded file size, respect quotas and additional space + // todo: check if there are gpg header and footer + // todo: handle upload interruptions + + collection, err := ctx.Collections.GetCollectionById(c.Param("collectionId")) + if err != nil { + NotFoundResponse(c, errors.New("cannot find specified collection")) + return + } + + println(collection) + }) +} diff --git a/server-go/http/main.go b/server-go/http/main.go index dce83ba5..c0ad2b8e 100644 --- a/server-go/http/main.go +++ b/server-go/http/main.go @@ -29,6 +29,7 @@ func SpawnHttpApplication(ctx *core.ApplicationContainer) { addWhoamiRoute(router, ctx) addLogoutRoute(router, ctx) addGrantedAccessSearchRoute(router, ctx) + addUploadRoute(router, ctx) } _ = r.Run() diff --git a/server-go/main.go b/server-go/main.go index 4b97bb74..9480d576 100644 --- a/server-go/main.go +++ b/server-go/main.go @@ -2,6 +2,7 @@ package main import ( "github.com/jessevdk/go-flags" + "github.com/riotkit-org/backup-repository/collections" "github.com/riotkit-org/backup-repository/config" "github.com/riotkit-org/backup-repository/core" "github.com/riotkit-org/backup-repository/db" @@ -66,12 +67,14 @@ func main() { usersService := users.NewUsersService(configProvider) gaService := security.NewService(dbDriver) + collectionsService := collections.NewService(configProvider) ctx := core.ApplicationContainer{ Config: &configProvider, Users: &usersService, GrantedAccesses: &gaService, JwtSecretKey: opts.JwtSecretKey, + Collections: &collectionsService, } // todo: First thread - HTTP