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

Build checks catalog #3

Merged
merged 9 commits into from
Mar 18, 2022
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
20 changes: 11 additions & 9 deletions runner/ansible/meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
gather_facts: false

tasks:
- name: Include load_facts
import_role:
name: load_facts
- name: Find providers
find:
paths: "{{ playbook_dir }}/vars"
file_type: directory
register: providers

- name: Find checks
find:
paths: "{{ playbook_dir }}/roles/checks"
file_type: directory
register: checks

- name: Store metadata
- name: Build catalog data
include_role:
name: post-metadata
tasks_from: store
tasks_from: build_catalog
vars:
metadata_path: "{{ item.path }}/defaults/main.yml"
loop: "{{ checks.files|sort(attribute='path') }}"
provider: "{{ item.path | basename }}"
loop: "{{ providers.files|sort(attribute='path') }}"

- name: Post metadata
- name: Dump metadata
import_role:
name: post-metadata
tasks_from: post
tasks_from: dump
2 changes: 1 addition & 1 deletion runner/ansible/roles/load_facts/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- name: load environment variables
include_vars:
dir: "{{ playbook_dir }}/vars/{{ env | default('azure') }}"
dir: "{{ playbook_dir }}/vars/{{ provider | default('azure') }}"
delegate_to: localhost
run_once: true

Expand Down
13 changes: 13 additions & 0 deletions runner/ansible/roles/post-metadata/tasks/build_catalog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- name: Include load_facts
import_role:
name: load_facts

- name: Store metadata
include_role:
name: post-metadata
tasks_from: store
vars:
metadata_path: "{{ check_file.path }}/defaults/main.yml"
loop: "{{ checks.files|sort(attribute='path') }}"
loop_control:
loop_var: check_file
5 changes: 5 additions & 0 deletions runner/ansible/roles/post-metadata/tasks/dump.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: Dump catalog data fo a file
copy:
dest: '{{ lookup("env", "CATALOG_DESTINATION") }}'
content: "{{ metadata | to_json }}"
mode: '0644'
14 changes: 0 additions & 14 deletions runner/ansible/roles/post-metadata/tasks/post.yml

This file was deleted.

24 changes: 13 additions & 11 deletions runner/ansible/roles/post-metadata/tasks/store.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
set_fact:
metadata: |
{{ (metadata|default({})) | combine(
{'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')
{ 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')
}}
5 changes: 5 additions & 0 deletions runner/ansiblerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

const (
CatalogDestination = "CATALOG_DESTINATION"
TrentoWebApiHost = "TRENTO_WEB_API_HOST"
TrentoWebApiPort = "TRENTO_WEB_API_PORT"
AnsibleConfigFileEnv = "ANSIBLE_CONFIG"
Expand Down Expand Up @@ -64,6 +65,10 @@ func (a *AnsibleRunner) SetConfigFile(confFile string) {
a.setEnv(AnsibleConfigFileEnv, confFile)
}

func (a *AnsibleRunner) SetCatalogDestination(destination string) {
a.setEnv(CatalogDestination, destination)
}

func (a *AnsibleRunner) SetTrentoApiData(host string, port int) {
a.setEnv(TrentoWebApiHost, host)
a.setEnv(TrentoWebApiPort, fmt.Sprintf("%d", port))
Expand Down
9 changes: 0 additions & 9 deletions runner/api.go

This file was deleted.

37 changes: 0 additions & 37 deletions runner/api_test.go

This file was deleted.

36 changes: 25 additions & 11 deletions runner/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,32 @@ type Config struct {
type App struct {
config *Config
webEngine *gin.Engine
runner *Runner
deps Dependencies
}

type Dependencies struct {
runnerService RunnerService
}

func DefaultDependencies(config *Config) Dependencies {
runnerService, err := NewRunnerService(config)
if err != nil {
log.Fatalf("Failed to create the runner instance: %s", err)
}

return Dependencies{
runnerService: runnerService,
}
}

func NewApp(config *Config) (*App, error) {
return NewAppWithDeps(config, DefaultDependencies(config))
}

func NewAppWithDeps(config *Config, deps Dependencies) (*App, error) {
app := &App{
config: config,
deps: deps,
}

engine := gin.New()
Expand All @@ -38,17 +58,11 @@ func NewApp(config *Config) (*App, error) {
mode := os.Getenv(gin.EnvGinMode)
gin.SetMode(mode)

runner_instance, err := NewRunner(config)
if err != nil {
log.Errorf("Failed to create the runner instance: %s", err)
return nil, err
}

app.runner = runner_instance

apiGroup := engine.Group("/api")
{
apiGroup.GET("/health", HealthHandler)
apiGroup.GET("/ready", ReadyHandler(deps.runnerService))
apiGroup.GET("/catalog", CatalogHandler(deps.runnerService))
}

app.webEngine = engine
Expand Down Expand Up @@ -77,9 +91,9 @@ func (a *App) Start(ctx context.Context) error {
return nil
})

log.Infof("Starting runner....")
log.Infof("Building catalog....")
g.Go(func() error {
err := a.runner.Start(ctx)
err := a.deps.runnerService.BuildCatalog()
if err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions runner/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package runner

const (
CatalogDestinationFile = "ansible/catalog.json"
)

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

type CatalogCheck struct {
ID string `json:"id,omitempty" binding:"required"`
Name string `json:"name,omitempty" binding:"required"`
Group string `json:"group,omitempty" binding:"required"`
Description string `json:"description,omitempty"`
Remediation string `json:"remediation,omitempty"`
Implementation string `json:"implementation,omitempty"`
Labels string `json:"labels,omitempty"`
Premium bool `json:"premium,omitempty"`
}
11 changes: 11 additions & 0 deletions runner/catalog_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package runner

import (
"github.com/gin-gonic/gin"
)

func CatalogHandler(runnerService RunnerService) gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(200, runnerService.GetCatalog())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This returns always 200 state, even though you request the catalog and it is not ready.
We can modify it of course

}
}
61 changes: 61 additions & 0 deletions runner/catalog_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package runner

import (
"encoding/json"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/suite"
)

type CatalogApiTestCase struct {
suite.Suite
config *Config
}

func TestCatalogApiTestCase(t *testing.T) {
suite.Run(t, new(CatalogApiTestCase))
}

func (suite *CatalogApiTestCase) SetupTest() {
suite.config = &Config{}
}

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,
},
},
},
}

mockRunnerService := new(MockRunnerService)
mockRunnerService.On("GetCatalog").Return(returnedCatalog)

deps := Dependencies{
mockRunnerService,
}

app, err := NewAppWithDeps(suite.config, deps)
if err != nil {
suite.T().Fatal(err)
}

resp := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/catalog", nil)
app.webEngine.ServeHTTP(resp, req)

expectedJson, _ := json.Marshal(returnedCatalog)
suite.Equal(200, resp.Code)
suite.JSONEq(string(expectedJson), resp.Body.String())
}
15 changes: 15 additions & 0 deletions runner/health_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package runner

import (
"github.com/gin-gonic/gin"
)

func HealthHandler(c *gin.Context) {
c.JSON(200, map[string]string{"status": "ok"})
}

func ReadyHandler(runnerService RunnerService) gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(200, map[string]bool{"ready": runnerService.IsCatalogReady()})
}
}
Loading