Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for dataset type #501

Merged
merged 2 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Generate index.json file. [#470](https://github.com/elastic/package-registry/pull/470)
* Stream archived package content. [#472](https://github.com/elastic/package-registry/pull/472)
* Generate package index.json files. [#479](https://github.com/elastic/package-registry/pull/479)
* Add validation for dataset type. [#](https://github.com/elastic/package-registry/pull/)
ruflin marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

Expand Down
16 changes: 16 additions & 0 deletions util/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const (
DirIngestPipeline = "ingest-pipeline"
)

var ValidTypes = map[string]interface{}{
ruflin marked this conversation as resolved.
Show resolved Hide resolved
"logs": nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might cause problems in the future if someone would like to check if the value logs exists. They would get a nil value :) Maybe bool instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went an other approach and made the second part a String which is something we could also show in a UI if needed. Thinking of the case that someday we might have a /types endpoint if someone wants to know all the available dataset types.

"metrics": nil,
// TODO: Remove as soon as endpoint package does not use it anymore
"events": nil,
}

type DataSet struct {
ID string `config:"id" json:"id,omitempty" yaml:"id,omitempty"`
Title string `config:"title" json:"title" validate:"required"`
Expand Down Expand Up @@ -140,6 +147,10 @@ func (d *DataSet) Validate() error {
return fmt.Errorf("dataset name is not allowed to contain `-`: %s", d.ID)
}

if !d.ValidType() {
return fmt.Errorf("type is not valid: %s", d.Type)
}

if d.IngestPipeline == "" {
// Check that no ingest pipeline exists in the directory except default
for _, path := range paths {
Expand Down Expand Up @@ -191,6 +202,11 @@ func (d *DataSet) Validate() error {
return nil
}

func (d *DataSet) ValidType() bool {
ruflin marked this conversation as resolved.
Show resolved Hide resolved
_, exists := ValidTypes[d.Type]
return exists
}

func validateIngestPipelineFile(pipelinePath string) error {
f, err := ioutil.ReadFile(pipelinePath)
if err != nil {
Expand Down
24 changes: 23 additions & 1 deletion util/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (p *Package) Validate() error {
}
}

return nil
return p.ValidateDatasets()
}

// GetDatasetPaths returns a list with the dataset paths inside this package
Expand Down Expand Up @@ -396,6 +396,28 @@ func (p *Package) LoadDataSets(packagePath string) error {
return nil
}

// ValidateDatasets loads all datasets and with it validates them
func (p *Package) ValidateDatasets() error {

datasetPaths, err := p.GetDatasetPaths()
if err != nil {
return err
}

datasetsBasePath := filepath.Join(p.BasePath, "dataset")

for _, datasetPath := range datasetPaths {

datasetBasePath := filepath.Join(datasetsBasePath, datasetPath)

_, err := NewDataset(datasetBasePath, p)
if err != nil {
return err
}
}
return nil
}

func (p *Package) GetPath() string {
return p.Name + "/" + p.Version
}
Expand Down
1 change: 1 addition & 0 deletions util/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func GetPackages(packagesBasePath string) ([]Package, error) {
if err != nil {
return nil, err
}

packageList = append(packageList, *p)
}

Expand Down