github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/jujud/agent/model/agent.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package model 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 "github.com/juju/juju/agent" 11 "github.com/juju/juju/api" 12 ) 13 14 // WrapAgent wraps an agent.Agent (expected to be a machine agent, fwiw) 15 // such that its references the supplied model rather than the controller 16 // model; its config is immutable; and it doesn't use OldPassword. 17 // 18 // It's a strong sign that the agent package needs some work... 19 func WrapAgent(a agent.Agent, controllerUUID, modelUUID string) (agent.Agent, error) { 20 if !names.IsValidModel(modelUUID) { 21 return nil, errors.NotValidf("model uuid %q", modelUUID) 22 } 23 if !names.IsValidController(controllerUUID) { 24 return nil, errors.NotValidf("controller uuid %q", controllerUUID) 25 } 26 return &modelAgent{ 27 Agent: a, 28 modelUUID: modelUUID, 29 controllerUUID: controllerUUID, 30 }, nil 31 } 32 33 type modelAgent struct { 34 agent.Agent 35 modelUUID string 36 controllerUUID string 37 } 38 39 // ChangeConfig is part of the agent.Agent interface. This implementation 40 // always returns an error. 41 func (a *modelAgent) ChangeConfig(_ agent.ConfigMutator) error { 42 return errors.New("model agent config is immutable") 43 } 44 45 // CurrentConfig is part of the agent.Agent interface. This implementation 46 // returns an agent.Config that reports tweaked API connection information. 47 func (a *modelAgent) CurrentConfig() agent.Config { 48 return &modelAgentConfig{ 49 Config: a.Agent.CurrentConfig(), 50 modelUUID: a.modelUUID, 51 controllerUUID: a.controllerUUID, 52 } 53 } 54 55 type modelAgentConfig struct { 56 agent.Config 57 modelUUID string 58 controllerUUID string 59 } 60 61 // Model is part of the agent.Config interface. This implementation always 62 // returns the configured model tag. 63 func (c *modelAgentConfig) Model() names.ModelTag { 64 return names.NewModelTag(c.modelUUID) 65 } 66 67 // Controller is part of the agent.Config interface. This implementation always 68 // returns the configured controller tag. 69 func (c *modelAgentConfig) Controller() names.ControllerTag { 70 return names.NewControllerTag(c.controllerUUID) 71 } 72 73 // APIInfo is part of the agent.Config interface. This implementation always 74 // replaces the target model tag with the configured model tag. 75 func (c *modelAgentConfig) APIInfo() (*api.Info, bool) { 76 info, ok := c.Config.APIInfo() 77 if !ok { 78 return nil, false 79 } 80 info.ModelTag = names.NewModelTag(c.modelUUID) 81 return info, true 82 } 83 84 // OldPassword is part of the agent.Config interface. This implementation 85 // always returns an empty string -- which, we hope, is never valid. 86 func (*modelAgentConfig) OldPassword() string { 87 return "" 88 }