From 75cd34b3944e3a203318247b2fe8d1d1719982f0 Mon Sep 17 00:00:00 2001 From: simisoft-exo Date: Thu, 12 Sep 2024 15:02:27 +0100 Subject: [PATCH] create and show rsyslog --- cmd/dbaas_external_endpoint_create.go | 4 +- cmd/dbaas_external_endpoint_create_rsyslog.go | 74 ++++++++++++++++++ cmd/dbaas_external_endpoint_show.go | 2 + cmd/dbaas_external_endpoint_show_rsyslog.go | 77 +++++++++++++++++++ 4 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 cmd/dbaas_external_endpoint_create_rsyslog.go create mode 100644 cmd/dbaas_external_endpoint_show_rsyslog.go diff --git a/cmd/dbaas_external_endpoint_create.go b/cmd/dbaas_external_endpoint_create.go index 709632e2..733e9ec2 100644 --- a/cmd/dbaas_external_endpoint_create.go +++ b/cmd/dbaas_external_endpoint_create.go @@ -101,8 +101,8 @@ func (c *dbaasExternalEndpointCreateCmd) cmdRun(cmd *cobra.Command, args []strin return c.createElasticsearch(cmd, args) case "prometheus": return c.createPrometheus(cmd, args) - // case "rsyslog": - // return c.createRsyslog(cmd, args) + case "rsyslog": + return c.createRsyslog(cmd, args) default: return fmt.Errorf("unsupported external endpoint type %q", c.Type) } diff --git a/cmd/dbaas_external_endpoint_create_rsyslog.go b/cmd/dbaas_external_endpoint_create_rsyslog.go new file mode 100644 index 00000000..39df2ee2 --- /dev/null +++ b/cmd/dbaas_external_endpoint_create_rsyslog.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "github.com/exoscale/cli/pkg/account" + "github.com/exoscale/cli/pkg/globalstate" + v3 "github.com/exoscale/egoscale/v3" +) + +func (c *dbaasExternalEndpointCreateCmd) createRsyslog(_ *cobra.Command, _ []string) error { + ctx := gContext + client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone)) + if err != nil { + return err + } + + rsyslogRequestPayload := v3.CreateDBAASExternalEndpointRsyslogRequest{ + Settings: &v3.DBAASEndpointRsyslog{}, + } + + if c.RsyslogCA != "" { + rsyslogRequestPayload.Settings.CA = c.RsyslogCA + } + if c.RsyslogCert != "" { + rsyslogRequestPayload.Settings.Cert = c.RsyslogCert + } + if c.RsyslogFormat != "" { + rsyslogRequestPayload.Settings.Format = v3.EnumRsyslogFormat(c.RsyslogFormat) + } + if c.RsyslogLogline != "" { + rsyslogRequestPayload.Settings.Logline = c.RsyslogLogline + } + if c.RsyslogKey != "" { + rsyslogRequestPayload.Settings.Key = c.RsyslogKey + } + if c.RsyslogPort != 0 { + rsyslogRequestPayload.Settings.Port = c.RsyslogPort + } + if c.RsyslogMaxMessageSize != 0 { + rsyslogRequestPayload.Settings.MaxMessageSize = c.RsyslogMaxMessageSize + } + if c.RsyslogSD != "" { + rsyslogRequestPayload.Settings.SD = c.RsyslogSD + } + if c.RsyslogServer != "" { + rsyslogRequestPayload.Settings.Server = c.RsyslogServer + } + if c.RsyslogTls { + rsyslogRequestPayload.Settings.Tls = v3.Bool(c.RsyslogTls) + } + + op, err := client.CreateDBAASExternalEndpointRsyslog(ctx, c.Name, rsyslogRequestPayload) + if err != nil { + return err + } + + decorateAsyncOperation(fmt.Sprintf("Creating DBaaS Rsyslog external Endpoint %q", c.Name), func() { + op, err = client.Wait(ctx, op, v3.OperationStateSuccess) + }) + if err != nil { + return err + } + + endpointID := op.Reference.ID.String() + if !globalstate.Quiet { + return (&dbaasExternalEndpointShowCmd{ + cliCommandSettings: defaultCLICmdSettings(), + EndpointID: endpointID, + Type: "rsyslog", + }).cmdRun(nil, nil) + } + return nil +} diff --git a/cmd/dbaas_external_endpoint_show.go b/cmd/dbaas_external_endpoint_show.go index 6340e4e2..102e616c 100644 --- a/cmd/dbaas_external_endpoint_show.go +++ b/cmd/dbaas_external_endpoint_show.go @@ -40,6 +40,8 @@ func (c *dbaasExternalEndpointShowCmd) cmdRun(cmd *cobra.Command, args []string) return c.outputFunc(c.showElasticsearch()) case "prometheus": return c.outputFunc(c.showPrometheus()) + case "rsyslog": + return c.outputFunc(c.showRsyslog()) default: return fmt.Errorf("unsupported external endpoint type %q", c.Type) } diff --git a/cmd/dbaas_external_endpoint_show_rsyslog.go b/cmd/dbaas_external_endpoint_show_rsyslog.go new file mode 100644 index 00000000..e5f5439b --- /dev/null +++ b/cmd/dbaas_external_endpoint_show_rsyslog.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "fmt" + "os" + "strconv" + + "github.com/exoscale/cli/pkg/account" + "github.com/exoscale/cli/pkg/globalstate" + "github.com/exoscale/cli/pkg/output" + "github.com/exoscale/cli/table" + v3 "github.com/exoscale/egoscale/v3" +) + +type rsyslogOutput struct { + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Settings v3.DBAASEndpointRsyslog `json:"settings"` +} + +func (o *rsyslogOutput) ToJSON() { output.JSON(o) } +func (o *rsyslogOutput) ToText() { output.Text(o) } +func (o *rsyslogOutput) ToTable() { + t := table.NewTable(os.Stdout) + t.SetHeader([]string{"Rsyslog External Endpoint"}) + defer t.Render() + + t.Append([]string{"Endpoint ID", o.ID}) + t.Append([]string{"Endpoint Name", o.Name}) + t.Append([]string{"Endpoint Type", o.Type}) + + settings := o.Settings + tls := "false" + +if settings.Tls != nil && *settings.Tls { + tls = "true" + } + + t.Append([]string{"Server", settings.Server}) + t.Append([]string{"Port", strconv.FormatInt(settings.Port,10)}) + t.Append([]string{"Tls", tls}) + t.Append([]string{"CA Certificate", truncateString(settings.CA, 50)}) + t.Append([]string{"Client Certificate", truncateString(settings.Cert, 50)}) + t.Append([]string{"Client Key", truncateString(settings.Key, 50)}) + t.Append([]string{"Max Message Size", strconv.FormatInt(settings.MaxMessageSize, 10)}) + t.Append([]string{"Structured data block", settings.SD}) + t.Append([]string{"Custom logline format", settings.Logline}) + t.Append([]string{"Custom logline format", settings.Logline}) +} + +func (c *dbaasExternalEndpointShowCmd) showRsyslog() (output.Outputter, error) { + ctx := gContext + client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone)) + if err != nil { + return nil, err + } + + endpointUUID, err := v3.ParseUUID(c.EndpointID) + if err != nil { + return nil, fmt.Errorf("invalid endpoint ID: %w", err) + } + + endpointResponse, err := client.GetDBAASExternalEndpointRsyslog(ctx, endpointUUID) + if err != nil { + return nil, fmt.Errorf("error getting Rsyslog external endpoint: %w", err) + } + + output := &rsyslogOutput{ + ID: endpointResponse.ID.String(), + Name: endpointResponse.Name, + Type: string(endpointResponse.Type), + Settings: *endpointResponse.Settings, + } + + return output, nil +}