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

Flatten checks catalog ouptut #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 12 additions & 14 deletions runner/ansible/roles/post-metadata/tasks/store.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
- name: Set metadata {{ id }}
set_fact:
metadata: |
{{ (metadata|default({})) | combine(
{ provider | default('azure'):
{ 'checks': [{
'id': id|string,
'name': name,
'group': group,
'description': description,
'remediation': remediation,
'labels': labels,
'implementation': implementation,
'premium': metadata_vars.premium|default(False)
}]
},
},recursive=True, list_merge='append')
{{ (metadata|default([])) +
[{
'id': id|string,
'name': name,
'provider': provider,
'group': group,
'description': description,
'remediation': remediation,
'labels': labels,
'implementation': implementation,
'premium': metadata_vars.premium|default(False)
}]
}}
7 changes: 3 additions & 4 deletions runner/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ const (
CatalogDestinationFile = "ansible/catalog.json"
)

type Catalog struct {
Checks []*CatalogCheck `json:"checks" binding:"required"`
}
type Catalog []*CatalogCheck

type CatalogCheck struct {
ID string `json:"id,omitempty" binding:"required"`
Name string `json:"name,omitempty" binding:"required"`
Group string `json:"group,omitempty" binding:"required"`
Group string `json:"group" binding:"required"`
Provider string `json:"provider" binding:"required"`
Description string `json:"description,omitempty"`
Remediation string `json:"remediation,omitempty"`
Implementation string `json:"implementation,omitempty"`
Expand Down
25 changes: 11 additions & 14 deletions runner/catalog_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ func (suite *CatalogApiTestCase) SetupTest() {
}

func (suite *CatalogApiTestCase) Test_GetCatalogTest() {
returnedCatalog := map[string]*Catalog{
"azure": &Catalog{
Checks: []*CatalogCheck{
{
ID: "156F64",
Name: "1.1.1",
Group: "Corosync",
Description: "description azure",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
},
returnedCatalog := &Catalog{
&CatalogCheck{
ID: "156F64",
Name: "1.1.1",
Group: "Corosync",
Provider: "azure",
Description: "description azure",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
}

Expand Down
12 changes: 6 additions & 6 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
type RunnerService interface {
IsCatalogReady() bool
BuildCatalog() error
GetCatalog() map[string]*Catalog
GetCatalog() *Catalog
GetChannel() chan *ExecutionEvent
ScheduleExecution(e *ExecutionEvent) error
Execute(e *ExecutionEvent) error
Expand All @@ -41,7 +41,7 @@ type runnerService struct {
config *Config
workerPoolChannel chan *ExecutionEvent
callbacksClient CallbacksClient
catalog map[string]*Catalog
catalog *Catalog
ready bool
}

Expand Down Expand Up @@ -77,13 +77,13 @@ func (c *runnerService) BuildCatalog() error {
}

// After the playbook is done, recover back the file content
catalogRaw, err := ioutil.ReadFile(metaRunner.Envs[CatalogDestination])
catalogFile, err := ioutil.ReadFile(metaRunner.Envs[CatalogDestination])
if err != nil {
log.Fatal("Error when opening the catalog file: ", err)
}

var catalog map[string]*Catalog
err = json.Unmarshal(catalogRaw, &catalog)
var catalog *Catalog
err = json.Unmarshal(catalogFile, &catalog)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
Expand All @@ -94,7 +94,7 @@ func (c *runnerService) BuildCatalog() error {
return nil
}

func (c *runnerService) GetCatalog() map[string]*Catalog {
func (c *runnerService) GetCatalog() *Catalog {
return c.catalog
}

Expand Down
8 changes: 4 additions & 4 deletions runner/runner_mock.go

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

50 changes: 22 additions & 28 deletions runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,34 @@ func (suite *RunnerTestCase) Test_BuildCatalog() {

err := suite.runnerService.BuildCatalog()

expectedMap := map[string]*Catalog{
"azure": &Catalog{
Checks: []*CatalogCheck{
{
ID: "156F64",
Name: "1.1.1",
Group: "Corosync",
Description: "description azure",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
},
expectedCatalog := &Catalog{
&CatalogCheck{
ID: "156F64",
Name: "1.1.1",
Group: "Corosync",
Provider: "azure",
Description: "description azure",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
"dev": &Catalog{
Checks: []*CatalogCheck{
{
ID: "156F64",
Name: "1.1.1",
Group: "Corosync",
Description: "description dev",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
},
&CatalogCheck{
ID: "156F64",
Name: "1.1.1",
Group: "Corosync",
Provider: "dev",
Description: "description dev",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
}

suite.NoError(err)
suite.Equal(true, suite.runnerService.IsCatalogReady())
suite.Equal(expectedMap, suite.runnerService.GetCatalog())
suite.Equal(expectedCatalog, suite.runnerService.GetCatalog())
}

func (suite *RunnerTestCase) Test_ScheduleExecution() {
Expand Down
48 changes: 20 additions & 28 deletions test/fixtures/catalog.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
{
"azure":
[
{
"checks": [
{
"group": "Corosync",
"name": "1.1.1",
"implementation": "implementation",
"labels": "generic",
"remediation": "remediation",
"premium": false,
"id": "156F64",
"description": "description azure"
}
]
"group": "Corosync",
"provider": "azure",
"name": "1.1.1",
"implementation": "implementation",
"labels": "generic",
"remediation": "remediation",
"premium": false,
"id": "156F64",
"description": "description azure"
},
"dev":
{
"checks": [
{
"group": "Corosync",
"name": "1.1.1",
"implementation": "implementation",
"labels": "generic",
"remediation": "remediation",
"premium": false,
"id": "156F64",
"description": "description dev"
}
]
"group": "Corosync",
"provider": "dev",
"name": "1.1.1",
"implementation": "implementation",
"labels": "generic",
"remediation": "remediation",
"premium": false,
"id": "156F64",
"description": "description dev"
}
}
]