Skip to content

Commit

Permalink
added endpoint updates
Browse files Browse the repository at this point in the history
  • Loading branch information
simisoft-exo committed Sep 25, 2024
1 parent a1dde71 commit 61fe51b
Show file tree
Hide file tree
Showing 7 changed files with 442 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/dbaas_external_endpoint_show_rsyslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type rsyslogOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Settings v3.DBAASEndpointRsyslog `json:"settings"`
Settings v3.DBAASEndpointRsyslogOptionalFields `json:"settings"`
}

func (o *rsyslogOutput) ToJSON() { output.JSON(o) }
Expand Down
116 changes: 116 additions & 0 deletions cmd/dbaas_external_endpoint_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

type dbaasExternalEndpointUpdateCmd struct {
cliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"update"`

Type string `cli-arg:"#"`
ID string `cli-arg:"#"`

HelpDatadog bool `cli-usage:"show usage for flags specific to the datadog external endpoint type"`
HelpElasticsearch bool `cli-usage:"show usage for flags specific to the elasticsearch external endpoint type"`
HelpOpensearch bool `cli-usage:"show usage for flags specific to the opensearch external endpoint type"`
HelpPrometheus bool `cli-usage:"show usage for flags specific to the prometheus external endpoint type"`
HelpRsyslog bool `cli-usage:"show usage for flags specific to the rsyslog external endpoint type"`

DatadogAPIKey string `cli-flag:"datadog-api-key" cli-usage:"Datadog API key" cli-hidden:""`
DatadogSite string `cli-flag:"datadog-site" cli-usage:"Datadog intake site. Defaults to datadoghq.com" cli-hidden:""`
DatadogTags string `cli-flag:"datadog-tags" cli-usage:"Datadog tags. Example. '[{\"comment\": \"ex\", \"tag\": \"aiven-asdfasda\"}]'" cli-hidden:""`
DatadogDisableConsumerStats bool `cli-flag:"datadog-disable-consumer-stats" cli-usage:"Disable consumer group metrics" cli-hidden:""`
DatadogKafkaConsumerCheckInstances int64 `cli-flag:"datadog-kafka-consumer-check-instances" cli-usage:"Number of separate instances to fetch kafka consumer statistics with" cli-hidden:""`
DatadogKafkaConsumerStatsTimeout int64 `cli-flag:"datadog-kafka-consumer-stats-timeout" cli-usage:"Number of seconds that datadog will wait to get consumer statistics from brokers" cli-hidden:""`
DatadogMaxPartitionContexts int64 `cli-flag:"datadog-max-partition-contexts" cli-usage:"Maximum number of partition contexts to send" cli-hidden:""`

ElasticsearchURL string `cli-flag:"elasticsearch-url" cli-usage:"Elasticsearch connection URL" cli-hidden:""`
ElasticsearchIndexPrefix string `cli-flag:"elasticsearch-index-prefix" cli-usage:"Elasticsearch index prefix" cli-hidden:""`
ElasticsearchCA string `cli-flag:"elasticsearch-ca" cli-usage:"PEM encoded CA certificate" cli-hidden:""`
ElasticsearchIndexDaysMax int64 `cli-flag:"elasticsearch-index-days-max" cli-usage:"Maximum number of days of logs to keep" cli-hidden:""`
ElasticsearchTimeout int64 `cli-flag:"elasticsearch-timeout" cli-usage:"Elasticsearch request timeout limit" cli-hidden:""`

OpensearchURL string `cli-flag:"opensearch-url" cli-usage:"OpenSearch connection URL" cli-hidden:""`
OpensearchIndexPrefix string `cli-flag:"opensearch-index-prefix" cli-usage:"OpenSearch index prefix" cli-hidden:""`
OpensearchCA string `cli-flag:"opensearch-ca" cli-usage:"PEM encoded CA certificate" cli-hidden:""`
OpensearchIndexDaysMax int64 `cli-flag:"opensearch-index-days-max" cli-usage:"Maximum number of days of logs to keep" cli-hidden:""`
OpensearchTimeout int64 `cli-flag:"opensearch-timeout" cli-usage:"OpenSearch request timeout limit" cli-hidden:""`

PrometheusBasicAuthUsername string `cli-flag:"prometheus-basic-auth-username" cli-usage:"Prometheus basic authentication username" cli-hidden:""`
PrometheusBasicAuthPassword string `cli-flag:"prometheus-basic-auth-password" cli-usage:"Prometheus basic authentication password" cli-hidden:""`

RsyslogServer string `cli-flag:"rsyslog-server" cli-usage:"Rsyslog server IP address or hostname" cli-hidden:""`
RsyslogPort int64 `cli-flag:"rsyslog-port" cli-usage:"Rsyslog server port" cli-hidden:""`
RsyslogTls bool `cli-flag:"rsyslog-tls" cli-usage:"Require TLS" cli-hidden:""`
RsyslogFormat string `cli-flag:"rsyslog-format" cli-usage:"Message format" cli-hidden:""`
RsyslogKey string `cli-flag:"rsyslog-key" cli-usage:"PEM encoded client key" cli-hidden:""`
RsyslogLogline string `cli-flag:"rsyslog-logline" cli-usage:"Custom syslog message format" cli-hidden:""`
RsyslogCA string `cli-flag:"rsyslog-ca" cli-usage:"PEM encoded CA certificate" cli-hidden:""`
RsyslogCert string `cli-flag:"rsyslog-cert" cli-usage:"PEM encoded client certificate" cli-hidden:""`
RsyslogSD string `cli-flag:"rsyslog-sd" cli-usage:"Structured data block for log message" cli-hidden:""`
RsyslogMaxMessageSize int64 `cli-flag:"rsyslog-max-message-size" cli-usage:"Rsyslog max message size" cli-hidden:""`
}

func (c *dbaasExternalEndpointUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
switch {
case cmd.Flags().Changed("help-datadog"):
cmdShowHelpFlags(cmd.Flags(), "datadog-")
os.Exit(0)
case cmd.Flags().Changed("help-elasticsearch"):
cmdShowHelpFlags(cmd.Flags(), "elasticsearch-")
os.Exit(0)
case cmd.Flags().Changed("help-opensearch"):
cmdShowHelpFlags(cmd.Flags(), "opensearch-")
os.Exit(0)
case cmd.Flags().Changed("help-prometheus"):
cmdShowHelpFlags(cmd.Flags(), "prometheus-")
os.Exit(0)
case cmd.Flags().Changed("help-rsyslog"):
cmdShowHelpFlags(cmd.Flags(), "rsyslog-")
os.Exit(0)
}

return cliCommandDefaultPreRun(c, cmd, args)
}

func (c *dbaasExternalEndpointUpdateCmd) cmdAliases() []string {
return nil
}

func (c *dbaasExternalEndpointUpdateCmd) cmdLong() string {
return "Update an existing external endpoint for DBaaS"
}

func (c *dbaasExternalEndpointUpdateCmd) cmdShort() string {
return "Update an existing external endpoint for DBaaS"
}

func (c *dbaasExternalEndpointUpdateCmd) cmdRun(cmd *cobra.Command, args []string) error {
// Implement the command's main logic here
switch c.Type {
case "datadog":
return c.updateDatadog(cmd, args)
case "opensearch":
return c.updateOpensearch(cmd, args)
case "elasticsearch":
return c.updateElasticsearch(cmd, args)
case "prometheus":
return c.updatePrometheus(cmd, args)
case "rsyslog":
return c.updateRsyslog(cmd, args)
default:
return fmt.Errorf("unsupported external endpoint type %q", c.Type)
}

}

func init() {
cobra.CheckErr(registerCLICommand(dbaasExternalEndpointCmd, &dbaasExternalEndpointUpdateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}
80 changes: 80 additions & 0 deletions cmd/dbaas_external_endpoint_update_datadog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cmd

import (
"encoding/json"
"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 *dbaasExternalEndpointUpdateCmd) updateDatadog(_ *cobra.Command, _ []string) error {
ctx := gContext

client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone))
if err != nil {
return err
}

var datadogTags []v3.DBAASDatadogTag
if c.DatadogTags != "" {
if err := json.Unmarshal([]byte(c.DatadogTags), &datadogTags); err != nil {
return fmt.Errorf("failed to parse datadog tags: %v", err)
}
}

datadogRequestPayload := v3.DBAASEndpointDatadogInputUpdate{
Settings: &v3.DBAASEndpointDatadogInputUpdateSettings{},
}

if c.DatadogAPIKey != "" {
datadogRequestPayload.Settings.DatadogAPIKey = c.DatadogAPIKey
}
if c.DatadogSite != "" {
datadogRequestPayload.Settings.Site = v3.EnumDatadogSite(c.DatadogSite)
}
if c.DatadogTags != "" {
datadogRequestPayload.Settings.DatadogTags = datadogTags
}
if c.DatadogDisableConsumerStats {
datadogRequestPayload.Settings.DisableConsumerStats = v3.Bool(c.DatadogDisableConsumerStats)
}
if c.DatadogKafkaConsumerCheckInstances != 0 {
datadogRequestPayload.Settings.KafkaConsumerCheckInstances = int64(c.DatadogKafkaConsumerCheckInstances)
}
if c.DatadogKafkaConsumerStatsTimeout != 0 {
datadogRequestPayload.Settings.KafkaConsumerStatsTimeout = int64(c.DatadogKafkaConsumerStatsTimeout)
}
if c.DatadogMaxPartitionContexts != 0 {
datadogRequestPayload.Settings.MaxPartitionContexts = int64(c.DatadogMaxPartitionContexts)
}

op, err := client.UpdateDBAASExternalEndpointDatadog(ctx, v3.UUID(c.ID), datadogRequestPayload)

if err != nil {
return err
}

decorateAsyncOperation(fmt.Sprintf("Updating DBaaS Datadog external Endpoint %q", c.ID), 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: "datadog",
}).cmdRun(nil, nil)
}

return nil
}
59 changes: 59 additions & 0 deletions cmd/dbaas_external_endpoint_update_elasticsearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 *dbaasExternalEndpointUpdateCmd) updateElasticsearch(_ *cobra.Command, _ []string) error {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone))
if err != nil {
return err
}

elasticsearchRequestPayload := v3.DBAASEndpointElasticsearchInputUpdate{
Settings: &v3.DBAASEndpointElasticsearchInputUpdateSettings{},
}

if c.ElasticsearchURL != "" {
elasticsearchRequestPayload.Settings.URL = c.ElasticsearchURL
}
if c.ElasticsearchIndexPrefix != "" {
elasticsearchRequestPayload.Settings.IndexPrefix = c.ElasticsearchIndexPrefix
}
if c.ElasticsearchCA != "" {
elasticsearchRequestPayload.Settings.CA = c.ElasticsearchCA
}
if c.ElasticsearchIndexDaysMax != 0 {
elasticsearchRequestPayload.Settings.IndexDaysMax = c.ElasticsearchIndexDaysMax
}
if c.ElasticsearchTimeout != 0 {
elasticsearchRequestPayload.Settings.Timeout = c.ElasticsearchTimeout
}

op, err := client.UpdateDBAASExternalEndpointElasticsearch(ctx, v3.UUID(c.ID), elasticsearchRequestPayload)
if err != nil {
return err
}

decorateAsyncOperation(fmt.Sprintf("Updating DBaaS ElasticSearch external Endpoint %q", c.ID), 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: "elasticsearch",
}).cmdRun(nil, nil)
}
return nil
}
59 changes: 59 additions & 0 deletions cmd/dbaas_external_endpoint_update_opensearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 *dbaasExternalEndpointUpdateCmd) updateOpensearch(_ *cobra.Command, _ []string) error {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone))
if err != nil {
return err
}

opensearchRequestPayload := v3.DBAASEndpointOpensearchInputUpdate{
Settings: &v3.DBAASEndpointOpensearchInputUpdateSettings{},
}

if c.OpensearchURL != "" {
opensearchRequestPayload.Settings.URL = c.OpensearchURL
}
if c.OpensearchIndexPrefix != "" {
opensearchRequestPayload.Settings.IndexPrefix = c.OpensearchIndexPrefix
}
if c.OpensearchCA != "" {
opensearchRequestPayload.Settings.CA = c.OpensearchCA
}
if c.OpensearchIndexDaysMax != 0 {
opensearchRequestPayload.Settings.IndexDaysMax = c.OpensearchIndexDaysMax
}
if c.OpensearchTimeout != 0 {
opensearchRequestPayload.Settings.Timeout = c.OpensearchTimeout
}

op, err := client.UpdateDBAASExternalEndpointOpensearch(ctx, v3.UUID(c.ID), opensearchRequestPayload)
if err != nil {
return err
}

decorateAsyncOperation(fmt.Sprintf("Updating DBaaS OpenSearch external Endpoint %q", c.ID), 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: "opensearch",
}).cmdRun(nil, nil)
}
return nil
}
50 changes: 50 additions & 0 deletions cmd/dbaas_external_endpoint_update_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 *dbaasExternalEndpointUpdateCmd) updatePrometheus(_ *cobra.Command, _ []string) error {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone))
if err != nil {
return err
}

prometheusRequestPayload := v3.DBAASEndpointPrometheusPayload{
Settings: &v3.DBAASEndpointPrometheusPayloadSettings{},
}

if c.PrometheusBasicAuthPassword != "" {
prometheusRequestPayload.Settings.BasicAuthPassword= c.PrometheusBasicAuthPassword
}
if c.PrometheusBasicAuthUsername != "" {
prometheusRequestPayload.Settings.BasicAuthUsername= c.PrometheusBasicAuthUsername
}

op, err := client.UpdateDBAASExternalEndpointPrometheus(ctx, v3.UUID(c.ID), prometheusRequestPayload)
if err != nil {
return err
}

decorateAsyncOperation(fmt.Sprintf("Updating DBaaS Prometheus external Endpoint %q", c.ID), 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
}
Loading

0 comments on commit 61fe51b

Please sign in to comment.