@@ -4,6 +4,7 @@ package provider
44
55import (
66 "context"
7+ "fmt"
78 "strconv"
89
910 "github.com/hashicorp/terraform/helper/schema"
@@ -22,37 +23,64 @@ func VolumeBackupResource() *schema.Resource {
2223 Update : updateVolumeBackup ,
2324 Delete : deleteVolumeBackup ,
2425 Schema : map [string ]* schema.Schema {
25- // Required
26+ // Optional
2627 "volume_id" : {
27- Type : schema .TypeString ,
28- Required : true ,
29- ForceNew : true ,
28+ Type : schema .TypeString ,
29+ Optional : true ,
30+ ForceNew : true ,
31+ Computed : true ,
32+ ConflictsWith : []string {"source_details" },
33+ },
34+ "source_details" : {
35+ Type : schema .TypeList ,
36+ Optional : true ,
37+ ForceNew : true ,
38+ MaxItems : 1 ,
39+ MinItems : 1 ,
40+ ConflictsWith : []string {"volume_id" },
41+ Elem : & schema.Resource {
42+ Schema : map [string ]* schema.Schema {
43+ // Required
44+ "region" : {
45+ Type : schema .TypeString ,
46+ Required : true ,
47+ ForceNew : true ,
48+ },
49+ // Required
50+ "volume_backup_id" : {
51+ Type : schema .TypeString ,
52+ Required : true ,
53+ ForceNew : true ,
54+ },
55+ },
56+ },
3057 },
31-
32- // Optional
3358 "defined_tags" : {
3459 Type : schema .TypeMap ,
3560 Optional : true ,
3661 Computed : true ,
3762 DiffSuppressFunc : definedTagsDiffSuppressFunction ,
3863 Elem : schema .TypeString ,
64+ ConflictsWith : []string {"source_details" },
3965 },
4066 "display_name" : {
4167 Type : schema .TypeString ,
4268 Optional : true ,
4369 Computed : true ,
4470 },
4571 "freeform_tags" : {
46- Type : schema .TypeMap ,
47- Optional : true ,
48- Computed : true ,
49- Elem : schema .TypeString ,
72+ Type : schema .TypeMap ,
73+ Optional : true ,
74+ Computed : true ,
75+ Elem : schema .TypeString ,
76+ ConflictsWith : []string {"source_details" },
5077 },
5178 "type" : {
52- Type : schema .TypeString ,
53- Optional : true ,
54- Computed : true ,
55- ForceNew : true ,
79+ Type : schema .TypeString ,
80+ Optional : true ,
81+ Computed : true ,
82+ ForceNew : true ,
83+ ConflictsWith : []string {"source_details" },
5684 },
5785
5886 // Computed
@@ -77,6 +105,10 @@ func VolumeBackupResource() *schema.Resource {
77105 Type : schema .TypeString ,
78106 Computed : true ,
79107 },
108+ "source_volume_backup_id" : {
109+ Type : schema .TypeString ,
110+ Computed : true ,
111+ },
80112 "state" : {
81113 Type : schema .TypeString ,
82114 Computed : true ,
@@ -138,6 +170,7 @@ func deleteVolumeBackup(d *schema.ResourceData, m interface{}) error {
138170type VolumeBackupResourceCrud struct {
139171 BaseCrud
140172 Client * oci_core.BlockstorageClient
173+ SourceRegionClient * oci_core.BlockstorageClient
141174 Res * oci_core.VolumeBackup
142175 DisableNotFoundRetries bool
143176}
@@ -174,6 +207,67 @@ func (s *VolumeBackupResourceCrud) DeletedTarget() []string {
174207}
175208
176209func (s * VolumeBackupResourceCrud ) Create () error {
210+ if s .isCopyCreate () {
211+ return s .createVolumeBackupCopy ()
212+ }
213+
214+ return s .CreateVolumeBackup ()
215+ }
216+
217+ func (s * VolumeBackupResourceCrud ) isCopyCreate () bool {
218+ if sourceDetails , ok := s .D .GetOkExists ("source_details" ); ok {
219+ if tmpList := sourceDetails .([]interface {}); len (tmpList ) > 0 {
220+ return true
221+ }
222+ }
223+ return false
224+ }
225+
226+ func (s * VolumeBackupResourceCrud ) createVolumeBackupCopy () error {
227+ copyVolumeBackupRequest := oci_core.CopyVolumeBackupRequest {}
228+
229+ configProvider := * s .Client .ConfigurationProvider ()
230+ if configProvider == nil {
231+ return fmt .Errorf ("cannot access ConfigurationProvider" )
232+ }
233+ currentRegion , error := configProvider .Region ()
234+ if error != nil {
235+ return fmt .Errorf ("cannot access Region for the current ConfigurationProvider" )
236+ }
237+
238+ if sourceDetails , ok := s .D .GetOkExists ("source_details" ); ok && sourceDetails != nil {
239+ fieldKeyFormat := fmt .Sprintf ("%s.%d.%%s" , "source_details" , 0 )
240+
241+ if region , ok := s .D .GetOkExists (fmt .Sprintf (fieldKeyFormat , "region" )); ok {
242+ tmp := region .(string )
243+ err := s .createBlockStorageSourceRegionClient (tmp )
244+ if err != nil {
245+ return err
246+ }
247+ }
248+ copyVolumeBackupRequest .DestinationRegion = & currentRegion
249+
250+ if volumeBackupId , ok := s .D .GetOkExists (fmt .Sprintf (fieldKeyFormat , "volume_backup_id" )); ok {
251+ tmp := volumeBackupId .(string )
252+ copyVolumeBackupRequest .VolumeBackupId = & tmp
253+ }
254+ }
255+
256+ if displayName , ok := s .D .GetOkExists ("display_name" ); ok {
257+ tmp := displayName .(string )
258+ copyVolumeBackupRequest .DisplayName = & tmp
259+ }
260+
261+ response , err := s .SourceRegionClient .CopyVolumeBackup (context .Background (), copyVolumeBackupRequest )
262+ if err != nil {
263+ return err
264+ }
265+
266+ s .Res = & response .VolumeBackup
267+ return nil
268+ }
269+
270+ func (s * VolumeBackupResourceCrud ) CreateVolumeBackup () error {
177271 request := oci_core.CreateVolumeBackupRequest {}
178272
179273 if definedTags , ok := s .D .GetOkExists ("defined_tags" ); ok {
@@ -305,6 +399,10 @@ func (s *VolumeBackupResourceCrud) SetData() error {
305399
306400 s .D .Set ("source_type" , s .Res .SourceType )
307401
402+ if s .Res .SourceVolumeBackupId != nil {
403+ s .D .Set ("source_volume_backup_id" , * s .Res .SourceVolumeBackupId )
404+ }
405+
308406 s .D .Set ("state" , s .Res .LifecycleState )
309407
310408 if s .Res .TimeCreated != nil {
0 commit comments