Skip to content

Commit

Permalink
Accept paths in the newSpecV2Resource constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lillchan committed Jul 23, 2019
1 parent 47bc854 commit 6566185
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
7 changes: 4 additions & 3 deletions openapi/openapi_v2_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ type SpecV2Resource struct {
}

// newSpecV2Resource creates a SpecV2Resource with no region and default host
func newSpecV2Resource(path string, schemaDefinition spec.Schema, rootPathItem, instancePathItem spec.PathItem, schemaDefinitions map[string]spec.Schema) (*SpecV2Resource, error) {
return newSpecV2ResourceWithRegion("", path, schemaDefinition, rootPathItem, instancePathItem, schemaDefinitions)
func newSpecV2Resource(path string, schemaDefinition spec.Schema, rootPathItem, instancePathItem spec.PathItem, schemaDefinitions map[string]spec.Schema, paths map[string]spec.PathItem) (*SpecV2Resource, error) {
return newSpecV2ResourceWithRegion("", path, schemaDefinition, rootPathItem, instancePathItem, schemaDefinitions, paths)
}

func newSpecV2ResourceWithRegion(region, path string, schemaDefinition spec.Schema, rootPathItem, instancePathItem spec.PathItem, schemaDefinitions map[string]spec.Schema) (*SpecV2Resource, error) {
func newSpecV2ResourceWithRegion(region, path string, schemaDefinition spec.Schema, rootPathItem, instancePathItem spec.PathItem, schemaDefinitions map[string]spec.Schema, paths map[string]spec.PathItem) (*SpecV2Resource, error) {
if path == "" {
return nil, fmt.Errorf("path must not be empty")
}
Expand All @@ -104,6 +104,7 @@ func newSpecV2ResourceWithRegion(region, path string, schemaDefinition spec.Sche
RootPathItem: rootPathItem,
InstancePathItem: instancePathItem,
SchemaDefinitions: schemaDefinitions,
Paths: paths,
}
name, err := resource.buildResourceName()
if err != nil {
Expand Down
60 changes: 51 additions & 9 deletions openapi/openapi_v2_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewSpecV2Resource(t *testing.T) {
}
Convey("When getResourceName method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, nil)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand All @@ -40,7 +40,7 @@ func TestNewSpecV2Resource(t *testing.T) {
}
Convey("When getResourceName method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, nil)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand All @@ -59,7 +59,7 @@ func TestNewSpecV2Resource(t *testing.T) {
}
Convey("When getResourceName method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, nil)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand All @@ -78,7 +78,7 @@ func TestNewSpecV2Resource(t *testing.T) {
}
Convey("When getResourceName method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, nil)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand All @@ -97,7 +97,7 @@ func TestNewSpecV2Resource(t *testing.T) {
}
Convey("When getResourceName method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, nil)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand All @@ -109,14 +109,21 @@ func TestNewSpecV2Resource(t *testing.T) {

Convey("Given a root path which has path parameters '/api/v1/nodes/{name}/proxy' and a root path item", t, func() {
path := "/api/v1/nodes/{name}/proxy"
paths := map[string]spec.PathItem{
"/api/v1/nodes": {
PathItemProps: spec.PathItemProps{
Post: &spec.Operation{},
},
},
}
rootPathItem := spec.PathItem{
PathItemProps: spec.PathItemProps{
Post: &spec.Operation{},
},
}
Convey("When getResourceName method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, paths)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand All @@ -141,7 +148,7 @@ func TestNewSpecV2Resource(t *testing.T) {
}
Convey("When getResourceName method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
r, err := newSpecV2Resource(path, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, nil)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand All @@ -162,7 +169,7 @@ func TestNewSpecV2Resource(t *testing.T) {
}
Convey("When newSpecV2Resource method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
_, err := newSpecV2Resource(invalidRootPath, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions)
_, err := newSpecV2Resource(invalidRootPath, spec.Schema{}, rootPathItem, spec.PathItem{}, schemaDefinitions, nil)
Convey("And the err returned should not be nil", func() {
So(err, ShouldNotBeNil)
})
Expand All @@ -173,7 +180,7 @@ func TestNewSpecV2Resource(t *testing.T) {
path := ""
Convey("When newSpecV2Resource method is called", func() {
schemaDefinitions := map[string]spec.Schema{}
_, err := newSpecV2Resource(path, spec.Schema{}, spec.PathItem{}, spec.PathItem{}, schemaDefinitions)
_, err := newSpecV2Resource(path, spec.Schema{}, spec.PathItem{}, spec.PathItem{}, schemaDefinitions, nil)
Convey("And the err returned should not be nil", func() {
So(err, ShouldNotBeNil)
})
Expand Down Expand Up @@ -508,6 +515,41 @@ func TestParentResourceInfo(t *testing.T) {
})
})
})

Convey("Given a SpecV2Resource configured with a base path that is indeed a sub-resource", t, func() {
r := SpecV2Resource{
Path: "/api/v1/nodes/{name}/proxy",
Paths: map[string]spec.PathItem{
"/api/v1/nodes": {
PathItemProps: spec.PathItemProps{
Post: &spec.Operation{},
},
},
},
}
Convey("When parentResourceInfo is called", func() {
parentResourceInfo := r.getParentResourceInfo()
Convey("Then the the parentResourceInfo struct returned shouldn't be nil", func() {
So(parentResourceInfo, ShouldNotBeNil)
})
Convey("And the parentResourceNames should not be empty and contain the right items", func() {
So(len(parentResourceInfo.parentResourceNames), ShouldEqual, 1)
So(parentResourceInfo.parentResourceNames[0], ShouldEqual, "nodes_v1")
})
Convey("And the fullParentResourceName should match the expected name", func() {
So(parentResourceInfo.fullParentResourceName, ShouldEqual, "nodes_v1_proxy")
})
Convey("And the parentURIs contain the expected parent URIs", func() {
So(len(parentResourceInfo.parentURIs), ShouldEqual, 1)
So(parentResourceInfo.parentURIs[0], ShouldEqual, "/api/v1/nodes")
})
Convey("And the parentInstanceURIs contain the expected instances URIs", func() {
So(len(parentResourceInfo.parentInstanceURIs), ShouldEqual, 1)
So(parentResourceInfo.parentInstanceURIs[0], ShouldEqual, "/api/v1/nodes/{name}")
})
})
})

Convey("Given a SpecV2Resource configured with a path that is indeed a sub-resource (no versioning)", t, func() {
r := SpecV2Resource{
Path: "/cdns/{id}/firewalls",
Expand Down
4 changes: 2 additions & 2 deletions openapi/openapi_v2_spec_analyser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (specAnalyser *specV2Analyser) GetTerraformCompliantResources() ([]SpecReso
if isMultiRegion {
log.Printf("[INFO] resource '%s' is configured with host override AND multi region; creating one reasource per region", resourceRootPath)
for regionName := range regions {
r, err := newSpecV2ResourceWithRegion(regionName, resourceRootPath, *resourcePayloadSchemaDef, *resourceRoot, pathItem, specAnalyser.d.Spec().Definitions)
r, err := newSpecV2ResourceWithRegion(regionName, resourceRootPath, *resourcePayloadSchemaDef, *resourceRoot, pathItem, specAnalyser.d.Spec().Definitions, specAnalyser.d.Spec().Paths.Paths)
if err != nil {
log.Printf("[WARN] ignoring resource '%s' due to an error while creating a creating the SpecV2Resource: %s", resourceRootPath, err)
continue
Expand All @@ -77,7 +77,7 @@ func (specAnalyser *specV2Analyser) GetTerraformCompliantResources() ([]SpecReso
continue
}

r, err := newSpecV2Resource(resourceRootPath, *resourcePayloadSchemaDef, *resourceRoot, pathItem, specAnalyser.d.Spec().Definitions)
r, err := newSpecV2Resource(resourceRootPath, *resourcePayloadSchemaDef, *resourceRoot, pathItem, specAnalyser.d.Spec().Definitions, specAnalyser.d.Spec().Paths.Paths)
if err != nil {
log.Printf("[WARN] ignoring resource '%s' due to an error while creating a creating the SpecV2Resource: %s", resourceRootPath, err)
continue
Expand Down

0 comments on commit 6566185

Please sign in to comment.