pulumi/pkg/resource/deploy/source_fixed.go
joeduffy 26cf93f759 Implement get functions on all resources
This change implements the `get` function for resources.  Per pulumi/lumi#83,
this allows Lumi scripts to actually read from the target environment.

For example, we can now look up a SecurityGroup from its ARN:

    let group = aws.ec2.SecurityGroup.get(
        "arn:aws:ec2:us-west-2:153052954103:security-group:sg-02150d79");

The returned object is a fully functional resource object.  So, we can then
link it up with an EC2 instance, for example, in the usual ways:

    let instance = new aws.ec2.Instance(..., {
        securityGroups: [ group ],
    });

This didn't require any changes to the RPC or provider model, since we
already implement the Get function.

There are a few loose ends; two are short term:

    1) URNs are not rehydrated.
    2) Query is not yet implemented.

One is mid-term:

    3) We probably want a URN-based lookup function.  But we will likely
       wait until we tackle pulumi/lumi#109 before adding this.

And one is long term (and subtle):

    4) These amount to I/O and are not repeatable!  A change in the target
       environment may cause a script to generate a different plan
       intermittently.  Most likely we want to apply a different kind of
       deployment "policy" for such scripts.  These are inching towards the
       scripting model of pulumi/lumi#121, which is an entirely different
       beast than the repeatable immutable infrastructure deployments.

Finally, it is worth noting that with this, we have some of the fundamental
underpinnings required to finally tackle "inference" (pulumi/lumi#142).
2017-06-19 17:29:02 -07:00

73 lines
2.1 KiB
Go

// Licensed to Pulumi Corporation ("Pulumi") under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// Pulumi licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deploy
import (
"github.com/pulumi/lumi/pkg/resource"
"github.com/pulumi/lumi/pkg/tokens"
)
// NewFixedSource returns a valid planning source that is comprised of a list of pre-computed resource objects.
func NewFixedSource(ctx tokens.Module, resources []*resource.Object) Source {
return &fixedSource{ctx: ctx, resources: resources}
}
// A fixedSource just returns from a fixed set of resource states.
type fixedSource struct {
ctx tokens.Module
resources []*resource.Object
}
func (src *fixedSource) Close() error {
return nil // nothing to do.
}
func (src *fixedSource) Info() interface{} {
return nil
}
func (src *fixedSource) Iterate() (SourceIterator, error) {
return &fixedSourceIterator{
src: src,
current: -1,
}, nil
}
// fixedSourceIterator always returns nil, nil in response to Next, indicating that it is done.
type fixedSourceIterator struct {
src *fixedSource
current int
}
func (iter *fixedSourceIterator) Close() error {
return nil // nothing to do.
}
func (iter *fixedSourceIterator) Produce(res *resource.Object) {
// ignore
}
func (iter *fixedSourceIterator) Next() (*SourceAllocation, *SourceQuery, error) {
iter.current++
if iter.current >= len(iter.src.resources) {
return nil, nil, nil
}
return &SourceAllocation{
Obj: iter.src.resources[iter.current],
Ctx: iter.src.ctx,
}, nil, nil
}