Skip to content

Commit fce3e26

Browse files
committed
add tests for MutableIdentity resource behaviour
1 parent 4238df6 commit fce3e26

File tree

1 file changed

+333
-0
lines changed

1 file changed

+333
-0
lines changed

helper/schema/grpc_provider_test.go

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5798,6 +5798,103 @@ New Identity: cty.ObjectVal(map[string]cty.Value{"identity":cty.StringVal("chang
57985798
},
57995799
},
58005800
},
5801+
"update-resource-identity-may-change-if-mutable-identity-allowed": {
5802+
server: NewGRPCProviderServer(&Provider{
5803+
ResourcesMap: map[string]*Resource{
5804+
"test": {
5805+
ResourceBehavior: ResourceBehavior{
5806+
MutableIdentity: true,
5807+
},
5808+
SchemaVersion: 1,
5809+
Schema: map[string]*Schema{
5810+
"id": {
5811+
Type: TypeString,
5812+
Required: true,
5813+
},
5814+
"test": {
5815+
Type: TypeString,
5816+
},
5817+
},
5818+
Identity: &ResourceIdentity{
5819+
Version: 1,
5820+
SchemaFunc: func() map[string]*Schema {
5821+
return map[string]*Schema{
5822+
"identity": {
5823+
Type: TypeString,
5824+
RequiredForImport: true,
5825+
},
5826+
}
5827+
},
5828+
},
5829+
ReadContext: func(ctx context.Context, d *ResourceData, meta interface{}) diag.Diagnostics {
5830+
identity, err := d.Identity()
5831+
if err != nil {
5832+
return diag.FromErr(err)
5833+
}
5834+
err = identity.Set("identity", "changed")
5835+
if err != nil {
5836+
return diag.FromErr(err)
5837+
}
5838+
5839+
return nil
5840+
},
5841+
},
5842+
},
5843+
}),
5844+
req: &tfprotov5.ReadResourceRequest{
5845+
TypeName: "test",
5846+
CurrentIdentity: &tfprotov5.ResourceIdentityData{
5847+
IdentityData: &tfprotov5.DynamicValue{
5848+
MsgPack: mustMsgpackMarshal(
5849+
cty.Object(map[string]cty.Type{
5850+
"identity": cty.String,
5851+
}),
5852+
cty.ObjectVal(map[string]cty.Value{
5853+
"identity": cty.StringVal("initial"),
5854+
}),
5855+
),
5856+
},
5857+
},
5858+
CurrentState: &tfprotov5.DynamicValue{
5859+
MsgPack: mustMsgpackMarshal(
5860+
cty.Object(map[string]cty.Type{
5861+
"test": cty.String,
5862+
"id": cty.String,
5863+
}),
5864+
cty.ObjectVal(map[string]cty.Value{
5865+
"test": cty.StringVal("hello"),
5866+
"id": cty.StringVal("initial"),
5867+
}),
5868+
),
5869+
},
5870+
},
5871+
expected: &tfprotov5.ReadResourceResponse{
5872+
NewState: &tfprotov5.DynamicValue{
5873+
MsgPack: mustMsgpackMarshal(
5874+
cty.Object(map[string]cty.Type{
5875+
"id": cty.String,
5876+
"test": cty.String,
5877+
}),
5878+
cty.ObjectVal(map[string]cty.Value{
5879+
"id": cty.StringVal("initial"),
5880+
"test": cty.StringVal("hello"),
5881+
}),
5882+
),
5883+
},
5884+
NewIdentity: &tfprotov5.ResourceIdentityData{
5885+
IdentityData: &tfprotov5.DynamicValue{
5886+
MsgPack: mustMsgpackMarshal(
5887+
cty.Object(map[string]cty.Type{
5888+
"identity": cty.String,
5889+
}),
5890+
cty.ObjectVal(map[string]cty.Value{
5891+
"identity": cty.StringVal("changed"),
5892+
}),
5893+
),
5894+
},
5895+
},
5896+
},
5897+
},
58015898
// "destroy-resource-identity-may-not-change": not required, as same request and response as update-resource-identity-may-not-change
58025899
// "upgraded-identity-version-identity-may-not-change": not required, as same request and response as update-resource-identity-may-not-change
58035900
}
@@ -7129,6 +7226,119 @@ Planned Identity: cty.ObjectVal(map[string]cty.Value{"identity":cty.StringVal("c
71297226
},
71307227
},
71317228
},
7229+
"update-resource-identity-may-change-if-mutable-identity-allowed": {
7230+
server: NewGRPCProviderServer(&Provider{
7231+
ResourcesMap: map[string]*Resource{
7232+
"test": {
7233+
ResourceBehavior: ResourceBehavior{
7234+
MutableIdentity: true,
7235+
},
7236+
SchemaVersion: 1,
7237+
Schema: map[string]*Schema{
7238+
"id": {
7239+
Type: TypeString,
7240+
Required: true,
7241+
},
7242+
"test": {
7243+
Type: TypeString,
7244+
},
7245+
},
7246+
Identity: &ResourceIdentity{
7247+
Version: 1,
7248+
SchemaFunc: func() map[string]*Schema {
7249+
return map[string]*Schema{
7250+
"identity": {
7251+
Type: TypeString,
7252+
RequiredForImport: true,
7253+
},
7254+
}
7255+
},
7256+
},
7257+
CustomizeDiff: func(ctx context.Context, d *ResourceDiff, meta interface{}) error {
7258+
identity, err := d.Identity()
7259+
if err != nil {
7260+
return err
7261+
}
7262+
err = identity.Set("identity", "changed")
7263+
if err != nil {
7264+
return err
7265+
}
7266+
return nil
7267+
},
7268+
},
7269+
},
7270+
}),
7271+
req: &tfprotov5.PlanResourceChangeRequest{
7272+
TypeName: "test",
7273+
PriorState: &tfprotov5.DynamicValue{
7274+
MsgPack: mustMsgpackMarshal(
7275+
cty.Object(map[string]cty.Type{
7276+
"id": cty.String,
7277+
"test": cty.String,
7278+
}),
7279+
cty.ObjectVal(map[string]cty.Value{
7280+
"id": cty.StringVal("initial"),
7281+
"test": cty.StringVal("initial"),
7282+
}),
7283+
),
7284+
},
7285+
PriorIdentity: &tfprotov5.ResourceIdentityData{
7286+
IdentityData: &tfprotov5.DynamicValue{
7287+
MsgPack: mustMsgpackMarshal(
7288+
cty.Object(map[string]cty.Type{
7289+
"identity": cty.String,
7290+
}),
7291+
cty.ObjectVal(map[string]cty.Value{
7292+
"identity": cty.StringVal("initial"),
7293+
}),
7294+
),
7295+
},
7296+
},
7297+
ProposedNewState: &tfprotov5.DynamicValue{
7298+
MsgPack: mustMsgpackMarshal(
7299+
cty.Object(map[string]cty.Type{
7300+
"id": cty.String,
7301+
"test": cty.String,
7302+
}),
7303+
cty.ObjectVal(map[string]cty.Value{
7304+
"id": cty.UnknownVal(cty.String),
7305+
"test": cty.StringVal("initial"),
7306+
}),
7307+
),
7308+
},
7309+
Config: &tfprotov5.DynamicValue{
7310+
MsgPack: mustMsgpackMarshal(
7311+
cty.Object(map[string]cty.Type{
7312+
"id": cty.String,
7313+
"test": cty.String,
7314+
}),
7315+
cty.ObjectVal(map[string]cty.Value{
7316+
"id": cty.NullVal(cty.String),
7317+
"test": cty.StringVal("initial"),
7318+
}),
7319+
),
7320+
},
7321+
},
7322+
expected: &tfprotov5.PlanResourceChangeResponse{
7323+
PlannedState: &tfprotov5.DynamicValue{
7324+
MsgPack: mustMsgpackMarshal(
7325+
cty.Object(map[string]cty.Type{
7326+
"id": cty.String,
7327+
"test": cty.String,
7328+
}),
7329+
cty.ObjectVal(map[string]cty.Value{
7330+
"id": cty.UnknownVal(cty.String),
7331+
"test": cty.StringVal("initial"),
7332+
}),
7333+
),
7334+
},
7335+
RequiresReplace: []*tftypes.AttributePath{
7336+
tftypes.NewAttributePath().WithAttributeName("id"),
7337+
},
7338+
PlannedPrivate: []byte(`{"_new_extra_shim":{}}`),
7339+
UnsafeToUseLegacyTypeSystem: true,
7340+
},
7341+
},
71327342
"update-resource-without-prior-identity-identity-may-change": {
71337343
server: NewGRPCProviderServer(&Provider{
71347344
ResourcesMap: map[string]*Resource{
@@ -8153,6 +8363,129 @@ New Identity: cty.ObjectVal(map[string]cty.Value{"identity":cty.StringVal("chang
81538363
},
81548364
},
81558365
},
8366+
"update-resource-identity-may-change-if-mutable-identity-allowed": {
8367+
server: NewGRPCProviderServer(&Provider{
8368+
ResourcesMap: map[string]*Resource{
8369+
"test": {
8370+
ResourceBehavior: ResourceBehavior{
8371+
MutableIdentity: true,
8372+
},
8373+
SchemaVersion: 1,
8374+
Schema: map[string]*Schema{
8375+
"id": {
8376+
Type: TypeString,
8377+
Required: true,
8378+
},
8379+
"test": {
8380+
Type: TypeString,
8381+
},
8382+
},
8383+
Identity: &ResourceIdentity{
8384+
Version: 1,
8385+
SchemaFunc: func() map[string]*Schema {
8386+
return map[string]*Schema{
8387+
"identity": {
8388+
Type: TypeString,
8389+
RequiredForImport: true,
8390+
},
8391+
}
8392+
},
8393+
},
8394+
UpdateContext: func(_ context.Context, rd *ResourceData, _ interface{}) diag.Diagnostics {
8395+
identity, err := rd.Identity()
8396+
if err != nil {
8397+
return diag.FromErr(err)
8398+
}
8399+
err = identity.Set("identity", "changed")
8400+
if err != nil {
8401+
return diag.FromErr(err)
8402+
}
8403+
rd.SetId("changed")
8404+
return nil
8405+
},
8406+
},
8407+
},
8408+
}),
8409+
req: &tfprotov5.ApplyResourceChangeRequest{
8410+
TypeName: "test",
8411+
PriorState: &tfprotov5.DynamicValue{
8412+
MsgPack: mustMsgpackMarshal(
8413+
cty.Object(map[string]cty.Type{
8414+
"id": cty.String,
8415+
"test": cty.String,
8416+
}),
8417+
cty.ObjectVal(map[string]cty.Value{
8418+
"id": cty.StringVal("initial"),
8419+
"test": cty.StringVal("initial"),
8420+
}),
8421+
),
8422+
},
8423+
PlannedState: &tfprotov5.DynamicValue{
8424+
MsgPack: mustMsgpackMarshal(
8425+
cty.Object(map[string]cty.Type{
8426+
"id": cty.String,
8427+
"test": cty.String,
8428+
}),
8429+
cty.ObjectVal(map[string]cty.Value{
8430+
"id": cty.StringVal("initial"),
8431+
"test": cty.StringVal("initial"),
8432+
}),
8433+
),
8434+
},
8435+
PlannedIdentity: &tfprotov5.ResourceIdentityData{
8436+
IdentityData: &tfprotov5.DynamicValue{
8437+
MsgPack: mustMsgpackMarshal(
8438+
cty.Object(map[string]cty.Type{
8439+
"identity": cty.String,
8440+
}),
8441+
cty.ObjectVal(map[string]cty.Value{
8442+
"identity": cty.StringVal("initial"),
8443+
}),
8444+
),
8445+
},
8446+
},
8447+
Config: &tfprotov5.DynamicValue{
8448+
MsgPack: mustMsgpackMarshal(
8449+
cty.Object(map[string]cty.Type{
8450+
"id": cty.String,
8451+
"test": cty.String,
8452+
}),
8453+
cty.ObjectVal(map[string]cty.Value{
8454+
"id": cty.NullVal(cty.String),
8455+
"test": cty.StringVal("initial"),
8456+
}),
8457+
),
8458+
},
8459+
},
8460+
expected: &tfprotov5.ApplyResourceChangeResponse{
8461+
NewState: &tfprotov5.DynamicValue{
8462+
MsgPack: mustMsgpackMarshal(
8463+
cty.Object(map[string]cty.Type{
8464+
"id": cty.String,
8465+
"test": cty.String,
8466+
}),
8467+
cty.ObjectVal(map[string]cty.Value{
8468+
"id": cty.StringVal("changed"),
8469+
"test": cty.StringVal("initial"),
8470+
}),
8471+
),
8472+
},
8473+
Private: []uint8(`{"schema_version":"1"}`),
8474+
UnsafeToUseLegacyTypeSystem: true,
8475+
NewIdentity: &tfprotov5.ResourceIdentityData{
8476+
IdentityData: &tfprotov5.DynamicValue{
8477+
MsgPack: mustMsgpackMarshal(
8478+
cty.Object(map[string]cty.Type{
8479+
"identity": cty.String,
8480+
}),
8481+
cty.ObjectVal(map[string]cty.Value{
8482+
"identity": cty.StringVal("changed"),
8483+
}),
8484+
),
8485+
},
8486+
},
8487+
},
8488+
},
81568489
"update-resource-without-planned-identity-identity-may-change": {
81578490
server: NewGRPCProviderServer(&Provider{
81588491
ResourcesMap: map[string]*Resource{

0 commit comments

Comments
 (0)