Skip to content

Commit

Permalink
feat(subscription): Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Jan 26, 2024
1 parent 7fda6f4 commit 90f7292
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 84 deletions.
76 changes: 3 additions & 73 deletions internal/resources/subscription/downgrade_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,6 @@ import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

var SubscriptionResourceV1 = tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"key": tftypes.String,
"version": tftypes.Number,

"changes": tftypes.Set{
ElementType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"resource_type_ids": tftypes.List{
ElementType: tftypes.String,
},
},
},
},
"destination": tftypes.List{
ElementType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"type": tftypes.String,
"topic_arn": tftypes.String,
"queue_url": tftypes.String,
"region": tftypes.String,
"account_id": tftypes.String,
"access_key": tftypes.String,
"access_secret": tftypes.String,
"uri": tftypes.String,
"connection_string": tftypes.String,
"project_id": tftypes.String,
"topic": tftypes.String,
},
},
},
"format": tftypes.List{
ElementType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"type": tftypes.String,
"cloud_events_version": tftypes.String,
},
},
},
"message": tftypes.Set{
ElementType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"resource_type_id": tftypes.String,
"types": tftypes.List{
ElementType: tftypes.String,
},
},
},
},
},
}

var SubscriptionResourceV2 = tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
Expand Down Expand Up @@ -115,7 +62,7 @@ var SubscriptionResourceV2 = tftypes.Object{
// Schema version 2 moves us to Single nested blocks, but it turned out to be
// not working correctly in terraform for now. So we moved back to the v1
// approach
func upgradeStateV2(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
func downgradeStateV2(_ context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
rawStateValue, err := req.RawState.Unmarshal(SubscriptionResourceV2)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -141,8 +88,8 @@ func upgradeStateV2(ctx context.Context, req resource.UpgradeStateRequest, resp
"key": rawState["key"],
"version": rawState["version"],
"changes": rawState["changes"],
"destination": valueToList(rawState, "destination"),
"format": valueToList(rawState, "format"),
"destination": valueDestinationV1(rawState, "destination"),
"format": valueToFormatV1(rawState, "format"),
"message": rawState["message"],
}),
)
Expand All @@ -156,20 +103,3 @@ func upgradeStateV2(ctx context.Context, req resource.UpgradeStateRequest, resp

resp.DynamicValue = &dynamicValue
}

func valueToList(state map[string]tftypes.Value, key string) tftypes.Value {
if state[key].IsNull() {
return tftypes.NewValue(
SubscriptionResourceV1.AttributeTypes[key],
[]tftypes.Value{},
)
}

if state[key].IsKnown() {
return tftypes.NewValue(
SubscriptionResourceV1.AttributeTypes[key],
[]tftypes.Value{state[key]},
)
}
return state[key]
}
8 changes: 4 additions & 4 deletions internal/resources/subscription/downgrade_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package subscription

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"

"github.com/hashicorp/terraform-plugin-framework/types"
)

func Test_DowngradeStateV2(t *testing.T) {
Expand Down Expand Up @@ -102,7 +102,7 @@ func Test_DowngradeStateV2(t *testing.T) {
},
}
resp := resource.UpgradeStateResponse{}
upgradeStateV2(ctx, req, &resp)
downgradeStateV2(ctx, req, &resp)
require.False(t, resp.Diagnostics.HasError(), resp.Diagnostics.Errors())
require.NotNil(t, resp.DynamicValue)

