Skip to content

Commit 35864dc

Browse files
author
Enda Phelan
committed
feat: use kafka cluster after creation
1 parent 93ea7e8 commit 35864dc

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

.vscode/launch.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"--url=localhost:8000"
1717
]
1818
},
19-
{
19+
{
2020
"name": "Logout",
2121
"type": "go",
2222
"request": "launch",
@@ -27,6 +27,19 @@
2727
"logout"
2828
]
2929
},
30+
{
31+
"name": "Create Kafka",
32+
"type": "go",
33+
"request": "launch",
34+
"mode": "auto",
35+
"program": "${workspaceFolder}/cmd/rhoas",
36+
"env": {},
37+
"args": [
38+
"kafka",
39+
"create",
40+
"--name=my-kafka-instance"
41+
]
42+
},
3043
{
3144
"name": "List Kafkas",
3245
"type": "go",

pkg/cmd/kafka/create/create.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ func NewCreateCommand() *cobra.Command {
4747
}
4848
opts.cfg = cfg
4949

50-
err = kafka.ValidateName(opts.name)
51-
if err != nil {
50+
if err = kafka.ValidateName(opts.name); err != nil {
5251
return err
5352
}
5453

@@ -59,8 +58,6 @@ func NewCreateCommand() *cobra.Command {
5958
cmd.Flags().StringVarP(&opts.name, flags.FlagName, "n", "", "Name of the new Kafka cluster")
6059
cmd.Flags().StringVar(&opts.provider, flags.FlagProvider, "aws", "Cloud provider ID")
6160
cmd.Flags().StringVar(&opts.region, flags.FlagRegion, "us-east-1", "Cloud Provider Region ID")
62-
// Hardcoded as only true is possible
63-
// cmd.Flags().BoolVar(&opts.multiAZ, flags.FlagMultiAZ, true, "Determines if cluster should be provisioned across multiple Availability Zones")
6461
cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", "Format to display the Kafka cluster. Choose from: \"json\", \"yaml\", \"yml\"")
6562

6663
_ = cmd.MarkFlagRequired(flags.FlagName)
@@ -69,29 +66,44 @@ func NewCreateCommand() *cobra.Command {
6966
}
7067

7168
func runCreate(opts *options) error {
72-
connection, err := opts.cfg.Connection()
69+
cfg := opts.cfg
70+
71+
connection, err := cfg.Connection()
7372
if err != nil {
7473
return fmt.Errorf("Can't create connection: %w", err)
7574
}
7675

7776
client := connection.NewMASClient()
7877

78+
fmt.Fprintln(os.Stderr, "Creating Kafka cluster")
79+
7980
kafkaRequest := managedservices.KafkaRequestPayload{Name: opts.name, Region: opts.region, CloudProvider: opts.provider, MultiAz: true}
8081
response, _, err := client.DefaultApi.CreateKafka(context.Background(), true, kafkaRequest)
8182

8283
if err != nil {
8384
return fmt.Errorf("Error while requesting new Kafka cluster: %w", err)
8485
}
8586

86-
fmt.Fprintf(os.Stderr, "Created new Kafka cluster:\n")
87+
fmt.Fprintln(os.Stderr, "Created Kafka cluster:")
8788

8889
switch opts.outputFormat {
8990
case "json":
9091
data, _ := json.MarshalIndent(response, "", cmdutil.DefaultJSONIndent)
91-
fmt.Print(string(data))
92+
fmt.Println(string(data))
9293
case "yaml", "yml":
9394
data, _ := yaml.Marshal(response)
94-
fmt.Print(string(data))
95+
fmt.Println(string(data))
96+
}
97+
98+
kafkaCfg := &config.KafkaConfig{
99+
ClusterName: response.Name,
100+
ClusterHost: response.BootstrapServerHost,
101+
ClusterID: response.Id,
102+
}
103+
104+
cfg.Services.SetKafka(kafkaCfg)
105+
if err := config.Save(cfg); err != nil {
106+
return fmt.Errorf("Unable to automatically use Kafka cluster: %w", err)
95107
}
96108

97109
return nil

0 commit comments

Comments
 (0)