~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/watcher.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
                "EntityWatcher", 2, newEntitiesWatcher,
52
52
                reflect.TypeOf((*srvEntitiesWatcher)(nil)),
53
53
        )
 
54
        common.RegisterFacade(
 
55
                "MigrationMasterWatcher", 1, newMigrationMasterWatcher,
 
56
                reflect.TypeOf((*srvMigrationMasterWatcher)(nil)),
 
57
        )
54
58
}
55
59
 
56
60
// NewAllModelWatcher returns a new API server endpoint for interacting
371
375
func (w *srvEntitiesWatcher) Stop() error {
372
376
        return w.resources.Stop(w.id)
373
377
}
 
378
 
 
379
func newMigrationMasterWatcher(
 
380
        st *state.State,
 
381
        resources *common.Resources,
 
382
        auth common.Authorizer,
 
383
        id string,
 
384
) (interface{}, error) {
 
385
        if !auth.AuthModelManager() {
 
386
                return nil, common.ErrPerm
 
387
        }
 
388
        w, ok := resources.Get(id).(state.NotifyWatcher)
 
389
        if !ok {
 
390
                return nil, common.ErrUnknownWatcher
 
391
        }
 
392
        return &srvMigrationMasterWatcher{
 
393
                watcher:   w,
 
394
                id:        id,
 
395
                resources: resources,
 
396
                st:        migrationGetter(st),
 
397
        }, nil
 
398
}
 
399
 
 
400
type srvMigrationMasterWatcher struct {
 
401
        watcher   state.NotifyWatcher
 
402
        id        string
 
403
        resources *common.Resources
 
404
        st        modelMigrationGetter
 
405
}
 
406
 
 
407
var migrationGetter = func(st *state.State) modelMigrationGetter {
 
408
        return st
 
409
}
 
410
 
 
411
type modelMigrationGetter interface {
 
412
        GetModelMigration() (state.ModelMigration, error)
 
413
}
 
414
 
 
415
// Next returns when a model migration is active for the associated
 
416
// model. The details for the migration's target controller are
 
417
// returned.
 
418
func (w *srvMigrationMasterWatcher) Next() (params.ModelMigrationTargetInfo, error) {
 
419
        empty := params.ModelMigrationTargetInfo{}
 
420
 
 
421
        if _, ok := <-w.watcher.Changes(); !ok {
 
422
                err := w.watcher.Err()
 
423
                if err == nil {
 
424
                        err = common.ErrStoppedWatcher
 
425
                }
 
426
                return empty, err
 
427
        }
 
428
 
 
429
        mig, err := w.st.GetModelMigration()
 
430
        if err != nil {
 
431
                return empty, errors.Annotate(err, "migration lookup")
 
432
        }
 
433
        info, err := mig.TargetInfo()
 
434
        if err != nil {
 
435
                return empty, errors.Trace(err)
 
436
        }
 
437
        return params.ModelMigrationTargetInfo{
 
438
                ControllerTag: info.ControllerTag.String(),
 
439
                Addrs:         info.Addrs,
 
440
                CACert:        info.CACert,
 
441
                AuthTag:       info.AuthTag.String(),
 
442
                Password:      info.Password,
 
443
        }, nil
 
444
}
 
445
 
 
446
// Stop stops the watcher.
 
447
func (w *srvMigrationMasterWatcher) Stop() error {
 
448
        return w.resources.Stop(w.id)
 
449
}