Skip to content

Commit

Permalink
Merge pull request #248 from carolynvs/enable-numbers
Browse files Browse the repository at this point in the history
Allow non-integer numbers in bundle.json
  • Loading branch information
carolynvs committed Jun 2, 2021
2 parents 20e825c + 7080152 commit 0082cac
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 268 deletions.
6 changes: 3 additions & 3 deletions action/example_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func Example_install() {
// },
// "files": {
// "/cnab/app/image-map.json": "{}",
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"install\",\"bundle\":{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"install\",\"bundle\":{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// },
// "outputs": {},
// "Bundle": {
// "schemaVersion": "1.0.1",
// "schemaVersion": "1.2.0",
// "name": "mybuns",
// "version": "1.0.0",
// "description": "",
Expand Down
6 changes: 3 additions & 3 deletions action/example_invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ func Example_invoke() {
// },
// "files": {
// "/cnab/app/image-map.json": "{}",
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"logs\",\"bundle\":{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"logs\",\"bundle\":{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// },
// "outputs": {},
// "Bundle": {
// "schemaVersion": "1.0.1",
// "schemaVersion": "1.2.0",
// "name": "mybuns",
// "version": "1.0.0",
// "description": "",
Expand Down
6 changes: 3 additions & 3 deletions action/example_running_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func Example_runningStatus() {
// },
// "files": {
// "/cnab/app/image-map.json": "{}",
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"upgrade\",\"bundle\":{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"upgrade\",\"bundle\":{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// },
// "outputs": {},
// "Bundle": {
// "schemaVersion": "1.0.1",
// "schemaVersion": "1.2.0",
// "name": "mybuns",
// "version": "1.0.0",
// "description": "",
Expand Down
6 changes: 3 additions & 3 deletions action/example_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func Example_upgrade() {
// },
// "files": {
// "/cnab/app/image-map.json": "{}",
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"upgrade\",\"bundle\":{\"schemaVersion\":\"1.0.1\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// "/cnab/bundle.json": "{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}",
// "/cnab/claim.json": "{\"schemaVersion\":\"1.0.0-DRAFT+b5ed2f3\",\"id\":\"claim-id\",\"installation\":\"hello\",\"revision\":\"claim-rev\",\"created\":\"2020-04-18T01:02:03.000000004Z\",\"action\":\"upgrade\",\"bundle\":{\"schemaVersion\":\"1.2.0\",\"name\":\"mybuns\",\"version\":\"1.0.0\",\"description\":\"\",\"invocationImages\":[{\"imageType\":\"docker\",\"image\":\"example.com/myorg/myinstaller\",\"contentDigest\":\"sha256:7cc0618539fe11e801ce68911a0c9441a3dfaa9ba63057526c4016cf9db19474\"}],\"actions\":{\"logs\":{}}}}"
// },
// "outputs": {},
// "Bundle": {
// "schemaVersion": "1.0.1",
// "schemaVersion": "1.2.0",
// "name": "mybuns",
// "version": "1.0.0",
// "description": "",
Expand Down
2 changes: 1 addition & 1 deletion bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// CNABSpecVersion represents the CNAB Spec version of the Bundle
// that this library implements
// This value is prefixed with e.g. `cnab-core-` so isn't itself valid semver.
var CNABSpecVersion string = "cnab-core-1.0.1"
var CNABSpecVersion string = "cnab-core-1.2.0"

// Bundle is a CNAB metadata document
type Bundle struct {
Expand Down
18 changes: 17 additions & 1 deletion bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ var exampleCred = Credential{
},
}

var exampleBundle = &Bundle{
var exampleBundle *Bundle = &Bundle{
SchemaVersion: "1.0.1",
Name: "testBundle",
Description: "something",
Expand Down Expand Up @@ -627,6 +627,12 @@ var exampleBundle = &Bundle{
"productKeyType": {
Type: "string",
},
"sizeType": {
Type: "number",
Default: 5.5,
Minimum: float64Ptr(3.5),
Maximum: float64Ptr(10.1),
},
},
Parameters: map[string]Parameter{
"port": {
Expand Down Expand Up @@ -662,6 +668,12 @@ var exampleBundle = &Bundle{
EnvironmentVariable: "PRODUCT_KEY",
},
},
"size": {
Definition: "sizeType",
Destination: &Location{
EnvironmentVariable: "SIZE",
},
},
},
Outputs: map[string]Output{
"clientCert": {
Expand All @@ -671,6 +683,10 @@ var exampleBundle = &Bundle{
},
}

func float64Ptr(value float64) *float64 {
return &value
}

func TestValidateExampleBundle(t *testing.T) {
err := exampleBundle.Validate()
require.NoError(t, err, "example bundle validation should succeed")
Expand Down
12 changes: 7 additions & 5 deletions bundle/definition/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ type Schema struct {
Else *Schema `json:"else,omitempty" yaml:"else,omitempty"`
Enum []interface{} `json:"enum,omitempty" yaml:"enum,omitempty"`
Examples []interface{} `json:"examples,omitempty" yaml:"examples,omitempty"`
ExclusiveMaximum *int `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"`
ExclusiveMinimum *int `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"`
ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"`
ExclusiveMinimum *float64 `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
If *Schema `json:"if,omitempty" yaml:"if,omitempty"`
//Items can be a Schema or an Array of Schema :(
Items interface{} `json:"items,omitempty" yaml:"items,omitempty"`
Maximum *int `json:"maximum,omitempty" yaml:"maximum,omitempty"`
Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"`
MaxLength *int `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
MinItems *int `json:"minItems,omitempty" yaml:"minItems,omitempty"`
MinLength *int `json:"minLength,omitempty" yaml:"minLength,omitempty"`
MinProperties *int `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
Minimum *int `json:"minimum,omitempty" yaml:"minimum,omitempty"`
MultipleOf *int `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
Not *Schema `json:"not,omitempty" yaml:"not,omitempty"`
OneOf *Schema `json:"oneOf,omitempty" yaml:"oneOf,omitempty"`

Expand Down Expand Up @@ -122,6 +122,8 @@ func (s *Schema) ConvertValue(val string) (interface{}, error) {
return val, nil
case "integer":
return strconv.Atoi(val)
case "number":
return strconv.ParseFloat(val, 64)
case "boolean":
switch strings.ToLower(val) {
case "true":
Expand Down
28 changes: 14 additions & 14 deletions bundle/definition/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,29 @@ func TestSimpleUnMarshalDefinition(t *testing.T) {
func TestSimpleSchema(t *testing.T) {
s := `
{
"default": 80,
"maximum": 10240,
"minimum": 10,
"type": "integer"
"default": 98.6,
"maximum": 10239.5,
"minimum": 0.1,
"type": "number"
}
`

definition := new(Schema)
err := json.Unmarshal([]byte(s), definition)
require.NoError(t, err, "should have been able to marshall definition")
assert.Equal(t, "integer", definition.Type, "type should have been an integer")
assert.Equal(t, "number", definition.Type, "type should have been a number")

maxVal := definition.Maximum
require.NotNil(t, maxVal, "maximum should have been loaded")
assert.Equal(t, 10240, int(*maxVal), "max should have been 10240")
assert.Equal(t, 10239.5, *maxVal, "max should have been 10239.5")

minVal := definition.Minimum
require.NotNil(t, minVal, "minimum should have been loaded")
assert.Equal(t, 10, int(*minVal), "min should have been 10")
assert.Equal(t, 0.1, *minVal, "min should have been 0.1")

def, ok := definition.Default.(float64)
require.True(t, ok, "default should h ave been float64")
assert.Equal(t, 80, int(def), "default should have been 80")
assert.Equal(t, 98.6, def, "default should have been 98.6")

}

Expand Down Expand Up @@ -310,13 +310,13 @@ func TestConvertValue(t *testing.T) {
is.Error(err)

pd.Type = "number"
_, err = pd.ConvertValue("123")
is.Error(err)
is.Contains(err.Error(), "invalid definition")
out, err = pd.ConvertValue("123")
is.NoError(err)
is.Equal(float64(123), out.(float64))

_, err = pd.ConvertValue("5.5")
is.Error(err)
is.Contains(err.Error(), "invalid definition")
out, err = pd.ConvertValue("5.5")
is.NoError(err)
is.Equal(float64(5.5), out.(float64))

_, err = pd.ConvertValue("nope")
is.Error(err)
Expand Down
6 changes: 3 additions & 3 deletions fetch-schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ const CNABSchemaDestPrefix = "./schema/schema"

func main() {
schemas := map[string]string{
"bundle": bundle.CNABSpecVersion,
"definitions": bundle.CNABSpecVersion,
"claim": claim.CNABSpecVersion,
"bundle": bundle.CNABSpecVersion,
"claim": claim.CNABSpecVersion,
}

for schema, version := range schemas {
Expand All @@ -39,6 +38,7 @@ func main() {

func fetchSchema(schemaType, schemaVersion string) ([]byte, error) {
schemaURL := fmt.Sprintf("%s/%s/%s.schema.json", CNABSchemaURLPrefix, schemaVersion, schemaType)
fmt.Println("Retrieving schema", schemaURL)
resp, err := http.Get(schemaURL)
if err != nil {
return nil, errors.Wrapf(err, "unable to fetch schema from %q", schemaURL)
Expand Down
13 changes: 12 additions & 1 deletion schema/schema/bundle.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"description": "A user-friendly description of this credential",
"type": "string"
},
"applyTo": {
"description": "An optional exhaustive list of actions handling this credential",
"items": {
"type": "string"
},
"type": "array"
},
"env": {
"description": "The environment variable name, such as MY_VALUE, into which the credential will be placed",
"type": "string"
Expand Down Expand Up @@ -185,6 +192,10 @@
"description": "A description of the purpose of this action",
"type": "string"
},
"title": {
"description": "A human-readable name for this action",
"type": "string"
},
"modifies": {
"description": "Must be set to true if the action can change any resource managed by this bundle",
"type": "boolean"
Expand Down Expand Up @@ -214,7 +225,7 @@
},
"definitions": {
"additionalProperties": {
"$ref": "https://cnab.io/v1/definitions.schema.json"
"$ref": "http://json-schema.org/draft-07/schema#"
},
"type": "object"
},
Expand Down
56 changes: 5 additions & 51 deletions schema/schema/claim.schema.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,6 @@
{
"$id": "https://cnab.io/v1/claim.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"result": {
"properties": {
"message": {
"description": "the last message from the invocation image or runtime",
"type": "string"
},
"modified": {
"description": "The date last modified, as an ISO-8601 Extended Format date string, as specified in the ECMAScript standard",
"type": "string"
},
"outputs": {
"description": "List of output names generated by the last action.",
"type": "array",
"items": {
"type": "string"
}
},
"started": {
"description": "The date when execution of the operation started, as an ISO-8601 Extended Format date string, as specified in the ECMAScript standard",
"type": "string"
},
"status": {
"description": "The status of the last action",
"enum": [
"cancelled",
"failed",
"succeeded",
"pending",
"running",
"unknown"
],
"type": "string"
}
},
"stopped": {
"description": "The date when execution of the operation stopped, as an ISO-8601 Extended Format date string, as specified in the ECMAScript standard",
"type": "string"
},
"required": [
"message",
"modified",
"status"
],
"type": "object"
}
},
"properties": {
"additionalProperties": false,
"action": {
Expand All @@ -69,6 +22,10 @@
"custom": {
"$comment": "reserved for custom extensions"
},
"id": {
"description": "the ID (a ULID)",
"type": "string"
},
"installation": {
"description": "the name of the installation",
"type": "string"
Expand All @@ -77,10 +34,6 @@
"description": "key/value pairs of parameter name to parameter value.",
"type": "object"
},
"result": {
"$ref": "#/definitions/result",
"description": "The result of the last action"
},
"revision": {
"description": "the revision ID (ideally a ULID)",
"type": "string"
Expand All @@ -90,6 +43,7 @@
"action",
"bundle",
"created",
"id",
"installation",
"revision"
],
Expand Down
Loading

0 comments on commit 0082cac

Please sign in to comment.