Skip to content

Commit 9c9f5b7

Browse files
Add support for cross bucket replication feature (#12562) (#20788)
[upstream:bcbb0985ec7afaee4687d61e685cf7e92ba30cdc] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 9e4e7bb commit 9c9f5b7

File tree

4 files changed

+372
-14
lines changed

4 files changed

+372
-14
lines changed

.changelog/12562.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
storagetransfer: add support for `replication_spec` fields to `google_storage_transfer_job` resource
3+
```

google/services/storagetransfer/resource_storage_transfer_job.go

Lines changed: 125 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,35 @@ import (
2121
)
2222

2323
var (
24-
objectConditionsKeys = []string{
24+
transferSpecObjectConditionsKeys = []string{
2525
"transfer_spec.0.object_conditions.0.min_time_elapsed_since_last_modification",
2626
"transfer_spec.0.object_conditions.0.max_time_elapsed_since_last_modification",
2727
"transfer_spec.0.object_conditions.0.include_prefixes",
2828
"transfer_spec.0.object_conditions.0.exclude_prefixes",
2929
"transfer_spec.0.object_conditions.0.last_modified_since",
3030
"transfer_spec.0.object_conditions.0.last_modified_before",
3131
}
32+
replicationSpecObjectConditionsKeys = []string{
33+
"replication_spec.0.object_conditions.0.min_time_elapsed_since_last_modification",
34+
"replication_spec.0.object_conditions.0.max_time_elapsed_since_last_modification",
35+
"replication_spec.0.object_conditions.0.include_prefixes",
36+
"replication_spec.0.object_conditions.0.exclude_prefixes",
37+
"replication_spec.0.object_conditions.0.last_modified_since",
38+
"replication_spec.0.object_conditions.0.last_modified_before",
39+
}
3240

33-
transferOptionsKeys = []string{
41+
transferSpecTransferOptionsKeys = []string{
3442
"transfer_spec.0.transfer_options.0.overwrite_objects_already_existing_in_sink",
3543
"transfer_spec.0.transfer_options.0.delete_objects_unique_in_sink",
3644
"transfer_spec.0.transfer_options.0.delete_objects_from_source_after_transfer",
3745
"transfer_spec.0.transfer_options.0.overwrite_when",
3846
}
47+
replicationSpecTransferOptionsKeys = []string{
48+
"replication_spec.0.transfer_options.0.overwrite_objects_already_existing_in_sink",
49+
"replication_spec.0.transfer_options.0.delete_objects_unique_in_sink",
50+
"replication_spec.0.transfer_options.0.delete_objects_from_source_after_transfer",
51+
"replication_spec.0.transfer_options.0.overwrite_when",
52+
}
3953

4054
transferSpecDataSourceKeys = []string{
4155
"transfer_spec.0.gcs_data_source",
@@ -49,6 +63,14 @@ var (
4963
"transfer_spec.0.gcs_data_sink",
5064
"transfer_spec.0.posix_data_sink",
5165
}
66+
67+
replicationSpecDataSourceKeys = []string{
68+
"replication_spec.0.gcs_data_source",
69+
}
70+
replicationSpecDataSinkKeys = []string{
71+
"replication_spec.0.gcs_data_sink",
72+
}
73+
5274
awsS3AuthKeys = []string{
5375
"transfer_spec.0.aws_s3_data_source.0.aws_access_key",
5476
"transfer_spec.0.aws_s3_data_source.0.role_arn",
@@ -90,10 +112,11 @@ func ResourceStorageTransferJob() *schema.Resource {
90112
Description: `The project in which the resource belongs. If it is not provided, the provider project is used.`,
91113
},
92114
"event_stream": {
93-
Type: schema.TypeList,
94-
Optional: true,
95-
MaxItems: 1,
96-
ConflictsWith: []string{"schedule"},
115+
Type: schema.TypeList,
116+
Optional: true,
117+
MaxItems: 1,
118+
ConflictsWith: []string{"schedule"},
119+
DiffSuppressFunc: diffSuppressEventStream,
97120
Elem: &schema.Resource{
98121
Schema: map[string]*schema.Schema{
99122
"name": {
@@ -116,14 +139,46 @@ func ResourceStorageTransferJob() *schema.Resource {
116139
},
117140
},
118141
},
142+
"replication_spec": {
143+
Type: schema.TypeList,
144+
MaxItems: 1,
145+
Optional: true,
146+
ConflictsWith: []string{"transfer_spec", "schedule"},
147+
ExactlyOneOf: []string{"transfer_spec", "replication_spec"},
148+
Elem: &schema.Resource{
149+
Schema: map[string]*schema.Schema{
150+
"object_conditions": objectConditionsSchema(replicationSpecObjectConditionsKeys),
151+
"transfer_options": transferOptionsSchema(replicationSpecTransferOptionsKeys),
152+
"gcs_data_sink": {
153+
Type: schema.TypeList,
154+
Optional: true,
155+
MaxItems: 1,
156+
Elem: gcsDataSchema(),
157+
ExactlyOneOf: replicationSpecDataSinkKeys,
158+
Description: `A Google Cloud Storage data sink.`,
159+
},
160+
"gcs_data_source": {
161+
Type: schema.TypeList,
162+
Optional: true,
163+
MaxItems: 1,
164+
Elem: gcsDataSchema(),
165+
ExactlyOneOf: replicationSpecDataSourceKeys,
166+
Description: `A Google Cloud Storage data source.`,
167+
},
168+
},
169+
},
170+
Description: `Replication specification.`,
171+
},
119172
"transfer_spec": {
120-
Type: schema.TypeList,
121-
Required: true,
122-
MaxItems: 1,
173+
Type: schema.TypeList,
174+
Optional: true,
175+
MaxItems: 1,
176+
ConflictsWith: []string{"replication_spec"},
177+
ExactlyOneOf: []string{"transfer_spec", "replication_spec"},
123178
Elem: &schema.Resource{
124179
Schema: map[string]*schema.Schema{
125-
"object_conditions": objectConditionsSchema(),
126-
"transfer_options": transferOptionsSchema(),
180+
"object_conditions": objectConditionsSchema(transferSpecObjectConditionsKeys),
181+
"transfer_options": transferOptionsSchema(transferSpecTransferOptionsKeys),
127182
"source_agent_pool_name": {
128183
Type: schema.TypeString,
129184
Optional: true,
@@ -303,7 +358,7 @@ func ResourceStorageTransferJob() *schema.Resource {
303358
}
304359
}
305360

306-
func objectConditionsSchema() *schema.Schema {
361+
func objectConditionsSchema(objectConditionsKeys []string) *schema.Schema {
307362
return &schema.Schema{
308363
Type: schema.TypeList,
309364
Optional: true,
@@ -364,7 +419,7 @@ func objectConditionsSchema() *schema.Schema {
364419
}
365420
}
366421

367-
func transferOptionsSchema() *schema.Schema {
422+
func transferOptionsSchema(transferOptionsKeys []string) *schema.Schema {
368423
return &schema.Schema{
369424
Type: schema.TypeList,
370425
Optional: true,
@@ -625,6 +680,7 @@ func resourceStorageTransferJobCreate(d *schema.ResourceData, meta interface{})
625680
Schedule: expandTransferSchedules(d.Get("schedule").([]interface{})),
626681
EventStream: expandEventStream(d.Get("event_stream").([]interface{})),
627682
TransferSpec: expandTransferSpecs(d.Get("transfer_spec").([]interface{})),
683+
ReplicationSpec: expandReplicationSpecs(d.Get("replication_spec").([]interface{})),
628684
NotificationConfig: expandTransferJobNotificationConfig(d.Get("notification_config").([]interface{})),
629685
}
630686

@@ -709,6 +765,11 @@ func resourceStorageTransferJobRead(d *schema.ResourceData, meta interface{}) er
709765
return err
710766
}
711767

768+
err = d.Set("replication_spec", flattenReplicationSpec(res.ReplicationSpec))
769+
if err != nil {
770+
return err
771+
}
772+
712773
err = d.Set("notification_config", flattenTransferJobNotificationConfig(res.NotificationConfig))
713774
if err != nil {
714775
return err
@@ -767,6 +828,13 @@ func resourceStorageTransferJobUpdate(d *schema.ResourceData, meta interface{})
767828
}
768829
}
769830

831+
if d.HasChange("replication_spec") {
832+
fieldMask = append(fieldMask, "replication_spec")
833+
if v, ok := d.GetOk("replication_spec"); ok {
834+
transferJob.ReplicationSpec = expandReplicationSpecs(v.([]interface{}))
835+
}
836+
}
837+
770838
if d.HasChange("notification_config") {
771839
fieldMask = append(fieldMask, "notification_config")
772840
if v, ok := d.GetOk("notification_config"); ok {
@@ -1249,6 +1317,9 @@ func expandTransferSpecs(transferSpecs []interface{}) *storagetransfer.TransferS
12491317
}
12501318

12511319
func flattenTransferSpec(transferSpec *storagetransfer.TransferSpec, d *schema.ResourceData) []map[string]interface{} {
1320+
if transferSpec == nil || reflect.DeepEqual(transferSpec, &storagetransfer.TransferSpec{}) {
1321+
return nil
1322+
}
12521323

12531324
data := map[string]interface{}{}
12541325

@@ -1326,3 +1397,44 @@ func flattenTransferJobNotificationConfig(notificationConfig *storagetransfer.No
13261397

13271398
return []map[string]interface{}{data}
13281399
}
1400+
1401+
func diffSuppressEventStream(k, old, new string, d *schema.ResourceData) bool {
1402+
// Check if it's a replication job.
1403+
_, is_replication := d.GetOk("replication_spec")
1404+
return is_replication
1405+
}
1406+
1407+
func expandReplicationSpecs(replicationSpecs []interface{}) *storagetransfer.ReplicationSpec {
1408+
if len(replicationSpecs) == 0 || replicationSpecs[0] == nil {
1409+
return nil
1410+
}
1411+
1412+
replicationSpec := replicationSpecs[0].(map[string]interface{})
1413+
return &storagetransfer.ReplicationSpec{
1414+
GcsDataSink: expandGcsData(replicationSpec["gcs_data_sink"].([]interface{})),
1415+
ObjectConditions: expandObjectConditions(replicationSpec["object_conditions"].([]interface{})),
1416+
TransferOptions: expandTransferOptions(replicationSpec["transfer_options"].([]interface{})),
1417+
GcsDataSource: expandGcsData(replicationSpec["gcs_data_source"].([]interface{})),
1418+
}
1419+
}
1420+
1421+
func flattenReplicationSpec(replicationSpec *storagetransfer.ReplicationSpec) []map[string]interface{} {
1422+
if replicationSpec == nil || reflect.DeepEqual(replicationSpec, &storagetransfer.ReplicationSpec{}) {
1423+
return nil
1424+
}
1425+
1426+
data := map[string]interface{}{}
1427+
if replicationSpec.GcsDataSink != nil {
1428+
data["gcs_data_sink"] = flattenGcsData(replicationSpec.GcsDataSink)
1429+
}
1430+
if replicationSpec.GcsDataSource != nil {
1431+
data["gcs_data_source"] = flattenGcsData(replicationSpec.GcsDataSource)
1432+
}
1433+
if replicationSpec.ObjectConditions != nil {
1434+
data["object_conditions"] = flattenObjectCondition(replicationSpec.ObjectConditions)
1435+
}
1436+
if replicationSpec.TransferOptions != nil {
1437+
data["transfer_options"] = flattenTransferOption(replicationSpec.TransferOptions)
1438+
}
1439+
return []map[string]interface{}{data}
1440+
}

0 commit comments

Comments
 (0)