Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test a special set diff case #2304

Merged
merged 7 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,27 @@
"t0",
"t1"
]
},
"testbridge:index/VlanNamesResVlanName:VlanNamesResVlanName": {
"properties": {
"name": {
"type": "string",
"description": "Name of the VLAN, string length must be from 1 to 32 characters\n"
},
"vlanId": {
"type": "string",
"description": "VLAN ID\n"
}
},
"type": "object",
"language": {
"nodejs": {
"requiredOutputs": [
"name",
"vlanId"
]
}
}
}
},
"provider": {
Expand Down Expand Up @@ -962,6 +983,48 @@
},
"type": "object"
}
},
"testbridge:index/testres:VlanNamesRes": {
"properties": {
"other": {
"type": "string"
},
"vlanNames": {
"type": "array",
"items": {
"$ref": "#/types/testbridge:index/VlanNamesResVlanName:VlanNamesResVlanName"
},
"description": "An array of named VLANs\n"
}
},
"inputProperties": {
"other": {
"type": "string"
},
"vlanNames": {
"type": "array",
"items": {
"$ref": "#/types/testbridge:index/VlanNamesResVlanName:VlanNamesResVlanName"
},
"description": "An array of named VLANs\n"
}
},
"stateInputs": {
"description": "Input properties used for looking up and filtering VlanNamesRes resources.\n",
"properties": {
"other": {
"type": "string"
},
"vlanNames": {
"type": "array",
"items": {
"$ref": "#/types/testbridge:index/VlanNamesResVlanName:VlanNamesResVlanName"
},
"description": "An array of named VLANs\n"
}
},
"type": "object"
}
}
},
"functions": {
Expand Down
2 changes: 2 additions & 0 deletions pf/tests/internal/testprovider/testbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func SyntheticTestBridgeProvider() tfbridge.ProviderInfo {
"id": {Type: "string"},
},
},
"testbridge_vlan_names_res": {Tok: "testbridge:index/testres:VlanNamesRes"},
},

DataSources: map[string]*tfbridge.DataSourceInfo{
Expand Down Expand Up @@ -334,5 +335,6 @@ func (p *syntheticProvider) Resources(context.Context) []func() resource.Resourc
newPrivst,
newAutoNameRes,
newIntIDRes,
newVlanNamesRes,
}
}
105 changes: 105 additions & 0 deletions pf/tests/internal/testprovider/testbridge_resource_vlan_names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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"

"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/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type vlanNamesRes struct{}

var _ resource.Resource = &vlanNamesRes{}

func newVlanNamesRes() resource.Resource {
return &vlanNamesRes{}
}

func (*vlanNamesRes) schema() rschema.Schema {
return rschema.Schema{
Attributes: map[string]rschema.Attribute{
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"other": schema.StringAttribute{Optional: true},
// borrowed from https://github.com/cisco-open/terraform-provider-meraki/blob/7b3e63a22f6706c110957609ef608e81956b7166/internal/provider/resource_meraki_networks_vlan_profiles.go#L120
"vlan_names": schema.SetNestedAttribute{
MarkdownDescription: `An array of named VLANs`,
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: `Name of the VLAN, string length must be from 1 to 32 characters`,
Computed: true,
Optional: true,
},
"vlan_id": schema.StringAttribute{
MarkdownDescription: `VLAN ID`,
Computed: true,
Optional: true,
},
},
},
},
},
}
}

func (e *vlanNamesRes) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_vlan_names_res"
}

func (e *vlanNamesRes) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = e.schema()
}

func (e *vlanNamesRes) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
resp.State.Raw = req.Plan.Raw.Copy() // Copy plan to state.
resp.State.SetAttribute(ctx, path.Root("id"), "some-id")
}

func (e *vlanNamesRes) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
}

func (e *vlanNamesRes) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.State.Raw = req.Plan.Raw.Copy() // Copy plan to state.
}

func (e *vlanNamesRes) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
resp.State = e.nilState(ctx)
}

func (e *vlanNamesRes) nilState(ctx context.Context) tfsdk.State {
typ := e.terraformType(ctx)
return tfsdk.State{
Raw: tftypes.NewValue(typ, nil),
Schema: e.schema(),
}
}

func (e *vlanNamesRes) terraformType(ctx context.Context) tftypes.Type {
return e.schema().Type().TerraformType(ctx)
}
102 changes: 102 additions & 0 deletions pf/tests/provider_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,105 @@ func TestDiffVersionUpgrade(t *testing.T) {
}`
testutils.Replay(t, server, testCase)
}

func TestSetNestedObjectAdded(t *testing.T) {
server := newProviderServer(t, testprovider.SyntheticTestBridgeProvider())
testCase := `
{
"method": "/pulumirpc.ResourceProvider/Diff",
"request": {
"id": "0",
"urn": "urn:pulumi:test-stack::basicprogram::testbridge:index/testres:VlanNamesRes::testres1",
"olds": {
"id": "0",
"vlanNames": [
{
"name": "default",
"vlanId": "1"
}
]
},
"news": {
"vlanNames": [
{
"name": "default",
"vlanId": "1"
},
{
"name": "guest",
"vlanId": "2"
}
]
},
"oldInputs": {
"vlanNames": [
{
"name": "default",
"vlanId": "1"
}
]
}
},
"response": {
"changes": "DIFF_SOME",
"diffs": [
"vlanNames"
]
}
}
`
testutils.Replay(t, server, testCase)
}

func TestSetNestedObjectAddedOtherDiff(t *testing.T) {
server := newProviderServer(t, testprovider.SyntheticTestBridgeProvider())
testCase := `
{
"method": "/pulumirpc.ResourceProvider/Diff",
"request": {
"id": "0",
"urn": "urn:pulumi:test-stack::basicprogram::testbridge:index/testres:VlanNamesRes::testres1",
"olds": {
"id": "0",
"other": "value",
"vlanNames": [
{
"name": "default",
"vlanId": "1"
}
]
},
"news": {
"other": "value1",
"vlanNames": [
{
"name": "default",
"vlanId": "1"
},
{
"name": "guest",
"vlanId": "2"
}
]
},
"oldInputs": {
"other": "value",
"vlanNames": [
{
"name": "default",
"vlanId": "1"
}
]
}
},
"response": {
"changes": "DIFF_SOME",
"diffs": [
"other",
"vlanNames"
]
}
}
`
testutils.Replay(t, server, testCase)
}