-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package bmc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// SOLTerminator for terminating SOL sessions on a BMC. | ||
type SOLTerminator interface { | ||
TerminateSOL(ctx context.Context) (err error) | ||
} | ||
|
||
// terminatorProvider is an internal struct to correlate an implementation/provider and its name | ||
type terminatorProvider struct { | ||
name string | ||
solTerminator SOLTerminator | ||
} | ||
|
||
// terminateSOL tries all implementations for a successful SOL termination | ||
func terminateSOL(ctx context.Context, timeout time.Duration, b []terminatorProvider) (ok bool, metadata Metadata, err error) { | ||
var metadataLocal Metadata | ||
|
||
for _, elem := range b { | ||
if elem.solTerminator == nil { | ||
continue | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
err = multierror.Append(err, ctx.Err()) | ||
|
||
return false, metadata, err | ||
default: | ||
metadataLocal.ProvidersAttempted = append(metadataLocal.ProvidersAttempted, elem.name) | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
err := elem.solTerminator.TerminateSOL(ctx) | ||
if err != nil { | ||
err = multierror.Append(err, errors.WithMessagef(err, "provider: %v", elem.name)) | ||
continue | ||
} | ||
metadataLocal.SuccessfulProvider = elem.name | ||
return true, metadataLocal, nil | ||
} | ||
} | ||
return false, metadataLocal, multierror.Append(err, errors.New("failed to terminate SOL session")) | ||
} | ||
|
||
// TerminateSOLFromInterfaces identifies implementations of the SOLTerminator interface and passes them to the terminateSOL() wrapper method. | ||
func TerminateSOLFromInterfaces(ctx context.Context, timeout time.Duration, generic []interface{}) (ok bool, metadata Metadata, err error) { | ||
terminators := make([]terminatorProvider, 0) | ||
for _, elem := range generic { | ||
temp := terminatorProvider{name: getProviderName(elem)} | ||
switch p := elem.(type) { | ||
case SOLTerminator: | ||
temp.solTerminator = p | ||
terminators = append(terminators, temp) | ||
default: | ||
e := fmt.Sprintf("not an SOLTerminator implementation: %T", p) | ||
err = multierror.Append(err, errors.New(e)) | ||
} | ||
} | ||
if len(terminators) == 0 { | ||
return false, metadata, multierror.Append(err, errors.New("no SOLTerminator implementations found")) | ||
} | ||
return terminateSOL(ctx, timeout, terminators) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters