Skip to content

Commit

Permalink
Propagate references from base schema to derived
Browse files Browse the repository at this point in the history
Change-Id: I19f444187c04f6db441f4cc1825a5d87423230dd
Closes-Bug: 1798784
  • Loading branch information
renkam committed Oct 23, 2018
1 parent 3469c4a commit 5a83418
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 47 deletions.
14 changes: 14 additions & 0 deletions extension/schemas/tag-schema.yml
@@ -0,0 +1,14 @@
api_style: contrail
extends:
- base
id: tag
plural: tags
prefix: /
schema:
properties:
tag_value:
description: Tag value string representation
operations: CR
presence: optional
type: string
type: object
8 changes: 4 additions & 4 deletions pkg/compilation/logic/routing_instance_test.go
Expand Up @@ -39,10 +39,10 @@ func TestCreateRoutingInstanceCreatesRouteTarget(t *testing.T) {

_, err := service.CreateRoutingInstance(context.Background(), &services.CreateRoutingInstanceRequest{
RoutingInstance: &models.RoutingInstance{
UUID: "a8edf702-7dd6-4cbc-b599-a7f8ace1d22b",
ParentUUID: "af68b258-6fc4-4959-8181-a2cfb6f93500",
ParentType: "virtual-network",
FQName: []string{"default-domain", "project-blue", "vn-blue", "vn-blue"},
UUID: "a8edf702-7dd6-4cbc-b599-a7f8ace1d22b",
ParentUUID: "af68b258-6fc4-4959-8181-a2cfb6f93500",
ParentType: "virtual-network",
FQName: []string{"default-domain", "project-blue", "vn-blue", "vn-blue"},
RoutingInstanceIsDefault: true,
},
})
Expand Down
16 changes: 8 additions & 8 deletions pkg/db/db_test.go
Expand Up @@ -137,15 +137,15 @@ func TestDBCreateRef(t *testing.T) {
{
name: "objects missing",
request: services.CreateRoutingInstanceRouteTargetRefRequest{
ID: "foo",
ID: "foo",
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: "bar"},
},
fails: true,
},
{
name: "valid ID invalid ref UUID",
request: services.CreateRoutingInstanceRouteTargetRefRequest{
ID: riUUID,
ID: riUUID,
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: "bar"},
},
fails: true,
Expand Down Expand Up @@ -243,35 +243,35 @@ func TestDBDeleteRef(t *testing.T) {
{
name: "objects missing",
request: services.DeleteRoutingInstanceRouteTargetRefRequest{
ID: "foo",
ID: "foo",
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: "bar"},
},
expected: &services.DeleteRoutingInstanceRouteTargetRefResponse{
ID: "foo",
ID: "foo",
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: "bar"},
},
shouldRefExist: true,
},
{
name: "valid ID invalid ref UUID",
request: services.DeleteRoutingInstanceRouteTargetRefRequest{
ID: riUUID,
ID: riUUID,
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: "bar"},
},
expected: &services.DeleteRoutingInstanceRouteTargetRefResponse{
ID: riUUID,
ID: riUUID,
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: "bar"},
},
shouldRefExist: true,
},
{
name: "valid ID valid ref UUID",
request: services.DeleteRoutingInstanceRouteTargetRefRequest{
ID: riUUID,
ID: riUUID,
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: rtUUID},
},
expected: &services.DeleteRoutingInstanceRouteTargetRefResponse{
ID: riUUID,
ID: riUUID,
RoutingInstanceRouteTargetRef: &models.RoutingInstanceRouteTargetRef{UUID: rtUUID},
},
shouldRefExist: false,
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/etcd/service_test.go
Expand Up @@ -119,7 +119,7 @@ func TestEtcdNotifierService(t *testing.T) {
},
},
{
"name": "lr_blue",
"name": "lr_blue",
"virtual_network_back_refs": "$null",
},
},
Expand Down
11 changes: 0 additions & 11 deletions pkg/models/firewall_rule.go
Expand Up @@ -169,14 +169,3 @@ func (fr *FirewallRule) GetEndpoints(

return endpoints, dbEndpoints
}

