/
services.go
84 lines (75 loc) · 3.2 KB
/
services.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
package cli
import (
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
// ServeAdminFlag is the admin service flag
ServeAdminFlag string = "serve-admin"
// ServeOrdersFlag is the orders service flag
ServeOrdersFlag string = "serve-orders"
// ServeAPIInternalFlag is the internal api service flag
ServeAPIInternalFlag string = "serve-api-internal"
// ServeGHCFlag is the ghc api service flag
ServeGHCFlag string = "serve-api-ghc"
// ServePrimeFlag is the prime api flag
ServePrimeFlag string = "serve-api-prime"
// ServeSupportFlag is the support api flag
ServeSupportFlag string = "serve-api-support"
// ServePrimeSimulatorFlag is the prime simulator api flag
ServePrimeSimulatorFlag string = "serve-prime-simulator"
// ServeClientCollectorFlag enables the endpoints to collect client
// telemetry and logs
ServeClientCollectorFlag string = "serve-client-collector"
)
// InitServiceFlags initializes the service command line flags
func InitServiceFlags(flag *pflag.FlagSet) {
flag.Bool(ServeAdminFlag, false, "Enable the Admin Service.")
flag.Bool(ServeOrdersFlag, false, "Enable the Orders Service.")
flag.Bool(ServeAPIInternalFlag, false, "Enable the Internal API Service.")
flag.Bool(ServeGHCFlag, false, "Enable the GHC API Service.")
flag.Bool(ServePrimeFlag, false, "Enable the Prime API Service.")
flag.Bool(ServeSupportFlag, false, "Enable the Support Service.")
flag.Bool(ServePrimeSimulatorFlag, false, "Enable the Prime Simulator Service.")
flag.Bool(ServeClientCollectorFlag, false, "Enable the Client Collector.")
}
// CheckServices validates these lovely service flags
func CheckServices(v *viper.Viper) error {
adminEnabled := v.GetBool(ServeAdminFlag)
ordersEnabled := v.GetBool(ServeOrdersFlag)
internalAPIEnabled := v.GetBool(ServeAPIInternalFlag)
ghcAPIEnabled := v.GetBool(ServeGHCFlag)
primeAPIEnabled := v.GetBool(ServePrimeFlag)
primeSimulatorEnabled := v.GetBool(ServePrimeSimulatorFlag)
// Oops none of the flags used
if (!adminEnabled) &&
(!ordersEnabled) &&
(!internalAPIEnabled) &&
(!ghcAPIEnabled) &&
(!primeAPIEnabled) &&
(!primeSimulatorEnabled) {
return fmt.Errorf("no service was enabled")
}
// if Orders is enabled then the mutualTLSListener is needed too
// if PRIME is enabled then the mutualTLSListener is needed too
mutualTLSEnabled := v.GetBool(MutualTLSListenerFlag)
currentEnvironment := v.GetString(EnvironmentFlag)
devOrReviewEnvironment := currentEnvironment == EnvironmentDevelopment ||
currentEnvironment == EnvironmentReview
if !devOrReviewEnvironment {
if ordersEnabled && !mutualTLSEnabled {
return fmt.Errorf("for orders service to be enabled both %s and the %s flags must be in use", ServeOrdersFlag, MutualTLSListenerFlag)
}
if primeAPIEnabled && !mutualTLSEnabled {
return fmt.Errorf("for prime service to be enabled both %s and the %s flags must be in use", ServePrimeFlag, MutualTLSListenerFlag)
}
if mutualTLSEnabled && !(ordersEnabled || primeAPIEnabled) {
return fmt.Errorf("either orders or prime service must be enabled for mutualTSL to be enabled")
}
}
if currentEnvironment == EnvironmentPrd && primeSimulatorEnabled {
return fmt.Errorf("Prime Simulator cannot be enabled in production")
}
return nil
}