From fc1c631c9833cfdbf4f085021d56499f117e8768 Mon Sep 17 00:00:00 2001 From: Donald Bakong Date: Thu, 19 Sep 2019 00:48:07 -0400 Subject: [PATCH] engine: resources: Change Res API from Compare to Cmp This will be done by refactoring the current method, to return an error message instead of a boolean value. This will also update a typo on the user res. --- engine/resources/aws_ec2.go | 28 ++++------- engine/resources/group.go | 18 ++----- engine/resources/hostname.go | 18 ++----- engine/resources/msg.go | 20 +++----- engine/resources/net.go | 20 +++----- engine/resources/nspawn.go | 20 +++----- engine/resources/password.go | 18 ++----- engine/resources/print.go | 14 ++---- engine/resources/svc.go | 18 ++----- engine/resources/test.go | 92 ++++++++++++++++-------------------- engine/resources/timer.go | 14 ++---- engine/resources/user.go | 36 ++++++-------- 12 files changed, 110 insertions(+), 206 deletions(-) diff --git a/engine/resources/aws_ec2.go b/engine/resources/aws_ec2.go index 24224a89..5c9a9d08 100644 --- a/engine/resources/aws_ec2.go +++ b/engine/resources/aws_ec2.go @@ -751,45 +751,37 @@ func (obj *AwsEc2Res) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *AwsEc2Res) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *AwsEc2Res) Compare(r engine.Res) bool { // we can only compare AwsEc2Res to others of the same resource kind res, ok := r.(*AwsEc2Res) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.State != res.State { - return false + return fmt.Errorf("the State differs") } if obj.Region != res.Region { - return false + return fmt.Errorf("the Region differs") } if obj.Type != res.Type { - return false + return fmt.Errorf("the Type differs") } if obj.ImageID != res.ImageID { - return false + return fmt.Errorf("the ImageID differs") } if obj.WatchEndpoint != res.WatchEndpoint { - return false + return fmt.Errorf("the WatchEndpoint differs") } if obj.WatchListenAddr != res.WatchListenAddr { - return false + return fmt.Errorf("the WatchListenAddr differs") } if obj.ErrorOnMalformedPost != res.ErrorOnMalformedPost { - return false + return fmt.Errorf("the ErrorOnMalformedPost differs") } if obj.UserData != res.UserData { - return false + return fmt.Errorf("the UserData differs") } - return true + return nil } func (obj *AwsEc2Res) prependName() string { diff --git a/engine/resources/group.go b/engine/resources/group.go index fecce752..fc0f77f5 100644 --- a/engine/resources/group.go +++ b/engine/resources/group.go @@ -220,32 +220,24 @@ func (obj *GroupRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *GroupRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *GroupRes) Compare(r engine.Res) bool { // we can only compare GroupRes to others of the same resource kind res, ok := r.(*GroupRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.State != res.State { - return false + return fmt.Errorf("the State differs") } if (obj.GID == nil) != (res.GID == nil) { - return false + return fmt.Errorf("the GID differs") } if obj.GID != nil && res.GID != nil { if *obj.GID != *res.GID { - return false + return fmt.Errorf("the GID differs") } } - return true + return nil } // GroupUID is the UID struct for GroupRes. diff --git a/engine/resources/hostname.go b/engine/resources/hostname.go index b0c9157a..0a338c5c 100644 --- a/engine/resources/hostname.go +++ b/engine/resources/hostname.go @@ -219,31 +219,23 @@ func (obj *HostnameRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *HostnameRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *HostnameRes) Compare(r engine.Res) bool { // we can only compare HostnameRes to others of the same resource kind res, ok := r.(*HostnameRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.PrettyHostname != res.PrettyHostname { - return false + return fmt.Errorf("the PrettyHostname differs") } if obj.StaticHostname != res.StaticHostname { - return false + return fmt.Errorf("the StaticHostname differs") } if obj.TransientHostname != res.TransientHostname { - return false + return fmt.Errorf("the TransientHostname differs") } - return true + return nil } // HostnameUID is the UID struct for HostnameRes. diff --git a/engine/resources/msg.go b/engine/resources/msg.go index 0ee52e4e..4c36d67e 100644 --- a/engine/resources/msg.go +++ b/engine/resources/msg.go @@ -200,36 +200,28 @@ func (obj *MsgRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *MsgRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *MsgRes) Compare(r engine.Res) bool { // we can only compare MsgRes to others of the same resource kind res, ok := r.(*MsgRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.Body != res.Body { - return false + return fmt.Errorf("the Body differs") } if obj.Priority != res.Priority { - return false + return fmt.Errorf("the Priority differs") } if len(obj.Fields) != len(res.Fields) { - return false + return fmt.Errorf("the length of Fields differs") } for field, value := range obj.Fields { if res.Fields[field] != value { - return false + return fmt.Errorf("the Fields differ") } } - return true + return nil } // MsgUID is a unique representation for a MsgRes object. diff --git a/engine/resources/net.go b/engine/resources/net.go index 8227acf5..4984b067 100644 --- a/engine/resources/net.go +++ b/engine/resources/net.go @@ -506,34 +506,26 @@ func (obj *NetRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *NetRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *NetRes) Compare(r engine.Res) bool { // we can only compare NetRes to others of the same resource kind res, ok := r.(*NetRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.State != res.State { - return false + return fmt.Errorf("the State differs") } if (obj.Addrs == nil) != (res.Addrs == nil) { - return false + return fmt.Errorf("the Addrs differ") } if err := util.SortedStrSliceCompare(obj.Addrs, res.Addrs); err != nil { - return false + return fmt.Errorf("the Addrs differ") } if obj.Gateway != res.Gateway { - return false + return fmt.Errorf("the Gateway differs") } - return true + return nil } // NetUID is a unique resource identifier. diff --git a/engine/resources/nspawn.go b/engine/resources/nspawn.go index 3af3b21a..f609e3f6 100644 --- a/engine/resources/nspawn.go +++ b/engine/resources/nspawn.go @@ -261,35 +261,27 @@ func (obj *NspawnRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *NspawnRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *NspawnRes) Compare(r engine.Res) bool { // we can only compare NspawnRes to others of the same resource kind res, ok := r.(*NspawnRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.State != res.State { - return false + return fmt.Errorf("the State differs") } // TODO: why is res.svc ever nil? if (obj.svc == nil) != (res.svc == nil) { // xor - return false + return fmt.Errorf("the svc differs") } if obj.svc != nil && res.svc != nil { - if !obj.svc.Compare(res.svc) { - return false + if err := obj.svc.Cmp(res.svc); err != nil { + return errwrap.Wrapf(err, "the svc differs") } } - return true + return nil } // NspawnUID is a unique resource identifier. diff --git a/engine/resources/password.go b/engine/resources/password.go index 4a941f25..7172c4eb 100644 --- a/engine/resources/password.go +++ b/engine/resources/password.go @@ -295,33 +295,25 @@ func (obj *PasswordRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *PasswordRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *PasswordRes) Compare(r engine.Res) bool { // we can only compare PasswordRes to others of the same resource kind res, ok := r.(*PasswordRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.Length != res.Length { - return false + return fmt.Errorf("the Length differs") } // TODO: we *could* optimize by allowing CheckApply to move from // saved->!saved, by removing the file, but not likely worth it! if obj.Saved != res.Saved { - return false + return fmt.Errorf("the Saved differs") } if obj.CheckRecovery != res.CheckRecovery { - return false + return fmt.Errorf("the CheckRecovery differs") } - return true + return nil } // PasswordUID is the UID struct for PasswordRes. diff --git a/engine/resources/print.go b/engine/resources/print.go index 6d0f48e4..cc2eb7f6 100644 --- a/engine/resources/print.go +++ b/engine/resources/print.go @@ -115,24 +115,16 @@ func (obj *PrintRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *PrintRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *PrintRes) Compare(r engine.Res) bool { // we can only compare PrintRes to others of the same resource kind res, ok := r.(*PrintRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.Msg != res.Msg { - return false + return fmt.Errorf("the Msg differs") } - return true + return nil } // PrintUID is the UID struct for PrintRes. diff --git a/engine/resources/svc.go b/engine/resources/svc.go index 5af511a3..6fc383e7 100644 --- a/engine/resources/svc.go +++ b/engine/resources/svc.go @@ -354,31 +354,23 @@ func (obj *SvcRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *SvcRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *SvcRes) Compare(r engine.Res) bool { // we can only compare SvcRes to others of the same resource kind res, ok := r.(*SvcRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.State != res.State { - return false + return fmt.Errorf("the State differs") } if obj.Startup != res.Startup { - return false + return fmt.Errorf("the Startup differs") } if obj.Session != res.Session { - return false + return fmt.Errorf("the Session differs") } - return true + return nil } // SvcUID is the UID struct for SvcRes. diff --git a/engine/resources/test.go b/engine/resources/test.go index ab26b16e..b621f5c6 100644 --- a/engine/resources/test.go +++ b/engine/resources/test.go @@ -199,25 +199,17 @@ func (obj *TestRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *TestRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *TestRes) Compare(r engine.Res) bool { // we can only compare TestRes to others of the same resource kind res, ok := r.(*TestRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } //if obj.Name != res.Name { // return false //} if obj.CompareFail || res.CompareFail { - return false + return fmt.Errorf("the CompareFail is true") } // TODO: yes, I know the long manual version is absurd, but I couldn't @@ -228,145 +220,145 @@ func (obj *TestRes) Compare(r engine.Res) bool { //} if obj.Bool != res.Bool { - return false + return fmt.Errorf("the Bool differs") } if obj.Str != res.Str { - return false + return fmt.Errorf("the Str differs") } if obj.Int != res.Int { - return false + return fmt.Errorf("the Str differs") } if obj.Int8 != res.Int8 { - return false + return fmt.Errorf("the Int8 differs") } if obj.Int16 != res.Int16 { - return false + return fmt.Errorf("the Int16 differs") } if obj.Int32 != res.Int32 { - return false + return fmt.Errorf("the Int32 differs") } if obj.Int64 != res.Int64 { - return false + return fmt.Errorf("the Int64 differs") } if obj.Uint != res.Uint { - return false + return fmt.Errorf("the Uint differs") } if obj.Uint8 != res.Uint8 { - return false + return fmt.Errorf("the Uint8 differs") } if obj.Uint16 != res.Uint16 { - return false + return fmt.Errorf("the Uint16 differs") } if obj.Uint32 != res.Uint32 { - return false + return fmt.Errorf("the Uint32 differs") } if obj.Uint64 != res.Uint64 { - return false + return fmt.Errorf("the Uint64 differs") } //if obj.Uintptr if obj.Byte != res.Byte { - return false + return fmt.Errorf("the Byte differs") } if obj.Rune != res.Rune { - return false + return fmt.Errorf("the Rune differs") } if obj.Float32 != res.Float32 { - return false + return fmt.Errorf("the Float32 differs") } if obj.Float64 != res.Float64 { - return false + return fmt.Errorf("the Float64 differs") } if obj.Complex64 != res.Complex64 { - return false + return fmt.Errorf("the Complex64 differs") } if obj.Complex128 != res.Complex128 { - return false + return fmt.Errorf("the Complex128 differs") } if (obj.BoolPtr == nil) != (res.BoolPtr == nil) { // xor - return false + return fmt.Errorf("the BoolPtr differs") } if obj.BoolPtr != nil && res.BoolPtr != nil { if *obj.BoolPtr != *res.BoolPtr { // compare - return false + return fmt.Errorf("the BoolPtr differs") } } if (obj.StringPtr == nil) != (res.StringPtr == nil) { // xor - return false + return fmt.Errorf("the StringPtr differs") } if obj.StringPtr != nil && res.StringPtr != nil { if *obj.StringPtr != *res.StringPtr { // compare - return false + return fmt.Errorf("the StringPtr differs") } } if (obj.Int64Ptr == nil) != (res.Int64Ptr == nil) { // xor - return false + return fmt.Errorf("the Int64Ptr differs") } if obj.Int64Ptr != nil && res.Int64Ptr != nil { if *obj.Int64Ptr != *res.Int64Ptr { // compare - return false + return fmt.Errorf("the Int64Ptr differs") } } if (obj.Int8Ptr == nil) != (res.Int8Ptr == nil) { // xor - return false + return fmt.Errorf("the Int8Ptr differs") } if obj.Int8Ptr != nil && res.Int8Ptr != nil { if *obj.Int8Ptr != *res.Int8Ptr { // compare - return false + return fmt.Errorf("the Int8Ptr differs") } } if (obj.Uint8Ptr == nil) != (res.Uint8Ptr == nil) { // xor - return false + return fmt.Errorf("the Uint8Ptr differs") } if obj.Uint8Ptr != nil && res.Uint8Ptr != nil { if *obj.Uint8Ptr != *res.Uint8Ptr { // compare - return false + return fmt.Errorf("the Uint8Ptr differs") } } if !reflect.DeepEqual(obj.Int8PtrPtrPtr, res.Int8PtrPtrPtr) { - return false + return fmt.Errorf("the Int8PtrPtrPtr differs") } if !reflect.DeepEqual(obj.SliceString, res.SliceString) { - return false + return fmt.Errorf("the SliceString differs") } if !reflect.DeepEqual(obj.MapIntFloat, res.MapIntFloat) { - return false + return fmt.Errorf("the MapIntFloat differs") } if !reflect.DeepEqual(obj.MixedStruct, res.MixedStruct) { - return false + return fmt.Errorf("the MixedStruct differs") } if !reflect.DeepEqual(obj.Interface, res.Interface) { - return false + return fmt.Errorf("the Interface differs") } if obj.AnotherStr != res.AnotherStr { - return false + return fmt.Errorf("the AnotherStr differs") } if obj.ValidateBool != res.ValidateBool { - return false + return fmt.Errorf("the ValidateBool differs") } if obj.ValidateError != res.ValidateError { - return false + return fmt.Errorf("the ValidateError differs") } if obj.AlwaysGroup != res.AlwaysGroup { - return false + return fmt.Errorf("the AlwaysGroup differs") } if obj.SendValue != res.SendValue { - return false + return fmt.Errorf("the SendValue differs") } if obj.Comment != res.Comment { - return false + return fmt.Errorf("the Comment differs") } - return true + return nil } // TestUID is the UID struct for TestRes. diff --git a/engine/resources/timer.go b/engine/resources/timer.go index ce098bbc..fa858380 100644 --- a/engine/resources/timer.go +++ b/engine/resources/timer.go @@ -113,25 +113,17 @@ func (obj *TimerRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *TimerRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *TimerRes) Compare(r engine.Res) bool { // we can only compare TimerRes to others of the same resource kind res, ok := r.(*TimerRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.Interval != res.Interval { - return false + return fmt.Errorf("the Interval differs") } - return true + return nil } // TimerUID is the UID struct for TimerRes. diff --git a/engine/resources/user.go b/engine/resources/user.go index ccaced67..6dd34fee 100644 --- a/engine/resources/user.go +++ b/engine/resources/user.go @@ -273,45 +273,37 @@ func (obj *UserRes) CheckApply(apply bool) (bool, error) { // Cmp compares two resources and returns an error if they are not equivalent. func (obj *UserRes) Cmp(r engine.Res) error { - if !obj.Compare(r) { - return fmt.Errorf("did not compare") - } - return nil -} - -// Compare two resources and return if they are equivalent. -func (obj *UserRes) Compare(r engine.Res) bool { // we can only compare UserRes to others of the same resource kind res, ok := r.(*UserRes) if !ok { - return false + return fmt.Errorf("not a %s", obj.Kind()) } if obj.State != res.State { - return false + return fmt.Errorf("the State differs") } if (obj.UID == nil) != (res.UID == nil) { - return false + return fmt.Errorf("the UID differs") } if obj.UID != nil && res.UID != nil { if *obj.UID != *res.UID { - return false + return fmt.Errorf("the UID differs") } } if (obj.GID == nil) != (res.GID == nil) { - return false + return fmt.Errorf("the GID differs") } if obj.GID != nil && res.GID != nil { if *obj.GID != *res.GID { - return false + return fmt.Errorf("the GID differs") } } if (obj.Groups == nil) != (res.Groups == nil) { - return false + return fmt.Errorf("the Group differs") } if obj.Groups != nil && res.Groups != nil { if len(obj.Groups) != len(res.Groups) { - return false + return fmt.Errorf("the Group differs") } objGroups := obj.Groups resGroups := res.Groups @@ -319,22 +311,22 @@ func (obj *UserRes) Compare(r engine.Res) bool { sort.Strings(resGroups) for i := range objGroups { if objGroups[i] != resGroups[i] { - return false + return fmt.Errorf("the Group differs at index: %d", i) } } } if (obj.HomeDir == nil) != (res.HomeDir == nil) { - return false + return fmt.Errorf("the HomeDirs differs") } if obj.HomeDir != nil && res.HomeDir != nil { - if *obj.HomeDir != *obj.HomeDir { - return false + if *obj.HomeDir != *res.HomeDir { + return fmt.Errorf("the HomeDir differs") } } if obj.AllowDuplicateUID != res.AllowDuplicateUID { - return false + return fmt.Errorf("the AllowDuplicateUID differs") } - return true + return nil } // UserUID is the UID struct for UserRes.