Skip to content

Commit

Permalink
Change context to have internal var functions. Construct once, use ev…
Browse files Browse the repository at this point in the history
…erywhere but customise eventually.
  • Loading branch information
anastasiamac committed May 1, 2018
1 parent 71935ad commit 5bf28b1
Show file tree
Hide file tree
Showing 38 changed files with 94 additions and 187 deletions.
17 changes: 3 additions & 14 deletions agent/agentbootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/juju/juju/controller/modelmanager"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/instance"
"github.com/juju/juju/mongo"
"github.com/juju/juju/network"
Expand Down Expand Up @@ -205,8 +206,8 @@ func InitializeState(
return nil, nil, errors.Annotate(err, "opening hosted model environment")
}

callCtx := &CallContext{}
if err := hostedModelEnv.Create(callCtx,
if err := hostedModelEnv.Create(
context.NewCloudCallContext(),
environs.CreateParams{
ControllerUUID: controllerUUID,
}); err != nil {
Expand Down Expand Up @@ -359,15 +360,3 @@ func machineJobFromParams(job multiwatcher.MachineJob) (state.MachineJob, error)
return -1, errors.Errorf("invalid machine job %q", job)
}
}

// CallContext is a placeholder for a provider call context that will provide useful
// callbacks and other functions. For example, there will be a callback to invalid cloud
// credential that a controller uses if provider will receive some errors
// that will indicate tht cloud considers that credential invalid.
// TODO (anastasiamac 2018-04-27) flesh it out.
type CallContext struct{}

// InvalidateCredentialCallback implements context.InvalidateCredentialCallback.
func (*CallContext) InvalidateCredentialCallback() error {
return errors.NotImplementedf("InvalidateCredentialCallback")
}
19 changes: 0 additions & 19 deletions apiserver/common/environ_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
package common

import (
"github.com/juju/errors"

"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
)

// EnvironConfigGetterFuncs holds implements environs.EnvironConfigGetter
Expand All @@ -27,19 +24,3 @@ func (f EnvironConfigGetterFuncs) ModelConfig() (*config.Config, error) {
func (f EnvironConfigGetterFuncs) CloudSpec() (environs.CloudSpec, error) {
return f.CloudSpecFunc()
}

// ProviderCallContext returns the context with all necessary functionality,
// including call backs, to make a call to a cloud provider.
// TODO (anastasiamac 2018-04-30) make it real
func ProviderCallContext() context.ProviderCallContext {
return &ProviderContext{}
}

// ProviderContext contains cloud provider call context for provider calls from
// within apiserver layer.
type ProviderContext struct{}

// InvalidateCredentialCallback implements ProviderCallContext.InvalidateCredentialCallback.
func (*ProviderContext) InvalidateCredentialCallback() error {
return errors.NotImplementedf("InvalidateCredentialCallback")
}
4 changes: 2 additions & 2 deletions apiserver/facades/agent/provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func NewProvisionerAPI(st *state.State, resources facade.Resources, authorizer f
storagePoolManager: poolmanager.New(state.NewStateSettings(st), storageProviderRegistry),
getAuthFunc: getAuthFunc,
getCanModify: getCanModify,
providerCallContext: common.ProviderCallContext(),
providerCallContext: context.NewCloudCallContext(),
}, nil
}

Expand Down Expand Up @@ -1134,7 +1134,7 @@ func (p *ProvisionerAPI) markOneMachineForRemoval(machineTag string, canAccess c
}

func (p *ProvisionerAPI) SetHostMachineNetworkConfig(args params.SetMachineNetworkConfig) error {
return p.SetObservedNetworkConfig(common.ProviderCallContext(), args)
return p.SetObservedNetworkConfig(p.providerCallContext, args)
}

// CACert returns the certificate used to validate the state connection.
Expand Down
3 changes: 2 additions & 1 deletion apiserver/facades/client/applicationoffers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/juju/juju/apiserver/params"
jujucrossmodel "github.com/juju/juju/core/crossmodel"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/permission"
)

Expand Down Expand Up @@ -497,7 +498,7 @@ func (api *BaseAPI) collectRemoteSpaces(backend Backend, spaceNames []string) (m
return nil, errors.Trace(err)
}

ctx := common.ProviderCallContext()
ctx := context.NewCloudCallContext()

