Skip to content

Commit

Permalink
Add validation for dataset type (#501)
Browse files Browse the repository at this point in the history
The valid dataset types at the moment are logs, metrics and events. events will be removed in the future but at the moment it is still used in the endpoint package. As soon as this is removed, the code here will be cleaned up.

With recent changes to the code, the datasets were not validated anymore during build or running the registry. This is now changed by adding validate for datasets every time a package is created.
  • Loading branch information
ruflin authored Jun 10, 2020
1 parent 7ca837b commit 314263e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
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. [#501](https://github.com/elastic/package-registry/pull/501)

### 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]string{
"logs": "Logs",
"metrics": "Metrics",
// TODO: Remove as soon as endpoint package does not use it anymore
"events": "Events",
}

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 {
_, 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() 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

0 comments on commit 314263e

Please sign in to comment.