boskos/ranch/storage.go
func (s *Storage) SyncResources(data []common.Resource) error {
	s.resourcesLock.Lock()
	defer s.resourcesLock.Unlock()

	resources, err := s.GetResources()
	if err != nil {
		logrus.WithError(err).Error("cannot find resources")
		return err
	}

	var finalError error

	// delete non-exist resource
	valid := 0
	for _, res := range resources {
		// If currently busy, yield deletion to later cycles.
		if res.Owner != "" {
			resources[valid] = res
			valid++
			continue
		}
		toDelete := true
		for _, newRes := range data {
			if res.Name == newRes.Name {
				resources[valid] = res
				valid++
				toDelete = false
				break
			}
		}
		if toDelete {
			logrus.Infof("Deleting resource %s", res.Name)
			if err := s.DeleteResource(res.Name); err != nil {
				finalError = multierror.Append(finalError, err)
				logrus.WithError(err).Errorf("unable to delete resource %s", res.Name)
			}
		}
	}
	resources = resources[:valid]

	// add new resource
	for _, p := range data {
		found := false
		for idx := range resources {
			exist := resources[idx]
			if p.Name == exist.Name {
				found = true
				logrus.Infof("Keeping resource %s", p.Name)
				break
			}
		}

		if !found {
			if p.State == "" {
				p.State = common.Free
			}
			logrus.Infof("Adding resource %s", p.Name)
			resources = append(resources, p)
			if err := s.AddResource(p); err != nil {
				logrus.WithError(err).Errorf("unable to add resource %s", p.Name)
				finalError = multierror.Append(finalError, err)
			}
		}
	}
	return finalError
}
func (s *Storage) SyncResources(data []common.Resource) error {
	s.resourcesLock.Lock()
	defer s.resourcesLock.Unlock()

	resources, err := s.GetResources()
	if err != nil {
		logrus.WithError(err).Error("cannot find resources")
		return err
	}

	var finalError error

	// delete non-exist resource
	valid := 0
	for _, res := range resources {
		// If currently busy, yield deletion to later cycles.
		if res.Owner != "" {
			resources[valid] = res
			valid++
			continue
		}
		toDelete := true
		for _, newRes := range data {
			if res.Name == newRes.Name {
				resources[valid] = res
				valid++
				toDelete = false
				break
			}
		}
		if toDelete {
			logrus.Infof("Deleting resource %s", res.Name)
			if err := s.DeleteResource(res.Name); err != nil {
				finalError = multierror.Append(finalError, err)
				logrus.WithError(err).Errorf("unable to delete resource %s", res.Name)
			}
		}
	}
	resources = resources[:valid]

	// add new resource
	for _, p := range data {
		found := false
		for idx := range resources {
			exist := resources[idx]
			if p.Name == exist.Name {
				found = true
				logrus.Infof("Keeping resource %s", p.Name)
				break
			}
		}

		if !found {
			if p.State == "" {
				p.State = common.Free
			}
			logrus.Infof("Adding resource %s", p.Name)
			resources = append(resources, p)
			if err := s.AddResource(p); err != nil {
				logrus.WithError(err).Errorf("unable to add resource %s", p.Name)
				finalError = multierror.Append(finalError, err)
			}
		}
	}
	return finalError
}
func ( s * Storage ) SyncResources ( data [ ] common . Resource ) error { s . resourcesLock . Lock ( ) defer s . resourcesLock . Unlock ( ) resources , err := s . GetResources ( ) if err != nil { logrus . WithError ( err ) . Error ( " " ) return err } var finalError error // delete non-exist resource valid := 0 for _ , res := range resources { // If currently busy, yield deletion to later cycles. if res . Owner != " " { resources [ valid ] = res valid ++ continue } toDelete := true for _ , newRes := range data { if res . Name == newRes . Name { resources [ valid ] = res valid ++ toDelete = false break } } if toDelete { logrus . Infof ( " " , res . Name ) if err := s . DeleteResource ( res . Name ) ; err != nil { finalError = multierror . Append ( finalError , err ) logrus . WithError ( err ) . Errorf ( " " , res . Name ) } } } resources = resources [ : valid ] // add new resource for _ , p := range data { found := false for idx := range resources { exist := resources [ idx ] if p . Name == exist . Name { found = true logrus . Infof ( " " , p . Name ) break } } if ! found { if p . State == " " { p . State = common . Free } logrus . Infof ( " " , p . Name ) resources = append ( resources , p ) if err := s . AddResource ( p ) ; err != nil { logrus . WithError ( err ) . Errorf ( " " , p . Name ) finalError = multierror . Append ( finalError , err ) } } } return finalError }
// SyncResources will update resources every 10 mins.
// It will append newly added resources to ranch.Resources,
// And try to remove newly deleted resources from ranch.Resources.
// If the newly deleted resource is currently held by a user, the deletion will
// yield to next update cycle.
SyncResources will update resources every 10 mins . It will append newly added resources to ranch . Resources And try to remove newly deleted resources from ranch . Resources . If the newly deleted resource is currently held by a user the deletion will yield to next update cycle .