Skip to content

Commit

Permalink
full spec/file upload, including from demo discovery app.
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Mar 8, 2020
1 parent 1cb5355 commit fbe181b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 18 deletions.
33 changes: 33 additions & 0 deletions apps/disco-flame/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Usage:
}
if arguments["--all"].(bool) {
if !arguments["--raw"].(bool) &&
!arguments["--upload"].(bool) &&
!arguments["--openapi2"].(bool) &&
!arguments["--openapi3"].(bool) &&
!arguments["--features"].(bool) &&
Expand Down Expand Up @@ -200,6 +201,38 @@ func handleExportArgumentsForBytes(arguments map[string]interface{}, bytes []byt
initFlame()
api := document
ctx := context.TODO()
// does the API exist? if not, create it
{
request := &rpcpb.GetProductRequest{}
request.Name = "projects/google/products/" + api.Name
response, err := FlameClient.GetProduct(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
if err != nil { // TODO only do this for NotFound errors
request := &rpcpb.CreateProductRequest{}
request.Parent = "projects/google"
request.ProductId = api.Name
request.Product = &rpcpb.Product{}
request.Product.DisplayName = api.Title
request.Product.Description = api.Description
response, err := FlameClient.CreateProduct(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
}
}
// does the version exist? if not create it
{
request := &rpcpb.GetVersionRequest{}
request.Name = "projects/google/products/" + api.Name + "/versions/" + api.Version
response, err := FlameClient.GetVersion(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
if err != nil {
request := &rpcpb.CreateVersionRequest{}
request.Parent = "projects/google/products/" + api.Name
request.VersionId = api.Version
request.Version = &rpcpb.Version{}
response, err := FlameClient.CreateVersion(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
}
}
// does the spec exist? if not, create it
{
request := &rpcpb.GetSpecRequest{}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/golang/protobuf v1.3.4
github.com/googleapis/gax-go/v2 v2.0.5
github.com/googleapis/gnostic v0.4.1
github.com/googleapis/gnostic v0.1.1-0.20200308034506-2af3d8e5d92a
github.com/spf13/cobra v0.0.6
github.com/spf13/viper v1.6.2
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
Expand All @@ -17,3 +17,5 @@ require (
google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383
google.golang.org/grpc v1.27.1
)

replace github.com/googleapis/gnostic v0.1.1-0.20200308034506-2af3d8e5d92a => github.com/timburks/gnostic v0.1.1-0.20200308034506-2af3d8e5d92a
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/timburks/gnostic v0.1.1-0.20200308034506-2af3d8e5d92a h1:HKO3x7JHI/3HnIZMHaNBtAPIa+VKHSyF/Brc55LEF+g=
github.com/timburks/gnostic v0.1.1-0.20200308034506-2af3d8e5d92a/go.mod h1:AQDghK/i6InCpKbR+4x4GhV5L9Olcza1PpNXMMcI8UQ=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down
14 changes: 14 additions & 0 deletions models/common.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package models

import (
"fmt"
"regexp"
)

const nameRegex = "([a-zA-Z0-9-_\\.]+)"

func validateID(id string) error {
r := regexp.MustCompile("^" + nameRegex + "$")
m := r.FindAllStringSubmatch(id, -1)
if m == nil {
return fmt.Errorf("invalid id '%s'", id)
}
return nil
}
15 changes: 8 additions & 7 deletions models/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func NewFileFromParentAndFileID(parent string, fileID string) (*File, error) {
if m == nil {
return nil, fmt.Errorf("invalid parent '%s'", parent)
}
if fileID == "" {
return nil, fmt.Errorf("invalid id '%s'", fileID)
if err := validateID(fileID); err != nil {
return nil, err
}
file := &File{}
file.ProjectID = m[0][1]
Expand Down Expand Up @@ -97,20 +97,21 @@ func NewFileFromMessage(message *rpc.File) (*File, error) {

// ResourceName generates the resource name of a file.
func (file *File) ResourceName() string {
return fmt.Sprintf("projects/%s/products/%s/versions/%s/specs/%s/files/%s", file.ProjectID, file.ProductID, file.VersionID, file.SpecID, file.FileID)
return fmt.Sprintf("projects/%s/products/%s/versions/%s/specs/%s/files/%s",
file.ProjectID, file.ProductID, file.VersionID, file.SpecID, file.FileID)
}

// Message returns a message representing a file.
func (file *File) Message() (message *rpc.File, err error) {
func (file *File) Message(view rpc.FileView) (message *rpc.File, err error) {
message = &rpc.File{}
message.Name = file.ResourceName()
message.DisplayName = file.DisplayName
message.Description = file.Description
message.CreateTime, err = ptypes.TimestampProto(file.CreateTime)
message.UpdateTime, err = ptypes.TimestampProto(file.UpdateTime)
message.Contents = file.Contents
//message.Availability = file.Availability
//message.RecommendedVersion = file.RecommendedVersion
if view == rpc.FileView_FULL {
message.Contents = file.Contents
}
return message, err
}

Expand Down
4 changes: 2 additions & 2 deletions models/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func NewProductFromParentAndProductID(parent string, productID string) (*Product
if m == nil {
return nil, fmt.Errorf("invalid parent '%s'", parent)
}
if productID == "" {
return nil, fmt.Errorf("invalid id '%s'", productID)
if err := validateID(productID); err != nil {
return nil, err
}
product := &Product{}
product.ProjectID = m[0][1]
Expand Down
4 changes: 2 additions & 2 deletions models/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func NewSpecFromParentAndSpecID(parent string, specID string) (*Spec, error) {
if m == nil {
return nil, fmt.Errorf("invalid parent '%s'", parent)
}
if specID == "" {
return nil, fmt.Errorf("invalid id '%s'", specID)
if err := validateID(specID); err != nil {
return nil, err
}
spec := &Spec{}
spec.ProjectID = m[0][1]
Expand Down
4 changes: 2 additions & 2 deletions models/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func NewVersionFromParentAndVersionID(parent string, versionID string) (*Version
if m == nil {
return nil, fmt.Errorf("invalid product '%s'", parent)
}
if versionID == "" {
return nil, fmt.Errorf("invalid id '%s'", versionID)
if err := validateID(versionID); err != nil {
return nil, err
}
version := &Version{}
version.ProjectID = m[0][1]
Expand Down
8 changes: 4 additions & 4 deletions server/actions_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *server) CreateFile(ctx context.Context, request *rpc.CreateFileRequest)
if err != nil {
return nil, err
}
return file.Message()
return file.Message(rpc.FileView_FULL)
}

func (s *server) DeleteFile(ctx context.Context, request *rpc.DeleteFileRequest) (*empty.Empty, error) {
Expand Down Expand Up @@ -76,7 +76,7 @@ func (s *server) GetFile(ctx context.Context, request *rpc.GetFileRequest) (*rpc
} else if err != nil {
return nil, internalError(err)
}
return file.Message()
return file.Message(request.GetView())
}

func (s *server) ListFiles(ctx context.Context, req *rpc.ListFilesRequest) (*rpc.ListFilesResponse, error) {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (s *server) ListFiles(ctx context.Context, req *rpc.ListFilesRequest) (*rpc
it := client.Run(ctx, q.Distinct())
_, err = it.Next(&file)
for err == nil {
fileMessage, _ := file.Message()
fileMessage, _ := file.Message(req.GetView())
fileMessages = append(fileMessages, fileMessage)
_, err = it.Next(&file)
}
Expand Down Expand Up @@ -144,5 +144,5 @@ func (s *server) UpdateFile(ctx context.Context, request *rpc.UpdateFileRequest)
if err != nil {
return nil, err
}
return file.Message()
return file.Message(rpc.FileView_FULL)
}

0 comments on commit fbe181b

Please sign in to comment.