Skip to content

Commit

Permalink
Fix bug in Pub/Sub Subscription avroConfig which was not sending empt…
Browse files Browse the repository at this point in the history
…y configurations. (#12108) (#8717)

[upstream:e5c4adb8ea7a58fcdb6a61b800c0e07b022934fa]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Nov 18, 2024
1 parent 9fd4fc2 commit 5bb3168
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/12108.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
pubsub: fixed the issue to allow successfully configure empty `cloud_storage_config.avro_config` field in `google_pubsub_subscription`
```
16 changes: 9 additions & 7 deletions google-beta/services/pubsub/resource_pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,6 @@ func flattenPubsubSubscriptionCloudStorageConfigAvroConfig(v interface{}, d *sch
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["write_metadata"] =
flattenPubsubSubscriptionCloudStorageConfigAvroConfigWriteMetadata(original["writeMetadata"], d, config)
Expand Down Expand Up @@ -1648,7 +1645,7 @@ func expandPubsubSubscriptionCloudStorageConfig(v interface{}, d tpgresource.Ter
transformedAvroConfig, err := expandPubsubSubscriptionCloudStorageConfigAvroConfig(original["avro_config"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAvroConfig); val.IsValid() && !tpgresource.IsEmptyValue(val) {
} else {
transformed["avroConfig"] = transformedAvroConfig
}

Expand Down Expand Up @@ -1696,24 +1693,29 @@ func expandPubsubSubscriptionCloudStorageConfigState(v interface{}, d tpgresourc

func expandPubsubSubscriptionCloudStorageConfigAvroConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
if len(l) == 0 {
return nil, nil
}

if l[0] == nil {
transformed := make(map[string]interface{})
return transformed, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedWriteMetadata, err := expandPubsubSubscriptionCloudStorageConfigAvroConfigWriteMetadata(original["write_metadata"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedWriteMetadata); val.IsValid() && !tpgresource.IsEmptyValue(val) {
} else {
transformed["writeMetadata"] = transformedWriteMetadata
}

transformedUseTopicSchema, err := expandPubsubSubscriptionCloudStorageConfigAvroConfigUseTopicSchema(original["use_topic_schema"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedUseTopicSchema); val.IsValid() && !tpgresource.IsEmptyValue(val) {
} else {
transformed["useTopicSchema"] = transformedUseTopicSchema
}

Expand Down
27 changes: 27 additions & 0 deletions google-beta/services/pubsub/resource_pubsub_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,31 @@ func TestAccPubsubSubscriptionCloudStorage_updateAvro(t *testing.T) {
})
}

func TestAccPubsubSubscriptionCloudStorage_emptyAvroConfig(t *testing.T) {
t.Parallel()

bucket := fmt.Sprintf("tf-test-bucket-%s", acctest.RandString(t, 10))
topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(t, 10))
subscriptionShort := fmt.Sprintf("tf-test-sub-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccPubsubSubscriptionCloudStorage_basic(bucket, topic, subscriptionShort, "pre-", "-suffix", "YYYY-MM-DD/hh_mm_ssZ", 1000, "300s", 1000, "", "empty-avro"),
},
{
ResourceName: "google_pubsub_subscription.foo",
ImportStateId: subscriptionShort,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccPubsubSubscriptionCloudStorage_serviceAccount(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -825,6 +850,8 @@ resource "google_storage_bucket_iam_member" "admin" {
use_topic_schema = true
}
`
} else if outputFormat == "empty-avro" {
outputFormatString = `avro_config {}`
}
return fmt.Sprintf(`
data "google_project" "project" { }
Expand Down

0 comments on commit 5bb3168

Please sign in to comment.