* Implement first-class providers for Python First-class providers are an explicit projection of providers themselves into Pulumi programs. For the most post, providers are just regular resources, but the addition of providers to the fray (and the ability of resources to be constructed by providers in the same program) requires some changes to the Python resource model. A summary of the changes: 1. Added ProviderResource, a custom resource that is the base class of all providers. 2. ResourceOptions now has 'provider' and 'providers' fields. 'provider', when passed to a custom resource, allows users to override the provider that is used to construct a resource to an instance of a ProviderResource. 'providers', when passed to a component resource, allows users to override providers used to construct children of the component resource. 3. 'protect', 'providers', and 'provider' are all now inherited from a resource's parent if they aren't specified in the child. This commit adds the requisite code for the above changes and, in addition, adds a number of new tests that exercise them and related code paths. * Rebase against master
72 lines
3 KiB
Python
72 lines
3 KiB
Python
# 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.
|
|
from pulumi import ProviderResource, CustomResource, ComponentResource, ResourceOptions
|
|
|
|
class Provider(ProviderResource):
|
|
def __init__(self, name, opts=None):
|
|
ProviderResource.__init__(self, "test", name, {}, opts)
|
|
|
|
class Resource(CustomResource):
|
|
def __init__(self, name, create_children, opts=None):
|
|
CustomResource.__init__(self, "test:index:Resource", name, {}, opts)
|
|
if create_children is not None:
|
|
create_children(name, self)
|
|
|
|
class Component(ComponentResource):
|
|
def __init__(self, name, create_children, opts=None):
|
|
ComponentResource.__init__(self, "test:index:Component", name, {}, opts)
|
|
create_children(name, self)
|
|
|
|
def create_resources(name, create_children=None, parent=None):
|
|
# Use all parent defaults.
|
|
Resource(f"{name}/r0", create_children, ResourceOptions(parent=parent))
|
|
|
|
# Override protect
|
|
Resource(f"{name}/r1", create_children, ResourceOptions(parent=parent, protect=False))
|
|
Resource(f"{name}/r2", create_children, ResourceOptions(parent=parent, protect=True))
|
|
|
|
# Override provider
|
|
prov = Provider(f"{name}-p", ResourceOptions(parent=parent))
|
|
Resource(f"{name}/r3", create_children, ResourceOptions(parent=parent, provider=prov))
|
|
|
|
def create_components(name, create_children=None, parent=None):
|
|
# Use all parent defaults.
|
|
Component(f"{name}/c0", create_children, ResourceOptions(parent=parent))
|
|
|
|
# Override protect
|
|
Component(f"{name}/c1", create_children, ResourceOptions(parent=parent, protect=False))
|
|
Component(f"{name}/c2", create_children, ResourceOptions(parent=parent, protect=True))
|
|
|
|
# Override providers
|
|
providers = {"test": Provider(f"{name}-p", ResourceOptions(parent=parent))}
|
|
Component(f"{name}/c3", create_children, ResourceOptions(parent=parent, providers=providers))
|
|
|
|
# Create default (unparent) resources
|
|
create_resources("unparented")
|
|
|
|
# Create singly-nested resources
|
|
create_components("single-nest", lambda name, parent: create_resources(name, None, parent))
|
|
|
|
# Create doubly-nested resources
|
|
create_components("double-nest", lambda name, parent: create_components(
|
|
name,
|
|
lambda name, parent: create_resources(name, None, parent),
|
|
parent))
|
|
|
|
# Create doubly-nested resources parented to other resources
|
|
create_components("double-nest-2", lambda name, parent: create_resources(
|
|
name,
|
|
lambda name, parent: create_resources(name, None, parent),
|
|
parent))
|