Skip to content

Commit 929b999

Browse files
committed
add tests for MutableIdentity resource behaviour
1 parent 5313f51 commit 929b999

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed

helper/schema/grpc_provider_test.go

Lines changed: 345 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,131 @@ 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+
PlannedIdentity: &tfprotov5.ResourceIdentityData{
7341+
IdentityData: &tfprotov5.DynamicValue{
7342+
MsgPack: mustMsgpackMarshal(
7343+
cty.Object(map[string]cty.Type{
7344+
"identity": cty.String,
7345+
}),
7346+
cty.ObjectVal(map[string]cty.Value{
7347+
"identity": cty.StringVal("changed"),
7348+
}),
7349+
),
7350+
},
7351+
},
7352+
},
7353+
},
71327354
"update-resource-without-prior-identity-identity-may-change": {
71337355
server: NewGRPCProviderServer(&Provider{
71347356
ResourcesMap: map[string]*Resource{
@@ -8153,6 +8375,129 @@ New Identity: cty.ObjectVal(map[string]cty.Value{"identity":cty.StringVal("chang
81538375
},
81548376
},
81558377
},
8378+
"update-resource-identity-may-change-if-mutable-identity-allowed": {
8379+
server: NewGRPCProviderServer(&Provider{
8380+
ResourcesMap: map[string]*Resource{
8381+
"test": {
8382+
ResourceBehavior: ResourceBehavior{
8383+
MutableIdentity: true,
8384+
},
8385+
SchemaVersion: 1,
8386+
Schema: map[string]*Schema{
8387+
"id": {
8388+
Type: TypeString,
8389+
Required: true,
8390+
},
8391+
"test": {
8392+
Type: TypeString,
8393+
},
8394+
},
8395+
Identity: &ResourceIdentity{
8396+
Version: 1,
8397+
SchemaFunc: func() map[string]*Schema {
8398+
return map[string]*Schema{
8399+
"identity": {
8400+
Type: TypeString,
8401+
RequiredForImport: true,
8402+
},
8403+
}
8404+
},
8405+
},
8406+
UpdateContext: func(_ context.Context, rd *ResourceData, _ interface{}) diag.Diagnostics {
8407+
identity, err := rd.Identity()
8408+
if err != nil {
8409+
return diag.FromErr(err)
8410+
}
8411+
err = identity.Set("identity", "changed")
8412+
if err != nil {
8413+
return diag.FromErr(err)
8414+
}
8415+
rd.SetId("changed")
8416+
return nil
8417+
},
8418+
},
8419+
},
8420+
}),
8421+
req: &tfprotov5.ApplyResourceChangeRequest{
8422+
TypeName: "test",
8423+
PriorState: &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+
PlannedState: &tfprotov5.DynamicValue{
8436+
MsgPack: mustMsgpackMarshal(
8437+
cty.Object(map[string]cty.Type{
8438+
"id": cty.String,
8439+
"test": cty.String,
8440+
}),
8441+
cty.ObjectVal(map[string]cty.Value{
8442+
"id": cty.StringVal("initial"),
8443+
"test": cty.StringVal("initial"),
8444+
}),
8445+
),
8446+
},
8447+
PlannedIdentity: &tfprotov5.ResourceIdentityData{
8448+
IdentityData: &tfprotov5.DynamicValue{
8449+
MsgPack: mustMsgpackMarshal(
8450+
cty.Object(map[string]cty.Type{
8451+
"identity": cty.String,
8452+
}),
8453+
cty.ObjectVal(map[string]cty.Value{
8454+
"identity": cty.StringVal("initial"),
8455+
}),
8456+
),
8457+
},
8458+
},
8459+
Config: &tfprotov5.DynamicValue{
8460+
MsgPack: mustMsgpackMarshal(
8461+
cty.Object(map[string]cty.Type{
8462+
"id": cty.String,
8463+
"test": cty.String,
8464+
}),
8465+
cty.ObjectVal(map[string]cty.Value{
8466+
"id": cty.NullVal(cty.String),
8467+
"test": cty.StringVal("initial"),
8468+
}),
8469+
),
8470+
},
8471+
},
8472+
expected: &tfprotov5.ApplyResourceChangeResponse{
8473+
NewState: &tfprotov5.DynamicValue{
8474+
MsgPack: mustMsgpackMarshal(
8475+
cty.Object(map[string]cty.Type{
8476+
"id": cty.String,
8477+
"test": cty.String,
8478+
}),
8479+
cty.ObjectVal(map[string]cty.Value{
8480+
"id": cty.StringVal("changed"),
8481+
"test": cty.StringVal("initial"),
8482+
}),
8483+
),
8484+
},
8485+
Private: []uint8(`{"schema_version":"1"}`),
8486+
UnsafeToUseLegacyTypeSystem: true,
8487+
NewIdentity: &tfprotov5.ResourceIdentityData{
8488+
IdentityData: &tfprotov5.DynamicValue{
8489+
MsgPack: mustMsgpackMarshal(
8490+
cty.Object(map[string]cty.Type{
8491+
"identity": cty.String,
8492+
}),
8493+
cty.ObjectVal(map[string]cty.Value{
8494+
"identity": cty.StringVal("changed"),
8495+
}),
8496+
),
8497+
},
8498+
},
8499+
},
8500+
},
81568501
"update-resource-without-planned-identity-identity-may-change": {
81578502
server: NewGRPCProviderServer(&Provider{
81588503
ResourcesMap: map[string]*Resource{

0 commit comments

Comments
 (0)