Skip to content

Commit

Permalink
Merge pull request #52 from Cray-HPE/import-sls
Browse files Browse the repository at this point in the history
Import sls
  • Loading branch information
rsjostrand-hpe authored Jun 16, 2023
2 parents 4be1d05 + 2580ab9 commit 79be5f6
Show file tree
Hide file tree
Showing 177 changed files with 36,640 additions and 400 deletions.
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,19 @@ generate-go:

# Generate clients from the following swagger files:
# System Layout Service: ./pkg/sls-client/openapi.yaml
generate-swagger: bin/swagger-codegen-cli.jar
java -jar bin/swagger-codegen-cli.jar generate -i ./pkg/sls-client/openapi.yaml -l go -o ./pkg/sls-client/ -DpackageName=sls_client
generate-swagger-sls-client: bin/swagger-codegen-cli.jar
java -jar bin/swagger-codegen-cli.jar generate -i ./pkg/sls-client/openapi.yaml -l go -o ./pkg/sls-client/ -DpackageName=sls_client -t ./pkg/sls-client/templates
go fmt ./pkg/sls-client/...
goimports -w ./pkg/sls-client

generate: generate-swagger generate-go
# Generate clients from the following swagger files:
# Hardware State Manager: ./pkg/hsm-client/openapi.yaml
generate-swagger-hsm-client: bin/swagger-codegen-cli.jar
java -jar bin/swagger-codegen-cli.jar generate -i ./pkg/hsm-client/openapi.yaml -l go -o ./pkg/hsm-client/ -DpackageName=hsm_client
go fmt ./pkg/hsm-client/...
goimports -w ./pkg/hsm-client

generate: generate-swagger-sls-client generate-swagger-hsm-client generate-go

# Jenkins doesn't have java installed, so the generate target fails to run
bin:
Expand Down
10 changes: 9 additions & 1 deletion cmd/cabinet/add_cabinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,16 @@ func addCabinet(cmd *cobra.Command, args []string) error {
}
}

// Push all the CLI flags that were provided into a generic map
// TODO Need to figure out how to specify to unset something
// Right now the build metadata function in the CSM provider will
// unset options if nil is passed in.
cabinetMetadata := map[string]interface{}{
"vlanID": vlanId,
}

