/
retriever.go
138 lines (128 loc) · 5.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
package config
import (
"fmt"
"github.com/redis/go-redis/v9"
)
// 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"`
RedisOptions *redis.Options `mapstructure:"redisOptions" koanf:"redisOptions"`
RedisPrefix string `mapstructure:"redisPrefix" koanf:"redisPrefix"`
}
// 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 {
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 == RedisRetriever {
return c.validateRedisRetriever()
}
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) validateRedisRetriever() error {
if c.RedisOptions == nil {
return fmt.Errorf("invalid retriever: no \"redisOptions\" 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"
RedisRetriever RetrieverKind = "redis"
)
// 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:
return nil
}
return fmt.Errorf("invalid retriever: kind \"%s\" is not supported", r)
}