Skip to content

Commit

Permalink
create and show prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
simisoft-exo committed Sep 12, 2024
1 parent 435139f commit ac1643a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/dbaas_external_endpoint_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
50 changes: 50 additions & 0 deletions cmd/dbaas_external_endpoint_create_prometheus.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions cmd/dbaas_external_endpoint_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
62 changes: 62 additions & 0 deletions cmd/dbaas_external_endpoint_show_prometheus.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ac1643a

Please sign in to comment.