Skip to content

Commit

Permalink
Update models for API return value inconsistencies and finish test as…
Browse files Browse the repository at this point in the history
…sertions
  • Loading branch information
thogarty committed Feb 20, 2025
1 parent 4eab6f7 commit 214d7f3
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
package streamsubscription_test

// Tested in resource_test.go because of the heavy resource setup constraints
98 changes: 53 additions & 45 deletions internal/resources/fabric/stream_subscription/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ func (m *BaseStreamSubscriptionModel) parse(ctx context.Context, streamSubscript
}
m.EventSelector = eventSelectorObject

planSinkModel := SinkModel{}
if !m.Sink.IsNull() && !m.Sink.IsUnknown() {
diags = m.Sink.As(ctx, &planSinkModel, basetypes.ObjectAsOptions{})
if diags.HasError() {
mDiags.Append(diags...)
return mDiags
}
}

// Parse Sink
streamSubSink := streamSubscription.GetSink()
sinkModel := SinkModel{
Expand All @@ -260,55 +269,42 @@ func (m *BaseStreamSubscriptionModel) parse(ctx context.Context, streamSubscript
Host: types.StringValue(streamSubSink.GetHost()),
}

if streamSubSink.Credential != nil {
sinkCredential := streamSubSink.GetCredential()
credentialModel := SinkCredentialModel{
Type: types.StringValue(string(sinkCredential.GetType())),
AccessToken: types.StringValue(sinkCredential.GetAccessToken()),
IntegrationKey: types.StringValue(sinkCredential.GetIntegrationKey()),
APIKey: types.StringValue(sinkCredential.GetApiKey()),
Username: types.StringValue(sinkCredential.GetUsername()),
Password: types.StringValue(sinkCredential.GetPassword()),
}
if planSinkModel.URI.ValueString() != "" {
sinkModel.URI = types.StringValue(planSinkModel.URI.ValueString())
}

if !m.Sink.IsNull() && !m.Sink.IsUnknown() {
planSinkModel := SinkModel{}
diags = m.Sink.As(ctx, &planSinkModel, basetypes.ObjectAsOptions{})
if diags.HasError() {
mDiags.Append(diags...)
return mDiags
}
planCredentialModel := SinkCredentialModel{}
diags = planSinkModel.Credential.As(ctx, &planCredentialModel, basetypes.ObjectAsOptions{})
if diags.HasError() {
mDiags.Append(diags...)
return mDiags
}
switch fabricv4.StreamSubscriptionSinkCredentialType(planCredentialModel.Type.ValueString()) {
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_ACCESS_TOKEN:
credentialModel.AccessToken = types.StringValue(planCredentialModel.AccessToken.ValueString())
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_API_KEY:
credentialModel.APIKey = types.StringValue(planCredentialModel.APIKey.ValueString())
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_INTEGRATION_KEY:
credentialModel.IntegrationKey = types.StringValue(planCredentialModel.IntegrationKey.ValueString())
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_USERNAME_PASSWORD:
credentialModel.Username = types.StringValue(planCredentialModel.Username.ValueString())
credentialModel.Password = types.StringValue(planCredentialModel.Password.ValueString())
}
}
sinkCredential := streamSubSink.GetCredential()
credentialModel := SinkCredentialModel{
Type: types.StringValue(string(sinkCredential.GetType())),
AccessToken: types.StringValue(sinkCredential.GetAccessToken()),
IntegrationKey: types.StringValue(sinkCredential.GetIntegrationKey()),
APIKey: types.StringValue(sinkCredential.GetApiKey()),
Username: types.StringValue(sinkCredential.GetUsername()),
Password: types.StringValue(sinkCredential.GetPassword()),
}

sinkModel.Credential = fwtypes.NewObjectValueOf[SinkCredentialModel](ctx, &credentialModel)
} else {
sinkModel.Credential = fwtypes.NewObjectValueOf[SinkCredentialModel](ctx, &SinkCredentialModel{
Type: types.StringValue(""),
AccessToken: types.StringValue(""),
IntegrationKey: types.StringValue(""),
APIKey: types.StringValue(""),
Username: types.StringValue(""),
Password: types.StringValue(""),
})
if !planSinkModel.Credential.IsNull() && !planSinkModel.Credential.IsUnknown() {
planCredentialModel := SinkCredentialModel{}
diags = planSinkModel.Credential.As(ctx, &planCredentialModel, basetypes.ObjectAsOptions{})
if diags.HasError() {
mDiags.Append(diags...)
return mDiags
}
switch fabricv4.StreamSubscriptionSinkCredentialType(planCredentialModel.Type.ValueString()) {
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_ACCESS_TOKEN:
credentialModel.AccessToken = types.StringValue(planCredentialModel.AccessToken.ValueString())
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_API_KEY:
credentialModel.APIKey = types.StringValue(planCredentialModel.APIKey.ValueString())
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_INTEGRATION_KEY:
credentialModel.IntegrationKey = types.StringValue(planCredentialModel.IntegrationKey.ValueString())
case fabricv4.STREAMSUBSCRIPTIONSINKCREDENTIALTYPE_USERNAME_PASSWORD:
credentialModel.Username = types.StringValue(planCredentialModel.Username.ValueString())
credentialModel.Password = types.StringValue(planCredentialModel.Password.ValueString())
}
}

sinkModel.Credential = fwtypes.NewObjectValueOf[SinkCredentialModel](ctx, &credentialModel)

sinkSettings := streamSubSink.GetSettings()
sinkSettingsModel := SinkSettingsModel{
EventIndex: types.StringValue(sinkSettings.GetEventIndex()),
Expand All @@ -320,6 +316,18 @@ func (m *BaseStreamSubscriptionModel) parse(ctx context.Context, streamSubscript
TransformAlerts: types.BoolValue(sinkSettings.GetTransformAlerts()),
}

if !planSinkModel.Settings.IsNull() && !planSinkModel.Settings.IsUnknown() {
planSettingsModel := SinkSettingsModel{}
diags = planSinkModel.Settings.As(ctx, &planSettingsModel, basetypes.ObjectAsOptions{})
if diags.HasError() {
mDiags.Append(diags...)
return mDiags
}
if planSettingsModel.ApplicationKey.ValueString() != "" {
sinkSettingsModel.ApplicationKey = types.StringValue(planSettingsModel.ApplicationKey.ValueString())
}
}

sinkModel.Settings = fwtypes.NewObjectValueOf[SinkSettingsModel](ctx, &sinkSettingsModel)

m.Sink = fwtypes.NewObjectValueOf[SinkModel](ctx, &sinkModel)
Expand Down
65 changes: 54 additions & 11 deletions internal/resources/fabric/stream_subscription/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func testAccFabricStreamSubscriptionConfig(streamTestData map[string]map[string]
enabled = false
sink = {
type = "SLACK"
uri = "https://hooks.slack.com/services/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"
uri = "%s"
}
}
Expand Down Expand Up @@ -133,12 +133,33 @@ func testAccFabricStreamSubscriptionConfig(streamTestData map[string]map[string]
uri = "%s"
}
}
data "equinix_fabric_stream_subscription" "by_ids" {
stream_id = equinix_fabric_stream.new_stream.id
subscription_id = equinix_fabric_stream_subscription.splunk.id
}
data "equinix_fabric_stream_subscriptions" "all" {
depends_on = [
equinix_fabric_stream_subscription.splunk,
equinix_fabric_stream_subscription.slack,
equinix_fabric_stream_subscription.pager_duty,
equinix_fabric_stream_subscription.datadog,
equinix_fabric_stream_subscription.msteams
]
stream_id = equinix_fabric_stream.new_stream.id
pagination = {
limit = 20
offset = 0
}
}
`,
streamTestData["splunk"]["uri"],
streamTestData["splunk"]["event_index"],
streamTestData["splunk"]["metric_index"],
streamTestData["splunk"]["source"],
streamTestData["splunk"]["accessToken"],
streamTestData["slack"]["uri"],
streamTestData["pagerDuty"]["host"],
streamTestData["pagerDuty"]["change_uri"],
streamTestData["pagerDuty"]["alert_uri"],
Expand All @@ -164,19 +185,41 @@ func TestAccFabricStreamSubscription_PFCR(t *testing.T) {
Config: testAccFabricStreamSubscriptionConfig(streamTestData),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"equinix_fabric_stream.new_stream", "name", "Subscription_Test_PFCR"),
"equinix_fabric_stream_subscription.splunk", "name", "Splunk_PFCR"),
resource.TestCheckResourceAttr(
"equinix_fabric_stream_subscription.splunk", "type", "STREAM_SUBSCRIPTION"),
resource.TestCheckResourceAttr(
"equinix_fabric_stream_subscription.splunk", "description", "Stream Subscription Splunk TF Testing"),
resource.TestCheckResourceAttr("equinix_fabric_stream_subscription.splunk", "sink.type", "SPLUNK_HEC"),
resource.TestCheckResourceAttr("equinix_fabric_stream_subscription.splunk", "sink.credential.type", "ACCESS_TOKEN"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream_subscription.splunk", "stream_id"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream_subscription.splunk", "sink.uri"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream_subscription.splunk", "sink.credential.access_token"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream_subscription.splunk", "sink.settings.event_index"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream_subscription.splunk", "sink.settings.metric_index"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream_subscription.splunk", "sink.settings.source"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream_subscription.splunk", "uuid"),
resource.TestCheckResourceAttr(
"equinix_fabric_stream.new_stream", "type", "TELEMETRY_STREAM"),
"data.equinix_fabric_stream_subscription.by_ids", "name", "Splunk_PFCR"),
resource.TestCheckResourceAttr(
"equinix_fabric_stream.new_stream", "project.project_id", "291639000636552"),
"data.equinix_fabric_stream_subscription.by_ids", "type", "STREAM_SUBSCRIPTION"),
resource.TestCheckResourceAttr(
"equinix_fabric_stream.new_stream", "description", "Testing stream subscriptions resource"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream.new_stream", "id"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream.new_stream", "href"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream.new_stream", "assets_count"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream.new_stream", "stream_subscriptions_count"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream.new_stream", "uuid"),
resource.TestCheckResourceAttrSet("equinix_fabric_stream.new_stream", "change_log.created_by"),
"data.equinix_fabric_stream_subscription.by_ids", "description", "Stream Subscription Splunk TF Testing"),
resource.TestCheckResourceAttr("data.equinix_fabric_stream_subscription.by_ids", "sink.type", "SPLUNK_HEC"),
resource.TestCheckResourceAttr("data.equinix_fabric_stream_subscription.by_ids", "sink.credential.type", "ACCESS_TOKEN"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscription.by_ids", "stream_id"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscription.by_ids", "sink.uri"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscription.by_ids", "sink.credential.access_token"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscription.by_ids", "sink.settings.event_index"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscription.by_ids", "sink.settings.metric_index"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscription.by_ids", "sink.settings.source"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscription.by_ids", "uuid"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscriptions.all", "data.0.name"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscriptions.all", "data.0.type"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscriptions.all", "data.0.description"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscriptions.all", "data.0.sink.type"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscriptions.all", "data.0.stream_id"),
resource.TestCheckResourceAttrSet("data.equinix_fabric_stream_subscriptions.all", "data.0.uuid"),
),
},
},
Expand Down

0 comments on commit 214d7f3

Please sign in to comment.