Skip to content

Commit 20d25bd

Browse files
authored
feat: service registry artifact commands (#859)
1 parent 46373d5 commit 20d25bd

File tree

21 files changed

+1645
-47
lines changed

21 files changed

+1645
-47
lines changed

.vscode/launch.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@
233233
"args": [
234234
"version"
235235
]
236-
}
236+
},
237+
{
238+
"name": "Artifact Delete",
239+
"type": "go",
240+
"request": "launch",
241+
"mode": "auto",
242+
"program": "${workspaceFolder}/cmd/rhoas",
243+
"env": {},
244+
"args": [
245+
"service-registry",
246+
"artifact",
247+
"delete",
248+
"--group=tgdsfgdfg",
249+
"--yes"
250+
]
251+
},
237252
]
238253
}

pkg/api/api.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"github.com/redhat-developer/app-services-cli/pkg/api/ams/amsclient"
55
kafkainstanceclient "github.com/redhat-developer/app-services-sdk-go/kafkainstance/apiv1internal/client"
66
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
7-
srsmgmtclient "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client"
7+
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
8+
registrymgmtclient "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client"
89
)
910

1011
// API is a type which defines a number of API creator functions
1112
type API struct {
12-
Kafka func() kafkamgmtclient.DefaultApi
13-
ServiceAccount func() kafkamgmtclient.SecurityApi
14-
KafkaAdmin func(kafkaID string) (*kafkainstanceclient.APIClient, *kafkamgmtclient.KafkaRequest, error)
15-
AccountMgmt func() amsclient.DefaultApi
13+
Kafka func() kafkamgmtclient.DefaultApi
14+
ServiceAccount func() kafkamgmtclient.SecurityApi
15+
KafkaAdmin func(kafkaID string) (*kafkainstanceclient.APIClient, *kafkamgmtclient.KafkaRequest, error)
16+
ServiceRegistryInstance func(registryID string) (*registryinstanceclient.APIClient, *registrymgmtclient.RegistryRest, error)
17+
AccountMgmt func() amsclient.DefaultApi
1618

17-
ServiceRegistryMgmt func() srsmgmtclient.RegistriesApi
19+
ServiceRegistryMgmt func() registrymgmtclient.RegistriesApi
1820
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package artifact
2+
3+
import (
4+
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
5+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/create"
6+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/delete"
7+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/get"
8+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/list"
9+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/crud/update"
10+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/download"
11+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/metadata"
12+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/versions"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
func NewArtifactsCommand(f *factory.Factory) *cobra.Command {
17+
cmd := &cobra.Command{
18+
Use: "artifact",
19+
Short: "Manage Service Registry Artifacts",
20+
Long: `
21+
Apicurio Registry Artifacts enables developers to manage and share the structure of their data.
22+
For example, client applications can dynamically push or pull the latest updates to or from the registry without needing to redeploy.
23+
Apicurio Registry also enables developers to create rules that govern how registry content can evolve over time.
24+
For example, this includes rules for content validation and version compatibility.
25+
26+
Registry commands enable client applications to manage the artifacts in the registry.
27+
This set of commands provide create, read, update, and delete operations for schema and API artifacts, rules, versions, and metadata.
28+
`,
29+
Example: `
30+
## Create artifact in my-group from schema.json file
31+
rhoas service-registry artifact create --artifact-id=my-artifact --group=my-group artifact.json
32+
33+
## Get artifact content
34+
rhoas service-registry artifact get --artifact-id=my-artifact --group=my-group file.json
35+
36+
## Delete artifact
37+
rhoas service-registry artifact delete --artifact-id=my-artifact
38+
39+
## Get artifact metadata
40+
rhoas service-registry artifact metadata --artifact-id=my-artifact --group=my-group
41+
42+
## Update artifact
43+
rhoas service-registry artifact update --artifact-id=my-artifact artifact-new.json
44+
45+
## List Artifacts
46+
rhoas service-registry artifact list --group=my-group --limit=10 page=1
47+
48+
## View artifact versions
49+
rhoas service-registry artifact versions --artifact-id=my-artifact --group=my-group
50+
`,
51+
Args: cobra.MinimumNArgs(1),
52+
}
53+
54+
// add sub-commands
55+
cmd.AddCommand(
56+
// CRUD
57+
create.NewCreateCommand(f),
58+
get.NewGetCommand(f),
59+
delete.NewDeleteCommand(f),
60+
list.NewListCommand(f),
61+
update.NewUpdateCommand(f),
62+
63+
// Misc
64+
metadata.NewMetadataCommand(f),
65+
versions.NewVersionsCommand(f),
66+
download.NewDownloadCommand(f),
67+
)
68+
69+
return cmd
70+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package create
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"os"
8+
9+
"github.com/redhat-developer/app-services-cli/pkg/connection"
10+
"github.com/redhat-developer/app-services-cli/pkg/dump"
11+
"github.com/redhat-developer/app-services-cli/pkg/localize"
12+
"github.com/redhat-developer/app-services-cli/pkg/serviceregistry/registryinstanceerror"
13+
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
14+
15+
"github.com/redhat-developer/app-services-cli/pkg/cmd/flag"
16+
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/util"
17+
flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags"
18+
19+
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
20+
21+
"github.com/redhat-developer/app-services-cli/pkg/logging"
22+
23+
"github.com/spf13/cobra"
24+
25+
"github.com/redhat-developer/app-services-cli/internal/config"
26+
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
27+
)
28+
29+
type Options struct {
30+
artifact string
31+
group string
32+
33+
file string
34+
artifactType string
35+
36+
registryID string
37+
outputFormat string
38+
39+
IO *iostreams.IOStreams
40+
Config config.IConfig
41+
Connection factory.ConnectionFunc
42+
Logger func() (logging.Logger, error)
43+
localizer localize.Localizer
44+
}
45+
46+
var longDescription = `
47+
Creates a new artifact by posting the artifact content to the registry.
48+
49+
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).
50+
The registry attempts to figure out what kind of artifact is being added from the following supported list:
51+
52+
- Avro (AVRO)
53+
- Protobuf (PROTOBUF)
54+
- JSON Schema (JSON)
55+
- Kafka Connect (KCONNECT)
56+
- OpenAPI (OPENAPI)
57+
- AsyncAPI (ASYNCAPI)
58+
- GraphQL (GRAPHQL)
59+
- Web Services Description Language (WSDL)
60+
- XML Schema (XSD)
61+
62+
An artifact is created using the content provided in the body of the request.
63+
This content is created under a unique artifact ID that can be provided by user.
64+
If not provided in the request, the server generates a unique ID for the artifact.
65+
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.
66+
If an artifact with the provided artifact ID already exists command will fail with error.
67+
68+
69+
When --group parameter is missing the command will create a new artifact under the "default" group.
70+
when --registry is missing the command will create a new artifact for currently active service registry (visible in rhoas service-registry describe)
71+
`
72+
73+
func NewCreateCommand(f *factory.Factory) *cobra.Command {
74+
opts := &Options{
75+
IO: f.IOStreams,
76+
Config: f.Config,
77+
Connection: f.Connection,
78+
Logger: f.Logger,
79+
localizer: f.Localizer,
80+
}
81+
82+
cmd := &cobra.Command{
83+
Use: "create",
84+
Short: "Creates new artifact from file or standard input",
85+
Long: longDescription,
86+
Example: `
87+
# Create an artifact in default group
88+
rhoas service-registry artifact create my-artifact.json
89+
90+
# Create an artifact with specified type
91+
rhoas service-registry artifact create --type=JSON my-artifact.json
92+
`,
93+
Args: cobra.RangeArgs(0, 1),
94+
RunE: func(cmd *cobra.Command, args []string) error {
95+
validOutputFormats := flagutil.ValidOutputFormats
96+
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
97+
return flag.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
98+
}
99+
100+
if len(args) > 0 {
101+
opts.file = args[0]
102+
}
103+
104+
if opts.registryID != "" {
105+
return runCreate(opts)
106+
}
107+
108+
cfg, err := opts.Config.Load()
109+
if err != nil {
110+
return err
111+
}
112+
113+
if opts.artifactType != "" {
114+
if _, err = registryinstanceclient.NewArtifactTypeFromValue(opts.artifactType); err != nil {
115+
return errors.New("invalid artifact type. Please use one of following values: " + util.GetAllowedArtifactTypeEnumValuesAsString())
116+
}
117+
}
118+
119+
if !cfg.HasServiceRegistry() {
120+
return errors.New("no service Registry selected. Use 'rhoas service-registry use' use to select your registry")
121+
}
122+
123+
opts.registryID = fmt.Sprint(cfg.Services.ServiceRegistry.InstanceID)
124+
return runCreate(opts)
125+
},
126+
}
127+
128+
cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("registry.cmd.flag.output.description"))
129+
cmd.Flags().StringVarP(&opts.file, "file", "f", "", "File location of the artifact")
130+
131+
cmd.Flags().StringVarP(&opts.artifact, "artifact-id", "a", "", "Id of the artifact")
132+
cmd.Flags().StringVarP(&opts.group, "group", "g", util.DefaultArtifactGroup, "Group of the artifact")
133+
cmd.Flags().StringVarP(&opts.artifactType, "type", "t", "", "Type of artifact. Choose from: "+util.GetAllowedArtifactTypeEnumValuesAsString())
134+
cmd.Flags().StringVarP(&opts.registryID, "instance-id", "", "", "Id of the registry to be used. By default uses currently selected registry")
135+
136+
flagutil.EnableOutputFlagCompletion(cmd)
137+
_ = cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
138+
return util.AllowedArtifactTypeEnumValues, cobra.ShellCompDirectiveNoSpace
139+
})
140+
141+
return cmd
142+
}
143+
144+
func runCreate(opts *Options) error {
145+
146+
logger, err := opts.Logger()
147+
if err != nil {
148+
return err
149+
}
150+
151+
conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth)
152+
if err != nil {
153+
return err
154+
}
155+
156+
dataAPI, _, err := conn.API().ServiceRegistryInstance(opts.registryID)
157+
if err != nil {
158+
return err
159+
}
160+
161+
if opts.group == util.DefaultArtifactGroup {
162+
logger.Info("Group was not specified. Using", util.DefaultArtifactGroup, "artifacts group.")
163+
opts.group = util.DefaultArtifactGroup
164+
}
165+
166+
var specifiedFile *os.File
167+
if opts.file != "" {
168+
logger.Info("Opening file: " + opts.file)
169+
specifiedFile, err = os.Open(opts.file)
170+
if err != nil {
171+
return err
172+
}
173+
} else {
174+
logger.Info("Reading file content from standard input")
175+
specifiedFile, err = util.CreateFileFromStdin()
176+
if err != nil {
177+
return err
178+
}
179+
}
180+
181+
ctx := context.Background()
182+
request := dataAPI.ArtifactsApi.CreateArtifact(ctx, opts.group)
183+
if opts.artifactType != "" {
184+
request = request.XRegistryArtifactType(registryinstanceclient.ArtifactType(opts.artifactType))
185+
}
186+
if opts.artifact != "" {
187+
request = request.XRegistryArtifactId(opts.artifact)
188+
}
189+
190+
request = request.Body(specifiedFile)
191+
metadata, _, err := request.Execute()
192+
if err != nil {
193+
return registryinstanceerror.TransformError(err)
194+
}
195+
logger.Info("Artifact created")
196+
197+
dump.PrintDataInFormat(opts.outputFormat, metadata, opts.IO.Out)
198+
199+
return nil
200+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package crud

0 commit comments

Comments
 (0)