sla, meter-status: Added sla and meter status to model description #5

Merged
merged 2 commits into from Mar 22, 2017
Jump to file or symbol
Failed to load files and symbols.
+184 −1
Split
View
@@ -0,0 +1,27 @@
+// Copyright 2017 Canonical Ltd.
+// Licensed under the LGPLv3, see LICENCE file for details.
+
+package description
+
+// MeterStatus represents the meter status of the model.
+type MeterStatus interface {
+ // Code returns the traffic light colour code of meter status.
+ Code() string
+ // Info returns extra information corresponding to the traffic light colour.
+ Info() string
+}
+
+type meterStatus struct {
+ Code_ string `yaml:"code"`
+ Info_ string `yaml:"info"`
+}
+
+// Code returns the traffic light colour code of meter status.
+func (m meterStatus) Code() string {
+ return m.Code_
+}
+
+// Info returns extra information corresponding to the traffic light colour.
+func (m meterStatus) Info() string {
+ return m.Info_
+}
View
@@ -89,6 +89,12 @@ type Model interface {
AddRemoteApplication(RemoteApplicationArgs) RemoteApplication
Validate() error
+
+ SetSLA(level, credentials string) SLA
+ SLA() SLA
+
+ SetMeterStatus(code, info string) MeterStatus
+ MeterStatus() MeterStatus
}
// ModelArgs represent the bare minimum information that is needed
@@ -223,6 +229,9 @@ type model struct {
StoragePools_ storagepools `yaml:"storage-pools"`
RemoteApplications_ remoteApplications `yaml:"remote-applications"`
+
+ SLA_ sla `yaml:"sla"`
+ MeterStatus_ meterStatus `yaml:"meter-status"`
}
func (m *model) Tag() names.ModelTag {
@@ -578,6 +587,34 @@ func (m *model) SetCloudCredential(args CloudCredentialArgs) {
m.CloudCredential_ = newCloudCredential(args)
}
+// SetSLA implements Model.
+func (m *model) SetSLA(level, creds string) SLA {
+ m.SLA_ = sla{
+ Level_: level,
+ Credentials_: creds,
+ }
+ return m.SLA_
+}
+
+// SetMeterStatus implements Model.
+func (m *model) SetMeterStatus(code, info string) MeterStatus {
+ m.MeterStatus_ = meterStatus{
+ Code_: code,
+ Info_: info,
+ }
+ return m.MeterStatus_
+}
+
+// SLA implements Model.
+func (m *model) SLA() SLA {
+ return m.SLA_
+}
+
+// MeterStatus implements Model.
+func (m *model) MeterStatus() MeterStatus {
+ return m.MeterStatus_
+}
+
// Volumes implements Model.
func (m *model) Volumes() []Volume {
var result []Volume
@@ -587,7 +624,7 @@ func (m *model) Volumes() []Volume {
return result
}
-// AddVolume implemets Model.
+// AddVolume implements Model.
func (m *model) AddVolume(args VolumeArgs) Volume {
volume := newVolume(args)
m.Volumes_.Volumes_ = append(m.Volumes_.Volumes_, volume)
@@ -702,6 +739,14 @@ func (m *model) setRemoteApplications(appList []*remoteApplication) {
}
}
+func (m *model) setSLA(sla sla) {
+ m.SLA_ = sla
+}
+
+func (m *model) setMeterStatus(ms meterStatus) {
+ m.MeterStatus_ = ms
+}
+
// Validate implements Model.
func (m *model) Validate() error {
// A model needs an owner.
@@ -1205,9 +1250,31 @@ func newModelFromValid(valid map[string]interface{}, importVersion int) (*model,
}
result.setRemoteApplications(remoteApplications)
+ if importVersion >= 2 {
+ sla := importSLA(valid["sla"].(map[string]interface{}))
+ result.setSLA(sla)
+
+ ms := importMeterStatus(valid["meter-status"].(map[string]interface{}))
+ result.setMeterStatus(ms)
+ }
+
return result, nil
}
+func importSLA(source map[string]interface{}) sla {
+ return sla{
+ Level_: source["level"].(string),
+ Credentials_: source["credentials"].(string),
+ }
+}
+
+func importMeterStatus(source map[string]interface{}) meterStatus {
+ return meterStatus{
+ Code_: source["code"].(string),
+ Info_: source["info"].(string),
+ }
+}
+
func importModelV1(source map[string]interface{}) (*model, error) {
fields, defaults := modelV1Fields()
checker := schema.FieldMap(fields, defaults)
@@ -1225,6 +1292,16 @@ func importModelV1(source map[string]interface{}) (*model, error) {
func importModelV2(source map[string]interface{}) (*model, error) {
fields, defaults := modelV1Fields()
fields["remote-applications"] = schema.StringMap(schema.Any())
+ fields["sla"] = schema.FieldMap(
+ schema.Fields{
+ "level": schema.String(),
+ "credentials": schema.String(),
+ }, nil)
+ fields["meter-status"] = schema.FieldMap(
+ schema.Fields{
+ "code": schema.String(),
+ "info": schema.String(),
+ }, nil)
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
View
@@ -724,6 +724,58 @@ func (*ModelSerializationSuite) TestRemoteApplicationsGetter(c *gc.C) {
c.Assert(result, gc.HasLen, 1)
}
+func (*ModelSerializationSuite) TestSetAndGetSLA(c *gc.C) {
+ model := NewModel(ModelArgs{Owner: names.NewUserTag("owner")})
+ sla := model.SetSLA("essential", "creds")
+ c.Assert(sla.Level(), gc.Equals, "essential")
+ c.Assert(sla.Credentials(), gc.Equals, "creds")
+
+ getSla := model.SLA()
+ c.Assert(getSla.Level(), gc.Equals, sla.Level())
+ c.Assert(getSla.Credentials(), jc.DeepEquals, sla.Credentials())
+}
+
+func (s *ModelSerializationSuite) TestSLA(c *gc.C) {
+ initial := NewModel(ModelArgs{Owner: names.NewUserTag("owner")})
+ sla := initial.SetSLA("essential", "creds")
+ c.Assert(sla.Level(), gc.Equals, "essential")
+ c.Assert(sla.Credentials(), gc.Equals, "creds")
+
+ bytes, err := yaml.Marshal(initial)
+ c.Assert(err, jc.ErrorIsNil)
+
+ model, err := Deserialize(bytes)
+ c.Assert(err, jc.ErrorIsNil)
+ c.Assert(model.SLA().Level(), gc.Equals, "essential")
+ c.Assert(model.SLA().Credentials(), gc.Equals, "creds")
+}
+
+func (*ModelSerializationSuite) TestGetAndSetMeterStatus(c *gc.C) {
+ model := NewModel(ModelArgs{Owner: names.NewUserTag("veils")})
+ ms := model.SetMeterStatus("RED", "info message")
+ c.Assert(ms.Code(), gc.Equals, "RED")
+ c.Assert(ms.Info(), gc.Equals, "info message")
+
+ getms := model.MeterStatus()
+ c.Assert(getms.Code(), gc.Equals, ms.Code())
+ c.Assert(getms.Info(), gc.Equals, ms.Info())
+}
+
+func (s *ModelSerializationSuite) TestMeterStatus(c *gc.C) {
+ initial := NewModel(ModelArgs{Owner: names.NewUserTag("owner")})
+ ms := initial.SetMeterStatus("RED", "info message")
+ c.Assert(ms.Code(), gc.Equals, "RED")
+ c.Assert(ms.Info(), gc.Equals, "info message")
+
+ bytes, err := yaml.Marshal(initial)
+ c.Assert(err, jc.ErrorIsNil)
+
+ model, err := Deserialize(bytes)
+ c.Assert(err, jc.ErrorIsNil)
+ c.Assert(model.MeterStatus().Code(), gc.Equals, "RED")
+ c.Assert(model.MeterStatus().Info(), gc.Equals, "info message")
+}
+
func (*ModelSerializationSuite) TestSerializesToVersion2(c *gc.C) {
initial := NewModel(ModelArgs{Owner: names.NewUserTag("ben-harper")})
data := asStringMap(c, initial)
View
27 sla.go
@@ -0,0 +1,27 @@
+// Copyright 2017 Canonical Ltd.
+// Licensed under the LGPLv3, see LICENCE file for details.
+
+package description
+
+// SLA represents the sla for the model.
+type SLA interface {
+ // Level returns the level of the sla.
+ Level() string
+ // Credentials returns the credentials of the sla.
+ Credentials() string
+}
+
+type sla struct {
+ Level_ string `yaml:"level"`
+ Credentials_ string `yaml:"credentials"`
+}
+
+// Level returns the level of the sla.
+func (s sla) Level() string {
+ return s.Level_
+}
+
+// Credentials returns the Credentials of the sla.
+func (s sla) Credentials() string {
+ return s.Credentials_
+}