Skip to content

Commit

Permalink
✨ Web UI schema validations (#934)
Browse files Browse the repository at this point in the history
* Allow validator to receive a config-schema

It will try to process a URL first, otherwise a file and if the file
is not present it will try to read the source as a cloud-config

Signed-off-by: Mauro Morales <[email protected]>

* Validate cloud-config on WebUI

Signed-off-by: Mauro Morales <[email protected]>

---------

Signed-off-by: Mauro Morales <[email protected]>
  • Loading branch information
mauromorales authored Feb 22, 2023
1 parent 9a99b19 commit 498b8ec
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
17 changes: 11 additions & 6 deletions internal/agent/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func JSONSchema(version string) (string, error) {
}

// Validate ensures that a given schema is Valid according to the Root Schema from the agent.
func Validate(file string) error {
func Validate(source string) error {
var yaml string

if strings.HasPrefix(file, "http") {
resp, err := http.Get(file)
if strings.HasPrefix(source, "http") {
resp, err := http.Get(source)
if err != nil {
return err
}
Expand All @@ -38,11 +38,16 @@ func Validate(file string) error {
//Convert the body to type string
yaml = string(body)
} else {
dat, err := os.ReadFile(file)
dat, err := os.ReadFile(source)
if err != nil {
return err
if strings.Contains(err.Error(), "no such file or directory") {
yaml = source
} else {
return err
}
} else {
yaml = string(dat)
}
yaml = string(dat)
}

config, err := schema.NewConfigFromYAML(yaml, schema.RootSchema{})
Expand Down
17 changes: 16 additions & 1 deletion internal/webui/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,22 @@ <h1>Welcome to the Installer!</h1>
try {
// Parse the YAML
const yaml = YAML.parse(this.content);
this.valid=true
const formData = new FormData()
formData.append('cloud-config', this.content)
const settings = {
method: 'POST',
body: formData,
};
fetch(`/validate`, settings)
.then((response) => response.text())
.then((text) => {
if (text === '') {
this.valid = true
} else {
this.error = text
this.valid = false
}
})
} catch(error) {
this.error = error
this.valid = false
Expand Down
15 changes: 15 additions & 0 deletions internal/webui/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ func Start(ctx context.Context) error {

ec.GET("/*", echo.WrapHandler(http.StripPrefix("/", assetHandler)))

ec.POST("/validate", func(c echo.Context) error {
formData := new(FormData)
if err := c.Bind(formData); err != nil {
return err
}
cloudConfig := formData.CloudConfig

err := agent.Validate(cloudConfig)
if err != nil {
return c.String(http.StatusOK, err.Error())
}

return c.String(http.StatusOK, "")
})

ec.POST("/install", func(c echo.Context) error {

s.Lock()
Expand Down

0 comments on commit 498b8ec

Please sign in to comment.