Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat(api): Process import package manifest and execute API tasks
Browse files Browse the repository at this point in the history
* feat(api): create entry point for import package processing
* feat(api): introduce model along with basic unmarshaling for import package manifest processing
* feat(api): wire up import handler and processor
* feat(api): Api task executor first impl
* feat(api): implement getResource() for import ZippedPackage
* feat(api): wire up the import "create project" api task

Signed-off-by: Paolo Chila <paolo.chila@dynatrace.com>
  • Loading branch information
pchila committed Jul 8, 2022
1 parent 7a01bd0 commit 74744aa
Show file tree
Hide file tree
Showing 33 changed files with 2,740 additions and 155 deletions.
62 changes: 62 additions & 0 deletions api/handlers/fake/project_checker_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 137 additions & 0 deletions api/handlers/fake/projectchecker_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 29 additions & 7 deletions api/handlers/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,52 @@ import (

const defaultImportArchiveExtension = ".zip"

//go:generate moq -pkg fake --skip-ensure -out ./fake/projectchecker_mock.go . projectChecker:ProjectCheckerMock
//go:generate moq -pkg handlers_mock --skip-ensure -out ./fake/projectchecker_mock.go . projectChecker:ProjectCheckerMock importPackageProcessor:MockImportPackageProcessor

type projectChecker interface {
ProjectExists(projectName string) (bool, error)
}

// ParseArchiveFunction is the function called to parse the uploaded file
type ParseArchiveFunction func(string, uint64) (*importer.ZippedPackage, error)
type importPackageProcessor interface {
Process(project string, ip importer.ImportPackage) error
}

// parseArchiveFunction is the function called to parse the uploaded file
type parseArchiveFunction func(string, uint64) (*ZippedPackage, error)

// ImportHandler is the rest handler for the /import endpoint
type ImportHandler struct {
checker projectChecker
tempStorageDir string
maxUncompressedPackageSize uint64
parseArchive ParseArchiveFunction
parseArchive parseArchiveFunction
processor importPackageProcessor
}

// GetImportHandlerFunc will instantiate a configured ImportHandler and return the method that can be used for
// handling http requests to the endpoint. See restapi.configureAPI for usage
func GetImportHandlerFunc(storagePath string, checker projectChecker, maxPackageSize uint64) func(
func GetImportHandlerFunc(
storagePath string, checker projectChecker, maxPackageSize uint64, processor importPackageProcessor,
) func(
params import_operations.ImportParams, principal *models.Principal,
) middleware.Responder {
ih := getImportHandlerInstance(storagePath, checker, maxPackageSize, importer.NewPackage)
ih := getImportHandlerInstance(
storagePath, checker, maxPackageSize, NewZippedPackage,
processor,
)
return ih.HandleImport
}

func getImportHandlerInstance(
storagePath string, checker projectChecker, maxPackageSize uint64,
parserFunction ParseArchiveFunction,
parserFunction parseArchiveFunction, processor importPackageProcessor,
) *ImportHandler {
return &ImportHandler{
checker: checker,
tempStorageDir: storagePath,
maxUncompressedPackageSize: maxPackageSize,
parseArchive: parserFunction,
processor: processor,
}
}

Expand Down Expand Up @@ -138,5 +149,16 @@ func (ih *ImportHandler) HandleImport(
}
}()

err = ih.processor.Process(params.Project, m)
if err != nil {
logger.Errorf("Error processing import archive: %v", err)
message := fmt.Sprintf("Error processing import archive: %s", err)
mError := models.Error{
Code: http.StatusBadRequest,
Message: &message,
}
return import_operations.NewImportBadRequest().WithPayload(&mError)
}

return import_operations.NewImportOK()
}
Loading

0 comments on commit 74744aa

Please sign in to comment.