boskos/ranch/ranch.go
func (r *Ranch) AcquireByState(state, dest, owner string, names []string) ([]common.Resource, error) {
	r.resourcesLock.Lock()
	defer r.resourcesLock.Unlock()

	if names == nil {
		return nil, fmt.Errorf("must provide names of expected resources")
	}

	rNames := map[string]bool{}
	for _, t := range names {
		rNames[t] = true
	}

	allResources, err := r.Storage.GetResources()
	if err != nil {
		logrus.WithError(err).Errorf("could not get resources")
		return nil, &ResourceNotFound{state}
	}

	var resources []common.Resource

	for idx := range allResources {
		res := allResources[idx]
		if state == res.State {
			if res.Owner != "" {
				continue
			}
			if rNames[res.Name] {
				res.LastUpdate = r.UpdateTime()
				res.Owner = owner
				res.State = dest
				if err := r.Storage.UpdateResource(res); err != nil {
					logrus.WithError(err).Errorf("could not update resource %s", res.Name)
					return nil, err
				}
				resources = append(resources, res)
				delete(rNames, res.Name)
			}
		}
	}
	if len(rNames) != 0 {
		var missingResources []string
		for n := range rNames {
			missingResources = append(missingResources, n)
		}
		err := &ResourceNotFound{state}
		logrus.WithError(err).Errorf("could not find required resources %s", strings.Join(missingResources, ", "))
		return resources, err
	}
	return resources, nil
}
func (r *Ranch) AcquireByState(state, dest, owner string, names []string) ([]common.Resource, error) {
	r.resourcesLock.Lock()
	defer r.resourcesLock.Unlock()

	if names == nil {
		return nil, fmt.Errorf("must provide names of expected resources")
	}

	rNames := map[string]bool{}
	for _, t := range names {
		rNames[t] = true
	}

	allResources, err := r.Storage.GetResources()
	if err != nil {
		logrus.WithError(err).Errorf("could not get resources")
		return nil, &ResourceNotFound{state}
	}

	var resources []common.Resource

	for idx := range allResources {
		res := allResources[idx]
		if state == res.State {
			if res.Owner != "" {
				continue
			}
			if rNames[res.Name] {
				res.LastUpdate = r.UpdateTime()
				res.Owner = owner
				res.State = dest
				if err := r.Storage.UpdateResource(res); err != nil {
					logrus.WithError(err).Errorf("could not update resource %s", res.Name)
					return nil, err
				}
				resources = append(resources, res)
				delete(rNames, res.Name)
			}
		}
	}
	if len(rNames) != 0 {
		var missingResources []string
		for n := range rNames {
			missingResources = append(missingResources, n)
		}
		err := &ResourceNotFound{state}
		logrus.WithError(err).Errorf("could not find required resources %s", strings.Join(missingResources, ", "))
		return resources, err
	}
	return resources, nil
}
func ( r * Ranch ) AcquireByState ( state , dest , owner string , names [ ] string ) ( [ ] common . Resource , error ) { r . resourcesLock . Lock ( ) defer r . resourcesLock . Unlock ( ) if names == nil { return nil , fmt . Errorf ( " " ) } rNames := map [ string ] bool { } for _ , t := range names { rNames [ t ] = true } allResources , err := r . Storage . GetResources ( ) if err != nil { logrus . WithError ( err ) . Errorf ( " " ) return nil , & ResourceNotFound { state } } var resources [ ] common . Resource for idx := range allResources { res := allResources [ idx ] if state == res . State { if res . Owner != " " { continue } if rNames [ res . Name ] { res . LastUpdate = r . UpdateTime ( ) res . Owner = owner res . State = dest if err := r . Storage . UpdateResource ( res ) ; err != nil { logrus . WithError ( err ) . Errorf ( " " , res . Name ) return nil , err } resources = append ( resources , res ) delete ( rNames , res . Name ) } } } if len ( rNames ) != 0 { var missingResources [ ] string for n := range rNames { missingResources = append ( missingResources , n ) } err := & ResourceNotFound { state } logrus . WithError ( err ) . Errorf ( " " , strings . Join ( missingResources , " " ) ) return resources , err } return resources , nil }
// AcquireByState checks out resources of a given type without an owner,
// that matches a list of resources names.
// In: state - current state of the requested resource
//     dest - destination state of the requested resource
//     owner - requester of the resource
//     names - names of resource to acquire
// Out: A valid list of Resource object on success, or
//      ResourceNotFound error if target type resource does not exist in target state.
AcquireByState checks out resources of a given type without an owner that matches a list of resources names . In : state - current state of the requested resource dest - destination state of the requested resource owner - requester of the resource names - names of resource to acquire Out : A valid list of Resource object on success or ResourceNotFound error if target type resource does not exist in target state .