Skip to content

Commit

Permalink
refactored to remove some duplicate code
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Burns <[email protected]>
  • Loading branch information
mattcburns committed Jan 3, 2024
1 parent 658753b commit 847b7a3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 100 deletions.
11 changes: 11 additions & 0 deletions grpc/oob/diagnostic/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

type Action struct {
common.Accessory
ActionName string
RPCName string
ScreenshotRequest *v1.ScreenshotRequest
ClearSystemEventLogRequest *v1.ClearSystemEventLogRequest
SystemEventLogRequest *v1.SystemEventLogRequest
Expand All @@ -30,5 +32,14 @@ func WithStatusMessage(s chan string) Option {
}
}

// WithLabels adds the custom tracing and logging labels to an Action struct.
func WithLabels(actionName string, rpcName string) Option {
return func(a *Action) error {
a.ActionName = actionName
a.RPCName = rpcName
return nil
}
}

// Option to add to an Actions.
type Option func(a *Action) error
129 changes: 34 additions & 95 deletions grpc/oob/diagnostic/getsel.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ func NewSystemEventLogAction(req interface{}, opts ...Option) (*Action, error) {
return a, nil
}

func (m Action) SystemEventLog(ctx context.Context) (result bmc.SystemEventLogEntries, err error) {
func (m Action) SystemEventLog(ctx context.Context) (entries bmc.SystemEventLogEntries, raw string, err error) {
labels := prometheus.Labels{
"service": "diagnostic",
"action": "system_event_log",
"action": m.ActionName,
}

timer := prometheus.NewTimer(metrics.ActionDuration.With(labels))
defer timer.ObserveDuration()

tracer := otel.Tracer("pbnj")
ctx, span := tracer.Start(ctx, "diagnostic.SystemEventLog", trace.WithAttributes(
ctx, span := tracer.Start(ctx, "diagnostic."+m.RPCName, trace.WithAttributes(
attribute.String("bmc.device", m.SystemEventLogRequest.GetAuthn().GetDirectAuthn().GetHost().GetHost()),
))
defer span.End()
Expand All @@ -61,7 +61,7 @@ func (m Action) SystemEventLog(ctx context.Context) (result bmc.SystemEventLogEn
host, user, password, parseErr := m.ParseAuth(m.SystemEventLogRequest.GetAuthn())
if parseErr != nil {
span.SetStatus(codes.Error, "error parsing credentials: "+parseErr.Error())
return result, parseErr
return entries, raw, parseErr
}

span.SetAttributes(attribute.String("bmc.host", host), attribute.String("bmc.username", user))
Expand All @@ -72,7 +72,16 @@ func (m Action) SystemEventLog(ctx context.Context) (result bmc.SystemEventLogEn
}

client := bmclib.NewClient(host, user, password, opts...)
client.Registry.Drivers = client.Registry.Supports(providers.FeatureGetSystemEventLog)

// Set the driver(s) to use based on the request type
switch {
case m.SystemEventLogRequest != nil:
client.Registry.Drivers = client.Registry.Supports(providers.FeatureGetSystemEventLog)
case m.SystemEventLogRawRequest != nil:
client.Registry.Drivers = client.Registry.Supports(providers.FeatureGetSystemEventLogRaw)
default:
return entries, raw, fmt.Errorf("unsupported request type")
}

m.SendStatusMessage("connecting to BMC")
err = client.Open(ctx)
Expand All @@ -81,7 +90,7 @@ func (m Action) SystemEventLog(ctx context.Context) (result bmc.SystemEventLogEn
attribute.StringSlice("bmc.open.successfulOpenConns", meta.SuccessfulOpenConns))
if err != nil {
span.SetStatus(codes.Error, err.Error())
return nil, &repository.Error{
return entries, raw, &repository.Error{
Code: v1.Code_value["PERMISSION_DENIED"],
Message: err.Error(),
}
Expand All @@ -94,107 +103,37 @@ func (m Action) SystemEventLog(ctx context.Context) (result bmc.SystemEventLogEn
log.Info("connected to BMC", logMetadata(client.GetMetadata())...)
m.SendStatusMessage("connected to BMC")

// Get the system event log
m.SendStatusMessage("getting system event log")
sel, err := client.GetSystemEventLog(ctx)
log = m.Log.WithValues(logMetadata(client.GetMetadata())...)
meta = client.GetMetadata()
span.SetAttributes(attribute.StringSlice("bmc.system_event_log.providersAttempted", meta.ProvidersAttempted),
attribute.StringSlice("bmc.system_event_log.successfulOpenConns", meta.SuccessfulOpenConns))
if err != nil {
log.Error(err, "error getting system event log")
span.SetStatus(codes.Error, "error getting system event log: "+err.Error())
m.SendStatusMessage(fmt.Sprintf("failed to get system event log %v", host))

return nil, &repository.Error{
Code: v1.Code_value["UNKNOWN"],
Message: err.Error(),
}
}

span.SetStatus(codes.Ok, "")
log.Info("got system event log", logMetadata(client.GetMetadata())...)
m.SendStatusMessage(fmt.Sprintf("got system event log on %v", host))

return sel, nil
}

func (m Action) SystemEventLogRaw(ctx context.Context) (result string, err error) {
labels := prometheus.Labels{
"service": "diagnostic",
"action": "system_event_log_raw",
}

timer := prometheus.NewTimer(metrics.ActionDuration.With(labels))
defer timer.ObserveDuration()

tracer := otel.Tracer("pbnj")
ctx, span := tracer.Start(ctx, "diagnostic.SystemEventLogRaw", trace.WithAttributes(
attribute.String("bmc.device", m.SystemEventLogRawRequest.GetAuthn().GetDirectAuthn().GetHost().GetHost()),
))
defer span.End()

if v := m.SystemEventLogRawRequest.GetVendor(); v != nil {
span.SetAttributes(attribute.String("bmc.vendor", v.GetName()))
}

host, user, password, parseErr := m.ParseAuth(m.SystemEventLogRawRequest.GetAuthn())
if parseErr != nil {
span.SetStatus(codes.Error, "error parsing credentials: "+parseErr.Error())
return result, parseErr
}

span.SetAttributes(attribute.String("bmc.host", host), attribute.String("bmc.username", user))
m.SendStatusMessage("getting " + m.ActionName + " on " + host)

opts := []bmclib.Option{
bmclib.WithLogger(m.Log),
bmclib.WithPerProviderTimeout(common.BMCTimeoutFromCtx(ctx)),
}

client := bmclib.NewClient(host, user, password, opts...)
client.Registry.Drivers = client.Registry.Supports(providers.FeatureGetSystemEventLogRaw)

m.SendStatusMessage("connecting to BMC")
err = client.Open(ctx)
meta := client.GetMetadata()
span.SetAttributes(attribute.StringSlice("bmc.open.providersAttempted", meta.ProvidersAttempted),
attribute.StringSlice("bmc.open.successfulOpenConns", meta.SuccessfulOpenConns))
if err != nil {
span.SetStatus(codes.Error, err.Error())
return "", &repository.Error{
Code: v1.Code_value["PERMISSION_DENIED"],
Message: err.Error(),
}
switch {
case m.SystemEventLogRequest != nil:
// Get the system event log
entries, err = client.GetSystemEventLog(ctx)
case m.SystemEventLogRawRequest != nil:
// Get the system event log
raw, err = client.GetSystemEventLogRaw(ctx)
default:
return entries, raw, fmt.Errorf("unsupported request type")
}
log := m.Log.WithValues("host", host, "user", user)
defer func() {
client.Close(ctx)
log.Info("closed connections", logMetadata(client.GetMetadata())...)
}()
log.Info("connected to BMC", logMetadata(client.GetMetadata())...)
m.SendStatusMessage("connected to BMC")

// Get the system event log
m.SendStatusMessage("getting system event log")
sel, err := client.GetSystemEventLogRaw(ctx)
log = m.Log.WithValues(logMetadata(client.GetMetadata())...)
meta = client.GetMetadata()
span.SetAttributes(attribute.StringSlice("bmc.get_system_event_log_raw.providersAttempted", meta.ProvidersAttempted),
attribute.StringSlice("bmc.get_system_event_log_raw.successfulOpenConns", meta.SuccessfulOpenConns))
span.SetAttributes(attribute.StringSlice("bmc."+m.ActionName+".providersAttempted", meta.ProvidersAttempted),
attribute.StringSlice("bmc."+m.ActionName+".successfulOpenConns", meta.SuccessfulOpenConns))
if err != nil {
log.Error(err, "error getting raw system event log")
span.SetStatus(codes.Error, "error getting raw system event log: "+err.Error())
m.SendStatusMessage(fmt.Sprintf("failed to get raw system event log %v", host))
log.Error(err, "error getting "+m.ActionName)
span.SetStatus(codes.Error, "error getting "+m.ActionName+": "+err.Error())
m.SendStatusMessage(fmt.Sprintf("failed to get "+m.ActionName+" %v", host))

return "", &repository.Error{
return entries, raw, &repository.Error{
Code: v1.Code_value["UNKNOWN"],
Message: err.Error(),
}
}

span.SetStatus(codes.Ok, "")
log.Info("got raw system event log", logMetadata(client.GetMetadata())...)
m.SendStatusMessage(fmt.Sprintf("got raw system event log on %v", host))
log.Info("got "+m.ActionName, logMetadata(client.GetMetadata())...)
m.SendStatusMessage(fmt.Sprintf("got "+m.ActionName+" on %v", host))

return sel, nil
return entries, raw, nil
}
12 changes: 7 additions & 5 deletions grpc/rpc/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ func (d *DiagnosticService) SystemEventLog(ctx context.Context, in *v1.SystemEve
"vendor", in.Vendor.GetName(),
)

Check warning on line 90 in grpc/rpc/diagnostic.go

View check run for this annotation

Codecov / codecov/patch

grpc/rpc/diagnostic.go#L87-L90

Added lines #L87 - L90 were not covered by tests

selaction, err := diagnostic.NewSystemEventLogAction(in, diagnostic.WithLogger(l))
selaction, err := diagnostic.NewSystemEventLogAction(in, diagnostic.WithLogger(l),
diagnostic.WithLabels("system_event_log", "SystemEventLog"))
if err != nil {
l.Error(err, "error creating system event log action")
return nil, err

Check warning on line 96 in grpc/rpc/diagnostic.go

View check run for this annotation

Codecov / codecov/patch

grpc/rpc/diagnostic.go#L92-L96

Added lines #L92 - L96 were not covered by tests
}

entries, err := selaction.SystemEventLog(ctx)
entries, _, err := selaction.SystemEventLog(ctx)
if err != nil {
l.Error(err, "error getting system event log")
return nil, err

Check warning on line 102 in grpc/rpc/diagnostic.go

View check run for this annotation

Codecov / codecov/patch

grpc/rpc/diagnostic.go#L99-L102

Added lines #L99 - L102 were not covered by tests
Expand All @@ -111,18 +112,19 @@ func (d *DiagnosticService) SystemEventLog(ctx context.Context, in *v1.SystemEve
func (d *DiagnosticService) SystemEventLogRaw(ctx context.Context, in *v1.SystemEventLogRawRequest) (*v1.SystemEventLogRawResponse, error) {
l := logging.ExtractLogr(ctx)

Check warning on line 113 in grpc/rpc/diagnostic.go

View check run for this annotation

Codecov / codecov/patch

grpc/rpc/diagnostic.go#L112-L113

Added lines #L112 - L113 were not covered by tests

l.Info("start Get System Event Log request",
l.Info("start Get System Event Log Raw request",
"username", in.Authn.GetDirectAuthn().GetUsername(),
"vendor", in.Vendor.GetName(),
)

Check warning on line 118 in grpc/rpc/diagnostic.go

View check run for this annotation

Codecov / codecov/patch

grpc/rpc/diagnostic.go#L115-L118

Added lines #L115 - L118 were not covered by tests

rawselaction, err := diagnostic.NewSystemEventLogAction(in, diagnostic.WithLogger(l))
rawselaction, err := diagnostic.NewSystemEventLogAction(in, diagnostic.WithLogger(l),
diagnostic.WithLabels("system_event_log_raw", "SystemEventLogRaw"))
if err != nil {
l.Error(err, "error creating raw system event log action")
return nil, err

Check warning on line 124 in grpc/rpc/diagnostic.go

View check run for this annotation

Codecov / codecov/patch

grpc/rpc/diagnostic.go#L120-L124

Added lines #L120 - L124 were not covered by tests
}

eventlog, err := rawselaction.SystemEventLogRaw(ctx)
_, eventlog, err := rawselaction.SystemEventLog(ctx)
if err != nil {
l.Error(err, "error getting raw system event log")
return nil, err

Check warning on line 130 in grpc/rpc/diagnostic.go

View check run for this annotation

Codecov / codecov/patch

grpc/rpc/diagnostic.go#L127-L130

Added lines #L127 - L130 were not covered by tests
Expand Down

0 comments on commit 847b7a3

Please sign in to comment.