Skip to content

Commit

Permalink
Merge pull request #472 from labd/373-add-confluent-cloud-as-subscrip…
Browse files Browse the repository at this point in the history
…tion-destination

feat(subscription): Added support for confluence cloud
  • Loading branch information
demeyerthom authored Jan 26, 2024
2 parents 9620839 + 776698c commit 0a5d9ad
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 122 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20240119-152201.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: Added support for confluence cloud subscriptions
time: 2024-01-19T15:22:01.736608086+01:00
27 changes: 16 additions & 11 deletions docs/resources/subscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,25 @@ Required:

Required:

- `type` (String)
- `type` (String) The type of the destination. See [Destination](https://docs.commercetools.com/api/projects/subscriptions#destination) for more information

Optional:

- `access_key` (String, Sensitive)
- `access_secret` (String, Sensitive)
- `account_id` (String)
- `connection_string` (String)
- `project_id` (String)
- `queue_url` (String)
- `region` (String)
- `topic` (String)
- `topic_arn` (String)
- `uri` (String)
- `access_key` (String, Sensitive) The access key of the SQS queue, SNS topic or EventBridge topic
- `access_secret` (String, Sensitive) The access secret of the SQS queue, SNS topic or EventBridge topic
- `account_id` (String) The AWS account ID of the SNS topic or EventBridge topic
- `acks` (String) The acks value of the Confluent Cloud topic
- `api_key` (String) The API key of the Confluent Cloud topic
- `api_secret` (String) The API secret of the Confluent Cloud topic
- `bootstrap_server` (String) The bootstrap server of the Confluent Cloud topic
- `connection_string` (String) The connection string of the Azure Service Bus
- `key` (String) The key of the Confluent Cloud topic
- `project_id` (String) The project ID of the Google Cloud Pub/Sub
- `queue_url` (String) The URL of the SQS queue
- `region` (String) The region of the SQS queue, SNS topic or EventBridge topic
- `topic` (String) The topic of the Google Cloud Pub/Sub or Confluent Cloud topic
- `topic_arn` (String) The ARN of the SNS topic
- `uri` (String) The URI of the EventGrid topic


<a id="nestedblock--format"></a>
Expand Down
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
56 changes: 44 additions & 12 deletions internal/resources/subscription/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
GoogleCloudPubSub = "GoogleCloudPubSub"
SNS = "SNS"
SQS = "SQS"
ConfluentCloud = "ConfluentCloud"
)

// Subscription is the main resource schema data
Expand Down Expand Up @@ -157,32 +158,38 @@ func (s *Subscription) updateActions(plan Subscription) platform.SubscriptionUpd
}

type Destination struct {
Type types.String `tfsdk:"type"`
TopicARN types.String `tfsdk:"topic_arn"`

// SNS, SQS, EventGrid
AccessKey types.String `tfsdk:"access_key"`
Type types.String `tfsdk:"type"`

// SNS, SQS, EventGrid
AccessKey types.String `tfsdk:"access_key"`
AccessSecret types.String `tfsdk:"access_secret"`
Region types.String `tfsdk:"region"`

// SQS
QueueURL types.String `tfsdk:"queue_url"`
// SNS
TopicARN types.String `tfsdk:"topic_arn"`

// SQS
QueueURL types.String `tfsdk:"queue_url"`
AccountID types.String `tfsdk:"account_id"`

// SQS, SNS
Region types.String `tfsdk:"region"`

// EventGrid
URI types.String `tfsdk:"uri"`

// AzureServiceBus
ConnectionString types.String `tfsdk:"connection_string"`

// For GooglePubSub
// GooglePubSub
ProjectID types.String `tfsdk:"project_id"`
Topic types.String `tfsdk:"topic"`

// GooglePubSub, ConfluentCloud
Topic types.String `tfsdk:"topic"`

// For ConfluentCloud
BootstrapServer types.String `tfsdk:"bootstrap_server"`
ApiKey types.String `tfsdk:"api_key"`
ApiSecret types.String `tfsdk:"api_secret"`
Acks types.String `tfsdk:"acks"`
Key types.String `tfsdk:"key"`
}

func (d *Destination) setSecretValues(state *Destination) {
Expand All @@ -209,6 +216,13 @@ func (d *Destination) setSecretValues(state *Destination) {
if d.AccessSecret.IsNull() {
d.AccessSecret = state.AccessSecret
}
case ConfluentCloud:
if d.ApiKey.IsUnknown() {
d.ApiKey = state.ApiKey
}
if d.ApiSecret.IsUnknown() {
d.ApiSecret = state.ApiSecret
}
}
}

Expand Down Expand Up @@ -241,6 +255,14 @@ func NewDestinationFromNative(n platform.Destination) *Destination {
d.Region = types.StringValue(v.Region)
d.AccessKey = utils.FromOptionalString(v.AccessKey)
d.AccessSecret = types.StringNull()
case platform.ConfluentCloudDestination:
d.Type = types.StringValue(ConfluentCloud)
d.BootstrapServer = types.StringValue(v.BootstrapServer)
d.ApiKey = types.StringUnknown()
d.ApiSecret = types.StringUnknown()
d.Acks = types.StringValue(v.Acks)
d.Topic = types.StringValue(v.Topic)
d.Key = utils.FromOptionalString(v.Key)
}
return d
}
Expand Down Expand Up @@ -291,6 +313,16 @@ func (d *Destination) ToNative() platform.Destination {
result.AuthenticationMode = &authMode
}
return result
case ConfluentCloud:
result := platform.ConfluentCloudDestination{
BootstrapServer: d.BootstrapServer.ValueString(),
ApiKey: d.ApiKey.ValueString(),
ApiSecret: d.ApiSecret.ValueString(),
Acks: d.Acks.ValueString(),
Topic: d.Topic.ValueString(),
Key: utils.OptionalString(d.Key),
}
return result
}
return nil
}
Expand Down
19 changes: 19 additions & 0 deletions internal/resources/subscription/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ func TestImport(t *testing.T) {
AccessSecret: types.StringValue("secret"),
},
},
{
name: "ConfluentCloudDestination",
n: platform.ConfluentCloudDestination{
BootstrapServer: "test-bootstrap-server",
ApiKey: "test-api-key",
ApiSecret: "test-api-secret",
Acks: "test-acks",
Topic: "test-topic",
},
state: nil,
wantDest: Destination{
Type: types.StringValue(ConfluentCloud),
BootstrapServer: types.StringValue("test-bootstrap-server"),
ApiKey: types.StringUnknown(),
ApiSecret: types.StringUnknown(),
Acks: types.StringValue("test-acks"),
Topic: types.StringValue("test-topic"),
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 0a5d9ad

Please sign in to comment.