github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/vsphere/session.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package vsphere 5 6 import ( 7 "golang.org/x/net/context" 8 9 callcontext "github.com/juju/juju/environs/context" 10 "github.com/juju/juju/provider/common" 11 ) 12 13 // sessionEnviron implements common.ZonedEnviron. An instance of 14 // sessionEnviron is scoped to the context of a single exported 15 // method call. 16 // 17 // NOTE(axw) this type's methods are not safe for concurrent use. 18 // It is the responsibility of the environ type to ensure that 19 // there are no concurrent calls to sessionEnviron's methods. 20 type sessionEnviron struct { 21 // environ is the environ that created this sessionEnviron. 22 // Take care to synchronise access to environ's attributes 23 // and methods as necessary. 24 *environ 25 26 ctx context.Context 27 client Client 28 29 // zones caches the results of AvailabilityZones to reduce 30 // the number of API calls required by StartInstance. 31 // We only cache per session, so there is no issue of 32 // staleness. 33 zones []common.AvailabilityZone 34 } 35 36 func (env *environ) withSession(callCtx callcontext.ProviderCallContext, f func(*sessionEnviron) error) error { 37 ctx := context.Background() 38 return env.withClient(ctx, callCtx, func(client Client) error { 39 return f(&sessionEnviron{ 40 environ: env, 41 ctx: ctx, 42 client: client, 43 }) 44 }) 45 }