// Apply applies the servers to the db
Apply applies the servers to the db
// Apply applies the servers to the db
func (s ServerService) Apply(ctx context.Context, r api.ServerApplyRequest) (*api.ServerApplyResponse, error) {
	logger := s.logger.With(
		zap.String("server", r.Hostname),
		zap.String("os", r.OperatingSystem),
		zap.String("nuix_path", r.NuixPath),
		zap.String("service_account", r.Username),
	)
	if r.OperatingSystem != "linux" && r.OperatingSystem != "windows" {
		logger.Error("specify operating system - 'linux' or 'windows'", zap.String("exception", "invalid operating system"))
		return nil, fmt.Errorf("specify operating_system for %s - 'linux' or 'windows'", r.Hostname)
	}
	logger.Debug("Checking if server already exists")
	var newSrv api.Server
	if err := s.db.Where("hostname = ?", r.Hostname).First(&newSrv).Error; err != nil {
		if !gorm.IsRecordNotFoundError(err) {
			logger.Error("Cannot get the server", zap.String("exception", err.Error()))
			return nil, err
		}
		logger.Debug("Server already exist - will update instead of create new")
	}
	logger.Debug("Checking if the server should be tested or not")
	if newSrv.ID == 0 {
		logger.Debug("Testing the server")
		logger.Info("Creating powershell-session for the testing server")
		session, err := s.shell.NewSession(r.Hostname, r.Username, r.Password)
		if err != nil {
			logger.Error("Failed to create remote-client for powershell", zap.String("exception", err.Error()))
			return nil, fmt.Errorf("failed to create remote-client for powershell: %v", err)
		}
		defer session.Close()
		logger.Debug("Enabling CredSSP")
		if err := session.EnableCredSSP(); err != nil {
			logger.Error("Failed to enable CredSSP", zap.String("exception", err.Error()))
			return nil, fmt.Errorf("failed to enable credssp: %v", err)
		}
	}
	session, err := s.shell.NewSessionCredSSP(r.Hostname, r.Username, r.Password)
	if err != nil {
		logger.Error("Failed to create remote-client for powershell with CredSSP", zap.String("exception", err.Error()))
		return nil, fmt.Errorf("failed to create remote-client for powershell: %v", err)
	}
	defer session.Close()
	if newSrv.NuixPath != r.NuixPath {
		if err := session.CheckPath(r.NuixPath); err != nil {
			logger.Error("Failed to test NuixPath for server", zap.String("exception", err.Error()))
			return nil, fmt.Errorf("failed to test NuixPath for server: %v", err)
		}
		logger.Debug("Nuix-path is ok for server")
	}
	if newSrv.AvianScripts != r.AvianScripts {
		if err := session.CheckPath(r.AvianScripts); err != nil {
			logger.Error("Failed to test AvianScripts-path for server", zap.String("exception", err.Error()))
			return nil, fmt.Errorf("failed to test avian-scripts-path for server: %v", err)
		}
		logger.Debug("Avian-scripts path is ok for server")
	}
	newSrv.Hostname = r.Hostname
	newSrv.Port = r.Port
	newSrv.Username = r.Username
	newSrv.Password = r.Password
	newSrv.OperatingSystem = r.OperatingSystem
	newSrv.NuixPath = r.NuixPath
	newSrv.AvianScripts = r.AvianScripts
	logger.Info("Saving server to the DB")
	if err := s.db.Save(&newSrv).Error; err != nil {
		logger.Error("Cannot save server to DB", zap.String("exception", err.Error()))
		return nil, fmt.Errorf("failed to apply server %s : %v", newSrv.Hostname, err)
	}
	logger.Debug("Server has been saved to the DB")
	return &api.ServerApplyResponse{}, nil
}
func ( s ServerService ) Apply ( ctx context . Context , r api . ServerApplyRequest ) ( * api . ServerApplyResponse , error ) { logger := s . logger . With ( zap . String ( " " , r . Hostname ) , zap . String ( " " , r . OperatingSystem ) , zap . String ( " " , r . NuixPath ) , zap . String ( " " , r . Username ) , ) if r . OperatingSystem != " " && r . OperatingSystem != " " { logger . Error ( " " , zap . String ( " " , " " ) ) return nil , fmt . Errorf ( " " , r . Hostname ) } logger . Debug ( " " ) var newSrv api . Server if err := s . db . Where ( " " , r . Hostname ) . First ( & newSrv ) . Error ; err != nil { if ! gorm . IsRecordNotFoundError ( err ) { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , err } logger . Debug ( " " ) } logger . Debug ( " " ) if newSrv . ID == 0 { logger . Debug ( " " ) logger . Info ( " " ) session , err := s . shell . NewSession ( r . Hostname , r . Username , r . Password ) if err != nil { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , fmt . Errorf ( " " , err ) } defer session . Close ( ) logger . Debug ( " " ) if err := session . EnableCredSSP ( ) ; err != nil { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , fmt . Errorf ( " " , err ) } } session , err := s . shell . NewSessionCredSSP ( r . Hostname , r . Username , r . Password ) if err != nil { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , fmt . Errorf ( " " , err ) } defer session . Close ( ) if newSrv . NuixPath != r . NuixPath { if err := session . CheckPath ( r . NuixPath ) ; err != nil { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , fmt . Errorf ( " " , err ) } logger . Debug ( " " ) } if newSrv . AvianScripts != r . AvianScripts { if err := session . CheckPath ( r . AvianScripts ) ; err != nil { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , fmt . Errorf ( " " , err ) } logger . Debug ( " " ) } newSrv . Hostname = r . Hostname newSrv . Port = r . Port newSrv . Username = r . Username newSrv . Password = r . Password newSrv . OperatingSystem = r . OperatingSystem newSrv . NuixPath = r . NuixPath newSrv . AvianScripts = r . AvianScripts logger . Info ( " " ) if err := s . db . Save ( & newSrv ) . Error ; err != nil { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , fmt . Errorf ( " " , newSrv . Hostname , err ) } logger . Debug ( " " ) return & api . ServerApplyResponse { } , nil }
Apply applies the servers to the db
Apply applies the servers to the db
// Check if the requested server exists (in that case update it) // return the error if it isn't a "record not found"-error // Test connection // create the client // close the client on exit // Set data to the new Server-model // Save the new NMS to the DB