Add suspended attribute to relation #25

Merged
merged 1 commit into from Sep 14, 2017
Jump to file or symbol
Failed to load files and symbols.
+59 −15
Split
View
@@ -413,7 +413,7 @@ func (m *model) AddRelation(args RelationArgs) Relation {
func (m *model) setRelations(relationList []*relation) {
m.Relations_ = relations{
- Version: 2,
+ Version: 3,
Relations_: relationList,
}
}
View
@@ -16,6 +16,7 @@ type Relation interface {
Id() int
Key() string
+ Suspended() bool
Endpoints() []Endpoint
AddEndpoint(EndpointArgs) Endpoint
@@ -30,19 +31,22 @@ type relation struct {
Id_ int `yaml:"id"`
Key_ string `yaml:"key"`
Endpoints_ *endpoints `yaml:"endpoints"`
+ Suspended_ bool `yaml:"suspended"`
Status_ *status `yaml:"status,omitempty"`
}
// RelationArgs is an argument struct used to specify a relation.
type RelationArgs struct {
- Id int
- Key string
+ Id int
+ Key string
+ Suspended bool
}
func newRelation(args RelationArgs) *relation {
relation := &relation{
- Id_: args.Id,
- Key_: args.Key,
+ Id_: args.Id,
+ Key_: args.Key,
+ Suspended_: args.Suspended,
}
relation.setEndpoints(nil)
return relation
@@ -58,6 +62,11 @@ func (r *relation) Key() string {
return r.Key_
}
+// Suspended implements Relation.
+func (r *relation) Suspended() bool {
+ return r.Suspended_
+}
+
// Status implements Relation.
func (r *relation) Status() Status {
// To avoid typed nils check nil here.
@@ -133,6 +142,7 @@ type relationDeserializationFunc func(map[string]interface{}) (*relation, error)
var relationDeserializationFuncs = map[int]relationDeserializationFunc{
1: newRelationImporter(1, schema.FieldMap(relationV1Fields())),
2: newRelationImporter(2, schema.FieldMap(relationV2Fields())),
+ 3: newRelationImporter(3, schema.FieldMap(relationV3Fields())),
}
func newRelationImporter(v int, checker schema.Checker) func(map[string]interface{}) (*relation, error) {
@@ -171,12 +181,24 @@ func relationV2Fields() (schema.Fields, schema.Defaults) {
return fields, defaults
}
+func relationV3Fields() (schema.Fields, schema.Defaults) {
+ fields, defaults := relationV2Fields()
+ fields["suspended"] = schema.Bool()
+ defaults["suspended"] = false
+ return fields, defaults
+}
+
func newRelationFromValid(valid map[string]interface{}, importVersion int) (*relation, error) {
- // We're always making a version 2 relation, no matter what we got on
+ suspended := false
+ if importVersion >= 3 {
+ suspended = valid["suspended"].(bool)
+ }
+ // We're always making a version 3 relation, no matter what we got on
// the way in.
result := &relation{
- Id_: int(valid["id"].(int64)),
- Key_: valid["key"].(string),
+ Id_: int(valid["id"].(int64)),
+ Key_: valid["key"].(string),
+ Suspended_: suspended,
}
// Version 1 relations don't have status info in the export yaml.
// Some relations also don't have status.
View
@@ -29,8 +29,9 @@ func (s *RelationSerializationSuite) SetUpTest(c *gc.C) {
func (s *RelationSerializationSuite) completeRelation() *relation {
relation := newRelation(RelationArgs{
- Id: 42,
- Key: "special",
+ Id: 42,
+ Key: "special",
+ Suspended: true,
})
relation.SetStatus(minimalStatusArgs())
@@ -51,12 +52,14 @@ func (s *RelationSerializationSuite) completeRelation() *relation {
func (s *RelationSerializationSuite) TestNewRelation(c *gc.C) {
relation := newRelation(RelationArgs{
- Id: 42,
- Key: "special",
+ Id: 42,
+ Key: "special",
+ Suspended: true,
})
c.Assert(relation.Id(), gc.Equals, 42)
c.Assert(relation.Key(), gc.Equals, "special")
+ c.Assert(relation.Suspended(), jc.IsTrue)
c.Assert(relation.Endpoints(), gc.HasLen, 0)
}
@@ -75,7 +78,7 @@ func (s *RelationSerializationSuite) TestRelationEndpoints(c *gc.C) {
func (s *RelationSerializationSuite) TestParsingSerializedData(c *gc.C) {
initial := relations{
- Version: 2,
+ Version: 3,
Relations_: []*relation{s.completeRelation()},
}
@@ -94,7 +97,7 @@ func (s *RelationSerializationSuite) TestParsingSerializedData(c *gc.C) {
func (s *RelationSerializationSuite) TestParsingSerializedDataNoStatus(c *gc.C) {
initial := relations{
- Version: 2,
+ Version: 3,
Relations_: []*relation{s.completeRelation()},
}
initial.Relations_[0].Status_ = nil
@@ -114,7 +117,7 @@ func (s *RelationSerializationSuite) TestParsingSerializedDataNoStatus(c *gc.C)
func (s *RelationSerializationSuite) TestVersion1Works(c *gc.C) {
initial := relations{
- Version: 2,
+ Version: 3,
Relations_: []*relation{s.completeRelation()},
}
bytes, err := yaml.Marshal(initial)
@@ -131,6 +134,25 @@ func (s *RelationSerializationSuite) TestVersion1Works(c *gc.C) {
c.Assert(relations[0].Status(), gc.IsNil)
}
+func (s *RelationSerializationSuite) TestVersion2Works(c *gc.C) {
+ initial := relations{
+ Version: 3,
+ Relations_: []*relation{s.completeRelation()},
+ }
+ bytes, err := yaml.Marshal(initial)
+ c.Assert(err, jc.ErrorIsNil)
+ var data map[string]interface{}
+ err = yaml.Unmarshal(bytes, &data)
+ c.Assert(err, jc.ErrorIsNil)
+ data["version"] = 2
+
+ relations, err := importRelations(data)
+ c.Assert(err, jc.ErrorIsNil)
+ c.Assert(relations, gc.HasLen, 1)
+ // V2 suspended is always false.
+ c.Assert(relations[0].Suspended(), jc.IsFalse)
+}
+
type EndpointSerializationSuite struct {
SliceSerializationSuite
}