pulumi/pkg/resource/deploy/deploytest/resourcemonitor.go
Sean Gillespie bea1bea93f
Load specific provider versions if requested (#2648)
* Load specific provider versions if requested

As part of pulumi/pulumi#2389, we need the ability for language hosts to
tell the engine that a particular resource registration, read, or invoke
needs to use a particular version of a resource provider. This was not
previously possible before; the engine prior to this commit loaded
plugins from a default provider map, which was inferred for every
resource provider based on the contents of a user's package.json, and
was itself prone to bugs.

This PR adds the engine support needed for language hosts to request a
particular version of a provider. If this occurs, the source evaluator
specifically records the intent to load a provider with a given version
and produces a "default" provider registration that requests exactly
that version. This allows the source evaluator to produce multiple
default providers for a signle package, which was previously not
possible.

This is accomplished by having the source evaluator deal in the
"ProviderRequest" type, which is a tuple of version and package. A
request to load a provider whose version matches the package of a
previously loaded provider will re-use the existing default provider. If
the version was not previously loaded, a new default provider is
injected.

* CR Feedback: raise error if semver is invalid

* CR: call String() if you want a hash key

* Update pkg/resource/deploy/providers/provider.go

Co-Authored-By: swgillespie <sean@pulumi.com>
2019-04-17 11:25:02 -07:00

148 lines
4.2 KiB
Go

// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed 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 deploytest
import (
"context"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/plugin"
"github.com/pulumi/pulumi/pkg/tokens"
pulumirpc "github.com/pulumi/pulumi/sdk/proto/go"
)
type ResourceMonitor struct {
resmon pulumirpc.ResourceMonitorClient
}
func (rm *ResourceMonitor) RegisterResource(t tokens.Type, name string, custom bool, parent resource.URN, protect bool,
dependencies []resource.URN, provider string, inputs resource.PropertyMap,
propertyDeps map[resource.PropertyKey][]resource.URN,
deleteBeforeReplace bool, version string) (resource.URN, resource.ID, resource.PropertyMap, error) {
// marshal inputs
ins, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{KeepUnknowns: true})
if err != nil {
return "", "", nil, err
}
// marshal dependencies
deps := []string{}
for _, d := range dependencies {
deps = append(deps, string(d))
}
inputDeps := make(map[string]*pulumirpc.RegisterResourceRequest_PropertyDependencies)
for pk, pd := range propertyDeps {
pdeps := []string{}
for _, d := range pd {
pdeps = append(pdeps, string(d))
}
inputDeps[string(pk)] = &pulumirpc.RegisterResourceRequest_PropertyDependencies{
Urns: pdeps,
}
}
// submit request
resp, err := rm.resmon.RegisterResource(context.Background(), &pulumirpc.RegisterResourceRequest{
Type: string(t),
Name: name,
Custom: custom,
Parent: string(parent),
Protect: protect,
Dependencies: deps,
Provider: provider,
Object: ins,
PropertyDependencies: inputDeps,
DeleteBeforeReplace: deleteBeforeReplace,
Version: version,
})
if err != nil {
return "", "", nil, err
}
// unmarshal outputs
outs, err := plugin.UnmarshalProperties(resp.Object, plugin.MarshalOptions{KeepUnknowns: true})
if err != nil {
return "", "", nil, err
}
return resource.URN(resp.Urn), resource.ID(resp.Id), outs, nil
}
func (rm *ResourceMonitor) ReadResource(t tokens.Type, name string, id resource.ID, parent resource.URN,
inputs resource.PropertyMap, provider string, version string) (resource.URN, resource.PropertyMap, error) {
// marshal inputs
ins, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{KeepUnknowns: true})
if err != nil {
return "", nil, err
}
// submit request
resp, err := rm.resmon.ReadResource(context.Background(), &pulumirpc.ReadResourceRequest{
Type: string(t),
Name: name,
Parent: string(parent),
Provider: provider,
Properties: ins,
Version: version,
})
if err != nil {
return "", nil, err
}
// unmarshal outputs
outs, err := plugin.UnmarshalProperties(resp.Properties, plugin.MarshalOptions{KeepUnknowns: true})
if err != nil {
return "", nil, err
}
return resource.URN(resp.Urn), outs, nil
}
func (rm *ResourceMonitor) Invoke(tok tokens.ModuleMember, inputs resource.PropertyMap,
provider string, version string) (resource.PropertyMap, []*pulumirpc.CheckFailure, error) {
// marshal inputs
ins, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{KeepUnknowns: true})
if err != nil {
return nil, nil, err
}
// submit request
resp, err := rm.resmon.Invoke(context.Background(), &pulumirpc.InvokeRequest{
Tok: string(tok),
Provider: provider,
Args: ins,
Version: version,
})
if err != nil {
return nil, nil, err
}
// handle failures
if len(resp.Failures) != 0 {
return nil, resp.Failures, nil
}
// unmarshal outputs
outs, err := plugin.UnmarshalProperties(resp.Return, plugin.MarshalOptions{KeepUnknowns: true})
if err != nil {
return nil, nil, err
}
return outs, nil, nil
}