netEnv, ok := environs.SupportsNetworking(env)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions apiserver/facades/client/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/manual/sshprovisioner"
"github.com/juju/juju/environs/manual/winrmprovisioner"
"github.com/juju/juju/instance"
Expand Down Expand Up @@ -586,9 +587,8 @@ func (c *Client) SetModelAgentVersion(args params.SetModelAgentVersion) error {
if err != nil {
return errors.Trace(err)
}
ctx := common.ProviderCallContext()

if err := environs.CheckProviderAPI(env, ctx); err != nil {
if err := environs.CheckProviderAPI(env, context.NewCloudCallContext()); err != nil {
return err
}
// If this is the controller model, also check to make sure that there are
Expand Down
3 changes: 2 additions & 1 deletion apiserver/facades/client/cloud/instance_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/state/stateenvirons"
)

Expand Down Expand Up @@ -79,7 +80,7 @@ func instanceTypes(api *CloudAPI,

itCons := common.NewInstanceTypeConstraints(
env,
common.ProviderCallContext(),
context.NewCloudCallContext(),
value,
)
it, err := common.InstanceTypes(itCons)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/state/stateenvirons"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func instanceTypes(mm *MachineManagerAPI,
}
itCons := common.NewInstanceTypeConstraints(
env,
common.ProviderCallContext(),
context.NewCloudCallContext(),
value,
)
it, err := common.InstanceTypes(itCons)
Expand Down
4 changes: 2 additions & 2 deletions apiserver/facades/client/modelmanager/modelmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/juju/juju/controller/modelmanager"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/permission"
"github.com/juju/juju/state"
"github.com/juju/juju/state/stateenvirons"
Expand Down Expand Up @@ -488,9 +489,8 @@ func (m *ModelManagerAPI) newIAASModel(
return nil, errors.Trace(err)
}

ctx := common.ProviderCallContext()
err = env.Create(
ctx,
context.NewCloudCallContext(),
environs.CreateParams{
ControllerUUID: controllerCfg.ControllerUUID(),
},
Expand Down
3 changes: 1 addition & 2 deletions apiserver/facades/client/spaces/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func NewAPI(st *state.State, res facade.Resources, auth facade.Authorizer) (API,
if err != nil {
return nil, errors.Trace(err)
}
ctx := common.ProviderCallContext()
return newAPIWithBacking(stateShim, ctx, res, auth)
return newAPIWithBacking(stateShim, context.NewCloudCallContext(), res, auth)
}

// newAPIWithBacking creates a new server-side Spaces API facade with
Expand Down
3 changes: 2 additions & 1 deletion apiserver/facades/client/sshclient/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/network"
"github.com/juju/juju/permission"
)
Expand Down Expand Up @@ -81,7 +82,7 @@ func (facade *Facade) AllAddresses(args params.Entities) (params.SSHAddressesRes
}

environ, supportsNetworking := environs.SupportsNetworking(env)
ctx := common.ProviderCallContext()
ctx := context.NewCloudCallContext()
getter := func(m SSHMachine) ([]network.Address, error) {
devicesAddresses, err := m.AllNetworkAddresses()
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions apiserver/facades/client/subnets/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func NewAPI(st *state.State, res facade.Resources, auth facade.Authorizer) (Subn
if err != nil {
return nil, errors.Trace(err)
}
ctx := common.ProviderCallContext()

return newAPIWithBacking(stateshim, ctx, res, auth)
return newAPIWithBacking(stateshim, context.NewCloudCallContext(), res, auth)
}

func (api *subnetsAPI) checkCanRead() error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/juju/juju/apiserver/params"
coremigration "github.com/juju/juju/core/migration"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/migration"
"github.com/juju/juju/permission"
"github.com/juju/juju/state"
Expand Down Expand Up @@ -226,8 +227,7 @@ func (api *API) AdoptResources(args params.AdoptResourcesArgs) error {
if err != nil {
return errors.Trace(err)
}
ctx := common.ProviderCallContext()
return errors.Trace(env.AdoptResources(ctx, st.ControllerUUID(), args.SourceControllerVersion))
return errors.Trace(env.AdoptResources(context.NewCloudCallContext(), st.ControllerUUID(), args.SourceControllerVersion))
}

// CheckMachines compares the machines in state with the ones reported
Expand Down Expand Up @@ -272,9 +272,8 @@ func (api *API) CheckMachines(args params.ModelArgs) (params.ErrorResults, error
if err != nil {
return empty, errors.Trace(err)
}
ctx := common.ProviderCallContext()

instances, err := env.AllInstances(ctx)
instances, err := env.AllInstances(context.NewCloudCallContext())
if err != nil {
return empty, errors.Trace(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/juju/backups/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/sync"
"github.com/juju/juju/juju"
"github.com/juju/juju/jujuclient"
Expand Down Expand Up @@ -283,7 +284,7 @@ func (c *restoreCommand) rebootstrap(ctx *cmd.Context, meta *params.BackupsMetad
return errors.Annotate(err, "opening environ for rebootstrapping")
}

cloudCallCtx := &common.CallContext{}
cloudCallCtx := context.NewCloudCallContext()

instanceIds, err := env.ControllerInstances(cloudCallCtx, params.ControllerConfig.ControllerUUID())
if err != nil && errors.Cause(err) != environs.ErrNotBootstrapped && !errors.IsNotFound(err) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/juju/cloud/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/juju/interact"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
)

type CloudMetadataStore interface {
Expand Down Expand Up @@ -78,7 +79,7 @@ type AddCloudCommand struct {

// NewAddCloudCommand returns a command to add cloud information.
func NewAddCloudCommand(cloudMetadataStore CloudMetadataStore) *AddCloudCommand {
cloudCallCtx := &common.CallContext{}
cloudCallCtx := context.NewCloudCallContext()
// Ping is provider.Ping except in tests where we don't actually want to
// require a valid cloud.
return &AddCloudCommand{
Expand Down
3 changes: 2 additions & 1 deletion cmd/juju/commands/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/sync"
"github.com/juju/juju/feature"
"github.com/juju/juju/instance"
Expand Down Expand Up @@ -485,7 +486,7 @@ func (c *bootstrapCommand) Run(ctx *cmd.Context) (resultErr error) {
return errors.Trace(err)
}

cloudCallCtx := &common.CallContext{}
cloudCallCtx := context.NewCloudCallContext()

hostedModelUUID, err := utils.NewUUID()
if err != nil {
Expand Down
12 changes: 0 additions & 12 deletions cmd/juju/common/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,3 @@ func BootstrapEndpointAddresses(environ environs.Environ, callContext context.Pr
}
return netAddrs, nil
}

// CallContext is a placeholder for a provider call context that will provide useful
// callbacks and other functions. For example, there will be a callback to invalid cloud
// credential that a controller uses if provider will receive some errors
// that will indicate tht cloud considers that credential invalid.
// TODO (anastasiamac 2018-04-27) flesh it out.
type CallContext struct{}

// InvalidateCredentialCallback implements context.InvalidateCredentialCallback.
func (*CallContext) InvalidateCredentialCallback() error {
return errors.NotImplementedf("InvalidateCredentialCallback")
}
4 changes: 2 additions & 2 deletions cmd/juju/controller/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"github.com/juju/juju/api/storage"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/jujuclient"
)

Expand Down Expand Up @@ -214,7 +214,7 @@ upgrade the controller to version 2.3 or greater.
return errors.Annotate(err, "getting controller environ")
}

cloudCallCtx := &common.CallContext{}
cloudCallCtx := context.NewCloudCallContext()

for {
// Attempt to destroy the controller.
Expand Down
6 changes: 3 additions & 3 deletions cmd/juju/controller/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

"github.com/juju/juju/api/controller"
"github.com/juju/juju/apiserver/common"
cmdcommon "github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
)

const killDoc = `
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *killCommand) Run(ctx *cmd.Context) error {
if err != nil {
return errors.Annotate(err, "getting controller environ")
}
cloudCallCtx := &cmdcommon.CallContext{}
cloudCallCtx := context.NewCloudCallContext()
// If we were unable to connect to the API, just destroy the controller through
// the environs interface.
if api == nil {
Expand Down Expand Up @@ -218,7 +218,7 @@ func (c *killCommand) DirectDestroyRemaining(ctx *cmd.Context, api destroyContro
hasErrors = true
continue
}
cloudCallCtx := &cmdcommon.CallContext{}
cloudCallCtx := context.NewCloudCallContext()
if err := env.Destroy(cloudCallCtx); err != nil {
logger.Errorf(err.Error())
hasErrors = true
Expand Down
15 changes: 2 additions & 13 deletions cmd/jujud/agent/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/juju/juju/container/kvm"
"github.com/juju/juju/core/presence"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/instance"
jujunames "github.com/juju/juju/juju/names"
"github.com/juju/juju/juju/paths"
Expand Down Expand Up @@ -524,7 +525,7 @@ func (a *MachineAgent) makeEngineCreator(previousAgentVersion version.Number) fu
if err != nil {
return false, errors.Annotate(err, "getting environ from state")
}
return environs.SupportsSpaces(&CallContext{}, env), nil
return environs.SupportsSpaces(context.NewCloudCallContext(), env), nil
}

manifolds := machineManifolds(machine.ManifoldsConfig{
Expand Down Expand Up @@ -1265,15 +1266,3 @@ func (a *MachineAgent) uninstallAgent() error {
var newDeployContext = func(st *apideployer.State, agentConfig agent.Config) deployer.Context {
return deployer.NewSimpleContext(agentConfig, st)
}

// CallContext is a placeholder for a provider call context that will provide useful
// callbacks and other functions. For example, there will be a callback to invalid cloud
// credential that a controller uses if provider will receive some errors
// that will indicate tht cloud considers that credential invalid.
// TODO (anastasiamac 2018-04-27) flesh it out.
type CallContext struct{}

// InvalidateCredentialCallback implements context.InvalidateCredentialCallback.
func (*CallContext) InvalidateCredentialCallback() error {
return errors.NotImplementedf("InvalidateCredentialCallback")
}
Loading

0 comments on commit 5bf28b1

Please sign in to comment.