pulumi/pkg/resource/resource_state.go
joeduffy fbfca58a3f Implement components
This change implements core support for "components" in the Pulumi
Fabric.  This work is described further in pulumi/pulumi#340, where
we are still discussing some of the finer points.

In a nutshell, resources no longer imply external providers.  It's
entirely possible to have a resource that logically represents
something but without having a physical manifestation that needs to
be tracked and managed by our typical CRUD operations.

For example, the aws/serverless/Function helper is one such type.
It aggregates Lambda-related resources and exposes a nice interface.
All of the Pulumi Cloud Framework resources are also examples.

To indicate that a resource does participate in the usual CRUD resource
provider, it simply derives from ExternalResource instead of Resource.

All resources now have the ability to adopt children.  This is purely
a metadata/tagging thing, and will help us roll up displays, provide
attribution to the developer, and even hide aspects of the resource
graph as appropriate (e.g., when they are implementation details).

Our use of this capability is ultra limited right now; in fact, the
only place we display children is in the CLI output.  For instance:

    + aws:serverless:Function: (create)
      [urn=urn:pulumi:demo::serverless::aws:serverless:Function::mylambda]
      => urn:pulumi:demo::serverless::aws:iam/role:Role::mylambda-iamrole
      => urn:pulumi:demo::serverless::aws:iam/rolePolicyAttachment:RolePolicyAttachment::mylambda-iampolicy-0
      => urn:pulumi:demo::serverless::aws:lambda/function:Function::mylambda

The bit indicating whether a resource is external or not is tracked
in the resulting checkpoint file, along with any of its children.
2017-10-14 18:30:59 -07:00

58 lines
2.5 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package resource
import (
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/contract"
)
// State is a structure containing state associated with a resource. This resource may have been serialized and
// deserialized, or snapshotted from a live graph of resource objects. The value's state is not, however, associated
// with any runtime objects in memory that may be actively involved in ongoing computations.
type State struct {
Type tokens.Type // the resource's type.
URN URN // the resource's object urn, a human-friendly, unique name for the resource.
External bool // true if managed external to Pulumi.
ID ID // the resource's unique ID, assigned by the resource provider (or blank if none/uncreated).
Inputs PropertyMap // the resource's input properties (as specified by the program).
Defaults PropertyMap // the resource's default property values (if any, given by the provider).
Outputs PropertyMap // the resource's complete output state (as returned by the resource provider).
Children []URN // an optional list of children belonging to this parent resource.
}
// NewState creates a new resource value from existing resource state information.
func NewState(t tokens.Type, urn URN, external bool, id ID,
inputs PropertyMap, defaults PropertyMap, outputs PropertyMap, children []URN) *State {
contract.Assert(t != "")
contract.Assert(external || id == "")
contract.Assert(inputs != nil)
return &State{
Type: t,
URN: urn,
External: external,
ID: id,
Inputs: inputs,
Defaults: defaults,
Outputs: outputs,
Children: children,
}
}
// All returns all resource state, including the inputs, defaults, and outputs, overlaid in that order.
func (s *State) All() PropertyMap {
return s.AllInputs().Merge(s.Outputs)
}
// AllInputs returns just the resource state's inputs plus any defaults supplied by the provider. This is to be used
// when diffing resource states that are entirely under the control of the developer, instead of a cloud provider.
func (s *State) AllInputs() PropertyMap {
return s.Defaults.Merge(s.Inputs)
}
// Synthesized returns all of the resource's "synthesized" state; this includes all properties that appeared in the
// default and output set, which may or may not override some or all of those that appeared in the input set.
func (s *State) Synthesized() PropertyMap {
return s.Defaults.Merge(s.Outputs)
}