Skip to content

Commit

Permalink
Merge branch 'main' of github.com:apigee/registry into remove-v1alpha1
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Dec 13, 2022
2 parents b75cff6 + 647db57 commit 7056a47
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cmd/registry/cmd/compute/vocabulary.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func vocabularyCommand() *cobra.Command {
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get jobs from flags")
}
taskQueue, wait := core.WorkerPool(ctx, jobs)
taskQueue, wait := core.WorkerPoolWithWarnings(ctx, jobs)
defer wait()

parsed, err := names.ParseSpecRevision(path)
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/upload/bulk/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func discoveryCommand() *cobra.Command {
parent: parent,
apiID: sanitize(api.Name),
versionID: sanitize(api.Version),
specID: "discovery.json",
specID: "discovery",
}
}
return nil
Expand Down
71 changes: 38 additions & 33 deletions cmd/registry/core/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,65 +108,70 @@ func PrintArtifactContents(artifact *rpc.Artifact) error {
return nil
}

messageType, err := MessageTypeForMimeType(artifact.GetMimeType())
message, err := GetArtifactMessageContents(artifact)
if err != nil {
return err
}
PrintMessage(message)
return nil
}

func PrintMessage(message proto.Message) {
fmt.Println(protojson.Format(message))
}

func GetArtifactMessageContents(artifact *rpc.Artifact) (proto.Message, error) {
messageType, err := MessageTypeForMimeType(artifact.GetMimeType())
if err != nil {
return nil, err
}
switch messageType {
case "gnostic.metrics.Complexity":
return unmarshalAndPrint(artifact.GetContents(), &metrics.Complexity{})
return unmarshal(artifact.GetContents(), &metrics.Complexity{})
case "gnostic.metrics.Vocabulary":
return unmarshalAndPrint(artifact.GetContents(), &metrics.Vocabulary{})
return unmarshal(artifact.GetContents(), &metrics.Vocabulary{})
case "gnostic.metrics.VersionHistory":
return unmarshalAndPrint(artifact.GetContents(), &metrics.VersionHistory{})
case "google.cloud.apigeeregistry.applications.v1alpha1.Lint":
return unmarshalAndPrint(artifact.GetContents(), &rpc.Lint{})
case "google.cloud.apigeeregistry.applications.v1alpha1.LintStats":
return unmarshalAndPrint(artifact.GetContents(), &rpc.LintStats{})
return unmarshal(artifact.GetContents(), &metrics.VersionHistory{})
case "google.cloud.apigeeregistry.v1.apihub.DisplaySettings":
return unmarshalAndPrint(artifact.GetContents(), &rpc.DisplaySettings{})
return unmarshal(artifact.GetContents(), &rpc.DisplaySettings{})
case "google.cloud.apigeeregistry.v1.apihub.Lifecycle":
return unmarshalAndPrint(artifact.GetContents(), &rpc.Lifecycle{})
return unmarshal(artifact.GetContents(), &rpc.Lifecycle{})
case "google.cloud.apigeeregistry.v1.apihub.ReferenceList":
return unmarshalAndPrint(artifact.GetContents(), &rpc.ReferenceList{})
return unmarshal(artifact.GetContents(), &rpc.ReferenceList{})
case "google.cloud.apigeeregistry.v1.apihub.TaxonomyList":
return unmarshalAndPrint(artifact.GetContents(), &rpc.TaxonomyList{})
return unmarshal(artifact.GetContents(), &rpc.TaxonomyList{})
case "google.cloud.apigeeregistry.v1.controller.Manifest":
return unmarshalAndPrint(artifact.GetContents(), &rpc.Manifest{})
return unmarshal(artifact.GetContents(), &rpc.Manifest{})
case "google.cloud.apigeeregistry.v1.controller.Receipt":
return unmarshalAndPrint(artifact.GetContents(), &rpc.Receipt{})
return unmarshal(artifact.GetContents(), &rpc.Receipt{})
case "google.cloud.apigeeregistry.v1.scoring.Score":
return unmarshalAndPrint(artifact.GetContents(), &rpc.Score{})
return unmarshal(artifact.GetContents(), &rpc.Score{})
case "google.cloud.apigeeregistry.v1.scoring.ScoreCard":
return unmarshalAndPrint(artifact.GetContents(), &rpc.ScoreCard{})
return unmarshal(artifact.GetContents(), &rpc.ScoreCard{})
case "google.cloud.apigeeregistry.v1.scoring.ScoreDefinition":
return unmarshalAndPrint(artifact.GetContents(), &rpc.ScoreDefinition{})
return unmarshal(artifact.GetContents(), &rpc.ScoreDefinition{})
case "google.cloud.apigeeregistry.v1.scoring.ScoreCardDefinition":
return unmarshalAndPrint(artifact.GetContents(), &rpc.ScoreCardDefinition{})
return unmarshal(artifact.GetContents(), &rpc.ScoreCardDefinition{})
case "google.cloud.apigeeregistry.v1.style.ConformanceReport":
return unmarshalAndPrint(artifact.GetContents(), &rpc.ConformanceReport{})
return unmarshal(artifact.GetContents(), &rpc.ConformanceReport{})
case "google.cloud.apigeeregistry.v1.style.Lint":
return unmarshal(artifact.GetContents(), &rpc.Lint{})
case "google.cloud.apigeeregistry.v1.style.LintStats":
return unmarshal(artifact.GetContents(), &rpc.LintStats{})
case "google.cloud.apigeeregistry.v1.style.StyleGuide":
return unmarshalAndPrint(artifact.GetContents(), &rpc.StyleGuide{})
return unmarshal(artifact.GetContents(), &rpc.StyleGuide{})
case "gnostic.openapiv2.Document":
return unmarshalAndPrint(artifact.GetContents(), &openapiv2.Document{})
return unmarshal(artifact.GetContents(), &openapiv2.Document{})
case "gnostic.openapiv3.Document":
return unmarshalAndPrint(artifact.GetContents(), &openapiv3.Document{})
return unmarshal(artifact.GetContents(), &openapiv3.Document{})
default:
// To get a parent context here would require changing the ArtifactHandler type in handlers.go
return fmt.Errorf("Unsupported message type: %s, please use --raw to get raw contents", messageType)
return nil, fmt.Errorf("unsupported message type: %s", messageType)
}
}

