-
Notifications
You must be signed in to change notification settings - Fork 72
/
statusBuilder.go
216 lines (175 loc) · 6.13 KB
/
statusBuilder.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package status
import (
"fmt"
"io"
"reflect"
"strings"
"text/tabwriter"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/redhat-developer/app-services-cli/pkg/shared/servicespec"
"github.com/redhat-developer/app-services-cli/pkg/shared/svcstatus"
registrymgmtclient "github.com/redhat-developer/app-services-sdk-go/registrymgmt/apiv1/client"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
"github.com/openconfig/goyang/pkg/indent"
)
const tagTitle = "title"
type serviceStatus struct {
Name string `json:"name,omitempty" title:"Service Context Name"`
Location string `json:"location,omitempty" title:"Context File Location"`
Kafka *kafkaStatus `json:"kafka,omitempty" title:"Kafka"`
Registry *registryStatus `json:"registry,omitempty" title:"Service Registry"`
}
func (s serviceStatus) hasStatus() bool {
return s.Kafka != nil || s.Registry != nil
}
type kafkaStatus struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
BootstrapServerHost string `json:"bootstrap_server_host,omitempty" title:"Bootstrap URL"`
FailedReason string `json:"failed_reason,omitempty" title:"Failed Reason"`
}
type registryStatus struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
RegistryUrl string `json:"registryUrl,omitempty" title:"Registry URL"`
}
type clientConfig struct {
f *factory.Factory
serviceConfig *servicecontext.ServiceConfig
}
type statusClient struct {
f *factory.Factory
serviceConfig *servicecontext.ServiceConfig
}
// newStatusClient returns a new client to fetch service statuses
// and build it into a service status config object
func newStatusClient(cfg *clientConfig) *statusClient {
return &statusClient{
f: cfg.f,
serviceConfig: cfg.serviceConfig,
}
}
// BuildStatus gets the status of all services currently set in the service context
func (c *statusClient) BuildStatus(services []string) (status *serviceStatus, err error) {
factory := c.f
status = &serviceStatus{}
if flagutil.StringInSlice(servicespec.KafkaServiceName, services) && c.serviceConfig.KafkaID != "" {
kafkaResponse, err1 := contextutil.GetKafkaForServiceConfig(c.serviceConfig, factory)
if err1 != nil {
return status, err1
}
kafkaStatus := c.getKafkaStatus(kafkaResponse)
status.Kafka = kafkaStatus
}
if flagutil.StringInSlice(servicespec.ServiceRegistryServiceName, services) && c.serviceConfig.ServiceRegistryID != "" {
registryResponse, err1 := contextutil.GetRegistryForServiceConfig(c.serviceConfig, factory)
if err1 != nil {
return status, err1
}
registry := c.getRegistryStatus(registryResponse)
status.Registry = registry
}
return status, err
}
func (c *statusClient) getKafkaStatus(kafkaResponse *kafkamgmtclient.KafkaRequest) (status *kafkaStatus) {
status = &kafkaStatus{
ID: kafkaResponse.GetId(),
Name: kafkaResponse.GetName(),
Status: kafkaResponse.GetStatus(),
BootstrapServerHost: kafkaResponse.GetBootstrapServerHost(),
}
if kafkaResponse.GetStatus() == svcstatus.StatusFailed {
status.FailedReason = kafkaResponse.GetFailedReason()
}
return status
}
func (c *statusClient) getRegistryStatus(registry *registrymgmtclient.Registry) (status *registryStatus) {
status = ®istryStatus{
ID: registry.GetId(),
Name: registry.GetName(),
RegistryUrl: registry.GetRegistryUrl(),
Status: string(registry.GetStatus()),
}
return status
}
// Print prints the status information of all set services
func Print(w io.Writer, status *serviceStatus) {
v := reflect.ValueOf(status).Elem()
indirectVal := reflect.Indirect(v)
for i := 0; i < indirectVal.NumField(); i++ {
fieldType := indirectVal.Type().Field(i)
fieldVal := indirectVal.Field(i)
title := getTitle(&fieldType)
if fieldVal.Kind() == reflect.String {
fmt.Fprintf(w, "%v:\t%v\n", title, fieldVal)
}
if fieldVal.Kind() == reflect.Ptr && !fieldVal.IsNil() {
fmt.Fprintln(w, "")
printServiceStatus(w, title, fieldVal)
}
}
}
// print the status of service v
func printServiceStatus(w io.Writer, name string, v reflect.Value) {
indentWriter := indent.NewWriter(w, " ")
// set table padding
padding := 5
// create a new tabwriter
tw := tabwriter.NewWriter(indentWriter, 0, 0, padding, ' ', tabwriter.TabIndent)
// tracks the longest row in chars so we can set an equal length divider
maxRowLen := 0
// iterate over every field in the type
indirectV := reflect.Indirect(v)
for i := 0; i < indirectV.NumField(); i++ {
// get field type metadata
fieldType := indirectV.Type().Field(i)
// get value of the field
fieldValue := indirectV.Field(i)
// get the title to use for the field
title := getTitle(&fieldType)
if !getOmitEmpty(&fieldType) || !fieldValue.IsZero() {
// print the row and take note of its character length
charLen, _ := fmt.Fprintf(tw, "%v:\t\t%v\n", title, fieldValue)
if charLen > maxRowLen {
maxRowLen = charLen
}
}
}
// print the service header
fmt.Fprintln(indentWriter, name)
// print the title divider
fmt.Fprintln(indentWriter, createDivider(maxRowLen+padding))
tw.Flush()
}
// get title of the field from the "title" tag
// If the tag does not exist, use the name of the field
func getTitle(f *reflect.StructField) string {
// Get the field tag value
tag := f.Tag.Get(tagTitle)
if tag == "" {
tag = f.Name
}
return tag
}
// check if omitempty is set
func getOmitEmpty(f *reflect.StructField) bool {
var omitempty bool
tag := f.Tag.Get("json")
if tag != "" {
omitempty = strings.Contains(tag, "omitempty")
}
return omitempty
}
// create a divider for the top of the table of n length
func createDivider(n int) string {
b := "-"
for i := 0; i <= n; i++ {
b += "-"
}
return b
}