A property.Value implementation to replace resource.PropertyValue (#15145)
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
The plan is to gradually replace resource.PropertyValue both internally
(in `pulumi/pulumi`) and in providers with this implementation.
This representation eliminates certain classes of bugs inherent with the
old representation:
- Modifiers (computed, secret, dependencies) live in a separate space
from values, so there is no longer a problem with different nestings.
- Equality is well defined and encapsulated.
- The distinction between `Output`, `Secret` and `Computed`.
- Names have been normalized (Input/Computed, Map/Object).
This is our chance to have a property value representation that we like,
so please leave comments if you think we can improve the API here.
## Checklist
- [X] I have run `make tidy` to update any new dependencies
- [X] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-03-14 19:58:59 +00:00
|
|
|
// Copyright 2016-2024, 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 resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/property"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Translate a Value into a PropertyValue.
|
|
|
|
//
|
|
|
|
// This is a lossless transition, such that this will be true:
|
|
|
|
//
|
|
|
|
// FromResourcePropertyValue(ToResourcePropertyValue(v)).Equals(v)
|
|
|
|
func ToResourcePropertyValue(v property.Value) PropertyValue {
|
|
|
|
var r PropertyValue
|
|
|
|
switch {
|
|
|
|
case v.IsBool():
|
|
|
|
r = NewBoolProperty(v.AsBool())
|
|
|
|
case v.IsNumber():
|
|
|
|
r = NewNumberProperty(v.AsNumber())
|
|
|
|
case v.IsString():
|
|
|
|
r = NewStringProperty(v.AsString())
|
|
|
|
case v.IsArray():
|
|
|
|
vArr := v.AsArray()
|
|
|
|
arr := make([]PropertyValue, len(vArr))
|
|
|
|
for i, vElem := range vArr {
|
|
|
|
arr[i] = ToResourcePropertyValue(vElem)
|
|
|
|
}
|
|
|
|
r = NewArrayProperty(arr)
|
|
|
|
case v.IsMap():
|
|
|
|
vMap := v.AsMap()
|
|
|
|
rMap := make(PropertyMap, len(vMap))
|
|
|
|
for k, vElem := range vMap {
|
|
|
|
rMap[PropertyKey(k)] = ToResourcePropertyValue(vElem)
|
|
|
|
}
|
|
|
|
r = NewObjectProperty(rMap)
|
|
|
|
case v.IsAsset():
|
|
|
|
r = NewAssetProperty(v.AsAsset())
|
|
|
|
case v.IsArchive():
|
|
|
|
r = NewArchiveProperty(v.AsArchive())
|
|
|
|
case v.IsResourceReference():
|
|
|
|
ref := v.AsResourceReference()
|
|
|
|
r = NewResourceReferenceProperty(ResourceReference{
|
|
|
|
URN: ref.URN,
|
|
|
|
ID: ToResourcePropertyValue(ref.ID),
|
|
|
|
PackageVersion: ref.PackageVersion,
|
|
|
|
})
|
|
|
|
case v.IsNull():
|
|
|
|
r = NewNullProperty()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(v.Dependencies()) > 0 || (v.Secret() && v.IsComputed()):
|
|
|
|
r = NewOutputProperty(Output{
|
|
|
|
Element: r,
|
|
|
|
Known: !v.IsComputed(),
|
|
|
|
Secret: v.Secret(),
|
|
|
|
Dependencies: v.Dependencies(),
|
|
|
|
})
|
|
|
|
case v.Secret():
|
|
|
|
r = MakeSecret(r)
|
|
|
|
case v.IsComputed():
|
|
|
|
r = MakeComputed(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate a PropertyValue into a Value.
|
|
|
|
//
|
|
|
|
// This is a normalizing transition, such that the last expression will be true:
|
|
|
|
//
|
|
|
|
// normalized := ToResourcePropertyValue(FromResourcePropertyValue(v))
|
|
|
|
// normalized.DeepEquals(ToResourcePropertyValue(FromResourcePropertyValue(v)))
|
|
|
|
func FromResourcePropertyValue(v PropertyValue) property.Value {
|
|
|
|
switch {
|
|
|
|
// Value types
|
|
|
|
case v.IsBool():
|
|
|
|
return property.New(v.BoolValue())
|
|
|
|
case v.IsNumber():
|
|
|
|
return property.New(v.NumberValue())
|
|
|
|
case v.IsString():
|
|
|
|
return property.New(v.StringValue())
|
|
|
|
case v.IsArray():
|
|
|
|
vArr := v.ArrayValue()
|
|
|
|
arr := make(property.Array, len(vArr))
|
|
|
|
for i, v := range vArr {
|
|
|
|
arr[i] = FromResourcePropertyValue(v)
|
|
|
|
}
|
|
|
|
return property.New(arr)
|
|
|
|
case v.IsObject():
|
|
|
|
vMap := v.ObjectValue()
|
|
|
|
rMap := make(property.Map, len(vMap))
|
|
|
|
for k, v := range vMap {
|
2024-03-25 17:26:41 +00:00
|
|
|
rMap[string(k)] = FromResourcePropertyValue(v)
|
A property.Value implementation to replace resource.PropertyValue (#15145)
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
The plan is to gradually replace resource.PropertyValue both internally
(in `pulumi/pulumi`) and in providers with this implementation.
This representation eliminates certain classes of bugs inherent with the
old representation:
- Modifiers (computed, secret, dependencies) live in a separate space
from values, so there is no longer a problem with different nestings.
- Equality is well defined and encapsulated.
- The distinction between `Output`, `Secret` and `Computed`.
- Names have been normalized (Input/Computed, Map/Object).
This is our chance to have a property value representation that we like,
so please leave comments if you think we can improve the API here.
## Checklist
- [X] I have run `make tidy` to update any new dependencies
- [X] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-03-14 19:58:59 +00:00
|
|
|
}
|
|
|
|
return property.New(rMap)
|
|
|
|
case v.IsAsset():
|
|
|
|
return property.New(v.AssetValue())
|
|
|
|
case v.IsArchive():
|
|
|
|
return property.New(v.ArchiveValue())
|
|
|
|
case v.IsResourceReference():
|
|
|
|
r := v.ResourceReferenceValue()
|
|
|
|
|
|
|
|
return property.New(property.ResourceReference{
|
|
|
|
URN: r.URN,
|
|
|
|
ID: FromResourcePropertyValue(r.ID),
|
|
|
|
PackageVersion: r.PackageVersion,
|
|
|
|
})
|
|
|
|
case v.IsNull():
|
|
|
|
return property.Value{}
|
|
|
|
|
|
|
|
// Flavor types
|
|
|
|
case v.IsComputed():
|
|
|
|
return property.New(property.Computed).WithSecret(
|
|
|
|
v.Input().Element.IsSecret() ||
|
|
|
|
(v.Input().Element.IsOutput() && v.Input().Element.OutputValue().Secret))
|
|
|
|
case v.IsSecret():
|
|
|
|
return FromResourcePropertyValue(v.SecretValue().Element).WithSecret(true)
|
|
|
|
case v.IsOutput():
|
|
|
|
o := v.OutputValue()
|
|
|
|
var elem property.Value
|
|
|
|
if !o.Known {
|
|
|
|
elem = property.New(property.Computed)
|
|
|
|
} else {
|
|
|
|
elem = FromResourcePropertyValue(o.Element)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the value is already secret, we leave it secret, otherwise we take
|
|
|
|
// the value from Output.
|
|
|
|
if o.Secret {
|
|
|
|
elem = elem.WithSecret(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
return elem.WithDependencies(o.Dependencies)
|
|
|
|
|
|
|
|
default:
|
|
|
|
contract.Failf("Unknown property value type %T", v.V)
|
|
|
|
return property.Value{}
|
|
|
|
}
|
|
|
|
}
|