Skip to content

Commit

Permalink
Add start and end line columns to the tables
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdatasourav committed Aug 24, 2023
1 parent 9319a44 commit b74edc8
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 14 deletions.
20 changes: 20 additions & 0 deletions openapi/common_columns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package openapi

import (
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

func openAPICommonColumns(columns []*plugin.Column) []*plugin.Column {
allColumns := definitionResourceColumns()
allColumns = append(allColumns, columns...)
return allColumns
}

func definitionResourceColumns() []*plugin.Column {
return []*plugin.Column{
{Name: "start_line", Type: proto.ColumnType_INT, Description: "The path to the definition file.", Transform: transform.FromField("StartLine").NullIfZero()},
{Name: "end_line", Type: proto.ColumnType_INT, Description: "The path to the definition file.", Transform: transform.FromField("EndLine").NullIfZero()},
}
}
17 changes: 14 additions & 3 deletions openapi/table_openapi_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi

import (
"context"
"os"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
Expand All @@ -20,7 +21,7 @@ func tableOpenAPIInfo(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIInfo,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "title", Description: "The title of the API.", Type: proto.ColumnType_STRING},
{Name: "description", Description: "A description of the API.", Type: proto.ColumnType_STRING},
{Name: "version", Description: "The version of the OpenAPI document.", Type: proto.ColumnType_STRING},
Expand All @@ -29,13 +30,15 @@ func tableOpenAPIInfo(ctx context.Context) *plugin.Table {
{Name: "license", Description: "The license information for the exposed API.", Type: proto.ColumnType_JSON},
{Name: "specification_version", Description: "The version of the OpenAPI specification.", Type: proto.ColumnType_STRING},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

type openAPIInfo struct {
Path string
SpecificationVersion string
StartLine int
EndLine int
openapi3.Info
}

Expand All @@ -46,13 +49,21 @@ func listOpenAPIInfo(ctx context.Context, d *plugin.QueryData, h *plugin.Hydrate
// available by the optional key column
path := h.Item.(filePath).Path

// get the start and end lines of the info block
file, err := os.Open(path)
if err != nil {
plugin.Logger(ctx).Error("openapi_info.listOpenAPIInfo", "file_open_error", err)
return nil, err
}
startLine, endLine := findBlockLines(file, "info", "")

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
plugin.Logger(ctx).Error("openapi_info.listOpenAPIInfo", "parse_error", err)
return nil, err
}
d.StreamListItem(ctx, openAPIInfo{path, doc.OpenAPI, *doc.Info})
d.StreamListItem(ctx, openAPIInfo{path, doc.OpenAPI, startLine, endLine, *doc.Info})

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

import (
"context"
"os"
p "path"
"strings"

Expand All @@ -23,7 +24,7 @@ func tableOpenAPIPath(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIPaths,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "api_path", Description: "A relative path to an individual endpoint.", Type: proto.ColumnType_STRING},
{Name: "method", Description: "Specify the HTTP method.", Type: proto.ColumnType_STRING},
{Name: "description", Description: "A verbose explanation of the operation behavior.", Type: proto.ColumnType_STRING, Transform: transform.FromField("Operation.Description")},
Expand All @@ -41,14 +42,16 @@ func tableOpenAPIPath(ctx context.Context) *plugin.Table {
{Name: "external_docs", Description: "Additional external documentation for this operation.", Type: proto.ColumnType_JSON, Transform: transform.FromField("Operation.ExternalDocs")},
{Name: "tags", Description: "A list of tags for API documentation control.", Type: proto.ColumnType_JSON, Transform: transform.FromField("Operation.Tags")},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

type openAPIPath struct {
Path string
ApiPath string
Method string
StartLine int
EndLine int
Operation *openapi3.Operation
}

Expand All @@ -59,6 +62,13 @@ func listOpenAPIPaths(ctx context.Context, d *plugin.QueryData, h *plugin.Hydrat
// available by the optional key column
path := h.Item.(filePath).Path

// get the start and end lines of the path block
file, err := os.Open(path)
if err != nil {
plugin.Logger(ctx).Error("openapi_path.listOpenAPIPaths", "file_open_error", err)
return nil, err
}

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -68,6 +78,7 @@ func listOpenAPIPaths(ctx context.Context, d *plugin.QueryData, h *plugin.Hydrat

// For each path, scan its arguments
for apiPath, item := range doc.Paths {
startLine, endLine := findBlockLines(file, "paths", apiPath)
for _, op := range OperationTypes {
operation := getOperationInfoByType(op, item)

Expand All @@ -80,6 +91,8 @@ func listOpenAPIPaths(ctx context.Context, d *plugin.QueryData, h *plugin.Hydrat
Path: path,
ApiPath: p.Join(apiPath, op),
Method: strings.ToUpper(op),
StartLine: startLine,
EndLine: endLine,
Operation: operation,
})

Expand Down
17 changes: 14 additions & 3 deletions openapi/table_openapi_path_request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi

import (
"context"
"os"
p "path"
"strings"

Expand All @@ -22,15 +23,15 @@ func tableOpenAPIPathRequestBody(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIPathRequestBodies,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "api_path", Description: "The key of the request body object definition.", Type: proto.ColumnType_STRING},
{Name: "api_method", Description: "Specifies the HTTP method.", Type: proto.ColumnType_STRING},
{Name: "description", Description: "A description of the request body.", Type: proto.ColumnType_STRING, Transform: transform.FromField("Raw.Description")},
{Name: "required", Description: "If true, the request body is required.", Type: proto.ColumnType_BOOL, Transform: transform.FromField("Raw.Required")},
{Name: "request_body_ref", Description: "The reference to the components request body object.", Type: proto.ColumnType_STRING},
{Name: "content", Description: "A map containing descriptions of potential request body payloads.", Type: proto.ColumnType_JSON},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

Expand All @@ -39,6 +40,8 @@ type openAPIPathRequestBody struct {
ApiPath string
ApiMethod string
RequestBodyRef string
StartLine int
EndLine int
Content []map[string]interface{}
Raw openapi3.RequestBody
}
Expand All @@ -50,6 +53,12 @@ func listOpenAPIPathRequestBodies(ctx context.Context, d *plugin.QueryData, h *p
// available by the optional key column
path := h.Item.(filePath).Path

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

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -71,12 +80,14 @@ func listOpenAPIPathRequestBodies(ctx context.Context, d *plugin.QueryData, h *p
if operation.RequestBody == nil {
continue
}

startLine, endLine := findBlockLines(file, "paths", apiPath, "requestBody")
requestBodyObject := openAPIPathRequestBody{
Path: path,
ApiPath: p.Join(apiPath, op),
ApiMethod: strings.ToUpper(op),
RequestBodyRef: operation.RequestBody.Ref,
StartLine: startLine,
EndLine: endLine,
}

for header, content := range operation.RequestBody.Value.Content {
Expand Down
16 changes: 14 additions & 2 deletions openapi/table_openapi_path_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi

import (
"context"
"os"
p "path"
"strings"

Expand All @@ -22,7 +23,7 @@ func tableOpenAPIPathResponse(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIPathResponses,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "api_path", Description: "The key of the response object definition.", Type: proto.ColumnType_STRING},
{Name: "api_method", Description: "Specifies the HTTP method.", Type: proto.ColumnType_STRING},
{Name: "response_status", Description: "The key of the response object definition.", Type: proto.ColumnType_STRING},
Expand All @@ -32,7 +33,7 @@ func tableOpenAPIPathResponse(ctx context.Context) *plugin.Table {
{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: "description", Description: "A description of the response.", Type: proto.ColumnType_STRING},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

Expand All @@ -42,6 +43,8 @@ type openAPIPathResponse struct {
ApiMethod string
ResponseStatus string
ResponseRef string
StartLine int
EndLine int
Content []map[string]interface{}
Description string
Raw openapi3.Response
Expand All @@ -54,6 +57,12 @@ func listOpenAPIPathResponses(ctx context.Context, d *plugin.QueryData, h *plugi
// available by the optional key column
path := h.Item.(filePath).Path

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

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -72,13 +81,16 @@ func listOpenAPIPathResponses(ctx context.Context, d *plugin.QueryData, h *plugi
}

for responseStatus, response := range operation.Responses {
startLine, endLine := findBlockLines(file, "paths", apiPath, responseStatus)
responseObject := openAPIPathResponse{
Path: path,
ApiPath: p.Join(apiPath, op),
ApiMethod: strings.ToUpper(op),
ResponseStatus: responseStatus,
Description: *response.Value.Description,
ResponseRef: response.Ref,
StartLine: startLine,
EndLine: endLine,
}

for header, content := range response.Value.Content {
Expand Down
18 changes: 14 additions & 4 deletions openapi/table_openapi_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi

import (
"context"
"os"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
Expand All @@ -21,17 +22,19 @@ func tableOpenAPIServer(ctx context.Context) *plugin.Table {
Hydrate: listOpenAPIServers,
KeyColumns: plugin.OptionalColumns([]string{"path"}),
},
Columns: []*plugin.Column{
Columns: openAPICommonColumns([]*plugin.Column{
{Name: "url", Description: "A URL to the target host.", Type: proto.ColumnType_STRING, Transform: transform.FromField("URL")},
{Name: "description", Description: "An optional string describing the host designated by the URL.", Type: proto.ColumnType_STRING},
{Name: "variables", Description: "A map between a variable name and its value, used for substitution in the server's URL template.", Type: proto.ColumnType_JSON},
{Name: "path", Description: "Path to the file.", Type: proto.ColumnType_STRING},
},
}),
}
}

type openAPIServer struct {
Path string
Path string
StartLine int
EndLine int
openapi3.Server
}

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

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

// Get the parsed contents
doc, err := getDoc(ctx, d, path)
if err != nil {
Expand All @@ -51,7 +60,8 @@ func listOpenAPIServers(ctx context.Context, d *plugin.QueryData, h *plugin.Hydr

// For each server, scan its arguments
for _, server := range doc.Servers {
d.StreamListItem(ctx, openAPIServer{path, *server})
startLine, endLine := findBlockLines(file, "servers", server.URL)
d.StreamListItem(ctx, openAPIServer{path, startLine, endLine, *server})

// 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 b74edc8

Please sign in to comment.