-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: initial boilerplate for dataplane commands
- Loading branch information
Showing
20 changed files
with
1,642 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package artifact | ||
|
||
import ( | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/create" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/delete" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/get" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/list" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/update" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/download" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/metadata" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/versions" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewArtifactsCommand(f *factory.Factory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "artifacts", | ||
Short: "Manage Service Registry Artifacts commands", | ||
Long: `Apicurio Registry Artifacts enables developers to manage and share the structure of their data. | ||
For example, client applications can dynamically push or pull the latest updates to or from the registry without needing to redeploy. | ||
Apicurio Registry also enables developers to create rules that govern how registry content can evolve over time. | ||
For example, this includes rules for content validation and version compatibility. | ||
Registry commands enable client applications to manage the artifacts in the registry. | ||
This set of commands provide create, read, update, and delete operations for schema and API artifacts, rules, versions, and metadata.`, | ||
Example: ` | ||
## Create artifact in my-group from schema.json file | ||
rhoas service-registry artifacts create my-group schema.json | ||
## List Artifacts | ||
rhoas service-registry artifacts list my-group | ||
`, | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
|
||
// add sub-commands | ||
cmd.AddCommand( | ||
// CRUD | ||
create.NewCreateCommand(f), | ||
get.NewGetCommand(f), | ||
delete.NewDeleteCommand(f), | ||
list.NewListCommand(f), | ||
update.NewUpdateCommand(f), | ||
|
||
// Misc | ||
metadata.NewMetadataCommand(f), | ||
versions.NewVersionsCommand(f), | ||
download.NewDownloadCommand(f), | ||
) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
package create | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/cmdutil" | ||
"github.com/redhat-developer/app-services-cli/pkg/connection" | ||
"github.com/redhat-developer/app-services-cli/pkg/dump" | ||
"github.com/redhat-developer/app-services-cli/pkg/localize" | ||
"github.com/redhat-developer/app-services-cli/pkg/serviceregistry/registryinstanceerror" | ||
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client" | ||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/cmd/flag" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/util" | ||
flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/iostreams" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/logging" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/redhat-developer/app-services-cli/internal/config" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory" | ||
) | ||
|
||
type Options struct { | ||
artifact string | ||
group string | ||
|
||
file string | ||
artifactType string | ||
|
||
registryID string | ||
outputFormat string | ||
|
||
IO *iostreams.IOStreams | ||
Config config.IConfig | ||
Connection factory.ConnectionFunc | ||
Logger func() (logging.Logger, error) | ||
localizer localize.Localizer | ||
} | ||
|
||
var longDescription = ` | ||
Creates a new artifact by posting the artifact content to the registry. | ||
Artifacts can be typically in JSON format for most of the supported types, but may be in another format for a few (for example, PROTOBUF). | ||
The registry attempts to figure out what kind of artifact is being added from the following supported list: | ||
- Avro (AVRO) | ||
- Protobuf (PROTOBUF) | ||
- JSON Schema (JSON) | ||
- Kafka Connect (KCONNECT) | ||
- OpenAPI (OPENAPI) | ||
- AsyncAPI (ASYNCAPI) | ||
- GraphQL (GRAPHQL) | ||
- Web Services Description Language (WSDL) | ||
- XML Schema (XSD) | ||
An artifact is created using the content provided in the body of the request. | ||
This content is created under a unique artifact ID that can be provided by user. | ||
If not provided in the request, the server generates a unique ID for the artifact. | ||
It is typically recommended that callers provide the ID, because this is a meaningful identifier, and for most use cases should be supplied by the caller. | ||
If an artifact with the provided artifact ID already exists command will fail with error. | ||
When --group parameter is missing the command will create a new artifact under the "default" group. | ||
when --registry is missing the command will create a new artifact for currently active service registry (visible in rhoas service-registry describe) | ||
` | ||
|
||
func NewCreateCommand(f *factory.Factory) *cobra.Command { | ||
opts := &Options{ | ||
IO: f.IOStreams, | ||
Config: f.Config, | ||
Connection: f.Connection, | ||
Logger: f.Logger, | ||
localizer: f.Localizer, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Creates new artifact from file or standard input", | ||
Long: longDescription, | ||
Example: ` | ||
## Create artifact in my-group from schema.json file | ||
rhoas service-registry artifacts create my-group schema.json | ||
# Create an artifact in default group | ||
rhoas service-registry artifacts create my-artifact.json | ||
# Create an artifact with specified type | ||
rhoas service-registry artifacts create --type=JSON my-artifact.json | ||
`, | ||
Args: cobra.RangeArgs(0, 1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
validOutputFormats := flagutil.ValidOutputFormats | ||
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) { | ||
return flag.InvalidValueError("output", opts.outputFormat, validOutputFormats...) | ||
} | ||
|
||
if len(args) > 0 { | ||
opts.file = args[0] | ||
} | ||
|
||
if opts.registryID != "" { | ||
return runCreate(opts) | ||
} | ||
|
||
cfg, err := opts.Config.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if opts.artifactType != "" { | ||
if _, err = registryinstanceclient.NewArtifactTypeFromValue(opts.artifactType); err != nil { | ||
return errors.New("Invalid artifact type. Please use one of following values: " + util.GetAllowedArtifactTypeEnumValuesAsString()) | ||
} | ||
} | ||
|
||
if !cfg.HasServiceRegistry() { | ||
return errors.New("No service Registry selected. Use 'rhoas service-registry use' use to select your registry") | ||
} | ||
|
||
opts.registryID = fmt.Sprint(cfg.Services.ServiceRegistry.InstanceID) | ||
return runCreate(opts) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("registry.cmd.flag.output.description")) | ||
cmd.Flags().StringVarP(&opts.file, "file", "f", "", "File location of the artifact") | ||
|
||
cmd.Flags().StringVarP(&opts.artifact, "artifact", "a", "", "Id of the artifact") | ||
cmd.Flags().StringVarP(&opts.group, "group", "g", "", "Group of the artifact") | ||
cmd.Flags().StringVarP(&opts.artifactType, "type", "t", "", "Type of artifact. Choose from: "+util.GetAllowedArtifactTypeEnumValuesAsString()) | ||
cmd.Flags().StringVarP(&opts.registryID, "instance-id", "", "", "Id of the registry to be used. By default uses currently selected registry") | ||
|
||
flagutil.EnableOutputFlagCompletion(cmd) | ||
_ = cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return util.AllowedArtifactTypeEnumValues, cobra.ShellCompDirectiveNoSpace | ||
}) | ||
|
||
return cmd | ||
} | ||
|
||
func runCreate(opts *Options) error { | ||
|
||
logger, err := opts.Logger() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dataAPI, _, err := conn.API().ServiceRegistryInstance(opts.registryID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if opts.group == "" { | ||
logger.Info("Group was not specified. Using " + util.DefaultArtifactGroup + " artifacts group.") | ||
opts.group = util.DefaultArtifactGroup | ||
} | ||
|
||
var specifiedFile *os.File | ||
if opts.file != "" { | ||
logger.Info("Opening file: " + opts.file) | ||
specifiedFile, err = os.Open(opts.file) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
logger.Info("Reading file content from stdin") | ||
specifiedFile, err = util.CreateFileFromStdin() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
ctx := context.Background() | ||
request := dataAPI.ArtifactsApi.CreateArtifact(ctx, opts.group) | ||
if opts.artifactType != "" { | ||
request = request.XRegistryArtifactType(registryinstanceclient.ArtifactType(opts.artifactType)) | ||
} | ||
if opts.artifact != "" { | ||
request = request.XRegistryArtifactId(opts.artifact) | ||
} | ||
|
||
request = request.Body(specifiedFile) | ||
metadata, _, err := request.Execute() | ||
if err != nil { | ||
return registryinstanceerror.TransformError(err) | ||
} | ||
logger.Info("Artifact created") | ||
|
||
switch opts.outputFormat { | ||
case "json": | ||
data, _ := json.MarshalIndent(metadata, "", cmdutil.DefaultJSONIndent) | ||
_ = dump.JSON(opts.IO.Out, data) | ||
case "yaml", "yml": | ||
data, _ := yaml.Marshal(metadata) | ||
_ = dump.YAML(opts.IO.Out, data) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package crud |
Oops, something went wrong.