Expand Down
4 changes: 2 additions & 2 deletions internal/resources/subscription/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ func TestImport(t *testing.T) {
wantDest: Destination{
Type: types.StringValue(ConfluentCloud),
BootstrapServer: types.StringValue("test-bootstrap-server"),
ApiKey: types.StringValue("test-api-key"),
ApiSecret: types.StringValue("test-api-secret"),
ApiKey: types.StringUnknown(),
ApiSecret: types.StringUnknown(),
Acks: types.StringValue("test-acks"),
Topic: types.StringValue("test-topic"),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/subscription/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (r *subscriptionResource) UpgradeState(_ context.Context) map[int64]resourc
StateUpgrader: upgradeStateV0,
},
1: {
StateUpgrader: upgradeStateV2,
StateUpgrader: downgradeStateV2,

Check warning on line 325 in internal/resources/subscription/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/resource.go#L325

Added line #L325 was not covered by tests
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/resources/subscription/upgrade_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Upgrade from V0 to V1
func upgradeStateV0(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
func upgradeStateV0(_ context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
rawStateValue, err := req.RawState.Unmarshal(SubscriptionResourceV2)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -35,8 +35,8 @@ func upgradeStateV0(ctx context.Context, req resource.UpgradeStateRequest, resp
"key": rawState["key"],
"version": rawState["version"],
"changes": rawState["changes"],
"destination": valueToList(rawState, "destination"),
"format": valueToList(rawState, "format"),
"destination": valueDestinationV1(rawState, "destination"),
"format": valueToFormatV1(rawState, "format"),
"message": rawState["message"],
}),
)
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/subscription/upgrade_v0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/require"
)

func Test_upgradeStateV0(t *testing.T) {
func Test_UpgradeStateV0(t *testing.T) {
oldState := []byte(`
{
"changes": [
Expand Down
118 changes: 118 additions & 0 deletions internal/resources/subscription/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package subscription

import "github.com/hashicorp/terraform-plugin-go/tftypes"

// SubscriptionResourceV1 represents the currently used structure of the
// subscription resource. This is used to map legacy structures to the current
// required structure.
var SubscriptionResourceV1 = tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"key": tftypes.String,
"version": tftypes.Number,

"changes": tftypes.Set{
ElementType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"resource_type_ids": tftypes.List{
ElementType: tftypes.String,
},
},
},
},
"destination": tftypes.List{
ElementType: destinationType,
},
"format": tftypes.List{
ElementType: formatType,
},
"message": tftypes.Set{
ElementType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"resource_type_id": tftypes.String,
"types": tftypes.List{
ElementType: tftypes.String,
},
},
},
},
},
}

var formatType = tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"type": tftypes.String,
"cloud_events_version": tftypes.String,
},
}

var destinationType = tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"type": tftypes.String,
"topic_arn": tftypes.String,
"queue_url": tftypes.String,
"region": tftypes.String,
"account_id": tftypes.String,
"access_key": tftypes.String,
"access_secret": tftypes.String,
"uri": tftypes.String,
"connection_string": tftypes.String,
"project_id": tftypes.String,
"topic": tftypes.String,
"bootstrap_server": tftypes.String,
"api_key": tftypes.String,
"api_secret": tftypes.String,
"acks": tftypes.String,
"key": tftypes.String,
},
}

func valueToFormatV1(state map[string]tftypes.Value, key string) tftypes.Value {
if state[key].IsNull() {
return tftypes.NewValue(
SubscriptionResourceV1.AttributeTypes[key],
[]tftypes.Value{},
)
}

Check warning on line 76 in internal/resources/subscription/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/utils.go#L72-L76

Added lines #L72 - L76 were not covered by tests

if state[key].IsKnown() {
return tftypes.NewValue(
SubscriptionResourceV1.AttributeTypes[key],
[]tftypes.Value{state[key]},
)
}
return state[key]

Check warning on line 84 in internal/resources/subscription/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/utils.go#L84

Added line #L84 was not covered by tests
}

func valueDestinationV1(state map[string]tftypes.Value, key string) tftypes.Value {
if state[key].IsNull() {
return tftypes.NewValue(
SubscriptionResourceV1.AttributeTypes[key],
[]tftypes.Value{},
)
}

Check warning on line 93 in internal/resources/subscription/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/utils.go#L89-L93

Added lines #L89 - L93 were not covered by tests

if state[key].IsKnown() {
newVal := map[string]tftypes.Value{}
val := state[key]
err := val.As(&newVal)
if err != nil {
panic(err)

Check warning on line 100 in internal/resources/subscription/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/utils.go#L100

Added line #L100 was not covered by tests
}

//Add additional fields to make the older versions compatible with new fields
newVal["bootstrap_server"] = tftypes.NewValue(tftypes.String, nil)
newVal["api_key"] = tftypes.NewValue(tftypes.String, nil)
newVal["api_secret"] = tftypes.NewValue(tftypes.String, nil)
newVal["acks"] = tftypes.NewValue(tftypes.String, nil)
newVal["key"] = tftypes.NewValue(tftypes.String, nil)

val = tftypes.NewValue(destinationType, newVal)

return tftypes.NewValue(
SubscriptionResourceV1.AttributeTypes[key],
[]tftypes.Value{val},
)
}
return state[key]

Check warning on line 117 in internal/resources/subscription/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/utils.go#L117

Added line #L117 was not covered by tests
}

0 comments on commit 90f7292

Please sign in to comment.