Skip to content

Commit

Permalink
Provider call context and its implementation placeholders.
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiamac committed Apr 30, 2018
1 parent b47854c commit 30b6fa7
Show file tree
Hide file tree
Showing 144 changed files with 1,527 additions and 1,122 deletions.
21 changes: 18 additions & 3 deletions agent/agentbootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,12 @@ func InitializeState(
if err != nil {
return nil, nil, errors.Annotate(err, "opening hosted model environment")
}
if err := hostedModelEnv.Create(environs.CreateParams{
ControllerUUID: controllerUUID,
}); err != nil {

callCtx := &CallContext{}
if err := hostedModelEnv.Create(callCtx,
environs.CreateParams{
ControllerUUID: controllerUUID,
}); err != nil {
return nil, nil, errors.Annotate(err, "creating hosted model environment")
}

Expand Down Expand Up @@ -356,3 +359,15 @@ 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")
}
18 changes: 18 additions & 0 deletions apiserver/common/environ_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
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 @@ -24,3 +26,19 @@ 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")
}
7 changes: 5 additions & 2 deletions apiserver/common/instancetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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/environs/instances"
)

Expand All @@ -35,23 +36,25 @@ func toParamsInstanceTypeResult(itypes []instances.InstanceType) []params.Instan

// NewInstanceTypeConstraints returns an instanceTypeConstraints with the passed
// parameters.
func NewInstanceTypeConstraints(env environs.Environ, constraints constraints.Value) instanceTypeConstraints {
func NewInstanceTypeConstraints(env environs.Environ, ctx context.ProviderCallContext, constraints constraints.Value) instanceTypeConstraints {
return instanceTypeConstraints{
environ: env,
constraints: constraints,
context: ctx,
}
}

// instanceTypeConstraints holds necesary params to filter instance types.
type instanceTypeConstraints struct {
constraints constraints.Value
environ environs.Environ
context context.ProviderCallContext
}

// InstanceTypes returns a list of the available instance types in the provider according
// to the passed constraints.
func InstanceTypes(cons instanceTypeConstraints) (params.InstanceTypesResult, error) {
instanceTypes, err := cons.environ.InstanceTypes(cons.constraints)
instanceTypes, err := cons.environ.InstanceTypes(cons.context, cons.constraints)
if err != nil {
return params.InstanceTypesResult{}, errors.Trace(err)
}
Expand Down
13 changes: 7 additions & 6 deletions apiserver/common/networkingcommon/networkconfigapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/state"
"github.com/juju/juju/state/stateenvirons"
)
Expand All @@ -33,7 +34,7 @@ func NewNetworkConfigAPI(st *state.State, getCanModify common.GetAuthFunc) *Netw
// SetObservedNetworkConfig reads the network config for the machine identified
// by the input args. This config is merged with the new network config supplied
// in the same args and updated if it has changed.
func (api *NetworkConfigAPI) SetObservedNetworkConfig(args params.SetMachineNetworkConfig) error {
func (api *NetworkConfigAPI) SetObservedNetworkConfig(ctx context.ProviderCallContext, args params.SetMachineNetworkConfig) error {
m, err := api.getMachineForSettingNetworkConfig(args.Tag)
if err != nil {
return errors.Trace(err)
Expand All @@ -48,7 +49,7 @@ func (api *NetworkConfigAPI) SetObservedNetworkConfig(args params.SetMachineNetw
return nil
}

providerConfig, err := api.getOneMachineProviderNetworkConfig(m)
providerConfig, err := api.getOneMachineProviderNetworkConfig(ctx, m)
if errors.IsNotProvisioned(err) {
logger.Infof("not updating machine %q network config: %v", m.Id(), err)
return nil
Expand Down Expand Up @@ -107,7 +108,7 @@ func (api *NetworkConfigAPI) fixUpFanSubnets(networkConfig []params.NetworkConfi

// SetProviderNetworkConfig sets the provider supplied network configuration
// contained in the input args against each machine supplied with said args.
func (api *NetworkConfigAPI) SetProviderNetworkConfig(args params.Entities) (params.ErrorResults, error) {
func (api *NetworkConfigAPI) SetProviderNetworkConfig(ctx context.ProviderCallContext, args params.Entities) (params.ErrorResults, error) {
logger.Tracef("SetProviderNetworkConfig %+v", args)
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
Expand All @@ -124,7 +125,7 @@ func (api *NetworkConfigAPI) SetProviderNetworkConfig(args params.Entities) (par
continue
}

providerConfig, err := api.getOneMachineProviderNetworkConfig(m)
providerConfig, err := api.getOneMachineProviderNetworkConfig(ctx, m)
if err != nil {
result.Results[i].Error = common.ServerError(err)
continue
Expand Down Expand Up @@ -177,7 +178,7 @@ func (api *NetworkConfigAPI) getMachine(tag names.MachineTag) (*state.Machine, e
return entity.(*state.Machine), nil
}

func (api *NetworkConfigAPI) getOneMachineProviderNetworkConfig(m *state.Machine) ([]params.NetworkConfig, error) {
func (api *NetworkConfigAPI) getOneMachineProviderNetworkConfig(ctx context.ProviderCallContext, m *state.Machine) ([]params.NetworkConfig, error) {
manual, err := m.IsManual()
if err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -210,7 +211,7 @@ func (api *NetworkConfigAPI) getOneMachineProviderNetworkConfig(m *state.Machine
return nil, errors.Trace(err)
}

interfaceInfos, err := netEnviron.NetworkInterfaces(instId)
interfaceInfos, err := netEnviron.NetworkInterfaces(ctx, instId)
if errors.IsNotSupported(err) {
// It's possible to have a networking environ, but not support
// NetworkInterfaces(). In leiu of adding SupportsNetworkInterfaces():
Expand Down
8 changes: 4 additions & 4 deletions apiserver/common/networkingcommon/networkconfigapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *networkConfigSuite) TestSetObservedNetworkConfig(c *gc.C) {
Config: observedConfig,
}

err = s.networkconfig.SetObservedNetworkConfig(args)
err = s.networkconfig.SetObservedNetworkConfig(common.ProviderCallContext(), args)
c.Assert(err, jc.ErrorIsNil)

devices, err = s.machine.AllLinkLayerDevices()
Expand All @@ -90,7 +90,7 @@ func (s *networkConfigSuite) TestSetObservedNetworkConfigPermissions(c *gc.C) {
Config: nil,
}

err := s.networkconfig.SetObservedNetworkConfig(args)
err := s.networkconfig.SetObservedNetworkConfig(common.ProviderCallContext(), args)
c.Assert(err, gc.ErrorMatches, "permission denied")
}

Expand All @@ -106,7 +106,7 @@ func (s *networkConfigSuite) TestSetProviderNetworkConfig(c *gc.C) {
{Tag: s.machine.Tag().String()},
}}

result, err := s.networkconfig.SetProviderNetworkConfig(args)
result, err := s.networkconfig.SetProviderNetworkConfig(common.ProviderCallContext(), args)
c.Assert(err, jc.ErrorIsNil)
c.Assert(result, gc.DeepEquals, params.ErrorResults{
Results: []params.ErrorResult{{nil}},
Expand All @@ -133,7 +133,7 @@ func (s *networkConfigSuite) TestSetProviderNetworkConfigPermissions(c *gc.C) {
{Tag: "machine-42"},
}}

result, err := s.networkconfig.SetProviderNetworkConfig(args)
result, err := s.networkconfig.SetProviderNetworkConfig(common.ProviderCallContext(), args)
c.Assert(err, jc.ErrorIsNil)
c.Assert(result, gc.DeepEquals, params.ErrorResults{
Results: []params.ErrorResult{
Expand Down
9 changes: 5 additions & 4 deletions apiserver/common/networkingcommon/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@ import (
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/network"
)

// SupportsSpaces checks if the environment implements NetworkingEnviron
// and also if it supports spaces.
func SupportsSpaces(backing environs.EnvironConfigGetter) error {
func SupportsSpaces(backing environs.EnvironConfigGetter, ctx context.ProviderCallContext) error {
env, err := environs.GetEnviron(backing, environs.New)
if err != nil {
return errors.Annotate(err, "getting environ")
}
if !environs.SupportsSpaces(env) {
if !environs.SupportsSpaces(ctx, env) {
return errors.NotSupportedf("spaces")
}
return nil
}

// CreateSpaces creates a new Juju network space, associating the
// specified subnets with it (optional; can be empty).
func CreateSpaces(backing NetworkBacking, args params.CreateSpacesParams) (results params.ErrorResults, err error) {
err = SupportsSpaces(backing)
func CreateSpaces(backing NetworkBacking, ctx context.ProviderCallContext, args params.CreateSpacesParams) (results params.ErrorResults, err error) {
err = SupportsSpaces(backing, ctx)
if err != nil {
return results, common.ServerError(errors.Trace(err))
}
Expand Down
35 changes: 18 additions & 17 deletions apiserver/common/networkingcommon/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
providercommon "github.com/juju/juju/provider/common"
Expand Down Expand Up @@ -93,14 +94,14 @@ func (cache *addSubnetsCache) validateSpace(spaceTag string) (*names.SpaceTag, e

// cacheZones populates the allZones and availableZones cache, if it's
// empty.
func (cache *addSubnetsCache) cacheZones() error {
func (cache *addSubnetsCache) cacheZones(ctx context.ProviderCallContext) error {
if cache.allZones != nil {
// Already cached.
logger.Tracef("using cached zones: %v", cache.allZones.SortedValues())
return nil
}

allZones, err := AllZones(cache.api)
allZones, err := AllZones(ctx, cache.api)
if err != nil {
return errors.Annotate(err, "given Zones cannot be validated")
}
Expand Down Expand Up @@ -136,7 +137,7 @@ func (cache *addSubnetsCache) cacheZones() error {
// providerZones and empty givenZones, it returns the providerZones (i.e. trusts
// the provider to know better). When no providerZones and only givenZones are
// set, only then the cache is used to validate givenZones.
func (cache *addSubnetsCache) validateZones(providerZones, givenZones []string) ([]string, error) {
func (cache *addSubnetsCache) validateZones(ctx context.ProviderCallContext, providerZones, givenZones []string) ([]string, error) {
givenSet := set.NewStrings(givenZones...)
providerSet := set.NewStrings(providerZones...)

Expand All @@ -159,7 +160,7 @@ func (cache *addSubnetsCache) validateZones(providerZones, givenZones []string)
}

// Otherwise we need the cache to validate.
if err := cache.cacheZones(); err != nil {
if err := cache.cacheZones(ctx); err != nil {
return nil, errors.Trace(err)
}

Expand All @@ -183,7 +184,7 @@ func (cache *addSubnetsCache) validateZones(providerZones, givenZones []string)
// handles the case when subnets have duplicated CIDRs but distinct ProviderIds.
// It also handles weird edge cases, like no CIDR and/or ProviderId set for a
// subnet.
func (cache *addSubnetsCache) cacheSubnets() error {
func (cache *addSubnetsCache) cacheSubnets(ctx context.ProviderCallContext) error {
if cache.allSubnets != nil {
// Already cached.
logger.Tracef("using %d cached subnets", len(cache.allSubnets))
Expand All @@ -194,7 +195,7 @@ func (cache *addSubnetsCache) cacheSubnets() error {
if err != nil {
return errors.Trace(err)
}
subnetInfo, err := netEnv.Subnets(instance.UnknownId, nil)
subnetInfo, err := netEnv.Subnets(ctx, instance.UnknownId, nil)
if err != nil {
return errors.Annotate(err, "cannot get provider subnets")
}
Expand Down Expand Up @@ -260,7 +261,7 @@ func (cache *addSubnetsCache) cacheSubnets() error {
// validateSubnet ensures either subnetTag or providerId is valid (not both),
// then uses the cache to validate and lookup the provider SubnetInfo for the
// subnet, if found.
func (cache *addSubnetsCache) validateSubnet(subnetTag, providerId string) (*network.SubnetInfo, error) {
func (cache *addSubnetsCache) validateSubnet(ctx context.ProviderCallContext, subnetTag, providerId string) (*network.SubnetInfo, error) {
haveTag := subnetTag != ""
haveProviderId := providerId != ""

Expand All @@ -279,7 +280,7 @@ func (cache *addSubnetsCache) validateSubnet(subnetTag, providerId string) (*net
}

// Otherwise we need the cache to validate.
if err := cache.cacheSubnets(); err != nil {
if err := cache.cacheSubnets(ctx); err != nil {
return nil, errors.Trace(err)
}

Expand Down Expand Up @@ -332,16 +333,16 @@ func (cache *addSubnetsCache) validateSubnet(subnetTag, providerId string) (*net

// addOneSubnet validates the given arguments, using cache for lookups
// (initialized on first use), then adds it to the backing store, if successful.
func addOneSubnet(api NetworkBacking, args params.AddSubnetParams, cache *addSubnetsCache) error {
subnetInfo, err := cache.validateSubnet(args.SubnetTag, args.SubnetProviderId)
func addOneSubnet(ctx context.ProviderCallContext, api NetworkBacking, args params.AddSubnetParams, cache *addSubnetsCache) error {
subnetInfo, err := cache.validateSubnet(ctx, args.SubnetTag, args.SubnetProviderId)
if err != nil {
return errors.Trace(err)
}
spaceTag, err := cache.validateSpace(args.SpaceTag)
if err != nil {
return errors.Trace(err)
}
zones, err := cache.validateZones(subnetInfo.AvailabilityZones, args.Zones)
zones, err := cache.validateZones(ctx, subnetInfo.AvailabilityZones, args.Zones)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -362,7 +363,7 @@ func addOneSubnet(api NetworkBacking, args params.AddSubnetParams, cache *addSub
}

// AddSubnets adds.
func AddSubnets(api NetworkBacking, args params.AddSubnetsParams) (params.ErrorResults, error) {
func AddSubnets(ctx context.ProviderCallContext, api NetworkBacking, args params.AddSubnetsParams) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Subnets)),
}
Expand All @@ -373,7 +374,7 @@ func AddSubnets(api NetworkBacking, args params.AddSubnetsParams) (params.ErrorR

cache := NewAddSubnetsCache(api)
for i, arg := range args.Subnets {
err := addOneSubnet(api, arg, cache)
err := addOneSubnet(ctx, api, arg, cache)
if err != nil {
results.Results[i].Error = common.ServerError(err)
}
Expand Down Expand Up @@ -453,7 +454,7 @@ func networkingEnviron(getter environs.EnvironConfigGetter) (environs.Networking
}

// AllZones is defined on the API interface.
func AllZones(api NetworkBacking) (params.ZoneResults, error) {
func AllZones(ctx context.ProviderCallContext, api NetworkBacking) (params.ZoneResults, error) {
var results params.ZoneResults

zonesAsString := func(zones []providercommon.AvailabilityZone) string {
Expand All @@ -473,7 +474,7 @@ func AllZones(api NetworkBacking) (params.ZoneResults, error) {
if len(zones) == 0 {
// This is likely the first time we're called.
// Fetch all zones from the provider and update.
zones, err = updateZones(api)
zones, err = updateZones(ctx, api)
if err != nil {
return results, errors.Annotate(err, "cannot update known zones")
}
Expand All @@ -495,12 +496,12 @@ func AllZones(api NetworkBacking) (params.ZoneResults, error) {
// updateZones attempts to retrieve all availability zones from the environment
// provider (if supported) and then updates the persisted list of zones in
// state, returning them as well on success.
func updateZones(api NetworkBacking) ([]providercommon.AvailabilityZone, error) {
func updateZones(ctx context.ProviderCallContext, api NetworkBacking) ([]providercommon.AvailabilityZone, error) {
zoned, err := zonedEnviron(api)
if err != nil {
return nil, errors.Trace(err)
}
zones, err := zoned.AvailabilityZones()
zones, err := zoned.AvailabilityZones(ctx)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
Loading

0 comments on commit 30b6fa7

Please sign in to comment.