Skip to content

Commit

Permalink
Add start and end line columns including yml support
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdatasourav committed Aug 25, 2023
1 parent b74edc8 commit b095c2c
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 47 deletions.
29 changes: 24 additions & 5 deletions openapi/table_openapi_component_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package openapi

import (
"context"
"os"
"strings"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
Expand All @@ -21,7 +23,7 @@ func tableOpenAPIComponentHeader(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIComponentHeaders,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "key", Description: "The key used to refer or search the header.", Type: proto.ColumnType_STRING},
{Name: "name", Description: "The name of the header.", Type: proto.ColumnType_STRING},
{Name: "location", Description: "The location of the header. Possible values are query, header, path or cookie.", Type: proto.ColumnType_STRING, Transform: transform.FromField("In").NullIfZero()},
Expand All @@ -35,13 +37,15 @@ func tableOpenAPIComponentHeader(ctx context.Context) *plugin.Table {
{Name: "schema", Description: "The schema of the header.", Type: proto.ColumnType_JSON, Transform: transform.FromField("Schema.Value")},
{Name: "schema_ref", Description: "The schema reference of the header.", Type: proto.ColumnType_STRING, Transform: transform.FromField("Schema.Ref").Transform(transform.NullIfZeroValue)},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

type openAPIComponentHeader struct {
Path string
Key string
Path string
Key string
StartLine int
EndLine int
openapi3.Header
}

Expand All @@ -52,6 +56,12 @@ func listOpenAPIComponentHeaders(ctx context.Context, d *plugin.QueryData, h *pl
// available by the optional key column
path := h.Item.(filePath).Path

file, err := os.Open(path)
if err != nil {
plugin.Logger(ctx).Error("openapi_component_header.listOpenAPIComponentHeaders", "file_open_error", err)
return nil, err
}

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -66,7 +76,16 @@ func listOpenAPIComponentHeaders(ctx context.Context, d *plugin.QueryData, h *pl

// For each header, scan its arguments
for k, v := range doc.Components.Headers {
d.StreamListItem(ctx, openAPIComponentHeader{path, k, *v.Value})

// fetch start and end line for each header
var startLine, endLine int
if strings.HasSuffix(path, "json") {
startLine, endLine = findBlockLinesFromJSON(file, "components", k)
} else {
startLine, endLine = findBlockLinesFromYML(file, "components", k)
}

d.StreamListItem(ctx, openAPIComponentHeader{path, k, startLine, endLine, *v.Value})

// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
Expand Down
29 changes: 24 additions & 5 deletions openapi/table_openapi_component_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package openapi

import (
"context"
"os"
"strings"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
Expand All @@ -21,7 +23,7 @@ func tableOpenAPIComponentParameter(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIComponentParameters,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "key", Description: "The key used to refer or search the parameter.", Type: proto.ColumnType_STRING},
{Name: "name", Description: "The name of the parameter.", Type: proto.ColumnType_STRING},
{Name: "location", Description: "The location of the parameter. Possible values are query, header, path or cookie.", Type: proto.ColumnType_STRING, Transform: transform.FromField("In")},
Expand All @@ -35,13 +37,15 @@ func tableOpenAPIComponentParameter(ctx context.Context) *plugin.Table {
{Name: "schema", Description: "The schema of the parameter.", Type: proto.ColumnType_JSON, Transform: transform.FromField("Schema.Value")},
{Name: "schema_ref", Description: "The schema reference of the parameter.", Type: proto.ColumnType_STRING, Transform: transform.FromField("Schema.Ref").Transform(transform.NullIfZeroValue)},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

type openAPIComponentParameter struct {
Path string
Key string
Path string
Key string
StartLine int
EndLine int
openapi3.Parameter
}

Expand All @@ -52,6 +56,12 @@ func listOpenAPIComponentParameters(ctx context.Context, d *plugin.QueryData, h
// available by the optional key column
path := h.Item.(filePath).Path

file, err := os.Open(path)
if err != nil {
plugin.Logger(ctx).Error("openapi_component_parameter.listOpenAPIComponentParameters", "file_open_error", err)
return nil, err
}

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -66,7 +76,16 @@ func listOpenAPIComponentParameters(ctx context.Context, d *plugin.QueryData, h

// For each parameter, scan its arguments
for k, v := range doc.Components.Parameters {
d.StreamListItem(ctx, openAPIComponentParameter{path, k, *v.Value})

// fetch start and end line for each parameter
var startLine, endLine int
if strings.HasSuffix(path, "json") {
startLine, endLine = findBlockLinesFromJSON(file, "components", k)
} else {
startLine, endLine = findBlockLinesFromYML(file, "components", k)
}

d.StreamListItem(ctx, openAPIComponentParameter{path, k, startLine, endLine, *v.Value})

// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
Expand Down
37 changes: 29 additions & 8 deletions openapi/table_openapi_component_request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package openapi

import (
"context"
"os"
"strings"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
Expand All @@ -20,21 +22,23 @@ func tableOpenAPIComponentRequestBody(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIComponentRequestBodies,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "key", Description: "The key used to refer or search the request body.", Type: proto.ColumnType_STRING},
{Name: "description", Description: "A brief description of the request body.", Type: proto.ColumnType_STRING},
{Name: "required", Description: "True, if the request body is required.", Type: proto.ColumnType_BOOL},
{Name: "content", Description: "The content of the request body.", Type: proto.ColumnType_JSON},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

type openAPIComponentRequestBody struct {
Path string
Key string
Content []map[string]interface{}
Raw openapi3.RequestBody
Path string
Key string
StartLine int
EndLine int
Content []map[string]interface{}
Raw openapi3.RequestBody
}

//// LIST FUNCTION
Expand All @@ -44,6 +48,12 @@ func listOpenAPIComponentRequestBodies(ctx context.Context, d *plugin.QueryData,
// available by the optional key column
path := h.Item.(filePath).Path

file, err := os.Open(path)
if err != nil {
plugin.Logger(ctx).Error("openapi_component_request_body.listOpenAPIComponentRequestBodies", "file_open_error", err)
return nil, err
}

doc, err := getDoc(ctx, d, path)
if err != nil {
plugin.Logger(ctx).Error("openapi_component_request_body.listOpenAPIComponentRequestBodies", "parse_error", err)
Expand All @@ -57,9 +67,20 @@ func listOpenAPIComponentRequestBodies(ctx context.Context, d *plugin.QueryData,

// For each request body, scan its arguments
for k, v := range doc.Components.RequestBodies {

// fetch start and end line for each requestBody
var startLine, endLine int
if strings.HasSuffix(path, "json") {
startLine, endLine = findBlockLinesFromJSON(file, "components", k)
} else {
startLine, endLine = findBlockLinesFromYML(file, "components", k)
}

requestBodyObject := openAPIComponentRequestBody{
Path: path,
Key: k,
Path: path,
Key: k,
StartLine: startLine,
EndLine: endLine,
}

for header, content := range v.Value.Content {
Expand Down
25 changes: 23 additions & 2 deletions openapi/table_openapi_component_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package openapi

import (
"context"
"os"
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
Expand All @@ -20,14 +22,14 @@ func tableOpenAPIComponentResponse(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIComponentResponses,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "key", Description: "The key of the response object definition.", Type: proto.ColumnType_STRING},
{Name: "description", Description: "A description of the response.", Type: proto.ColumnType_STRING},
{Name: "content", Description: "A map containing descriptions of potential response payloads.", Type: proto.ColumnType_JSON},
{Name: "headers", Description: "Maps a header name to its definition.", Type: proto.ColumnType_JSON, Transform: transform.FromField("Raw.Headers")},
{Name: "links", Description: "A map of operations links that can be followed from the response.", Type: proto.ColumnType_JSON, Transform: transform.FromField("Raw.Links")},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

Expand All @@ -36,6 +38,8 @@ type openAPIComponentResponse struct {
Content []map[string]interface{}
Key string
Description string
StartLine int
EndLine int
Raw openapi3.Response
}

Expand All @@ -46,6 +50,12 @@ func listOpenAPIComponentResponses(ctx context.Context, d *plugin.QueryData, h *
// available by the optional key column
path := h.Item.(filePath).Path

file, err := os.Open(path)
if err != nil {
plugin.Logger(ctx).Error("openapi_component_response.listOpenAPIComponentResponses", "file_open_error", err)
return nil, err
}

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -60,10 +70,21 @@ func listOpenAPIComponentResponses(ctx context.Context, d *plugin.QueryData, h *

// For each response, scan its arguments
for k, v := range doc.Components.Responses {

// fetch start and end line for each response
var startLine, endLine int
if strings.HasSuffix(path, "json") {
startLine, endLine = findBlockLinesFromJSON(file, "components", k)
} else {
startLine, endLine = findBlockLinesFromYML(file, "components", k)
}

responseObject := openAPIComponentResponse{
Path: path,
Key: k,
Description: *v.Value.Description,
StartLine: startLine,
EndLine: endLine,
}

for header, content := range v.Value.Content {
Expand Down
29 changes: 24 additions & 5 deletions openapi/table_openapi_component_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package openapi

import (
"context"
"os"
"strings"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
Expand All @@ -21,7 +23,7 @@ func tableOpenAPIComponentSchema(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIComponentSchemas,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "name", Description: "The name of the property.", Type: proto.ColumnType_STRING},
{Name: "type", Description: "The type of the schema.", Type: proto.ColumnType_STRING},
{Name: "format", Description: "The format of a specific schema type.", Type: proto.ColumnType_STRING},
Expand Down Expand Up @@ -57,13 +59,15 @@ func tableOpenAPIComponentSchema(ctx context.Context) *plugin.Table {
{Name: "required", Description: "If true, the property must be defined.", Type: proto.ColumnType_JSON},
{Name: "properties", Description: "Describes the schema properties.", Type: proto.ColumnType_JSON},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

type openAPIComponentSchema struct {
Path string
Name string
Path string
Name string
StartLine int
EndLine int
openapi3.Schema
Properties map[string]interface{}
}
Expand All @@ -75,6 +79,12 @@ func listOpenAPIComponentSchemas(ctx context.Context, d *plugin.QueryData, h *pl
// available by the optional key column
path := h.Item.(filePath).Path

file, err := os.Open(path)
if err != nil {
plugin.Logger(ctx).Error("openapi_component_schema.listOpenAPIComponentSchemas", "file_open_error", err)
return nil, err
}

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -89,11 +99,20 @@ func listOpenAPIComponentSchemas(ctx context.Context, d *plugin.QueryData, h *pl

// For each schema, scan its arguments
for k, v := range doc.Components.Schemas {

// fetch start and end line for each schemas
var startLine, endLine int
if strings.HasSuffix(path, "json") {
startLine, endLine = findBlockLinesFromJSON(file, "components", k)
} else {
startLine, endLine = findBlockLinesFromYML(file, "components", k)
}

properties := map[string]interface{}{}
for i, j := range v.Value.Properties {
properties[i] = j.Value
}
d.StreamListItem(ctx, openAPIComponentSchema{path, k, *v.Value, properties})
d.StreamListItem(ctx, openAPIComponentSchema{path, k, startLine, endLine, *v.Value, properties})

// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
Expand Down
Loading

0 comments on commit b095c2c

Please sign in to comment.