-
Notifications
You must be signed in to change notification settings - Fork 72
/
util.go
174 lines (146 loc) · 5.63 KB
/
util.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package aclcmdutil
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/kafkacmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/color"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
kafkainstanceclient "github.com/redhat-developer/app-services-sdk-go/kafkainstance/apiv1/client"
)
// CrudOptions is the interface used for options of create and delete command
type CrudOptions struct {
Connection factory.ConnectionFunc
Logger logging.Logger
IO *iostreams.IOStreams
Localizer localize.Localizer
Context context.Context
ServiceContext servicecontext.IContext
Cluster bool
PatternType string
ResourceType string
ResourceName string
Permission string
Operation string
Group string
Topic string
TransactionalID string
Principal string
SkipConfirm bool
Output string
InstanceID string
}
// When the value of the `--topic`, `--group`, `user` or `service-account` option is one of
// the keys of this map, it will be replaced by the corresponding value.
var commonArgAliases = map[string]string{
AllAlias: Wildcard,
}
// ExecuteACLRuleCreate makes request to create an ACL rule
func ExecuteACLRuleCreate(req kafkainstanceclient.ApiCreateAclRequest, localizer localize.Localizer, kafkaInstanceName string) error {
httpRes, err := req.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
return ValidateAPIError(httpRes, localizer, err, "create", kafkaInstanceName)
}
// FormatPrincipal formats the provided principal ID to "User:principal"
func FormatPrincipal(userID string) string {
return fmt.Sprintf("User:%s", userID)
}
// GetResourceName returns the name of the resource
// transformed into a server recognized format
func GetResourceName(resourceName string) string {
if commonArgAliases[resourceName] == Wildcard {
return Wildcard
}
return resourceName
}
// IsValidResourceOperation checks if the operation is valid, and returns the list valid operations when invalid
func IsValidResourceOperation(resourceType string, operation string, resourceOperationsMap map[string][]string) (bool, []string) {
resourceTypeMapped := resourceTypeOperationKeyMap[resourceType]
resourceOperations := resourceOperationsMap[resourceTypeMapped]
for i, op := range resourceOperations {
if operationMapped, ok := validOperationsResponseMap[op]; ok {
resourceOperations[i] = operationMapped
} else {
resourceOperations[i] = op
}
if resourceOperations[i] == operation {
return true, nil
}
}
return false, resourceOperations
}
// SetACLResources sets resources options and returns number of changed resources
func SetACLResources(opts *CrudOptions) int {
var selectedResourceTypeCount int
if opts.Topic != "" {
selectedResourceTypeCount++
opts.ResourceType = ResourceTypeTOPIC
opts.ResourceName = opts.Topic
}
if opts.Group != "" {
selectedResourceTypeCount++
opts.ResourceType = ResourceTypeGROUP
opts.ResourceName = opts.Group
}
if opts.TransactionalID != "" {
selectedResourceTypeCount++
opts.ResourceType = ResourceTypeTRANSACTIONAL_ID
opts.ResourceName = opts.TransactionalID
}
if opts.Cluster {
selectedResourceTypeCount++
opts.ResourceType = ResourceTypeCLUSTER
opts.ResourceName = KafkaCluster
}
return selectedResourceTypeCount
}
// ValidateAPIError checks for a HTTP error and maps it to a user friendly error
func ValidateAPIError(httpRes *http.Response, localizer localize.Localizer, err error, operation string, instanceName string) error {
if err == nil {
return nil
}
if httpRes == nil {
return err
}
operationTmplPair := localize.NewEntry("Operation", operation)
switch httpRes.StatusCode {
case http.StatusUnauthorized:
return localizer.MustLocalizeError("kafka.acl.common.error.unauthorized", operationTmplPair)
case http.StatusForbidden:
return localizer.MustLocalizeError("kafka.acl.common.error.forbidden", operationTmplPair)
case http.StatusInternalServerError:
return localizer.MustLocalizeError("kafka.acl.common.error.internalServerError")
case http.StatusServiceUnavailable:
return localizer.MustLocalizeError("kafka.acl.common.error.unableToConnectToKafka", localize.NewEntry("Name", instanceName))
default:
return err
}
}
// BuildInstructions accepts a slice of errors and creates a single formatted error object
func BuildInstructions(errorCollection []error) error {
errString := "invalid or missing option(s):" + "\n"
for _, err := range errorCollection {
errString += fmt.Sprintf(" %s ", color.Error("*")) + err.Error() + "\n"
}
return errors.New(errString)
}
// RegisterTopicCompletionFunc enables dynamic autocompletion for topic flag
func RegisterTopicCompletionFunc(cmd *cobra.Command, f *factory.Factory) error {
return cmd.RegisterFlagCompletionFunc("topic", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return kafkacmdutil.FilterValidTopicNameArgs(f, toComplete)
})
}
// RegisterGroupCompletionFunc enables dynamic autocompletion for group flag
func RegisterGroupCompletionFunc(cmd *cobra.Command, f *factory.Factory) error {
return cmd.RegisterFlagCompletionFunc("group", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return kafkacmdutil.FilterValidConsumerGroupIDs(f, toComplete)
})
}