-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
168 lines (147 loc) · 8.1 KB
/
api.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
package main
import (
"fmt"
"os"
goruntime "runtime"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
"sigs.k8s.io/apiserver-runtime/pkg/builder"
ctrl "sigs.k8s.io/controller-runtime"
billingv1 "github.com/appuio/control-api/apis/billing/v1"
orgv1 "github.com/appuio/control-api/apis/organization/v1"
userv1 "github.com/appuio/control-api/apis/user/v1"
"github.com/appuio/control-api/apiserver/authwrapper"
billingStore "github.com/appuio/control-api/apiserver/billing"
"github.com/appuio/control-api/apiserver/billing/odoostorage"
"github.com/appuio/control-api/apiserver/billing/odoostorage/odoo/odoo16"
"github.com/appuio/control-api/apiserver/billing/odoostorage/odoo/odoo8"
"github.com/appuio/control-api/apiserver/billing/odoostorage/odoo/odoo8/countries"
orgStore "github.com/appuio/control-api/apiserver/organization"
"github.com/appuio/control-api/apiserver/secretstorage"
"github.com/appuio/control-api/apiserver/user"
)
// APICommand creates a new command allowing to start the API server
func APICommand() *cobra.Command {
roles := []string{}
usernamePrefix := ""
var allowEmptyBillingEntity, skipBillingEntityValidation bool
ob := &odooStorageBuilder{}
ost := orgStore.New(&roles, &usernamePrefix, &allowEmptyBillingEntity, &skipBillingEntityValidation)
ib := &invitationStorageBuilder{usernamePrefix: &usernamePrefix}
cmd, err := builder.APIServer.
WithResourceAndHandler(&orgv1.Organization{}, ost).
WithResourceAndHandler(organizationStatusRegisterer{&orgv1.Organization{}}, ost).
WithResourceAndHandler(&billingv1.BillingEntity{}, ob.Build).
WithResourceAndHandler(&userv1.Invitation{}, ib.Build).
WithResourceAndHandler(secretstorage.NewStatusSubResourceRegisterer(&userv1.Invitation{}), ib.Build).
WithResourceAndHandler(&userv1.InvitationRedeemRequest{}, ib.BuildRedeem).
WithoutEtcd().
ExposeLoopbackAuthorizer().
ExposeLoopbackMasterClientConfig().
Build()
if err != nil {
ctrl.Log.WithName("setup").Error(err, "Failed to setup API server")
}
cmd.Use = "api"
cmd.Flags().StringSliceVar(&roles, "cluster-roles", []string{}, "Cluster Roles to bind when creating an organization")
cmd.Flags().StringVar(&usernamePrefix, "username-prefix", "", "Prefix prepended to username claims. Usually the same as \"--oidc-username-prefix\" of the Kubernetes API server")
cmd.Flags().BoolVar(&allowEmptyBillingEntity, "allow-empty-billing-entity", true, "Allow empty billing entity references")
cmd.Flags().BoolVar(&skipBillingEntityValidation, "organization-skip-billing-entity-validation", false, "Skip validation of billing entity references")
cmd.Flags().StringVar(&ob.billingEntityStorage, "billing-entity-storage", "fake", "Storage backend for billing entities. Supported values: fake, odoo8, odoo16")
cmd.Flags().BoolVar(&ob.billingEntityFakeMetadataSupport, "billing-entity-fake-metadata-support", false, "Enable metadata support for the fake storage backend")
cmd.Flags().StringVar(&ob.odoo8URL, "billing-entity-odoo8-url", "http://localhost:8069", "URL of the Odoo instance to use for billing entities")
cmd.Flags().BoolVar(&ob.odoo8DebugTransport, "billing-entity-odoo8-debug-transport", false, "Enable debug logging for the Odoo transport")
cmd.Flags().StringVar(&ob.odoo8CountryListPath, "billing-entity-odoo8-country-list", "countries.yaml", "Path to the country list file in the format of [{name: \"Germany\", code: \"DE\", id: 81},...]")
cmd.Flags().StringVar(&ob.odoo8AccountingContactDisplayName, "billing-entity-odoo8-accounting-contact-display-name", "Accounting", "Display name of the accounting contact")
cmd.Flags().StringVar(&ob.odoo8LanguagePreference, "billing-entity-odoo8-language-preference", "en_US", "Language preference of the Odoo record")
cmd.Flags().IntVar(&ob.odoo8PaymentTermID, "billing-entity-odoo8-payment-term-id", 2, "Payment term ID of the Odoo record")
cmd.Flags().StringVar(&ob.odoo16URL, "billing-entity-odoo16-url", "http://localhost:8069", "URL of the Odoo instance to use for billing entities")
cmd.Flags().StringVar(&ob.odoo16Db, "billing-entity-odoo16-db", "odooDB", "Database of the Odoo instance to use for billing entities")
cmd.Flags().StringVar(&ob.odoo16Account, "billing-entity-odoo16-account", "Admin", "Odoo Account name to use for billing entities")
cmd.Flags().StringVar(&ob.odoo16Password, "billing-entity-odoo16-password", "superSecret1238", "Odoo Account password to use for billing entities")
cmd.Flags().StringVar(&ob.odoo16CountryListPath, "billing-entity-odoo16-country-list", "countries.yaml", "Path to the country list file in the format of [{name: \"Germany\", code: \"DE\", id: 81},...]")
cmd.Flags().StringVar(&ob.odoo16LanguagePreference, "billing-entity-odoo16-language-preference", "en_US", "Language preference of the Odoo record")
cmd.Flags().IntVar(&ob.odoo16PaymentTermID, "billing-entity-odoo16-payment-term-id", 2, "Payment term ID of the Odoo record")
cmd.Flags().StringVar(&ib.backingNS, "invitation-storage-backing-ns", "default", "Namespace to store invitation secrets in")
rf := cmd.Run
cmd.Run = func(cmd *cobra.Command, args []string) {
ctrl.Log.WithName("setup").WithValues(
"version", version,
"date", date,
"commit", commit,
"go_os", goruntime.GOOS,
"go_arch", goruntime.GOARCH,
"go_version", goruntime.Version(),
"uid", os.Getuid(),
"gid", os.Getgid(),
).Info("Starting control-api…")
rf(cmd, args)
}
return cmd
}
type odooStorageBuilder struct {
billingEntityStorage, odoo8URL, odoo8CountryListPath string
odoo8AccountingContactDisplayName, odoo8LanguagePreference string
odoo8PaymentTermID int
billingEntityFakeMetadataSupport, odoo8DebugTransport bool
odoo16LanguagePreference string
odoo16URL, odoo16CountryListPath string
odoo16Db, odoo16Account, odoo16Password string
odoo16PaymentTermID int
}
func (o *odooStorageBuilder) Build(s *runtime.Scheme, g genericregistry.RESTOptionsGetter) (rest.Storage, error) {
switch o.billingEntityStorage {
case "fake":
return billingStore.New(odoostorage.NewFakeStorage(o.billingEntityFakeMetadataSupport).(authwrapper.StorageScoper))(s, g)
case "odoo8":
countryIDs, err := countries.LoadCountryIDs(o.odoo8CountryListPath)
if err != nil {
return nil, err
}
return billingStore.New(odoostorage.NewOdoo8Storage(o.odoo8URL, o.odoo8DebugTransport, odoo8.Config{
AccountingContactDisplayName: o.odoo8AccountingContactDisplayName,
LanguagePreference: o.odoo8LanguagePreference,
PaymentTermID: o.odoo8PaymentTermID,
CountryIDs: countryIDs,
}).(authwrapper.StorageScoper))(s, g)
case "odoo16":
countryIDs, err := countries.LoadCountryIDs(o.odoo16CountryListPath)
if err != nil {
return nil, err
}
return billingStore.New(odoostorage.NewOdoo16Storage(
odoo16.OdooCredentials{
URL: o.odoo16URL,
Admin: o.odoo16Account,
Password: o.odoo16Password,
Database: o.odoo16Db,
}, odoo16.Config{
LanguagePreference: o.odoo16LanguagePreference,
PaymentTermID: o.odoo16PaymentTermID,
CountryIDs: countryIDs,
}).(authwrapper.StorageScoper))(s, g)
default:
return nil, fmt.Errorf("unknown billing entity storage: %s", o.billingEntityStorage)
}
}
type invitationStorageBuilder struct {
usernamePrefix *string
backingNS string
}
func (i *invitationStorageBuilder) Build(s *runtime.Scheme, g genericregistry.RESTOptionsGetter) (rest.Storage, error) {
return user.NewInvitationStorage(i.backingNS)(s, g)
}
func (i *invitationStorageBuilder) BuildRedeem(s *runtime.Scheme, g genericregistry.RESTOptionsGetter) (rest.Storage, error) {
return user.NewInvitationRedeemStorage(*i.usernamePrefix)(s, g)
}
type organizationStatusRegisterer struct {
*orgv1.Organization
}
func (o organizationStatusRegisterer) GetGroupVersionResource() schema.GroupVersionResource {
gvr := o.Organization.GetGroupVersionResource()
gvr.Resource = fmt.Sprintf("%s/status", gvr.Resource)
return gvr
}