Skip to content

Commit 056935b

Browse files
Merge pull request #19 from fabi200123/generate-jsonschema
Add json-schema generator
2 parents e716669 + af0a0f4 commit 056935b

43 files changed

Lines changed: 6528 additions & 160 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ toolchain go1.22.3
66

77
require (
88
github.com/BurntSushi/toml v1.2.1
9-
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae
9+
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce
1010
github.com/google/uuid v1.6.0
1111
github.com/gophercloud/gophercloud v1.11.0
1212
github.com/gophercloud/utils v0.0.0-20230324070755-05e9e7f5ea4d
13+
github.com/invopop/jsonschema v0.12.0
1314
github.com/stretchr/testify v1.9.0
1415
gopkg.in/yaml.v2 v2.4.0
1516
)
1617

17-
require github.com/stretchr/objx v0.5.2 // indirect
18+
require (
19+
github.com/bahlo/generic-list-go v0.2.0 // indirect
20+
github.com/buger/jsonparser v1.1.1 // indirect
21+
github.com/mailru/easyjson v0.7.7 // indirect
22+
github.com/stretchr/objx v0.5.2 // indirect
23+
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
24+
)
1825

1926
require (
2027
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
22
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
3-
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae h1:GDcVb/ForsCtpGj2kFH+iPu6/IIj0b5pV/h27qL6+GI=
4-
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae/go.mod h1:sK26i2NpjjAjhanNKiWw8iPkqt+XeohTKpFnEP7JdZ4=
3+
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
4+
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
5+
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
6+
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
7+
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce h1:spSF26dB6llkdyEeVPDdobdbv2z09zL10MUJG/YYkoE=
8+
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce/go.mod h1:sK26i2NpjjAjhanNKiWw8iPkqt+XeohTKpFnEP7JdZ4=
59
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
610
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
711
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -17,6 +21,11 @@ github.com/gophercloud/utils v0.0.0-20230324070755-05e9e7f5ea4d/go.mod h1:z4Dey7
1721
github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
1822
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
1923
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
24+
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
25+
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
26+
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
27+
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
28+
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
2029
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
2130
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
2231
github.com/minio/sio v0.4.0 h1:u4SWVEm5lXSqU42ZWawV0D9I5AZ5YMmo2RXpEQ/kRhc=
@@ -35,6 +44,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
3544
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3645
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
3746
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
47+
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
48+
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
3849
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
3950
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
4051
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=

provider/spec.go

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
2727
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
2828
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
29+
"github.com/invopop/jsonschema"
2930
"github.com/xeipuuv/gojsonschema"
3031

3132
"github.com/cloudbase/garm-provider-openstack/config"
@@ -42,104 +43,35 @@ var (
4243
DefaultGetCloudconfig GetCloudConfigFunc = cloudconfig.GetCloudConfig
4344
)
4445

45-
const jsonSchema string = `
46-
47-
{
48-
"$schema": "http://cloudbase.it/garm-provider-openstack/schemas/extra_specs#",
49-
"type": "object",
50-
"description": "Schema defining supported extra specs for the Garm OpenStack Provider",
51-
"properties": {
52-
"security_groups": {
53-
"type": "array",
54-
"items": {
55-
"type": "string"
56-
}
57-
},
58-
"network_id": {
59-
"type": "string",
60-
"description": "The tenant network to which runners will be connected to."
61-
},
62-
"storage_backend": {
63-
"type": "string",
64-
"description": "The cinder backend to use when creating volumes."
65-
},
66-
"boot_from_volume": {
67-
"type": "boolean",
68-
"description": "Whether to boot from volume or not. Use this option if the root disk size defined by the flavor is not enough."
69-
},
70-
"boot_disk_size": {
71-
"type": "integer",
72-
"description": "The size of the root disk in GB. Default is 50 GB."
73-
},
74-
"use_config_drive": {
75-
"type": "boolean",
76-
"description": "Use config drive."
77-
},
78-
"enable_boot_debug": {
79-
"type": "boolean",
80-
"description": "Enable cloud-init debug mode. Adds 'set -x' into the cloud-init script."
81-
},
82-
"allowed_image_owners": {
83-
"type": "array",
84-
"items": {
85-
"type": "string"
86-
},
87-
"description": "A list of image owners to allow when creating the instance. If not specified, all images will be allowed."
88-
},
89-
"image_visibility": {
90-
"type": "string",
91-
"description": "The visibility of the image to use."
92-
},
93-
"disable_updates": {
94-
"type": "boolean",
95-
"description": "Disable automatic updates on the VM."
96-
},
97-
"extra_packages": {
98-
"type": "array",
99-
"description": "Extra packages to install on the VM.",
100-
"items": {
101-
"type": "string"
102-
}
103-
},
104-
"runner_install_template": {
105-
"type": "string",
106-
"description": "This option can be used to override the default runner install template. If used, the caller is responsible for the correctness of the template as well as the suitability of the template for the target OS. Use the extra_context extra spec if your template has variables in it that need to be expanded."
107-
},
108-
"extra_context": {
109-
"type": "object",
110-
"description": "Extra context that will be passed to the runner_install_template.",
111-
"additionalProperties": {
112-
"type": "string"
113-
}
114-
},
115-
"pre_install_scripts": {
116-
"type": "object",
117-
"description": "A map of pre-install scripts that will be run before the runner install script. These will run as root and can be used to prep a generic image before we attempt to install the runner. The key of the map is the name of the script as it will be written to disk. The value is a byte array with the contents of the script.",
118-
"additionalProperties": {
119-
"type": "string"
120-
}
121-
}
122-
},
123-
"additionalProperties": false
124-
}
125-
`
126-
12746
type extraSpecs struct {
12847
SecurityGroups []string `json:"security_groups,omitempty"`
129-
AllowedImageOwners []string `json:"allowed_image_owners,omitempty"`
130-
ImageVisibility string `json:"image_visibility,omitempty"`
131-
NetworkID string `json:"network_id"`
132-
StorageBackend string `json:"storage_backend,omitempty"`
133-
BootFromVolume *bool `json:"boot_from_volume,omitempty"`
134-
BootDiskSize *int64 `json:"boot_disk_size,omitempty"`
135-
UseConfigDrive *bool `json:"use_config_drive"`
136-
EnableBootDebug *bool `json:"enable_boot_debug"`
137-
DisableUpdates *bool `json:"disable_updates"`
138-
ExtraPackages []string `json:"extra_packages"`
48+
AllowedImageOwners []string `json:"allowed_image_owners,omitempty" jsonschema:"description=A list of image owners to allow when creating the instance. If not specified, all images will be allowed."`
49+
ImageVisibility string `json:"image_visibility,omitempty" jsonschema:"description=The visibility of the image to use."`
50+
NetworkID string `json:"network_id,omitempty" jsonschema:"description=The tenant network to which runners will be connected to."`
51+
StorageBackend string `json:"storage_backend,omitempty" jsonschema:"description=The cinder backend to use when creating volumes."`
52+
BootFromVolume *bool `json:"boot_from_volume,omitempty" jsonschema:"description=Whether to boot from volume or not. Use this option if the root disk size defined by the flavor is not enough."`
53+
BootDiskSize *int64 `json:"boot_disk_size,omitempty" jsonschema:"description=The size of the root disk in GB. Default is 50 GB."`
54+
UseConfigDrive *bool `json:"use_config_drive,omitempty" jsonschema:"description=Use config drive."`
55+
EnableBootDebug *bool `json:"enable_boot_debug,omitempty" jsonschema:"description=Enable cloud-init debug mode. Adds 'set -x' into the cloud-init script."`
56+
DisableUpdates *bool `json:"disable_updates,omitempty" jsonschema:"description=Disable automatic updates on the VM."`
57+
ExtraPackages []string `json:"extra_packages,omitempty" jsonschema:"description=Extra packages to install on the VM."`
58+
// The Cloudconfig struct from common package
59+
cloudconfig.CloudConfigSpec
60+
}
61+
62+
func generateJSONSchema() *jsonschema.Schema {
63+
reflector := jsonschema.Reflector{
64+
AllowAdditionalProperties: false,
65+
}
66+
// Reflect the extraSpecs struct
67+
schema := reflector.Reflect(extraSpecs{})
68+
69+
return schema
13970
}
14071

14172
func jsonSchemaValidation(schema json.RawMessage) error {
142-
schemaLoader := gojsonschema.NewStringLoader(jsonSchema)
73+
jsonSchema := generateJSONSchema()
74+
schemaLoader := gojsonschema.NewGoLoader(jsonSchema)
14375
extraSpecsLoader := gojsonschema.NewBytesLoader(schema)
14476
result, err := gojsonschema.Validate(schemaLoader, extraSpecsLoader)
14577
if err != nil {

0 commit comments

Comments
 (0)