| 1 |
package resources |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/gogo/protobuf/proto" |
| 5 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 6 |
) |
| 7 |
|
| 8 |
func Validate(resources ...mesos.Resource) error { |
| 9 |
type withSpec interface { |
| 10 |
WithSpec(mesos.Resource) |
| 11 |
} |
| 12 |
for i := range resources { |
| 13 |
err := resources[i].Validate() |
| 14 |
if err != nil { |
| 15 |
|
| 16 |
if resourceError, ok := err.(withSpec); ok { |
| 17 |
r := proto.Clone(&resources[i]).(*mesos.Resource) |
| 18 |
resourceError.WithSpec(*r) |
| 19 |
} |
| 20 |
return err |
| 21 |
} |
| 22 |
} |
| 23 |
return nil |
| 24 |
} |
| 25 |
|
| 26 |
func Equivalent(subject, that []mesos.Resource) bool { |
| 27 |
return ContainsAll(subject, that) && ContainsAll(that, subject) |
| 28 |
} |
| 29 |
|
| 30 |
func Contains(subject []mesos.Resource, that mesos.Resource) bool { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
return that.Validate() == nil && contains(subject, that) |
| 35 |
} |
| 36 |
|
| 37 |
func contains(subject []mesos.Resource, that mesos.Resource) bool { |
| 38 |
for i := range subject { |
| 39 |
if subject[i].Contains(that) { |
| 40 |
return true |
| 41 |
} |
| 42 |
} |
| 43 |
return false |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func ContainsAll(subject, that []mesos.Resource) bool { |
| 48 |
remaining := mesos.Resources(subject).Clone() |
| 49 |
for i := range that { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if !contains(remaining, that[i]) { |
| 54 |
return false |
| 55 |
} |
| 56 |
remaining.Subtract1(that[i]) |
| 57 |
} |
| 58 |
return true |
| 59 |
} |