-
Notifications
You must be signed in to change notification settings - Fork 28
/
config.go
165 lines (143 loc) · 4.49 KB
/
config.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
package service
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/spf13/afero"
"github.com/authgear/authgear-server/pkg/api/apierrors"
apimodel "github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/config/configsource"
portalconfig "github.com/authgear/authgear-server/pkg/portal/config"
"github.com/authgear/authgear-server/pkg/util/log"
"github.com/authgear/authgear-server/pkg/util/resource"
)
var ErrDuplicatedAppID = apierrors.AlreadyExists.WithReason("DuplicatedAppID").
New("duplicated app ID")
var ErrGetStaticAppIDsNotSupported = errors.New("only local FS config source can get static app ID")
type IngressTemplateData struct {
AppID string
DomainID string
IsCustom bool
Host string
TLSSecretName string
}
type ConfigServiceLogger struct{ *log.Logger }
func NewConfigServiceLogger(lf *log.Factory) ConfigServiceLogger {
return ConfigServiceLogger{lf.New("config-service")}
}
type CreateAppOptions struct {
AppID string
Resources map[string][]byte
PlanName string
}
type ConfigService struct {
Context context.Context
Logger ConfigServiceLogger
AppConfig *portalconfig.AppConfig
Controller *configsource.Controller
ConfigSource *configsource.ConfigSource
DomainImplementation portalconfig.DomainImplementationType
Kubernetes *Kubernetes
}
func (s *ConfigService) ResolveContext(appID string) (*config.AppContext, error) {
return s.ConfigSource.ContextResolver.ResolveContext(appID)
}
func (s *ConfigService) GetStaticAppIDs() ([]string, error) {
switch src := s.Controller.Handle.(type) {
case *configsource.Database:
return nil, ErrGetStaticAppIDsNotSupported
case *configsource.LocalFS:
return src.AllAppIDs()
default:
return nil, errors.New("unsupported configuration source")
}
}
func (s *ConfigService) Create(opts *CreateAppOptions) error {
switch src := s.Controller.Handle.(type) {
case *configsource.Database:
err := s.createDatabase(src, opts)
if err != nil {
return err
}
case *configsource.LocalFS:
return apierrors.NewForbidden("cannot create app for local FS")
default:
return errors.New("unsupported configuration source")
}
return nil
}
func (s *ConfigService) UpdateResources(appID string, files []*resource.ResourceFile) error {
switch src := s.Controller.Handle.(type) {
case *configsource.Database:
err := s.updateDatabase(src, appID, files)
if err != nil {
return err
}
s.Controller.ReloadApp(appID)
case *configsource.LocalFS:
err := s.updateLocalFS(src, appID, files)
if err != nil {
return err
}
s.Controller.ReloadApp(appID)
default:
return errors.New("unsupported configuration source")
}
return nil
}
func (s *ConfigService) CreateDomain(appID string, domainID string, domain string, isCustom bool) error {
if s.DomainImplementation == portalconfig.DomainImplementationTypeKubernetes {
err := s.Kubernetes.CreateResourcesForDomain(appID, domainID, domain, isCustom)
if err != nil {
return fmt.Errorf("failed to create domain k8s resources: %w", err)
}
}
return nil
}
func (s *ConfigService) DeleteDomain(domain *apimodel.Domain) error {
if s.DomainImplementation == portalconfig.DomainImplementationTypeKubernetes {
err := s.Kubernetes.DeleteResourcesForDomain(domain.ID)
if err != nil {
return fmt.Errorf("failed to delete domain k8s resources: %w", err)
}
}
return nil
}
func (s *ConfigService) updateLocalFS(l *configsource.LocalFS, appID string, updates []*resource.ResourceFile) error {
fs := l.Fs
for _, file := range updates {
if file.Data == nil {
err := fs.Remove(file.Location.Path)
// Ignore file not found errors
if err != nil && !os.IsNotExist(err) {
return err
}
} else {
err := fs.MkdirAll(filepath.Dir(file.Location.Path), 0777)
if err != nil {
return err
}
err = afero.WriteFile(fs, file.Location.Path, file.Data, 0666)
if err != nil {
return err
}
}
}
return nil
}
func (s *ConfigService) updateDatabase(d *configsource.Database, appID string, updates []*resource.ResourceFile) error {
return d.UpdateDatabaseSource(appID, updates)
}
func (s *ConfigService) createDatabase(d *configsource.Database, opts *CreateAppOptions) error {
err := d.CreateDatabaseSource(opts.AppID, opts.Resources, opts.PlanName)
if err != nil {
if errors.Is(err, configsource.ErrDuplicatedAppID) {
return ErrDuplicatedAppID
}
return err
}
return nil
}