github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"github.com/juju/names"
     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, uuid string) (agent.Agent, error) {
    20  	if !names.IsValidModel(uuid) {
    21  		return nil, errors.NotValidf("model uuid %q", uuid)
    22  	}
    23  	return &modelAgent{
    24  		Agent: a,
    25  		uuid:  uuid,
    26  	}, nil
    27  }
    28  
    29  type modelAgent struct {
    30  	agent.Agent
    31  	uuid string
    32  }
    33  
    34  // ChangeConfig is part of the agent.Agent interface. This implementation
    35  // always returns an error.
    36  func (a *modelAgent) ChangeConfig(_ agent.ConfigMutator) error {
    37  	return errors.New("model agent config is immutable")
    38  }
    39  
    40  // CurrentConfig is part of the agent.Agent interface. This implementation
    41  // returns an agent.Config that reports tweaked API connection information.
    42  func (a *modelAgent) CurrentConfig() agent.Config {
    43  	return &modelAgentConfig{
    44  		Config: a.Agent.CurrentConfig(),
    45  		uuid:   a.uuid,
    46  	}
    47  }
    48  
    49  type modelAgentConfig struct {
    50  	agent.Config
    51  	uuid string
    52  }
    53  
    54  // Model is part of the agent.Config interface. This implementation always
    55  // returns the configured model tag.
    56  func (c *modelAgentConfig) Model() names.ModelTag {
    57  	return names.NewModelTag(c.uuid)
    58  }
    59  
    60  // APIInfo is part of the agent.Config interface. This implementation always
    61  // replaces the target model tag with the configured model tag.
    62  func (c *modelAgentConfig) APIInfo() (*api.Info, bool) {
    63  	info, ok := c.Config.APIInfo()
    64  	if !ok {
    65  		return nil, false
    66  	}
    67  	info.ModelTag = names.NewModelTag(c.uuid)
    68  	return info, true
    69  }
    70  
    71  // OldPassword is part of the agent.Config interface. This implementation
    72  // always returns an empty string -- which, we hope, is never valid.
    73  func (*modelAgentConfig) OldPassword() string {
    74  	return ""
    75  }