// Add the cabinet to the inventory using domain methods
result, err := d.AddCabinet(cmd.Context(), args[0], cabinetNumber)
result, err := d.AddCabinet(cmd.Context(), args[0], cabinetNumber, cabinetMetadata)
if errors.Is(err, provider.ErrDataValidationFailure) {
// TODO the following should probably suggest commands to fix the issue?
log.Error().Msgf("Inventory data validation errors encountered")
Expand Down
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func setupLogging() {
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
// Fancy, human-friendly console logger (but slower)
// TODO Set no-color based off a flag
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
if Debug {
// enable debug output globally
Expand Down
1 change: 1 addition & 0 deletions cmd/session/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
root.SessionCmd.AddCommand(SessionStopCmd)
root.SessionCmd.AddCommand(SessionStatusCmd)
root.SessionCmd.AddCommand(SessionSummaryCmd)
root.SessionCmd.AddCommand(SessionImportCmd)

// Session start flags
// TODO need a quick simulation environment flag
Expand Down
61 changes: 61 additions & 0 deletions cmd/session/session_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package session

import (
"fmt"
"os"

root "github.com/Cray-HPE/cani/cmd"
"github.com/Cray-HPE/cani/internal/domain"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

// SessionStopCmd represents the session stop command
var SessionImportCmd = &cobra.Command{
Use: "import",
Short: "TODO THIS IS JUST A SHIM COMMAND",
Long: `TODO THIS IS JUST A SHIM COMMAND`,
SilenceUsage: true, // Errors are more important than the usage
PersistentPreRunE: DatastoreExists, // A session must be active to write to a datastore
RunE: importSession,
// PersistentPostRunE: writeSession,
}

// stopSession stops a session if one exists
func importSession(cmd *cobra.Command, args []string) error {
// Setup profiling
// f, err := os.Create("cpu_profile")
// if err != nil {
// panic(err)
// }

// pprof.StartCPUProfile(f)
// defer pprof.StopCPUProfile()

ds := root.Conf.Session.DomainOptions.DatastorePath
providerName := root.Conf.Session.DomainOptions.Provider
d, err := domain.New(root.Conf.Session.DomainOptions)
if err != nil {
return err
}

if root.Conf.Session.Active {
// Check that the datastore exists before proceeding since we cannot continue without it
_, err := os.Stat(ds)
if err != nil {
return fmt.Errorf("Session is STOPPED with provider '%s' but datastore '%s' does not exist", providerName, ds)
}
log.Info().Msgf("Session is STOPPED")
} else {
log.Info().Msgf("Session with provider '%s' and datastore '%s' is already STOPPED", providerName, ds)
}

log.Info().Msgf("Committing changes to session")

// Commit the external inventory
if err := d.Import(cmd.Context()); err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/session/session_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func startSession(cmd *cobra.Command, args []string) error {
}

// Validate the external inventory
result, err := root.Conf.Session.Domain.Validate(cmd.Context())
result, err := root.Conf.Session.Domain.Validate(cmd.Context(), false)
if errors.Is(err, provider.ErrDataValidationFailure) {
// TODO the following should probably suggest commands to fix the issue?
log.Error().Msgf("Inventory data validation errors encountered")
Expand Down
2 changes: 1 addition & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func validateInventory(cmd *cobra.Command, args []string) error {
return err
}
// Validate the external inventory
result, err := d.Validate(cmd.Context())
result, err := d.Validate(cmd.Context(), true)
if errors.Is(err, provider.ErrDataValidationFailure) {
// TODO the following should probably suggest commands to fix the issue?
log.Error().Msgf("Inventory data validation errors encountered")
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/Cray-HPE/hms-sls/v2 v2.1.0
github.com/Cray-HPE/hms-xname v1.1.0
github.com/Cray-HPE/hms-xname v1.1.1-0.20230602152417-25bcdeda83c9
github.com/antihax/optional v1.0.0
github.com/fatih/color v1.13.0
github.com/google/uuid v1.3.0
Expand All @@ -15,6 +15,7 @@ require (
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
golang.org/x/oauth2 v0.8.0
gopkg.in/yaml.v3 v3.0.1
inet.af/netaddr v0.0.0-20220617031823-097006376321
Expand All @@ -23,6 +24,7 @@ require (
require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -31,6 +33,7 @@ require (
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
go4.org/intern v0.0.0-20220617035311-6925f38cc365 // indirect
Expand All @@ -40,4 +43,5 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
)
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ github.com/Cray-HPE/hms-base/v2 v2.0.1/go.mod h1:Mq+Ao3q4YtNZJZ1ly9wnEIKyvc3+QaA
github.com/Cray-HPE/hms-s3 v1.9.2/go.mod h1:p5pVsMDeOdXKssd9qyFtXo4ztrn2lhD04nrO8+NOi7g=
github.com/Cray-HPE/hms-sls/v2 v2.1.0 h1:NSHlsITIA2pXajnG4kuoZz59+StYWKSLhDb7lkmYqa0=
github.com/Cray-HPE/hms-sls/v2 v2.1.0/go.mod h1:+MSzZViGwiVaip5pZhbrtFTv/8uTXeGrjSnsJMlU8uw=
github.com/Cray-HPE/hms-xname v1.1.0 h1:y/JEM2PbBaD8VbU7u85+/YIugf1Evji59z5IeSffeO8=
github.com/Cray-HPE/hms-xname v1.1.0/go.mod h1:3A70QF0ddmkt/nz0jis5o8UIB4zAmsgsUiN71dr97n4=
github.com/Cray-HPE/hms-xname v1.1.1-0.20230602152417-25bcdeda83c9 h1:mL9LycLicBSk0ROgwqqTy+CQOLME5RDH0mb/7BW0ib8=
github.com/Cray-HPE/hms-xname v1.1.1-0.20230602152417-25bcdeda83c9/go.mod h1:XKdjQSzoTps5KDOE8yWojBTAWASGaS6LfRrVDxwTQO8=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
Expand Down Expand Up @@ -77,6 +78,7 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cznic/mathutil v0.0.0-20180504122225-ca4c9f2c1369/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20200620013148-b91950f658ec/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down Expand Up @@ -281,6 +283,7 @@ github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down Expand Up @@ -318,6 +321,7 @@ github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xanzy/go-gitlab v0.15.0/go.mod h1:8zdQa/ri1dfn8eS3Ir1SyfvOKlw7WBJ8DVThkpGiXrs=
Expand Down Expand Up @@ -647,6 +651,7 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
11 changes: 6 additions & 5 deletions internal/domain/blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ func (d *Domain) AddBlade(ctx context.Context, deviceTypeSlug string, cabinetOrd
locationOrdinal := hardwareBuildOut.OrdinalPath[len(hardwareBuildOut.OrdinalPath)-1]

hardware := inventory.Hardware{
ID: hardwareBuildOut.ID,
Parent: hardwareBuildOut.ParentID,
Type: hardwareBuildOut.DeviceType.HardwareType,
Vendor: hardwareBuildOut.DeviceType.Manufacturer,
Model: hardwareBuildOut.DeviceType.Model,
ID: hardwareBuildOut.ID,
Parent: hardwareBuildOut.ParentID,
Type: hardwareBuildOut.DeviceType.HardwareType,
DeviceTypeSlug: hardwareBuildOut.DeviceType.Slug,
Vendor: hardwareBuildOut.DeviceType.Manufacturer,
Model: hardwareBuildOut.DeviceType.Model,

LocationOrdinal: &locationOrdinal,

Expand Down
18 changes: 12 additions & 6 deletions internal/domain/cabinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// AddCabinet adds a cabinet to the inventory
func (d *Domain) AddCabinet(ctx context.Context, deviceTypeSlug string, cabinetOrdinal int) (AddHardwareResult, error) {
func (d *Domain) AddCabinet(ctx context.Context, deviceTypeSlug string, cabinetOrdinal int, metadata map[string]interface{}) (AddHardwareResult, error) {
// Validate provided cabinet exists
// Craft the path to the cabinet
cabinetLocationPath := inventory.LocationPath{
Expand Down Expand Up @@ -73,17 +73,23 @@ func (d *Domain) AddCabinet(ctx context.Context, deviceTypeSlug string, cabinetO
locationOrdinal := hardwareBuildOut.OrdinalPath[len(hardwareBuildOut.OrdinalPath)-1]

hardware := inventory.Hardware{
ID: hardwareBuildOut.ID,
Parent: hardwareBuildOut.ParentID,
Type: hardwareBuildOut.DeviceType.HardwareType,
Vendor: hardwareBuildOut.DeviceType.Manufacturer,
Model: hardwareBuildOut.DeviceType.Model,
ID: hardwareBuildOut.ID,
Parent: hardwareBuildOut.ParentID,
Type: hardwareBuildOut.DeviceType.HardwareType,
DeviceTypeSlug: hardwareBuildOut.DeviceType.Slug,
Vendor: hardwareBuildOut.DeviceType.Manufacturer,
Model: hardwareBuildOut.DeviceType.Model,

LocationOrdinal: &locationOrdinal,

Status: inventory.HardwareStatusStaged,
}

// Ask the inventory provider to craft a metadata object for this information
if err := d.externalInventoryProvider.BuildHardwareMetadata(&hardware, metadata); err != nil {
return AddHardwareResult{}, err
}

log.Debug().Any("id", hardware.ID).Msg("Hardware")
log.Debug().Str("path", hardwareBuildOut.LocationPathString()).Msg("Hardware Build out")

Expand Down
2 changes: 1 addition & 1 deletion internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func New(opts *NewOpts) (*Domain, error) {
// Determine which external inventory provider to use
switch inventoryProvider {
case inventory.CSMProvider:
domain.externalInventoryProvider, err = csm.New(&opts.CsmOptions)
domain.externalInventoryProvider, err = csm.New(&opts.CsmOptions, domain.hardwareTypeLibrary)
if err != nil {
return nil, errors.Join(
fmt.Errorf("failed to initialize CSM external inventory provider"),
Expand Down
9 changes: 9 additions & 0 deletions internal/domain/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain

import (
"context"
)

func (d *Domain) Import(ctx context.Context) error {
return d.externalInventoryProvider.Import(ctx, d.datastore)
}
4 changes: 2 additions & 2 deletions internal/domain/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type ValidateResult struct {
ProviderValidationErrors map[uuid.UUID]provider.HardwareValidationResult
}

func (d *Domain) Validate(ctx context.Context) (ValidateResult, error) {
func (d *Domain) Validate(ctx context.Context, checkRequiredData bool) (ValidateResult, error) {
var result ValidateResult

// Validate the current state of CANI's inventory data against the provider plugin
// for provider specific data.
if failedValidations, err := d.externalInventoryProvider.ValidateInternal(ctx, d.datastore, true); len(failedValidations) > 0 {
if failedValidations, err := d.externalInventoryProvider.ValidateInternal(ctx, d.datastore, checkRequiredData); len(failedValidations) > 0 {
result.ProviderValidationErrors = failedValidations
return result, provider.ErrDataValidationFailure
} else if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/inventory/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ type Datastore interface {
GetSystem(hardware Hardware) (Hardware, error) // Not yet implemented until multiple systems are supported

// TODO for search properties

// Clone creates a in-memory version of the datastore to perform location operations
Clone() (Datastore, error)

// Merge the contents of the remote datastore (most likely a in-memory one with changes)
Merge(Datastore) error
}
Loading

0 comments on commit 79be5f6

Please sign in to comment.