// Apply applies the nms-servers to the database
Apply applies the nms-servers to the database
// Apply applies the nms - servers to the database
func (s NmsService) Apply(ctx context.Context, r api.NmsApplyRequests) (*api.NmsApplyResponse, error) {
	s.logger.Debug("Starting db-transaction for NMS-apply")
	tx := s.db.BeginTx(ctx, nil)
	client := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		},
		Timeout: 10 * time.Second,
	}
	var resp api.NmsApplyResponse
	for _, nms := range r.Nms {
		if _, err := client.Get(fmt.Sprintf("https://%s:%d", nms.Address, nms.Port)); err != nil {
			return nil, fmt.Errorf("Cannot establish connection to NMS with address: %s:%d - %v", nms.Address, nms.Port, err)
		}
		s.logger.Debug("Checking if nms already exists", zap.String("nms", nms.Address))
		var newNms api.Nms
		if err := tx.Preload("Licences").Where("address = ?", nms.Address).First(&newNms).Error; err != nil {
			if !gorm.IsRecordNotFoundError(err) {
				s.logger.Error("Cannot get nms-server", zap.String("nms", nms.Address), zap.String("exception", err.Error()))
				return nil, err
			}
			s.logger.Debug("NMS already exists - will update", zap.String("nms", nms.Address))
		}
		newNms.Address = nms.Address
		newNms.Port = nms.Port
		newNms.Username = nms.Username
		newNms.Password = nms.Password
		newNms.Workers = nms.Workers
		newNms.IsRelay = nms.IsRelay
		s.logger.Debug("Creating hash-map for nms-licences", zap.String("nms", nms.Address))
		hashMap := make(map[string]api.Licence)
		for _, lic := range newNms.Licences {
			hashMap[lic.Type] = lic
		}
		s.logger.Debug("Iterating through licences to see which to update or create", zap.String("nms", nms.Address))
		for _, lic := range nms.Licences {
			var newLicence api.Licence
			if existing, found := hashMap[lic.Licence.Type]; found {
				newLicence = existing
				s.logger.Debug("Updating licence", zap.String("nms", nms.Address), zap.String("licence", newLicence.Type))
			} else {
				newLicence.Type = lic.Licence.Type
				s.logger.Debug("Adding licence", zap.String("nms", nms.Address), zap.String("licence", newLicence.Type))
			}
			newLicence.Amount = lic.Licence.Amount
			newNms.Licences = append(newNms.Licences, newLicence)
		}
		s.logger.Debug("Saving NMS-to db", zap.String("nms", nms.Address))
		if err := tx.Save(&newNms).Error; err != nil {
			tx.Rollback()
			s.logger.Error("Cannot save NMS-to db - rolling back transaction", zap.String("nms", nms.Address), zap.String("exception", err.Error()))
			return nil, fmt.Errorf("failed to apply server %s : %v", newNms.Address, err)
		}
		resp.Nms = append(resp.Nms, newNms)
	}
	s.logger.Debug("Commiting transaction")
	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		s.logger.Error("Commit failed", zap.String("exception", err.Error()))
		return nil, err
	}
	s.logger.Debug("Commit successful")
	return &resp, nil
}
func ( s NmsService ) Apply ( ctx context . Context , r api . NmsApplyRequests ) ( * api . NmsApplyResponse , error ) { s . logger . Debug ( " " ) tx := s . db . BeginTx ( ctx , nil ) client := & http . Client { Transport : & http . Transport { TLSClientConfig : & tls . Config { InsecureSkipVerify : true } , } , Timeout : 10 * time . Second , } var resp api . NmsApplyResponse for _ , nms := range r . Nms { if _ , err := client . Get ( fmt . Sprintf ( " " , nms . Address , nms . Port ) ) ; err != nil { return nil , fmt . Errorf ( " " , nms . Address , nms . Port , err ) } s . logger . Debug ( " " , zap . String ( " " , nms . Address ) ) var newNms api . Nms if err := tx . Preload ( " " ) . Where ( " " , nms . Address ) . First ( & newNms ) . Error ; err != nil { if ! gorm . IsRecordNotFoundError ( err ) { s . logger . Error ( " " , zap . String ( " " , nms . Address ) , zap . String ( " " , err . Error ( ) ) ) return nil , err } s . logger . Debug ( " " , zap . String ( " " , nms . Address ) ) } newNms . Address = nms . Address newNms . Port = nms . Port newNms . Username = nms . Username newNms . Password = nms . Password newNms . Workers = nms . Workers newNms . IsRelay = nms . IsRelay s . logger . Debug ( " " , zap . String ( " " , nms . Address ) ) hashMap := make ( map [ string ] api . Licence ) for _ , lic := range newNms . Licences { hashMap [ lic . Type ] = lic } s . logger . Debug ( " " , zap . String ( " " , nms . Address ) ) for _ , lic := range nms . Licences { var newLicence api . Licence if existing , found := hashMap [ lic . Licence . Type ] ; found { newLicence = existing s . logger . Debug ( " " , zap . String ( " " , nms . Address ) , zap . String ( " " , newLicence . Type ) ) } else { newLicence . Type = lic . Licence . Type s . logger . Debug ( " " , zap . String ( " " , nms . Address ) , zap . String ( " " , newLicence . Type ) ) } newLicence . Amount = lic . Licence . Amount newNms . Licences = append ( newNms . Licences , newLicence ) } s . logger . Debug ( " " , zap . String ( " " , nms . Address ) ) if err := tx . Save ( & newNms ) . Error ; err != nil { tx . Rollback ( ) s . logger . Error ( " " , zap . String ( " " , nms . Address ) , zap . String ( " " , err . Error ( ) ) ) return nil , fmt . Errorf ( " " , newNms . Address , err ) } resp . Nms = append ( resp . Nms , newNms ) } s . logger . Debug ( " " ) if err := tx . Commit ( ) . Error ; err != nil { tx . Rollback ( ) s . logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return nil , err } s . logger . Debug ( " " ) return & resp , nil }
Apply applies the nms-servers to the database
Apply applies the nms - servers to the database
// Start transaction to fall back // if we get an error // Create http-client for testing the nms with insecure tls // Iterate over the requested nm-servers // and append them to the response // Test the http-connection to the NMS // Check if the requested NMS exists (in that case update it) // return the error if it isn't a "record not found"-error // Set data to the new Nms-model // Create hash-map for the existing licences // append the requested licences to the new nms-model // Check if the licencetype exists in the hashmap and update the amount // else create a new licence // Save the new NMS to the DB // Append the new nms to the response