-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PF] Allow overriding Number with String for PF fields (#2155)
This is motivated by [`terraform-provider-vantage blocked on numeric ID support #1198`](#1198). My plan for #1198 is to override the "id" fields of each resource to be a `"string"`, which is how we currently handle SDK based providers with the same problem.
- Loading branch information
Showing
12 changed files
with
390 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
109 changes: 109 additions & 0 deletions
109
pf/tests/internal/testprovider/testbridge_resource_int_id.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2016-2023, 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 testprovider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
type intIDRes struct{} | ||
|
||
var _ resource.Resource = &intIDRes{} | ||
|
||
func newIntIDRes() resource.Resource { | ||
return &intIDRes{} | ||
} | ||
|
||
func (*intIDRes) schema() rschema.Schema { | ||
return rschema.Schema{ | ||
Attributes: map[string]rschema.Attribute{ | ||
"id": schema.Int64Attribute{ | ||
Computed: true, | ||
PlanModifiers: []planmodifier.Int64{ | ||
int64planmodifier.UseStateForUnknown(), | ||
}, | ||
}, | ||
"name": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (e *intIDRes) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_int_id_res" | ||
} | ||
|
||
func (e *intIDRes) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = e.schema() | ||
} | ||
|
||
func (e *intIDRes) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
resp.State.Raw = req.Plan.Raw.Copy() // Copy plan to state. | ||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), 1234)...) | ||
} | ||
|
||
func (e *intIDRes) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
} | ||
|
||
func (e *intIDRes) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var id int64 | ||
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("id"), &id)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
if id != 1234 { | ||
resp.Diagnostics.AddAttributeError(path.Root("id"), "unexpected value", | ||
fmt.Sprintf("expected 1234, found %d", id)) | ||
} | ||
|
||
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("id"), &id)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
if id != 5678 { | ||
resp.Diagnostics.AddAttributeError(path.Root("id"), "unexpected value", | ||
fmt.Sprintf("expected 5678, found %d", id)) | ||
} | ||
|
||
resp.State.Raw = req.Plan.Raw.Copy() // Copy plan to state. | ||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), 90)...) | ||
} | ||
|
||
func (e *intIDRes) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
resp.State = e.nilState(ctx) | ||
} | ||
|
||
func (e *intIDRes) nilState(ctx context.Context) tfsdk.State { | ||
typ := e.terraformType(ctx) | ||
return tfsdk.State{ | ||
Raw: tftypes.NewValue(typ, nil), | ||
Schema: e.schema(), | ||
} | ||
} | ||
|
||
func (e *intIDRes) terraformType(ctx context.Context) tftypes.Type { | ||
return e.schema().Type().TerraformType(ctx) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 convert | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource" | ||
) | ||
|
||
// adaptedEncoder wraps an encoder in an adapter during encoding. | ||
// | ||
// Given [resource.PropertyValue] types P_i and P_j and an encoder P_j -> T, the adapter | ||
// function should translate P_i -> P_j. | ||
type adaptedEncoder[T Encoder] struct { | ||
adapter func(resource.PropertyValue) (resource.PropertyValue, error) | ||
encoder T | ||
} | ||
|
||
func (e adaptedEncoder[T]) fromPropertyValue(v resource.PropertyValue) (tftypes.Value, error) { | ||
adapted, err := e.adapter(v) | ||
if err != nil { | ||
return tftypes.Value{}, fmt.Errorf("failed to adapt for %T: %w", e.encoder, err) | ||
} | ||
return e.encoder.fromPropertyValue(adapted) | ||
} | ||
|
||
type adaptedDecoder[T Decoder] struct { | ||
adapter func(tftypes.Value) (tftypes.Value, error) | ||
decoder T | ||
} | ||
|
||
func (d adaptedDecoder[T]) toPropertyValue(v tftypes.Value) (resource.PropertyValue, error) { | ||
adapted, err := d.adapter(v) | ||
if err != nil { | ||
return resource.PropertyValue{}, fmt.Errorf("failed to adapt for %T: %w", d.decoder, err) | ||
} | ||
return d.decoder.toPropertyValue(adapted) | ||
} | ||
|
||
func newIntOverrideStringEncoder() Encoder { | ||
return adaptedEncoder[*numberEncoder]{ | ||
adapter: func(v resource.PropertyValue) (resource.PropertyValue, error) { | ||
if v.IsString() { | ||
f, err := strconv.ParseFloat(v.StringValue(), 64) | ||
if err != nil { | ||
return resource.PropertyValue{}, err | ||
} | ||
return resource.NewProperty(f), nil | ||
} | ||
return v, nil | ||
}, | ||
encoder: &numberEncoder{}, | ||
} | ||
} | ||
|
||
func newStringOverIntDecoder() Decoder { | ||
return adaptedDecoder[*stringDecoder]{ | ||
adapter: func(v tftypes.Value) (tftypes.Value, error) { | ||
if !v.Type().Is(tftypes.Number) { | ||
return v, nil | ||
} | ||
if !v.IsKnown() { | ||
return tftypes.NewValue(tftypes.String, tftypes.UnknownValue), nil | ||
} | ||
var f big.Float | ||
if err := v.As(&f); err != nil { | ||
return tftypes.Value{}, err | ||
} | ||
return tftypes.NewValue(tftypes.String, f.Text('f', -1)), nil | ||
}, | ||
decoder: &stringDecoder{}, | ||
} | ||
} |
Oops, something went wrong.