sparkles: Web UI schema validations (#934)

* 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 <mauro.morales@spectrocloud.com>

* Validate cloud-config on WebUI

Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>

---------

Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>
This commit is contained in:
Mauro Morales
2023-02-22 08:54:45 +01:00
committed by Itxaka
parent 461516fec5
commit 0cf3da5e06
3 changed files with 42 additions and 7 deletions

View File

@@ -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
}
@@ -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{})