-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommand_statement_update.go
More file actions
139 lines (119 loc) · 4.58 KB
/
command_statement_update.go
File metadata and controls
139 lines (119 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package flink
import (
"fmt"
"github.com/spf13/cobra"
pcmd "github.com/confluentinc/cli/v4/pkg/cmd"
"github.com/confluentinc/cli/v4/pkg/examples"
"github.com/confluentinc/cli/v4/pkg/output"
"github.com/confluentinc/cli/v4/pkg/resource"
)
func (c *command) newStatementUpdateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "update <name>",
Short: "Update a Flink SQL statement.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: pcmd.NewValidArgsFunction(c.validStatementArgs),
RunE: c.statementUpdate,
Example: examples.BuildExampleString(
examples.Example{
Text: `Request to resume the currently stopped statement "my-statement" using original principal id and under the original compute pool.`,
Code: "confluent flink statement update my-statement --stopped=false",
},
examples.Example{
Text: `Request to resume the currently stopped statement "my-statement" using service account "sa-123456".`,
Code: "confluent flink statement update my-statement --stopped=false --principal sa-123456",
},
examples.Example{
Text: `Request to resume the currently stopped statement "my-statement" using user account "u-987654".`,
Code: "confluent flink statement update my-statement --stopped=false --principal u-987654",
},
examples.Example{
Text: `Request to resume the currently stopped statement "my-statement" and under a different compute pool "lfcp-123456".`,
Code: "confluent flink statement update my-statement --stopped=false --compute-pool lfcp-123456",
},
examples.Example{
Text: `Request to resume the currently stopped statement "my-statement" using service account "sa-123456" and under a different compute pool "lfcp-123456".`,
Code: "confluent flink statement update my-statement --stopped=false --principal sa-123456 --compute-pool lfcp-123456",
},
examples.Example{
Text: `Request to stop the currently running statement "my-statement".`,
Code: "confluent flink statement update my-statement --stopped=true",
},
),
}
c.addPrincipalFlag(cmd)
c.addComputePoolFlag(cmd)
cmd.Flags().Bool("stopped", false, "Request to stop or resume the statement.")
pcmd.AddCloudFlag(cmd)
pcmd.AddRegionFlagFlink(cmd, c.AuthenticatedCLICommand)
pcmd.AddEnvironmentFlag(cmd, c.AuthenticatedCLICommand)
pcmd.AddContextFlag(cmd, c.CLICommand)
cmd.MarkFlagsOneRequired("principal", "compute-pool", "stopped")
return cmd
}
func (c *command) addPrincipalFlag(cmd *cobra.Command) {
cmd.Flags().String("principal", "", "A user or service account the statement runs as.")
pcmd.RegisterFlagCompletionFunc(cmd, "principal", func(cmd *cobra.Command, args []string) []string {
if err := c.PersistentPreRunE(cmd, args); err != nil {
return nil
}
users, err := c.V2Client.ListIamUsers()
if err != nil {
return nil
}
serviceAccounts, err := c.V2Client.ListIamServiceAccounts(nil)
if err != nil {
return nil
}
suggestions := make([]string, len(users)+len(serviceAccounts))
for i, user := range users {
suggestions[i] = fmt.Sprintf("%s\t%s", user.GetId(), user.GetFullName())
}
for i, serviceAccount := range serviceAccounts {
suggestions[len(users)+i] = fmt.Sprintf("%s\t%s", serviceAccount.GetId(), serviceAccount.GetDescription())
}
return suggestions
})
}
func (c *command) statementUpdate(cmd *cobra.Command, args []string) error {
environmentId, err := c.Context.EnvironmentId()
if err != nil {
return err
}
client, err := c.GetFlinkGatewayClient(false)
if err != nil {
return err
}
statement, err := client.GetStatement(environmentId, args[0], c.Context.GetCurrentOrganization())
if err != nil {
return err
}
principal, err := cmd.Flags().GetString("principal")
if err != nil {
return err
}
if principal != "" {
statement.Spec.SetPrincipal(principal)
}
computePool, err := cmd.Flags().GetString("compute-pool")
if err != nil {
return err
}
if computePool != "" {
statement.Spec.SetComputePoolId(computePool)
}
if cmd.Flags().Changed("stopped") {
stopped, err := cmd.Flags().GetBool("stopped")
if err != nil {
return err
}
statement.Spec.SetStopped(stopped)
}
// the UPDATE statement is an async API
// An accepted response 202 doesn't necessarily mean the UPDATE will be successful/complete
if err := client.UpdateStatement(environmentId, args[0], c.Context.GetCurrentOrganization(), statement); err != nil {
return fmt.Errorf("failed to update %s \"%s\": %w", resource.FlinkStatement, args[0], err)
}
output.Printf(c.Config.EnableColor, "Requested to update %s \"%s\".\n", resource.FlinkStatement, args[0])
return nil
}