-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathretriever.go
187 lines (173 loc) · 7.37 KB
/
retriever.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
175
176
177
178
179
180
181
182
183
184
185
186
187
package config
import (
"fmt"
"github.com/redis/go-redis/v9"
"golang.org/x/exp/slices"
)
// RetrieverConf contains all the field to configure a retriever
type RetrieverConf struct {
Kind RetrieverKind `mapstructure:"kind" koanf:"kind"`
RepositorySlug string `mapstructure:"repositorySlug" koanf:"repositoryslug"`
Branch string `mapstructure:"branch" koanf:"branch"`
Path string `mapstructure:"path" koanf:"path"`
// Deprecated: Please use AuthToken instead
GithubToken string `mapstructure:"githubToken" koanf:"githubtoken"`
URL string `mapstructure:"url" koanf:"url"`
Timeout int64 `mapstructure:"timeout" koanf:"timeout"`
HTTPMethod string `mapstructure:"method" koanf:"method"`
HTTPBody string `mapstructure:"body" koanf:"body"`
HTTPHeaders map[string][]string `mapstructure:"headers" koanf:"headers"`
Bucket string `mapstructure:"bucket" koanf:"bucket"`
Object string `mapstructure:"object" koanf:"object"`
Item string `mapstructure:"item" koanf:"item"`
Namespace string `mapstructure:"namespace" koanf:"namespace"`
ConfigMap string `mapstructure:"configmap" koanf:"configmap"`
Key string `mapstructure:"key" koanf:"key"`
BaseURL string `mapstructure:"baseUrl" koanf:"baseurl"`
AuthToken string `mapstructure:"token" koanf:"token"`
URI string `mapstructure:"uri" koanf:"uri"`
Database string `mapstructure:"database" koanf:"database"`
Collection string `mapstructure:"collection" koanf:"collection"`
Type string `mapstructure:"type" koanf:"type"`
Table string `mapstructure:"table" koanf:"table"`
Column string `mapstructure:"column" koanf:"column"`
RedisOptions *redis.Options `mapstructure:"redisOptions" koanf:"redisOptions"`
RedisPrefix string `mapstructure:"redisPrefix" koanf:"redisPrefix"`
AccountName string `mapstructure:"accountName" koanf:"accountname"`
AccountKey string `mapstructure:"accountKey" koanf:"accountkey"`
Container string `mapstructure:"container" koanf:"container"`
}
// IsValid validate the configuration of the retriever
// nolint:gocognit
func (c *RetrieverConf) IsValid() error {
if err := c.Kind.IsValid(); err != nil {
return err
}
if c.Kind == GitHubRetriever || c.Kind == GitlabRetriever || c.Kind == BitbucketRetriever {
return c.validateGitRetriever()
}
if c.Kind == S3Retriever && c.Item == "" {
return fmt.Errorf("invalid retriever: no \"item\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == HTTPRetriever && c.URL == "" {
return fmt.Errorf("invalid retriever: no \"url\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == GoogleStorageRetriever && c.Object == "" {
return fmt.Errorf("invalid retriever: no \"object\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == FileRetriever && c.Path == "" {
return fmt.Errorf("invalid retriever: no \"path\" property found for kind \"%s\"", c.Kind)
}
if (c.Kind == S3Retriever || c.Kind == GoogleStorageRetriever) && c.Bucket == "" {
return fmt.Errorf("invalid retriever: no \"bucket\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == KubernetesRetriever {
return c.validateKubernetesRetriever()
}
if c.Kind == MongoDBRetriever {
return c.validateMongoDBRetriever()
}
if c.Kind == PostgreSQLRetriever {
return c.validatePostgreSQLRetriever()
}
if c.Kind == RedisRetriever {
return c.validateRedisRetriever()
}
if c.Kind == AzBlobStorageRetriever {
return c.validateAzBlobStorageRetriever()
}
return nil
}
func (c *RetrieverConf) validateGitRetriever() error {
if c.RepositorySlug == "" {
return fmt.Errorf("invalid retriever: no \"repositorySlug\" property found for kind \"%s\"", c.Kind)
}
if c.Path == "" {
return fmt.Errorf("invalid retriever: no \"path\" property found for kind \"%s\"", c.Kind)
}
return nil
}
func (c *RetrieverConf) validateKubernetesRetriever() error {
if c.ConfigMap == "" {
return fmt.Errorf("invalid retriever: no \"configmap\" property found for kind \"%s\"", c.Kind)
}
if c.Namespace == "" {
return fmt.Errorf("invalid retriever: no \"namespace\" property found for kind \"%s\"", c.Kind)
}
if c.Key == "" {
return fmt.Errorf("invalid retriever: no \"key\" property found for kind \"%s\"", c.Kind)
}
return nil
}
func (c *RetrieverConf) validateMongoDBRetriever() error {
if c.Collection == "" {
return fmt.Errorf("invalid retriever: no \"collection\" property found for kind \"%s\"", c.Kind)
}
if c.Database == "" {
return fmt.Errorf("invalid retriever: no \"database\" property found for kind \"%s\"", c.Kind)
}
if c.URI == "" {
return fmt.Errorf("invalid retriever: no \"uri\" property found for kind \"%s\"", c.Kind)
}
return nil
}
func (c *RetrieverConf) validatePostgreSQLRetriever() error {
validTypes := []string{"json", "relational"}
if c.Type == "json" && c.Column == "" {
return fmt.Errorf("invalid retriever: no \"column\" property found for kind \"%s\"", c.Kind)
}
if c.Type == "json" && c.Table == "" {
return fmt.Errorf("invalid retriever: no \"table\" property found for kind \"%s\"", c.Kind)
}
if c.URI == "" {
return fmt.Errorf("invalid retriever: no \"uri\" property found for kind \"%s\"", c.Kind)
}
if c.Type == "" || !slices.Contains(validTypes, c.Type) {
return fmt.Errorf("invalid retriever: no \"type\" property or not a valid type in kind \"%s\"", c.Kind)
}
return nil
}
func (c *RetrieverConf) validateRedisRetriever() error {
if c.RedisOptions == nil {
return fmt.Errorf("invalid retriever: no \"redisOptions\" property found for kind \"%s\"", c.Kind)
}
return nil
}
func (c *RetrieverConf) validateAzBlobStorageRetriever() error {
if c.AccountName == "" {
return fmt.Errorf("invalid retriever: no \"accountName\" property found for kind \"%s\"", c.Kind)
}
if c.Container == "" {
return fmt.Errorf("invalid retriever: no \"container\" property found for kind \"%s\"", c.Kind)
}
if c.Object == "" {
return fmt.Errorf("invalid retriever: no \"object\" property found for kind \"%s\"", c.Kind)
}
return nil
}
// RetrieverKind is an enum containing all accepted Retriever kind
type RetrieverKind string
const (
HTTPRetriever RetrieverKind = "http"
GitHubRetriever RetrieverKind = "github"
GitlabRetriever RetrieverKind = "gitlab"
S3Retriever RetrieverKind = "s3"
FileRetriever RetrieverKind = "file"
GoogleStorageRetriever RetrieverKind = "googleStorage"
KubernetesRetriever RetrieverKind = "configmap"
MongoDBRetriever RetrieverKind = "mongodb"
PostgreSQLRetriever RetrieverKind = "postgresql"
RedisRetriever RetrieverKind = "redis"
BitbucketRetriever RetrieverKind = "bitbucket"
AzBlobStorageRetriever RetrieverKind = "azureBlobStorage"
)
// IsValid is checking if the value is part of the enum
func (r RetrieverKind) IsValid() error {
switch r {
case HTTPRetriever, GitHubRetriever, GitlabRetriever, S3Retriever, RedisRetriever,
FileRetriever, GoogleStorageRetriever, KubernetesRetriever, MongoDBRetriever,
PostgreSQLRetriever, BitbucketRetriever, AzBlobStorageRetriever:
return nil
}
return fmt.Errorf("invalid retriever: kind \"%s\" is not supported", r)
}