Last active
January 24, 2025 15:54
-
-
Save aaomidi/0d6a2de928f9e16cf01ddd392d0c6426 to your computer and use it in GitHub Desktop.
Pulumi doesn't have a good way of combining resource and invoke options into a single container that we can pass down to functions. This solves that problem. Keywords: pulumi.ResourceOrInvokeOption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2025 SPIRL | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
// Package options exposes a structure that combines both resource and invoke options. | |
// | |
// The intention is to allow both resource and invoke options to be passed into functions | |
// that can use this struct directly without having to determine the type of options that are being passed in. | |
package options | |
import ( | |
"github.com/pulumi/pulumi/sdk/v3/go/pulumi" | |
) | |
// Options is a struct that implements both ResourceOption and InvokeOption. | |
// Note: Use NewOptions() to create a new Options container. | |
type Options struct { | |
pulumi.ResourceOption | |
pulumi.InvokeOption | |
} | |
func NewOptions() Options { | |
return Options{ | |
ResourceOption: pulumi.Composite(), | |
InvokeOption: pulumi.CompositeInvoke(), | |
} | |
} | |
// WithResourceOptions creates a new Options container with the provided resource options added to the base. | |
// Note: The existing options container is not mutated. | |
func (o Options) WithResourceOptions(opts ...pulumi.ResourceOption) (result Options) { | |
result.ResourceOption = pulumi.Composite(o.ResourceOption) | |
result.InvokeOption = pulumi.CompositeInvoke(o.InvokeOption) | |
for _, opt := range opts { | |
result.ResourceOption = pulumi.Composite(result.ResourceOption, opt) | |
} | |
return result | |
} | |
// WithInvokeOptions creates a new Options container with the provided invoke options added to the base. | |
// Note: The existing options container is not mutated. | |
func (o Options) WithInvokeOptions(opts ...pulumi.InvokeOption) (result Options) { | |
result.ResourceOption = pulumi.Composite(o.ResourceOption) | |
result.InvokeOption = pulumi.CompositeInvoke(o.InvokeOption) | |
for _, opt := range opts { | |
result.InvokeOption = pulumi.CompositeInvoke(result.InvokeOption, opt) | |
} | |
return result | |
} | |
// WithResourceOrInvokeOptions creates a new Options container with the provided resource and invoke options added to the base. | |
// Note: The existing options container is not mutated. | |
func (o Options) WithResourceOrInvokeOptions(opts ...pulumi.ResourceOrInvokeOption) (result Options) { | |
result.ResourceOption = pulumi.Composite(o.ResourceOption) | |
result.InvokeOption = pulumi.CompositeInvoke(o.InvokeOption) | |
for _, opt := range opts { | |
result.ResourceOption = pulumi.Composite(result.ResourceOption, opt) | |
} | |
for _, opt := range opts { | |
result.InvokeOption = pulumi.CompositeInvoke(result.InvokeOption, opt) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment