|
| 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 | +} |
0 commit comments