// NewFacade returns a new CAASOperator facade.
498357
func NewFacade(
	resources facade.Resources,
	authorizer facade.Authorizer,
	st CAASOperatorState,
) (*Facade, error) {
	if !authorizer.AuthApplicationAgent() {
		return nil, common.ErrPerm
	}
	model, err := st.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}
	canRead := common.AuthAny(
		common.AuthFuncForTagKind(names.ApplicationTagKind),
		common.AuthFuncForTagKind(names.UnitTagKind),
	)
	accessUnit := func() (common.AuthFunc, error) {
		switch tag := authorizer.GetAuthTag().(type) {
		case names.ApplicationTag:
			// Any of the units belonging to
			// the application can be accessed.
			app, err := st.Application(tag.Name)
			if err != nil {
				return nil, errors.Trace(err)
			}
			allUnits, err := app.AllUnits()
			if err != nil {
				return nil, errors.Trace(err)
			}
			return func(tag names.Tag) bool {
				for _, u := range allUnits {
					if u.Tag() == tag {
						return true
					}
				}
				return false
			}, nil
		default:
			return nil, errors.Errorf("expected names.ApplicationTag, got %T", tag)
		}
	}
	return &Facade{
		LifeGetter:         common.NewLifeGetter(st, canRead),
		APIAddresser:       common.NewAPIAddresser(st, resources),
		AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, canRead),
		Remover:            common.NewRemover(st, true, accessUnit),
		ToolsSetter:        common.NewToolsSetter(st, common.AuthFuncForTag(authorizer.GetAuthTag())),
		auth:               authorizer,
		resources:          resources,
		state:              st,
		model:              model,
	}, nil
}
1355112
func NewFacade(ctx facade.Context) (*Facade, error) {
	st := ctx.State()
	m, err := st.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return internalFacade(&backend{stateenvirons.EnvironConfigGetter{State: st, Model: m}}, ctx.Auth(), state.CallContext(st))
}
326343
func NewFacade(st *state.State, _ facade.Resources, auth facade.Authorizer) (*LogForwardingAPI, error) {
	return NewLogForwardingAPI(&stateAdapter{st}, auth)
}
130416
func NewFacade(caller base.APICaller) *Facade {
	facadeCaller := base.NewFacadeCaller(caller, apiName)
	return &Facade{facade: facadeCaller, ModelWatcher: common.NewModelWatcher(facadeCaller)}
}
896229
func NewModelGenerationFacade(ctx facade.Context) (*API, error) {
	authorizer := ctx.Auth()
	st := &stateShim{State: ctx.State()}
	m, err := st.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return NewModelGenerationAPI(st, authorizer, m)
}
976529
func NewFacadeV2(context facade.Context) (*CloudAPIV2, error) {
	v3, err := NewFacadeV3(context)
	if err != nil {
		return nil, err
	}
	return &CloudAPIV2{v3}, nil
}
1142286
func NewFacadeV5(ctx facade.Context) (*MachineManagerAPIV5, error) {
	machineManagerAPIv6, err := NewFacadeV6(ctx)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return &MachineManagerAPIV5{machineManagerAPIv6}, nil
}
1073763
func NewFacade(ctx facade.Context) (*MetricsManagerAPI, error) {
	return NewMetricsManagerAPI(
		ctx.State(),
		ctx.Resources(),
		ctx.Auth(),
		ctx.StatePool(),
		clock.WallClock,
	)
}
1017750
func NewFacade(
	resources facade.Resources,
	authorizer facade.Authorizer,
	st CAASFirewallerState,
) (*Facade, error) {
	if !authorizer.AuthController() {
		return nil, common.ErrPerm
	}
	accessApplication := common.AuthFuncForTagKind(names.ApplicationTagKind)
	return &Facade{
		LifeGetter: common.NewLifeGetter(
			st, common.AuthAny(
				common.AuthFuncForTagKind(names.ApplicationTagKind),
				common.AuthFuncForTagKind(names.UnitTagKind),
			),
		),
		AgentEntityWatcher: common.NewAgentEntityWatcher(
			st,
			resources,
			accessApplication,
		),
		resources: resources,
		state:     st,
	}, nil
}
811039
func NewFacade(st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*AgentToolsAPI, error) {
	newEnviron := func() (environs.Environ, error) {
		newEnviron := stateenvirons.GetNewEnvironFunc(environs.New)
		return newEnviron(st)
	}
	return NewAgentToolsAPI(st, newEnviron, findTools, envVersionUpdate, authorizer)
}
39864
func NewPublicFacade(st *state.State, _ facade.Resources, authorizer facade.Authorizer) (*Facade, error) {
	if !authorizer.AuthClient() {
		return nil, common.ErrPerm
	}

	rst, err := st.Resources()
	if err != nil {
		return nil, errors.Trace(err)
	}
	controllerCfg, err := st.ControllerConfig()
	if err != nil {
		return nil, errors.Trace(err)
	}
	newClient := func() (CharmStore, error) {
		return charmstore.NewCachingClient(state.MacaroonCache{st}, controllerCfg.CharmStoreURL())
	}
	facade, err := NewFacade(rst, newClient)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return facade, nil
}
148402
func NewFacade(ctx facade.Context) (*API, error) {
	authorizer := ctx.Auth()
	if !authorizer.AuthClient() {
		return nil, common.ErrPerm
	}

	st := ctx.State()
	m, err := st.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}

	return &API{
		authorizer: authorizer,
		backend:    getState(st, m),
	}, nil
}
1026365
func NewStateFacade(ctx facade.Context) (*Facade, error) {
	authorizer := ctx.Auth()
	if !authorizer.AuthMachineAgent() {
		return nil, common.ErrPerm
	}

	resources := ctx.Resources()
	model, err := ctx.State().Model()
	if err != nil {
		return nil, errors.Trace(err)
	}
	cloudSpecAPI := cloudspec.NewCloudSpec(
		resources,
		cloudspec.MakeCloudSpecGetterForModel(ctx.State()),
		cloudspec.MakeCloudSpecWatcherForModel(ctx.State()),
		common.AuthFuncForTag(model.ModelTag()),
	)
	return &Facade{
		CloudSpecAPI:        cloudSpecAPI,
		ModelWatcher:        common.NewModelWatcher(model, resources, authorizer),
		ControllerConfigAPI: common.NewStateControllerConfig(ctx.State()),
		auth:                authorizer,
		resources:           resources,
	}, nil
}
936918
func NewFacade(apiCaller base.APICaller, claimant names.MachineTag, entity names.Tag) (Facade, error) {
	facade, err := singular.NewAPI(apiCaller, claimant, entity)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return facade, nil
}
1252860
func wrapNewFacade(newFunc interface{}) (Factory, reflect.Type, error) {
	funcValue := reflect.ValueOf(newFunc)
	nice, err := validateNewFacade(funcValue)
	if err != nil {
		return nil, reflect.TypeOf(nil), err
	}
	var wrapped Factory
	if nice {
		wrapped = func(context Context) (Facade, error) {
			if context.ID() != "" {
				return nil, errors.New("id not expected")
			}
			in := []reflect.Value{reflect.ValueOf(context)}
			out := funcValue.Call(in)
			if out[1].Interface() != nil {
				err := out[1].Interface().(error)
				return nil, err
			}
			return out[0].Interface(), nil
		}
	} else {
		// So we know newFunc is a func with the right args in and out, so
		// wrap it into a helper function that matches the Factory.
		wrapped = func(context Context) (Facade, error) {
			if context.ID() != "" {
				return nil, errors.New("id not expected")
			}
			st := context.State()
			auth := context.Auth()
			resources := context.Resources()
			// st, resources, or auth is nil, then reflect.Call dies
			// because reflect.ValueOf(anynil) is the Zero Value.
			// So we use &obj.Elem() which gives us a concrete Value object
			// that can refer to nil.
			in := []reflect.Value{
				reflect.ValueOf(&st).Elem(),
				reflect.ValueOf(&resources).Elem(),
				reflect.ValueOf(&auth).Elem(),
			}
			out := funcValue.Call(in)
			if out[1].Interface() != nil {
				err := out[1].Interface().(error)
				return nil, err
			}
			return out[0].Interface(), nil
		}

	}
	return wrapped, funcValue.Type().Out(0), nil
}
770999
func NewFacadeV5(ctx facade.Context) (*APIv5, error) {
	api, err := NewFacadeV6(ctx)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return &APIv5{api}, nil
}
178220
func NewFacadeV8(ctx facade.Context) (*APIv8, error) {
	api, err := NewFacadeV9(ctx)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return &APIv8{api}, nil
}