// FirewallRuleTagRef stub to be removed when tag refs implemented
type FirewallRuleTagRef struct {
UUID string
To []string
}

// GetTagRefs to be removed when tag refs implemented
func (fr *FirewallRule) GetTagRefs() []*FirewallRuleTagRef {
return []*FirewallRuleTagRef{}
}
4 changes: 2 additions & 2 deletions pkg/models/validation_test.go
Expand Up @@ -314,7 +314,7 @@ func TestSchemaValidationJobTemplate(t *testing.T) {
JobTemplatePlaybooks: &PlaybookInfoListType{},
JobTemplateMultiDeviceJob: true,
JobTemplateConcurrencyLevel: "fabric",
FQName: []string{"a", "b"},
FQName: []string{"a", "b"},
},
},
{
Expand All @@ -327,7 +327,7 @@ func TestSchemaValidationJobTemplate(t *testing.T) {
JobTemplatePlaybooks: &PlaybookInfoListType{},
JobTemplateMultiDeviceJob: true,
JobTemplateConcurrencyLevel: "fabric",
FQName: []string{"a", "b"},
FQName: []string{"a", "b"},
},
fails: true,
},
Expand Down
7 changes: 5 additions & 2 deletions pkg/schema/schema.go
Expand Up @@ -557,7 +557,7 @@ func (api *API) resolveRef(schema *JSONSchema) error {
}
definition, err := api.loadType(parseRef(schema.Ref))
if err != nil {
return err
return errors.Wrapf(err, "required by %v", schema.ID)
}
schema.Update(definition)
return nil
Expand Down Expand Up @@ -653,7 +653,8 @@ func (api *API) resolveAllRelation() error {
s.References[linkTo] = reference
linkToSchema := api.SchemaByID(linkTo)
if linkToSchema == nil {
return fmt.Errorf("can't find linked schema %s", linkTo)
return fmt.Errorf("missing linked schema '%s' for reference '%v' in schema %v [%v]",
linkTo, linkTo, s.ID, s.FileName)
}
linkToSchema.BackReferences[s.ID] = &BackReference{
LinkTo: s,
Expand Down Expand Up @@ -750,6 +751,7 @@ func (api *API) resolveExtend() error {
continue
}
s.JSONSchema.Update(baseSchema.JSONSchema)
s.ReferencesSlice = append(s.ReferencesSlice, baseSchema.ReferencesSlice...)
}
}
return nil
Expand Down Expand Up @@ -919,6 +921,7 @@ func MakeAPI(dirs []string, overrideSubdir string) (*API, error) {
Definitions: []*Schema{},
Types: map[string]*JSONSchema{},
}
log.Printf("Making API from schema dirs: %v", dirs)
for _, dir := range dirs {
overrides := &Schema{}
overridePath := ""
Expand Down
22 changes: 22 additions & 0 deletions pkg/schema/schema_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSchema(t *testing.T) {
Expand Down Expand Up @@ -49,6 +50,27 @@ func TestSchemaEnums(t *testing.T) {
checkPropertyRepeated(t, enumArrOvrd)
}

func TestReferencesExtendBase(t *testing.T) {
api, err := MakeAPI([]string{"test_data/schema_extend"}, "")
require.Nil(t, err, "API reading failed")
assert.Equal(t, 5, len(api.Schemas))

base := api.SchemaByID("base")
require.NotNil(t, base, "Base object can't be <nil>")
assert.Equal(t, 1, len(base.ReferencesSlice))
assert.Equal(t, 0, len(base.References)) // References in base schemas are not processed

zeroRefObj := api.SchemaByID("derived_object")
require.NotNil(t, zeroRefObj, "derived_object schema shouldn't be <nil>")
assert.Equal(t, 1, len(zeroRefObj.ReferencesSlice))
assert.Equal(t, 1, len(zeroRefObj.References))

ownRefObj := api.SchemaByID("derived_own_refs_object")
require.NotNil(t, ownRefObj, "derived_own_refs_object schema shouldn't be <nil>")
assert.Equal(t, 2, len(ownRefObj.ReferencesSlice))
assert.Equal(t, 2, len(ownRefObj.References))
}

func checkPropertyRepeated(t *testing.T, obj *JSONSchema) {
assert.NotNil(t, obj)
assert.Equal(t, 1, len(obj.Properties))
Expand Down
20 changes: 20 additions & 0 deletions pkg/schema/test_data/schema_extend/base.yml
@@ -0,0 +1,20 @@
---
plural: "base"
prefix: "/"
references:
tag:
operations: "CRUD"
description: "Tag attached to an object - has a type and value"
presence: "optional"
parents: {}
type: "abstract"
id: "base"
schema:
required: []
type: "object"
properties:
uuid:
presence: "true"
description: "UUID of the object, system automatically allocates one if not provided"
type: "string"

12 changes: 12 additions & 0 deletions pkg/schema/test_data/schema_extend/derived.yml
@@ -0,0 +1,12 @@
api_style: contrail
extends:
- base
id: derived_object
parents: {}
plural: derived_objects
prefix: "/"
schema:
properties: {}
required: []
type: object

18 changes: 18 additions & 0 deletions pkg/schema/test_data/schema_extend/derived_with_refs.yml
@@ -0,0 +1,18 @@
api_style: contrail
extends:
- base
id: derived_own_refs_object
parents: {}
plural: derived_own_refs_objects
prefix: "/"
references:
own_tag:
operations: "CRUD"
description: "Tag attached to an object - has a type and value"
presence: "optional"
schema:
properties: {}
required: []
type: object


16 changes: 16 additions & 0 deletions pkg/schema/test_data/schema_extend/own_tag-schema.yml
@@ -0,0 +1,16 @@
api_style: contrail
extends:
- base
id: own_tag
parents: {}
plural: own_tags
prefix: /
schema:
properties:
tag_value:
description: Tag value string representation
operations: CR
presence: required
type: string
type: object

16 changes: 16 additions & 0 deletions pkg/schema/test_data/schema_extend/tag-schema.yml
@@ -0,0 +1,16 @@
api_style: contrail
extends:
- base
id: tag
parents: {}
plural: tags
prefix: /
schema:
properties:
tag_value:
description: Tag value string representation
operations: CR
presence: required
type: string
type: object

6 changes: 6 additions & 0 deletions pkg/schema/test_data/schema_extend/types.yml
@@ -0,0 +1,6 @@
---
definitions:
HogeHoge:
minimum: 1
maximum: 101
type: "integer"
8 changes: 7 additions & 1 deletion pkg/services/list_response_test.go
Expand Up @@ -42,6 +42,7 @@ var dataYAML = []struct {
FQName: []string{},
ProjectBackRefs: []*models.Project{},
FloatingIPs: []*models.FloatingIP{},
TagRefs: []*models.FloatingIPPoolTagRef{},
},
},
FloatingIPPoolCount: 1,
Expand All @@ -58,6 +59,7 @@ var dataYAML = []struct {
perms2: null
configuration_version: 0
floating_ip_pool_subnets: null
tag_refs: []
project_back_refs: []
floating_ips: []
`),
Expand Down Expand Up @@ -102,6 +104,10 @@ func TestListResponseYAMLUnmarshaling(t *testing.T) {
var dataStruct ListFloatingIPPoolResponse
err := yaml.Unmarshal(data.bytes, &dataStruct)
assert.NoError(t, err, "unmarshaling ListResponse failed")
assert.Equal(t, data.structure, dataStruct)
assert.EqualValues(t, len(data.structure.FloatingIPPools), len(dataStruct.FloatingIPPools))
for i := range data.structure.FloatingIPPools {
assert.EqualValues(t, data.structure.FloatingIPPools[i], dataStruct.FloatingIPPools[i])

}
}
}

0 comments on commit 5a83418

Please sign in to comment.