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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/api/watcher/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:
6
6
import (
7
7
        "sync"
8
8
 
 
9
        "github.com/juju/errors"
9
10
        "github.com/juju/loggo"
 
11
        "github.com/juju/names"
10
12
        "launchpad.net/tomb"
11
13
 
12
14
        "github.com/juju/juju/api/base"
13
15
        "github.com/juju/juju/apiserver/params"
 
16
        "github.com/juju/juju/core/migration"
14
17
        "github.com/juju/juju/watcher"
15
18
)
16
19
 
438
441
func (w *entitiesWatcher) Changes() watcher.StringsChannel {
439
442
        return w.out
440
443
}
 
444
 
 
445
// NewMigrationMasterWatcher takes the NotifyWatcherId returns by the
 
446
// MigrationMaster.Watch API and returns a watcher which will report
 
447
// details about a model migration (if and when it exists).
 
448
func NewMigrationMasterWatcher(caller base.APICaller, watcherId string) watcher.MigrationMasterWatcher {
 
449
        w := &migrationMasterWatcher{
 
450
                caller: caller,
 
451
                id:     watcherId,
 
452
                out:    make(chan migration.TargetInfo),
 
453
        }
 
454
        go func() {
 
455
                defer w.tomb.Done()
 
456
                w.tomb.Kill(w.loop())
 
457
        }()
 
458
        return w
 
459
}
 
460
 
 
461
type migrationMasterWatcher struct {
 
462
        commonWatcher
 
463
        caller base.APICaller
 
464
        id     string
 
465
        out    chan migration.TargetInfo
 
466
}
 
467
 
 
468
func (w *migrationMasterWatcher) loop() error {
 
469
        w.newResult = func() interface{} { return new(params.ModelMigrationTargetInfo) }
 
470
        w.call = makeWatcherAPICaller(w.caller, "MigrationMasterWatcher", w.id)
 
471
        w.commonWatcher.init()
 
472
        go w.commonLoop()
 
473
 
 
474
        for {
 
475
                var data interface{}
 
476
                var ok bool
 
477
 
 
478
                select {
 
479
                case data, ok = <-w.in:
 
480
                        if !ok {
 
481
                                // The tomb is already killed with the correct error
 
482
                                // at this point, so just return.
 
483
                                return nil
 
484
                        }
 
485
                case <-w.tomb.Dying():
 
486
                        return nil
 
487
                }
 
488
 
 
489
                info := data.(*params.ModelMigrationTargetInfo)
 
490
                controllerTag, err := names.ParseModelTag(info.ControllerTag)
 
491
                if err != nil {
 
492
                        return errors.Annotatef(err, "unable to parse %q", info.ControllerTag)
 
493
                }
 
494
                authTag, err := names.ParseUserTag(info.AuthTag)
 
495
                if err != nil {
 
496
                        return errors.Annotatef(err, "unable to parse %q", info.AuthTag)
 
497
                }
 
498
                outInfo := migration.TargetInfo{
 
499
                        ControllerTag: controllerTag,
 
500
                        Addrs:         info.Addrs,
 
501
                        CACert:        info.CACert,
 
502
                        AuthTag:       authTag,
 
503
                        Password:      info.Password,
 
504
                }
 
505
                select {
 
506
                case w.out <- outInfo:
 
507
                case <-w.tomb.Dying():
 
508
                        return nil
 
509
                }
 
510
        }
 
511
}
 
512
 
 
513
// Changes returns a channel that reports the details of an active
 
514
// migration for the model associated with the API connection.
 
515
func (w *migrationMasterWatcher) Changes() <-chan migration.TargetInfo {
 
516
        return w.out
 
517
}