From ac1643a23d861f5c98f0da1896ea29678a0462bb Mon Sep 17 00:00:00 2001 From: simisoft-exo Date: Thu, 12 Sep 2024 13:46:34 +0100 Subject: [PATCH] create and show prometheus --- cmd/dbaas_external_endpoint_create.go | 4 +- ...aas_external_endpoint_create_prometheus.go | 50 +++++++++++++++ cmd/dbaas_external_endpoint_show.go | 2 + ...dbaas_external_endpoint_show_prometheus.go | 62 +++++++++++++++++++ 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 cmd/dbaas_external_endpoint_create_prometheus.go create mode 100644 cmd/dbaas_external_endpoint_show_prometheus.go diff --git a/cmd/dbaas_external_endpoint_create.go b/cmd/dbaas_external_endpoint_create.go index 00b33453..709632e2 100644 --- a/cmd/dbaas_external_endpoint_create.go +++ b/cmd/dbaas_external_endpoint_create.go @@ -99,8 +99,8 @@ func (c *dbaasExternalEndpointCreateCmd) cmdRun(cmd *cobra.Command, args []strin return c.createOpensearch(cmd, args) case "elasticsearch": return c.createElasticsearch(cmd, args) - // case "prometheus": - // return c.createPrometheus(cmd, args) + case "prometheus": + return c.createPrometheus(cmd, args) // case "rsyslog": // return c.createRsyslog(cmd, args) default: diff --git a/cmd/dbaas_external_endpoint_create_prometheus.go b/cmd/dbaas_external_endpoint_create_prometheus.go new file mode 100644 index 00000000..c3098eb2 --- /dev/null +++ b/cmd/dbaas_external_endpoint_create_prometheus.go @@ -0,0 +1,50 @@ +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) createPrometheus(_ *cobra.Command, _ []string) error { + ctx := gContext + client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone)) + if err != nil { + return err + } + + prometheusRequestPayload := v3.CreateDBAASExternalEndpointPrometheusRequest{ + Settings: &v3.DBAASEndpointPrometheus{}, + } + + if c.PrometheusBasicAuthPassword != "" { + prometheusRequestPayload.Settings.BasicAuthPassword= c.PrometheusBasicAuthPassword + } + if c.PrometheusBasicAuthUsername != "" { + prometheusRequestPayload.Settings.BasicAuthUsername= c.PrometheusBasicAuthUsername + } + + op, err := client.CreateDBAASExternalEndpointPrometheus(ctx, c.Name, prometheusRequestPayload) + if err != nil { + return err + } + + decorateAsyncOperation(fmt.Sprintf("Creating DBaaS Prometheus 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: "prometheus", + }).cmdRun(nil, nil) + } + return nil +} diff --git a/cmd/dbaas_external_endpoint_show.go b/cmd/dbaas_external_endpoint_show.go index f7ea9598..6340e4e2 100644 --- a/cmd/dbaas_external_endpoint_show.go +++ b/cmd/dbaas_external_endpoint_show.go @@ -38,6 +38,8 @@ func (c *dbaasExternalEndpointShowCmd) cmdRun(cmd *cobra.Command, args []string) return c.outputFunc(c.showOpensearch()) case "elasticsearch": return c.outputFunc(c.showElasticsearch()) + case "prometheus": + return c.outputFunc(c.showPrometheus()) default: return fmt.Errorf("unsupported external endpoint type %q", c.Type) } diff --git a/cmd/dbaas_external_endpoint_show_prometheus.go b/cmd/dbaas_external_endpoint_show_prometheus.go new file mode 100644 index 00000000..5daac204 --- /dev/null +++ b/cmd/dbaas_external_endpoint_show_prometheus.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "fmt" + "os" + + "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 prometheusOutput struct { + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Settings v3.DBAASEndpointPrometheus `json:"settings"` +} + +func (o *prometheusOutput) ToJSON() { output.JSON(o) } +func (o *prometheusOutput) ToText() { output.Text(o) } +func (o *prometheusOutput) ToTable() { + t := table.NewTable(os.Stdout) + t.SetHeader([]string{"Prometheus 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 + t.Append([]string{"Basic Auth Username", settings.BasicAuthUsername}) + t.Append([]string{"Basic Auth Password", settings.BasicAuthPassword}) +} + +func (c *dbaasExternalEndpointShowCmd) showPrometheus() (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.GetDBAASExternalEndpointPrometheus(ctx, endpointUUID) + if err != nil { + return nil, fmt.Errorf("error getting Prometheus external endpoint: %w", err) + } + + output := &prometheusOutput{ + ID: endpointResponse.ID.String(), + Name: endpointResponse.Name, + Type: string(endpointResponse.Type), + Settings: *endpointResponse.Settings, + } + + return output, nil +}