本文整理汇总了Golang中github.com/juju/juju/api/agent.NewState函数的典型用法代码示例。如果您正苦于以下问题:Golang NewState函数的具体用法?Golang NewState怎么用?Golang NewState使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewState函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newWorker
func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
cfg := a.CurrentConfig()
// Grab the tag and ensure that it's for a machine.
tag, ok := cfg.Tag().(names.MachineTag)
if !ok {
return nil, errors.New("this manifold may only be used inside a machine agent")
}
// Get the machine agent's jobs.
// TODO(fwereade): this functionality should be on the
// deployer facade instead.
agentFacade := apiagent.NewState(apiCaller)
entity, err := agentFacade.Entity(tag)
if err != nil {
return nil, err
}
var isModelManager bool
for _, job := range entity.Jobs() {
if job == multiwatcher.JobManageModel {
isModelManager = true
break
}
}
if !isModelManager {
return nil, dependency.ErrMissing
}
return NewResumer(apiresumer.NewAPI(apiCaller)), nil
}
示例2: TestEntitySetPassword
func (s *machineSuite) TestEntitySetPassword(c *gc.C) {
entity, err := apiagent.NewState(s.st).Entity(s.machine.Tag())
c.Assert(err, jc.ErrorIsNil)
err = entity.SetPassword("foo")
c.Assert(err, gc.ErrorMatches, "password is only 3 bytes long, and is not a valid Agent password")
err = entity.SetPassword("foo-12345678901234567890")
c.Assert(err, jc.ErrorIsNil)
err = entity.ClearReboot()
c.Assert(err, jc.ErrorIsNil)
err = s.machine.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.machine.PasswordValid("bar"), jc.IsFalse)
c.Assert(s.machine.PasswordValid("foo-12345678901234567890"), jc.IsTrue)
// Check that we cannot log in to mongo with the correct password.
// This is because there's no mongo password set for s.machine,
// which has JobHostUnits
info := s.MongoInfo(c)
// TODO(dfc) this entity.Tag should return a Tag
tag, err := names.ParseTag(entity.Tag())
c.Assert(err, jc.ErrorIsNil)
info.Tag = tag
info.Password = "foo-12345678901234567890"
err = tryOpenState(s.State.ModelTag(), info)
c.Assert(errors.Cause(err), jc.Satisfies, errors.IsUnauthorized)
}
示例3: newWorker
// newWorker trivially wraps NewWorker for use in a engine.AgentAPIManifold.
func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
cfg := a.CurrentConfig()
// Grab the tag and ensure that it's for a machine.
tag, ok := cfg.Tag().(names.MachineTag)
if !ok {
return nil, errors.New("this manifold may only be used inside a machine agent")
}
// Get the machine agent's jobs.
entity, err := apiagent.NewState(apiCaller).Entity(tag)
if err != nil {
return nil, err
}
var isModelManager bool
for _, job := range entity.Jobs() {
if job == multiwatcher.JobManageModel {
isModelManager = true
break
}
}
if !isModelManager {
return nil, dependency.ErrMissing
}
return NewWorker(cfg)
}
示例4: TestIsMasterPermission
func (s *servingInfoSuite) TestIsMasterPermission(c *gc.C) {
st, _ := s.OpenAPIAsNewMachine(c)
_, err := apiagent.NewState(st).IsMaster()
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
Message: "permission denied",
Code: "unauthorized access",
})
}
示例5: manifoldStart
// manifoldStart creates a *Tracker given a base.APICaller.
func manifoldStart(apiCaller base.APICaller) (worker.Worker, error) {
w, err := NewTracker(Config{
Observer: agent.NewState(apiCaller),
})
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
示例6: SetUpTest
func (s *modelSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
conn, _ := s.OpenAPIAsNewMachine(c)
agentAPI := apiagent.NewState(conn)
c.Assert(agentAPI, gc.NotNil)
s.ModelWatcherTests = apitesting.NewModelWatcherTests(
agentAPI, s.BackingState, apitesting.NoSecrets)
}
示例7: ServingInfoSetterManifold
// ServingInfoSetterManifold defines a simple start function which
// runs after the API connection has come up. If the machine agent is
// a controller, it grabs the state serving info over the API and
// records it to agent configuration, and then stops.
func ServingInfoSetterManifold(config ServingInfoSetterConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.AgentName,
config.APICallerName,
},
Start: func(context dependency.Context) (worker.Worker, error) {
// Get the agent.
var agent coreagent.Agent
if err := context.Get(config.AgentName, &agent); err != nil {
return nil, err
}
// Grab the tag and ensure that it's for a machine.
tag, ok := agent.CurrentConfig().Tag().(names.MachineTag)
if !ok {
return nil, errors.New("agent's tag is not a machine tag")
}
// Get API connection.
var apiCaller base.APICaller
if err := context.Get(config.APICallerName, &apiCaller); err != nil {
return nil, err
}
apiState := apiagent.NewState(apiCaller)
// If the machine needs State, grab the state serving info
// over the API and write it to the agent configuration.
machine, err := apiState.Entity(tag)
if err != nil {
return nil, err
}
for _, job := range machine.Jobs() {
if job.NeedsState() {
info, err := apiState.StateServingInfo()
if err != nil {
return nil, errors.Errorf("cannot get state serving info: %v", err)
}
err = agent.ChangeConfig(func(config coreagent.ConfigSetter) error {
config.SetStateServingInfo(info)
return nil
})
if err != nil {
return nil, err
}
}
}
// All is well - we're done (no actual worker is actually returned).
return nil, dependency.ErrUninstall
},
}
}
示例8: isModelManager
// isModelManager returns whether the agent has JobManageModel,
// or an error.
func isModelManager(a agent.Agent, apiCaller base.APICaller) (bool, error) {
agentFacade := apiagent.NewState(apiCaller)
entity, err := agentFacade.Entity(a.CurrentConfig().Tag())
if err != nil {
return false, errors.Trace(err)
}
for _, job := range entity.Jobs() {
if job == multiwatcher.JobManageModel {
return true, nil
}
}
return false, nil
}
示例9: TestMachineEntity
func (s *machineSuite) TestMachineEntity(c *gc.C) {
tag := names.NewMachineTag("42")
m, err := apiagent.NewState(s.st).Entity(tag)
c.Assert(err, gc.ErrorMatches, "permission denied")
c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
c.Assert(m, gc.IsNil)
m, err = apiagent.NewState(s.st).Entity(s.machine.Tag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(m.Tag(), gc.Equals, s.machine.Tag().String())
c.Assert(m.Life(), gc.Equals, params.Alive)
c.Assert(m.Jobs(), gc.DeepEquals, []multiwatcher.MachineJob{multiwatcher.JobHostUnits})
err = s.machine.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
err = s.machine.Remove()
c.Assert(err, jc.ErrorIsNil)
m, err = apiagent.NewState(s.st).Entity(s.machine.Tag())
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("machine %s not found", s.machine.Id()))
c.Assert(err, jc.Satisfies, params.IsCodeNotFound)
c.Assert(m, gc.IsNil)
}
示例10: TestUnitEntity
func (s *unitSuite) TestUnitEntity(c *gc.C) {
tag := names.NewUnitTag("wordpress/1")
m, err := apiagent.NewState(s.st).Entity(tag)
c.Assert(err, gc.ErrorMatches, "permission denied")
c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
c.Assert(m, gc.IsNil)
m, err = apiagent.NewState(s.st).Entity(s.unit.Tag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(m.Tag(), gc.Equals, s.unit.Tag().String())
c.Assert(m.Life(), gc.Equals, params.Alive)
c.Assert(m.Jobs(), gc.HasLen, 0)
err = s.unit.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
err = s.unit.Remove()
c.Assert(err, jc.ErrorIsNil)
m, err = apiagent.NewState(s.st).Entity(s.unit.Tag())
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("unit %q not found", s.unit.Name()))
c.Assert(err, jc.Satisfies, params.IsCodeNotFound)
c.Assert(m, gc.IsNil)
}
示例11: TestIsMaster
func (s *servingInfoSuite) TestIsMaster(c *gc.C) {
calledIsMaster := false
var fakeMongoIsMaster = func(session *mgo.Session, m mongo.WithAddresses) (bool, error) {
calledIsMaster = true
return true, nil
}
s.PatchValue(&apiserveragent.MongoIsMaster, fakeMongoIsMaster)
st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
expected := true
result, err := apiagent.NewState(st).IsMaster()
c.Assert(err, jc.ErrorIsNil)
c.Assert(result, gc.Equals, expected)
c.Assert(calledIsMaster, jc.IsTrue)
}
示例12: Manifold
// Manifold returns a dependency manifold that runs a deployer worker,
// using the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
// newWorker trivially wraps NewDeployer for use in a util.PostUpgradeManifold.
//
// It's not tested at the moment, because the scaffolding
// necessary is too unwieldy/distracting to introduce at this point.
newWorker := func(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
cfg := a.CurrentConfig()
// Grab the tag and ensure that it's for a machine.
tag, ok := cfg.Tag().(names.MachineTag)
if !ok {
return nil, errors.New("agent's tag is not a machine tag")
}
// Get the machine agent's jobs.
// TODO(fwereade): this functionality should be on the
// deployer facade instead.
agentFacade := apiagent.NewState(apiCaller)
entity, err := agentFacade.Entity(tag)
if err != nil {
return nil, err
}
var isUnitHoster bool
for _, job := range entity.Jobs() {
if job == multiwatcher.JobHostUnits {
isUnitHoster = true
break
}
}
if !isUnitHoster {
return nil, dependency.ErrUninstall
}
deployerFacade := apideployer.NewState(apiCaller)
context := config.NewDeployContext(deployerFacade, cfg)
w, err := NewDeployer(deployerFacade, context)
if err != nil {
return nil, errors.Annotate(err, "cannot start unit agent deployer worker")
}
return w, nil
}
return util.PostUpgradeManifold(config.PostUpgradeManifoldConfig, newWorker)
}
示例13: TestClearReboot
func (s *machineSuite) TestClearReboot(c *gc.C) {
err := s.machine.SetRebootFlag(true)
c.Assert(err, jc.ErrorIsNil)
rFlag, err := s.machine.GetRebootFlag()
c.Assert(err, jc.ErrorIsNil)
c.Assert(rFlag, jc.IsTrue)
entity, err := apiagent.NewState(s.st).Entity(s.machine.Tag())
c.Assert(err, jc.ErrorIsNil)
err = entity.ClearReboot()
c.Assert(err, jc.ErrorIsNil)
rFlag, err = s.machine.GetRebootFlag()
c.Assert(err, jc.ErrorIsNil)
c.Assert(rFlag, jc.IsFalse)
}
示例14: Manifold
// Manifold returns a dependency manifold that runs a log forwarding
// worker, using the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
openLogStream := config.OpenLogStream
if openLogStream == nil {
openLogStream = func(caller base.APICaller, cfg params.LogStreamConfig, controllerUUID string) (LogStream, error) {
return logstream.Open(caller, cfg, controllerUUID)
}
}
openForwarder := config.OpenLogForwarder
if openForwarder == nil {
openForwarder = NewLogForwarder
}
return dependency.Manifold{
Inputs: []string{
config.StateName, // ...just to force it to run only on the controller.
config.APICallerName,
},
Start: func(context dependency.Context) (worker.Worker, error) {
var apiCaller base.APICaller
if err := context.Get(config.APICallerName, &apiCaller); err != nil {
return nil, errors.Trace(err)
}
agentFacade := apiagent.NewState(apiCaller)
controllerCfg, err := agentFacade.ControllerConfig()
if err != nil {
return nil, errors.Annotate(err, "cannot read controller config")
}
orchestrator, err := newOrchestratorForController(OrchestratorArgs{
ControllerUUID: controllerCfg.ControllerUUID(),
LogForwardConfig: agentFacade,
Caller: apiCaller,
Sinks: config.Sinks,
OpenLogStream: openLogStream,
OpenLogForwarder: openForwarder,
})
return orchestrator, errors.Annotate(err, "creating log forwarding orchestrator")
},
}
}
示例15: TestStateServingInfo
func (s *servingInfoSuite) TestStateServingInfo(c *gc.C) {
st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
ssi := state.StateServingInfo{
PrivateKey: "some key",
Cert: "Some cert",
SharedSecret: "really, really secret",
APIPort: 33,
StatePort: 44,
}
expected := params.StateServingInfo{
PrivateKey: ssi.PrivateKey,
Cert: ssi.Cert,
SharedSecret: ssi.SharedSecret,
APIPort: ssi.APIPort,
StatePort: ssi.StatePort,
}
s.State.SetStateServingInfo(ssi)
info, err := apiagent.NewState(st).StateServingInfo()
c.Assert(err, jc.ErrorIsNil)
c.Assert(info, jc.DeepEquals, expected)
}