func PrintMessage(message proto.Message) {
fmt.Println(protojson.Format(message))
}

func unmarshalAndPrint(value []byte, message proto.Message) error {
func unmarshal(value []byte, message proto.Message) (proto.Message, error) {
if err := proto.Unmarshal(value, message); err != nil {
return err
return nil, err
}

PrintMessage(message)
return nil
return message, nil
}
3 changes: 0 additions & 3 deletions cmd/registry/patch/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ func (p *patchGroup) add(task *applyFileTask) error {
return err
}
task.kind = header.Kind
if header.Metadata.Parent != "" {
task.parent = task.parent + "/" + header.Metadata.Parent
}
switch task.kind {
case "API":
p.apiTasks = append(p.apiTasks, task)
Expand Down
24 changes: 18 additions & 6 deletions cmd/registry/patch/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/apigee/registry/pkg/models"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/names"
metrics "github.com/google/gnostic/metrics"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -164,8 +165,11 @@ func applyArtifactPatchBytes(ctx context.Context, client connection.RegistryClie
return applyArtifactPatch(ctx, client, &artifact, parent)
}

func artifactName(parent, artifactID string) (names.Artifact, error) {
return names.ParseArtifact(parent + "/artifacts/" + artifactID)
func artifactName(parent string, metadata models.Metadata) (names.Artifact, error) {
if metadata.Parent != "" {
parent = parent + "/" + metadata.Parent
}
return names.ParseArtifact(parent + "/artifacts/" + metadata.Name)
}

func applyArtifactPatch(ctx context.Context, client connection.RegistryClient, content *models.Artifact, parent string) error {
Expand All @@ -177,7 +181,7 @@ func applyArtifactPatch(ctx context.Context, client connection.RegistryClient, c
return err
}
// Populate Id and Kind fields in the contents of the artifact
j, err = populateIdAndKind(j, content.Kind, content.Metadata.Name)
jWithIdAndKind, err := populateIdAndKind(j, content.Kind, content.Metadata.Name)
if err != nil {
return err
}
Expand All @@ -187,16 +191,22 @@ func applyArtifactPatch(ctx context.Context, client connection.RegistryClient, c
if err != nil {
return err
}
err = protojson.Unmarshal(j, m)
err = protojson.Unmarshal(jWithIdAndKind, m)
if err != nil {
return err
if strings.Contains(err.Error(), "unknown field") {
// Try unmarshaling the original YAML (without the additional Id and Kind fields).
err = protojson.Unmarshal(j, m)
if err != nil {
return err
}
}
}
// Marshal the message struct to bytes.
bytes, err := proto.Marshal(m)
if err != nil {
return err
}
name, err := artifactName(parent, content.Header.Metadata.Name)
name, err := artifactName(parent, content.Header.Metadata)
if err != nil {
return err
}
Expand Down Expand Up @@ -295,4 +305,6 @@ var artifactMessageTypes map[string]messageFactory = map[string]messageFactory{
"google.cloud.apigeeregistry.v1.style.StyleGuide": func() proto.Message { return new(rpc.StyleGuide) },
"google.cloud.apigeeregistry.v1.style.ConformanceReport": func() proto.Message { return new(rpc.ConformanceReport) },
"google.cloud.apigeeregistry.v1.style.Lint": func() proto.Message { return new(rpc.Lint) },
"gnostic.metrics.Complexity": func() proto.Message { return new(metrics.Complexity) },
"gnostic.metrics.Vocabulary": func() proto.Message { return new(metrics.Vocabulary) },
}
9 changes: 6 additions & 3 deletions cmd/registry/patch/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,23 @@ func applyApiDeploymentPatchBytes(ctx context.Context, client connection.Registr
return applyApiDeploymentPatch(ctx, client, &deployment, parent)
}

func deploymentName(parent, deploymentID string) (names.Deployment, error) {
func deploymentName(parent string, metadata models.Metadata) (names.Deployment, error) {
if metadata.Parent != "" {
parent = parent + "/" + metadata.Parent
}
api, err := names.ParseApi(parent)
if err != nil {
return names.Deployment{}, err
}
return api.Deployment(deploymentID), nil
return api.Deployment(metadata.Name), nil
}

func applyApiDeploymentPatch(
ctx context.Context,
client connection.RegistryClient,
deployment *models.ApiDeployment,
parent string) error {
name, err := deploymentName(parent, deployment.Metadata.Name)
name, err := deploymentName(parent, deployment.Metadata)
if err != nil {
return err
}
Expand Down
125 changes: 125 additions & 0 deletions cmd/registry/patch/patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package patch

import (
"context"
"os"
"testing"

"github.com/apigee/registry/cmd/registry/core"
"github.com/apigee/registry/pkg/connection"
"github.com/apigee/registry/pkg/connection/grpctest"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry"
"github.com/apigee/registry/server/registry/names"
"github.com/apigee/registry/server/registry/test/seeder"
metrics "github.com/google/gnostic/metrics"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
)

// TestMain will set up a local RegistryServer and grpc.Server for all
// tests in this package if APG_REGISTRY_ADDRESS env var is not set
// for the client.
func TestMain(m *testing.M) {
grpctest.TestMain(m, registry.Config{})
}

func TestSpecArtifactPatches(t *testing.T) {
tests := []struct {
artifactName string
yamlFileName string
message proto.Message
}{
{
artifactName: "complexity",
yamlFileName: "testdata/artifacts/complexity.yaml",
message: &metrics.Complexity{
PathCount: 76,
GetCount: 25,
PostCount: 27,
PutCount: 11,
DeleteCount: 13,
SchemaCount: 1150,
SchemaPropertyCount: 964,
},
},
{
artifactName: "vocabulary",
yamlFileName: "testdata/artifacts/vocabulary.yaml",
message: &metrics.Vocabulary{
Name: "sample-name",
Schemas: []*metrics.WordCount{{Word: "sample-schema", Count: 1}},
Properties: []*metrics.WordCount{{Word: "sample-property", Count: 2}},
Operations: []*metrics.WordCount{{Word: "sample-operation", Count: 3}},
Parameters: []*metrics.WordCount{{Word: "sample-parameter", Count: 4}},
},
},
}
ctx := context.Background()
adminClient, err := connection.NewAdminClient(ctx)
if err != nil {
t.Fatalf("Setup: failed to create client: %+v", err)
}
defer adminClient.Close()
registryClient, err := connection.NewRegistryClient(ctx)
if err != nil {
t.Fatalf("Setup: Failed to create registry client: %s", err)
}
defer registryClient.Close()
client := seeder.Client{
RegistryClient: registryClient,
AdminClient: adminClient,
}
spec := &rpc.ApiSpec{
Name: "projects/patch-project-test/locations/global/apis/a/versions/v/specs/s",
}
if err := seeder.SeedSpecs(ctx, client, spec); err != nil {
t.Fatalf("Setup/Seeding: Failed to seed registry: %s", err)
}
for _, test := range tests {
t.Run(test.artifactName, func(t *testing.T) {
b, err := os.ReadFile(test.yamlFileName)
if err != nil {
t.Fatalf("%s", err)
}
err = applyArtifactPatchBytes(ctx, registryClient, b, "projects/patch-project-test/locations/global")
if err != nil {
t.Fatalf("%s", err)
}
artifactName, err := names.ParseArtifact("projects/patch-project-test/locations/global/apis/a/versions/v/specs/s/artifacts/" + test.artifactName)
if err != nil {
t.Fatalf("%s", err)
}
err = core.GetArtifact(ctx, registryClient, artifactName, true,
func(artifact *rpc.Artifact) error {
contents, err := core.GetArtifactMessageContents(artifact)
if err != nil {
t.Fatalf("%s", err)
}
opts := cmp.Options{protocmp.Transform()}
if !cmp.Equal(test.message, contents, opts) {
t.Errorf("GetDiff returned unexpected diff (-want +got):\n%s", cmp.Diff(test.message, contents, opts))
}
out, header, err := ExportArtifact(ctx, registryClient, artifact)
if err != nil {
t.Fatalf("%s", err)
}
wantedParent := "apis/a/versions/v/specs/s"
if header.Metadata.Parent != wantedParent {
t.Errorf("Incorrect export parent. Wanted %s, got %s", wantedParent, header.Metadata.Parent)
}
if header.Metadata.Name != test.artifactName {
t.Errorf("Incorrect export name. Wanted %s, got %s", test.artifactName, header.Metadata.Name)
}
if !cmp.Equal(b, out, opts) {
t.Errorf("GetDiff returned unexpected diff (-want +got):\n%s", cmp.Diff(b, out, opts))
}
return nil
})
if err != nil {
t.Fatalf("%s", err)
}
})
}
}
9 changes: 6 additions & 3 deletions cmd/registry/patch/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,23 @@ func applyApiSpecPatchBytes(
return applyApiSpecPatch(ctx, client, &spec, parent)
}

func specName(parent, specID string) (names.Spec, error) {
func specName(parent string, metadata models.Metadata) (names.Spec, error) {
if metadata.Parent != "" {
parent = parent + "/" + metadata.Parent
}
version, err := names.ParseVersion(parent)
if err != nil {
return names.Spec{}, err
}
return version.Spec(specID), nil
return version.Spec(metadata.Name), nil
}

func applyApiSpecPatch(
ctx context.Context,
client connection.RegistryClient,
spec *models.ApiSpec,
parent string) error {
name, err := specName(parent, spec.Metadata.Name)
name, err := specName(parent, spec.Metadata)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/registry/patch/testdata/artifacts/complexity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: apigeeregistry/v1
kind: Complexity
metadata:
name: complexity
parent: apis/a/versions/v/specs/s
data:
pathCount: 76
getCount: 25
postCount: 27
putCount: 11
deleteCount: 13
schemaCount: 1150
schemaPropertyCount: 964
Loading

0 comments on commit 7056a47

Please sign in to comment.