Skip to content

Commit 6fee172

Browse files
committed
Add support for cross bucket replication feature
1 parent b1a07f0 commit 6fee172

File tree

2 files changed

+293
-7
lines changed

2 files changed

+293
-7
lines changed

mmv1/third_party/terraform/services/storagetransfer/resource_storage_transfer_job.go.tmpl

+117-7
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,35 @@ import (
1919
)
2020

2121
var (
22-
objectConditionsKeys = []string{
22+
transferSpecObjectConditionsKeys = []string{
2323
"transfer_spec.0.object_conditions.0.min_time_elapsed_since_last_modification",
2424
"transfer_spec.0.object_conditions.0.max_time_elapsed_since_last_modification",
2525
"transfer_spec.0.object_conditions.0.include_prefixes",
2626
"transfer_spec.0.object_conditions.0.exclude_prefixes",
2727
"transfer_spec.0.object_conditions.0.last_modified_since",
2828
"transfer_spec.0.object_conditions.0.last_modified_before",
2929
}
30+
replicationSpecObjectConditionsKeys = []string{
31+
"replication_spec.0.object_conditions.0.min_time_elapsed_since_last_modification",
32+
"replication_spec.0.object_conditions.0.max_time_elapsed_since_last_modification",
33+
"replication_spec.0.object_conditions.0.include_prefixes",
34+
"replication_spec.0.object_conditions.0.exclude_prefixes",
35+
"replication_spec.0.object_conditions.0.last_modified_since",
36+
"replication_spec.0.object_conditions.0.last_modified_before",
37+
}
3038

31-
transferOptionsKeys = []string{
39+
transferSpecTransferOptionsKeys = []string{
3240
"transfer_spec.0.transfer_options.0.overwrite_objects_already_existing_in_sink",
3341
"transfer_spec.0.transfer_options.0.delete_objects_unique_in_sink",
3442
"transfer_spec.0.transfer_options.0.delete_objects_from_source_after_transfer",
3543
"transfer_spec.0.transfer_options.0.overwrite_when",
3644
}
45+
replicationSpecTransferOptionsKeys = []string{
46+
"replication_spec.0.transfer_options.0.overwrite_objects_already_existing_in_sink",
47+
"replication_spec.0.transfer_options.0.delete_objects_unique_in_sink",
48+
"replication_spec.0.transfer_options.0.delete_objects_from_source_after_transfer",
49+
"replication_spec.0.transfer_options.0.overwrite_when",
50+
}
3751

3852
transferSpecDataSourceKeys = []string{
3953
"transfer_spec.0.gcs_data_source",
@@ -47,6 +61,14 @@ var (
4761
"transfer_spec.0.gcs_data_sink",
4862
"transfer_spec.0.posix_data_sink",
4963
}
64+
65+
replicationSpecDataSourceKeys = []string{
66+
"replication_spec.0.gcs_data_source",
67+
}
68+
replicationSpecDataSinkKeys = []string{
69+
"replication_spec.0.gcs_data_sink",
70+
}
71+
5072
awsS3AuthKeys = []string{
5173
"transfer_spec.0.aws_s3_data_source.0.aws_access_key",
5274
"transfer_spec.0.aws_s3_data_source.0.role_arn",
@@ -98,6 +120,7 @@ func ResourceStorageTransferJob() *schema.Resource {
98120
Optional: true,
99121
MaxItems: 1,
100122
ConflictsWith: []string{"schedule"},
123+
DiffSuppressFunc: diffSuppressEventStream,
101124
Elem: &schema.Resource{
102125
Schema: map[string]*schema.Schema{
103126
"name": {
@@ -120,14 +143,44 @@ func ResourceStorageTransferJob() *schema.Resource {
120143
},
121144
},
122145
},
146+
"replication_spec": {
147+
Type: schema.TypeList,
148+
MaxItems: 1,
149+
Optional: true,
150+
ConflictsWith: []string{"transfer_spec", "schedule"},
151+
ExactlyOneOf: []string{"transfer_spec", "replication_spec"},
152+
Elem: &schema.Resource{
153+
Schema: map[string]*schema.Schema{
154+
"object_conditions": objectConditionsSchema(replicationSpecObjectConditionsKeys),
155+
"transfer_options": transferOptionsSchema(replicationSpecTransferOptionsKeys),
156+
"gcs_data_sink": {
157+
Type: schema.TypeList,
158+
Optional: true,
159+
MaxItems: 1,
160+
Elem: gcsDataSchema(),
161+
ExactlyOneOf: replicationSpecDataSinkKeys,
162+
Description: `A Google Cloud Storage data sink.`,
163+
},
164+
"gcs_data_source": {
165+
Type: schema.TypeList,
166+
Optional: true,
167+
MaxItems: 1,
168+
Elem: gcsDataSchema(),
169+
ExactlyOneOf: replicationSpecDataSourceKeys,
170+
Description: `A Google Cloud Storage data source.`,
171+
},
172+
},
173+
},
174+
Description: `Replication specification.`,
175+
},
123176
"transfer_spec": {
124177
Type: schema.TypeList,
125-
Required: true,
178+
Optional: true,
126179
MaxItems: 1,
127180
Elem: &schema.Resource{
128181
Schema: map[string]*schema.Schema{
129-
"object_conditions": objectConditionsSchema(),
130-
"transfer_options": transferOptionsSchema(),
182+
"object_conditions": objectConditionsSchema(transferSpecObjectConditionsKeys),
183+
"transfer_options": transferOptionsSchema(transferSpecTransferOptionsKeys),
131184
"source_agent_pool_name": {
132185
Type: schema.TypeString,
133186
Optional: true,
@@ -307,7 +360,7 @@ func ResourceStorageTransferJob() *schema.Resource {
307360
}
308361
}
309362

310-
func objectConditionsSchema() *schema.Schema {
363+
func objectConditionsSchema(objectConditionsKeys []string) *schema.Schema {
311364
return &schema.Schema{
312365
Type: schema.TypeList,
313366
Optional: true,
@@ -368,7 +421,7 @@ func objectConditionsSchema() *schema.Schema {
368421
}
369422
}
370423

371-
func transferOptionsSchema() *schema.Schema {
424+
func transferOptionsSchema(transferOptionsKeys []string) *schema.Schema {
372425
return &schema.Schema{
373426
Type: schema.TypeList,
374427
Optional: true,
@@ -642,6 +695,7 @@ func resourceStorageTransferJobCreate(d *schema.ResourceData, meta interface{})
642695
Schedule: expandTransferSchedules(d.Get("schedule").([]interface{})),
643696
EventStream: expandEventStream(d.Get("event_stream").([]interface{})),
644697
TransferSpec: expandTransferSpecs(d.Get("transfer_spec").([]interface{})),
698+
ReplicationSpec: expandReplicationSpecs(d.Get("replication_spec").([]interface{})),
645699
NotificationConfig: expandTransferJobNotificationConfig(d.Get("notification_config").([]interface{})),
646700
}
647701

@@ -726,6 +780,11 @@ func resourceStorageTransferJobRead(d *schema.ResourceData, meta interface{}) er
726780
return err
727781
}
728782

783+
err = d.Set("replication_spec", flattenReplicationSpec(res.ReplicationSpec))
784+
if err != nil {
785+
return err
786+
}
787+
729788
err = d.Set("notification_config", flattenTransferJobNotificationConfig(res.NotificationConfig))
730789
if err != nil {
731790
return err
@@ -784,6 +843,13 @@ func resourceStorageTransferJobUpdate(d *schema.ResourceData, meta interface{})
784843
}
785844
}
786845

846+
if d.HasChange("replication_spec") {
847+
fieldMask = append(fieldMask, "replication_spec")
848+
if v, ok := d.GetOk("replication_spec"); ok {
849+
transferJob.ReplicationSpec = expandReplicationSpecs(v.([]interface{}))
850+
}
851+
}
852+
787853
if d.HasChange("notification_config") {
788854
fieldMask = append(fieldMask, "notification_config")
789855
if v, ok := d.GetOk("notification_config"); ok {
@@ -1277,6 +1343,9 @@ func expandTransferSpecs(transferSpecs []interface{}) *storagetransfer.TransferS
12771343
}
12781344

12791345
func flattenTransferSpec(transferSpec *storagetransfer.TransferSpec, d *schema.ResourceData) []map[string]interface{} {
1346+
if transferSpec == nil || reflect.DeepEqual(transferSpec, &storagetransfer.TransferSpec{}) {
1347+
return nil
1348+
}
12801349

12811350
data := map[string]interface{}{}
12821351

@@ -1354,3 +1423,44 @@ func flattenTransferJobNotificationConfig(notificationConfig *storagetransfer.No
13541423

13551424
return []map[string]interface{}{data}
13561425
}
1426+
1427+
func diffSuppressEventStream(k, old, new string, d *schema.ResourceData) bool {
1428+
// Check if it's a replication job.
1429+
_, is_replication := d.GetOk("replication_spec")
1430+
return is_replication
1431+
}
1432+
1433+
func expandReplicationSpecs(replicationSpecs []interface{}) *storagetransfer.ReplicationSpec {
1434+
if len(replicationSpecs) == 0 || replicationSpecs[0] == nil {
1435+
return nil
1436+
}
1437+
1438+
replicationSpec := replicationSpecs[0].(map[string]interface{})
1439+
return &storagetransfer.ReplicationSpec{
1440+
GcsDataSink: expandGcsData(replicationSpec["gcs_data_sink"].([]interface{})),
1441+
ObjectConditions: expandObjectConditions(replicationSpec["object_conditions"].([]interface{})),
1442+
TransferOptions: expandTransferOptions(replicationSpec["transfer_options"].([]interface{})),
1443+
GcsDataSource: expandGcsData(replicationSpec["gcs_data_source"].([]interface{})),
1444+
}
1445+
}
1446+
1447+
func flattenReplicationSpec(replicationSpec *storagetransfer.ReplicationSpec) []map[string]interface{} {
1448+
if replicationSpec == nil || reflect.DeepEqual(replicationSpec, &storagetransfer.ReplicationSpec{}) {
1449+
return nil
1450+
}
1451+
1452+
data := map[string]interface{}{}
1453+
if replicationSpec.GcsDataSink != nil {
1454+
data["gcs_data_sink"] = flattenGcsData(replicationSpec.GcsDataSink)
1455+
}
1456+
if replicationSpec.GcsDataSource != nil {
1457+
data["gcs_data_source"] = flattenGcsData(replicationSpec.GcsDataSource)
1458+
}
1459+
if replicationSpec.ObjectConditions != nil {
1460+
data["object_conditions"] = flattenObjectCondition(replicationSpec.ObjectConditions)
1461+
}
1462+
if replicationSpec.TransferOptions != nil {
1463+
data["transfer_options"] = flattenTransferOption(replicationSpec.TransferOptions)
1464+
}
1465+
return []map[string]interface{}{data}
1466+
}

0 commit comments

Comments
 (0)