github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/facadeversions.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  )
     9  
    10  // facadeVersions lists the best version of facades that we know about. This
    11  // will be used to pick out a default version for communication, given the list
    12  // of known versions that the API server tells us it is capable of supporting.
    13  // This map should be updated whenever the API server exposes a new version (so
    14  // that the client will use it whenever it is available).
    15  // New facades should start at 1.
    16  // Facades that existed before versioning start at 0.
    17  var facadeVersions = map[string]int{
    18  	"Action":                       1,
    19  	"Addresser":                    2,
    20  	"Agent":                        2,
    21  	"AgentTools":                   1,
    22  	"AllModelWatcher":              2,
    23  	"AllWatcher":                   1,
    24  	"Annotations":                  2,
    25  	"Backups":                      1,
    26  	"Block":                        2,
    27  	"CharmRevisionUpdater":         1,
    28  	"Charms":                       2,
    29  	"Cleaner":                      2,
    30  	"Client":                       1,
    31  	"Controller":                   2,
    32  	"Deployer":                     1,
    33  	"DiscoverSpaces":               2,
    34  	"DiskManager":                  2,
    35  	"EntityWatcher":                2,
    36  	"FilesystemAttachmentsWatcher": 2,
    37  	"Firewaller":                   2,
    38  	"HighAvailability":             2,
    39  	"HostKeyReporter":              1,
    40  	"ImageManager":                 2,
    41  	"ImageMetadata":                2,
    42  	"InstancePoller":               2,
    43  	"KeyManager":                   1,
    44  	"KeyUpdater":                   1,
    45  	"LeadershipService":            2,
    46  	"LifeFlag":                     1,
    47  	"Logger":                       1,
    48  	"MachineActions":               1,
    49  	"MachineManager":               2,
    50  	"Machiner":                     1,
    51  	"MeterStatus":                  1,
    52  	"MetricsAdder":                 2,
    53  	"MetricsDebug":                 1,
    54  	"MetricsManager":               1,
    55  	"MigrationFlag":                1,
    56  	"MigrationMaster":              1,
    57  	"MigrationMinion":              1,
    58  	"MigrationStatusWatcher":       1,
    59  	"MigrationTarget":              1,
    60  	"ModelManager":                 2,
    61  	"NotifyWatcher":                1,
    62  	"Pinger":                       1,
    63  	"Provisioner":                  2,
    64  	"ProxyUpdater":                 1,
    65  	"Reboot":                       2,
    66  	"RelationUnitsWatcher":         1,
    67  	"Resumer":                      2,
    68  	"RetryStrategy":                1,
    69  	"Service":                      3,
    70  	"ServiceScaler":                1,
    71  	"Singular":                     1,
    72  	"Spaces":                       2,
    73  	"StatusHistory":                2,
    74  	"Storage":                      2,
    75  	"StorageProvisioner":           2,
    76  	"StringsWatcher":               1,
    77  	"Subnets":                      2,
    78  	"Undertaker":                   1,
    79  	"UnitAssigner":                 1,
    80  	"Uniter":                       3,
    81  	"Upgrader":                     1,
    82  	"UserManager":                  1,
    83  	"VolumeAttachmentsWatcher":     2,
    84  }
    85  
    86  // RegisterFacadeVersion sets the API client to prefer the given version
    87  // for the facade.
    88  func RegisterFacadeVersion(name string, version int) error {
    89  	if ver, ok := facadeVersions[name]; ok && ver != version {
    90  		return errors.Errorf("facade %q already registered", name)
    91  	}
    92  	facadeVersions[name] = version
    93  	return nil
    94  }
    95  
    96  // bestVersion tries to find the newest version in the version list that we can
    97  // use.
    98  func bestVersion(desiredVersion int, versions []int) int {
    99  	best := 0
   100  	for _, version := range versions {
   101  		if version <= desiredVersion && version > best {
   102  			best = version
   103  		}
   104  	}
   105  	return best
   106  }