|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/exoscale/cli/pkg/globalstate" |
| 11 | + utils "github.com/exoscale/cli/utils" |
| 12 | + v3 "github.com/exoscale/egoscale/v3" |
| 13 | +) |
| 14 | + |
| 15 | +func (c *dbaasServiceCreateCmd) createValkey(_ *cobra.Command, _ []string) error { |
| 16 | + var err error |
| 17 | + |
| 18 | + ctx := gContext |
| 19 | + |
| 20 | + client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone)) |
| 21 | + |
| 22 | + if err != nil { |
| 23 | + return fmt.Errorf("unable to create client: %w", err) |
| 24 | + } |
| 25 | + |
| 26 | + databaseService := v3.CreateDBAASServiceValkeyRequest{ |
| 27 | + Plan: c.Plan, |
| 28 | + TerminationProtection: &c.TerminationProtection, |
| 29 | + } |
| 30 | + |
| 31 | + if c.ValkeyForkFrom != "" { |
| 32 | + databaseService.ForkFromService = v3.DBAASServiceName(c.ValkeyForkFrom) |
| 33 | + if c.ValkeyRecoveryBackupName != "" { |
| 34 | + databaseService.RecoveryBackupName = c.ValkeyRecoveryBackupName |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if len(c.ValkeyIPFilter) > 0 { |
| 39 | + databaseService.IPFilter = c.ValkeyIPFilter |
| 40 | + } |
| 41 | + |
| 42 | + if c.MaintenanceDOW != "" && c.MaintenanceTime != "" { |
| 43 | + databaseService.Maintenance = &v3.CreateDBAASServiceValkeyRequestMaintenance{ |
| 44 | + Dow: v3.CreateDBAASServiceValkeyRequestMaintenanceDow(c.MaintenanceDOW), |
| 45 | + Time: c.MaintenanceTime, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if c.ValkeySettings != "" { |
| 50 | + var settings map[string]interface{} |
| 51 | + |
| 52 | + if err := json.Unmarshal([]byte(c.ValkeySettings), &settings); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + ssl := utils.GetSettingBool(settings, "ssl") |
| 57 | + databaseService.ValkeySettings = &v3.JSONSchemaValkey{ |
| 58 | + AclChannelsDefault: v3.JSONSchemaValkeyAclChannelsDefault(utils.GetSettingString(settings, "acl_channels_default")), |
| 59 | + IoThreads: utils.GetSettingFloat64(settings, "io_threads"), |
| 60 | + LfuDecayTime: utils.GetSettingFloat64(settings, "lfu_decay_time"), |
| 61 | + LfuLogFactor: utils.GetSettingFloat64(settings, "lfu_log_factor"), |
| 62 | + MaxmemoryPolicy: v3.JSONSchemaValkeyMaxmemoryPolicy(utils.GetSettingString(settings, "maxmemory_policy")), |
| 63 | + NotifyKeyspaceEvents: utils.GetSettingString(settings, "notify_keyspace_events"), |
| 64 | + NumberOfDatabases: utils.GetSettingFloat64(settings, "number_of_databases"), |
| 65 | + Persistence: v3.JSONSchemaValkeyPersistence(utils.GetSettingString(settings, "persistence")), |
| 66 | + PubsubClientOutputBufferLimit: utils.GetSettingFloat64(settings, "pubsub_client_output_buffer_limit"), |
| 67 | + SSL: &ssl, |
| 68 | + Timeout: utils.GetSettingFloat64(settings, "timeout"), |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if c.ValkeyMigrationHost != "" { |
| 73 | + databaseService.Migration = &v3.CreateDBAASServiceValkeyRequestMigration{ |
| 74 | + Host: c.ValkeyMigrationHost, |
| 75 | + Port: c.ValkeyMigrationPort, |
| 76 | + Password: c.ValkeyMigrationPassword, |
| 77 | + Username: c.ValkeyMigrationUsername, |
| 78 | + Dbname: c.ValkeyMigrationDBName, |
| 79 | + } |
| 80 | + if c.ValkeyMigrationSSL { |
| 81 | + databaseService.Migration.SSL = &c.ValkeyMigrationSSL |
| 82 | + } |
| 83 | + if c.ValkeyMigrationMethod != "" { |
| 84 | + databaseService.Migration.Method = v3.EnumMigrationMethod(c.ValkeyMigrationMethod) |
| 85 | + } |
| 86 | + if len(c.ValkeyMigrationIgnoreDbs) > 0 { |
| 87 | + databaseService.Migration.IgnoreDbs = strings.Join(c.ValkeyMigrationIgnoreDbs, ",") |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + op, err := client.CreateDBAASServiceValkey(ctx, c.Name, databaseService) |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + decorateAsyncOperation(fmt.Sprintf("Creating DBaaS Datadog external Endpoint %q", c.Name), func() { |
| 98 | + op, err = client.Wait(ctx, op, v3.OperationStateSuccess) |
| 99 | + }) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + serviceName := op.Reference.ID.String() |
| 106 | + |
| 107 | + if !globalstate.Quiet { |
| 108 | + return c.outputFunc((&dbaasServiceShowCmd{ |
| 109 | + Name: serviceName, |
| 110 | + Zone: c.Zone, |
| 111 | + }).showDatabaseServiceValkey(ctx)) |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
0 commit comments