blob: 2fcdff2449488199ac7fd8d0419afd93cf1a265e [file] [log] [blame]
David K. Bainbridge9189c632021-03-26 21:52:21 +00001/*
2 * Copyright 2021-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package config
17
18import (
19 "log"
20 "os"
21 "time"
22)
23
24func NewDefaultConfig() *GlobalConfigSpec {
25 return &GlobalConfigSpec{
26 ApiVersion: "v3",
27 CurrentStack: "",
28 Stacks: []*StackConfigSpec{NewDefaultStack("default")},
29 }
30}
31
32func NewDefaultStack(name string) *StackConfigSpec {
33 return &StackConfigSpec{
34 Name: name,
35 Server: "localhost:55555",
36 Kafka: "localhost:9093",
37 KvStore: "localhost:2379",
38 Tls: TlsConfigSpec{
39 UseTls: false,
40 Verify: false,
41 },
42 Grpc: GrpcConfigSpec{
43 ConnectTimeout: 5 * time.Second,
44 Timeout: 5 * time.Minute,
45 MaxCallRecvMsgSize: "4MB",
46 },
47 KvStoreConfig: KvStoreConfigSpec{
48 Timeout: 5 * time.Second,
49 },
50 }
51}
52
53func (g *GlobalConfigSpec) StackByName(name string) *StackConfigSpec {
54 for _, stack := range g.Stacks {
55 if stack.Name == name {
56 return stack
57 }
58 }
59 return nil
60}
61
62func (g *GlobalConfigSpec) CurrentAsStack() *StackConfigSpec {
63 if g.CurrentStack == "" {
64 if len(g.Stacks) == 0 {
65 return nil
66 }
67 return g.Stacks[0]
68 }
69 return g.StackByName(g.CurrentStack)
70}
71
72func (g GlobalConfigSpec) Current() *StackConfigSpec {
73 stack := g.CurrentAsStack()
74 if stack == nil {
75 if len(g.Stacks) > 1 {
76 log.New(os.Stderr, "ERROR: ", 0).
77 Fatal("multiple stacks configured without current specified")
78 }
79 log.New(os.Stderr, "ERROR: ", 0).
80 Fatalf("current stack specified, '%s', does not exist as a configured stack",
81 g.CurrentStack)
82 }
83 return stack
84}