// BackendForPlan is similar to Backend, but uses backend settings that were
// stored in a plan.
//
// The current workspace name is also stored as part of the plan, and so this
// method will check that it matches the currently-selected workspace name
// and produce error diagnostics if not.
BackendForPlan is similar to Backend, but uses backend settings that were
stored in a plan.

The current workspace name is also stored as part of the plan, and so this
method will check that it matches the currently-selected workspace name
and produce error diagnostics if not.
// BackendForPlan is similar to Backend but uses backend settings that were // stored in a plan . // // The current workspace name is also stored as part of the plan and so this // method will check that it matches the currently - selected workspace name // and produce error diagnostics if not .
func (m *Meta) BackendForPlan(settings plans.Backend) (backend.Enhanced, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics
	f := backendInit.Backend(settings.Type)
	if f == nil {
		diags = diags.Append(fmt.Errorf(strings.TrimSpace(errBackendSavedUnknown), settings.Type))
		return nil, diags
	}
	b := f()
	log.Printf("[TRACE] Meta.BackendForPlan: instantiated backend of type %T", b)
	schema := b.ConfigSchema()
	configVal, err := settings.Config.Decode(schema.ImpliedType())
	if err != nil {
		diags = diags.Append(errwrap.Wrapf("saved backend configuration is invalid: {{err}}", err))
		return nil, diags
	}
	newVal, validateDiags := b.PrepareConfig(configVal)
	diags = diags.Append(validateDiags)
	if validateDiags.HasErrors() {
		return nil, diags
	}
	configureDiags := b.Configure(newVal)
	diags = diags.Append(configureDiags)
	if cli, ok := b.(backend.CLI); ok {
		cliOpts, err := m.backendCLIOpts()
		if err != nil {
			diags = diags.Append(err)
			return nil, diags
		}
		if err := cli.CLIInit(cliOpts); err != nil {
			diags = diags.Append(fmt.Errorf(
				"Error initializing backend %T: %s\n\n"+
					"This is a bug; please report it to the backend developer",
				b, err,
			))
			return nil, diags
		}
	}
	if enhanced, ok := b.(backend.Enhanced); ok {
		log.Printf("[TRACE] Meta.BackendForPlan: backend %T supports operations", b)
		return enhanced, nil
	}
	log.Printf("[TRACE] Meta.Backend: backend %T does not support operations, so wrapping it in a local backend", b)
	cliOpts, err := m.backendCLIOpts()
	if err != nil {
		diags = diags.Append(err)
		return nil, diags
	}
	cliOpts.Validation = false 
	local := backendLocal.NewWithBackend(b)
	if err := local.CLIInit(cliOpts); err != nil {
		panic(err)
	}
	return local, diags
}
func ( m * Meta ) BackendForPlan ( settings plans . Backend ) ( backend . Enhanced , tfdiags . Diagnostics ) { var diags tfdiags . Diagnostics f := backendInit . Backend ( settings . Type ) if f == nil { diags = diags . Append ( fmt . Errorf ( strings . TrimSpace ( errBackendSavedUnknown ) , settings . Type ) ) return nil , diags } b := f ( ) log . Printf ( " " , b ) schema := b . ConfigSchema ( ) configVal , err := settings . Config . Decode ( schema . ImpliedType ( ) ) if err != nil { diags = diags . Append ( errwrap . Wrapf ( " " , err ) ) return nil , diags } newVal , validateDiags := b . PrepareConfig ( configVal ) diags = diags . Append ( validateDiags ) if validateDiags . HasErrors ( ) { return nil , diags } configureDiags := b . Configure ( newVal ) diags = diags . Append ( configureDiags ) if cli , ok := b . ( backend . CLI ) ; ok { cliOpts , err := m . backendCLIOpts ( ) if err != nil { diags = diags . Append ( err ) return nil , diags } if err := cli . CLIInit ( cliOpts ) ; err != nil { diags = diags . Append ( fmt . Errorf ( " \n \n " + " " , b , err , ) ) return nil , diags } } if enhanced , ok := b . ( backend . Enhanced ) ; ok { log . Printf ( " " , b ) return enhanced , nil } log . Printf ( " " , b ) cliOpts , err := m . backendCLIOpts ( ) if err != nil { diags = diags . Append ( err ) return nil , diags } cliOpts . Validation = false local := backendLocal . NewWithBackend ( b ) if err := local . CLIInit ( cliOpts ) ; err != nil { panic ( err ) } return local , diags }
BackendForPlan is similar to Backend, but uses backend settings that were
stored in a plan.
BackendForPlan is similar to Backend but uses backend settings that were stored in a plan .
// If the backend supports CLI initialization, do it. // If the result of loading the backend is an enhanced backend, // then return that as-is. This works even if b == nil (it will be !ok). // Otherwise, we'll wrap our state-only remote backend in the local backend // to cause any operations to be run locally. // don't validate here in case config contains file(...) calls where the file doesn't exist // Local backend should never fail, so this is always a bug.