Skip to content

Commit

Permalink
Send relation changes from the model cache worker to the model cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
howbazaar committed Jan 17, 2020
1 parent 2f7af88 commit f31e3ad
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core/cache/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,44 @@ type RemoveUnit struct {
Name string
}

// RelationChange represents either a new relation, or a change
// to an existing relation in a model.
type RelationChange struct {
ModelUUID string
Key string
Endpoints []Endpoint
}

// Endpoint holds all relevant information about a relation endpoint.
type Endpoint struct {
Application string
Name string
Role string
Interface string
Optional bool
Limit int
Scope string
}

// copy returns a deep copy of the RelationChange.
func (c RelationChange) copy() RelationChange {
if existing := c.Endpoints; existing != nil {
endpoints := make([]Endpoint, len(existing))
for i, ep := range existing {
endpoints[i] = ep
}
c.Endpoints = existing
}
return c
}

// RemoveRelation represents the situation when a relation
// is removed from a model in the database.
type RemoveRelation struct {
ModelUUID string
Key string
}

// MachineChange represents either a new machine, or a change
// to an existing machine in a model.
type MachineChange struct {
Expand Down
39 changes: 39 additions & 0 deletions worker/modelcache/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ func (c *cacheWorker) translate(d multiwatcher.Delta) interface{} {
return c.translateMachine(d)
case multiwatcher.UnitKind:
return c.translateUnit(d)
case multiwatcher.RelationKind:
return c.translateRelation(d)
case multiwatcher.CharmKind:
return c.translateCharm(d)
case multiwatcher.BranchKind:
Expand Down Expand Up @@ -459,6 +461,43 @@ func (c *cacheWorker) translateUnit(d multiwatcher.Delta) interface{} {
}
}

func (c *cacheWorker) translateRelation(d multiwatcher.Delta) interface{} {
e := d.Entity
id := e.EntityID()

if d.Removed {
return cache.RemoveRelation{
ModelUUID: id.ModelUUID,
Name: id.ID,
}
}

value, ok := e.(*multiwatcher.RelationInfo)
if !ok {
c.config.Logger.Errorf("unexpected type %T", e)
return nil
}

endpoints := make([]cache.Endpoint, len(value.Endpoints))
for i, ep := range value.Endpoints{
endpoints[i] := cache.Endpoint{
Application: value.ApplicationName,
Name: value.Relation.Name,
Role: value.Relation.Role,
Interface: value.Relation.Interface,
Optional: value.Relation.Optional,
Limit: value.Relation.Limit,
Scope: value.Relation.Scope,
}
}

return cache.RelationChange{
ModelUUID: value.ModelUUID,
Key: value.Key,
Endpoints: endpoints,
}
}

func (c *cacheWorker) translateCharm(d multiwatcher.Delta) interface{} {
e := d.Entity
id := e.EntityID()
Expand Down

0 comments on commit f31e3ad

Please sign in to comment.