@@ -2,6 +2,7 @@ package provider
22
33import (
44 "context"
5+ "net/http"
56
67 "github.com/hashicorp/terraform-plugin-log/tflog"
78
@@ -16,9 +17,9 @@ func resourceOracleDsource() *schema.Resource {
1617 Description : "Resource for Oracle dSource creation." ,
1718
1819 CreateContext : resourceOracleDsourceCreate ,
19- ReadContext : resourceDsourceRead ,
20- UpdateContext : resourceDsourceUpdate ,
21- DeleteContext : resourceDsourceDelete ,
20+ ReadContext : resourceOracleDsourceRead ,
21+ UpdateContext : resourceOracleDsourceUpdate ,
22+ DeleteContext : resourceOracleDsourceDelete ,
2223
2324 Schema : map [string ]* schema.Schema {
2425 "name" : {
@@ -40,6 +41,7 @@ func resourceOracleDsource() *schema.Resource {
4041 "log_sync_enabled" : {
4142 Type : schema .TypeBool ,
4243 Optional : true ,
44+ Computed : true ,
4345 },
4446 "make_current_account_owner" : {
4547 Type : schema .TypeBool ,
@@ -456,22 +458,10 @@ func resourceOracleDsource() *schema.Resource {
456458 Type : schema .TypeString ,
457459 Computed : true ,
458460 },
459- "source_id" : {
460- Type : schema .TypeString ,
461- Computed : true ,
462- },
463461 "database_type" : {
464462 Type : schema .TypeString ,
465463 Computed : true ,
466464 },
467- "namespace_id" : {
468- Type : schema .TypeString ,
469- Computed : true ,
470- },
471- "namespace_name" : {
472- Type : schema .TypeString ,
473- Computed : true ,
474- },
475465 "is_replica" : {
476466 Type : schema .TypeBool ,
477467 Computed : true ,
@@ -488,10 +478,6 @@ func resourceOracleDsource() *schema.Resource {
488478 Type : schema .TypeString ,
489479 Computed : true ,
490480 },
491- "plugin_version" : {
492- Type : schema .TypeString ,
493- Computed : true ,
494- },
495481 "creation_date" : {
496482 Type : schema .TypeString ,
497483 Computed : true ,
@@ -504,10 +490,18 @@ func resourceOracleDsource() *schema.Resource {
504490 Type : schema .TypeBool ,
505491 Computed : true ,
506492 },
493+ "is_detached" : {
494+ Type : schema .TypeBool ,
495+ Computed : true ,
496+ },
507497 "engine_id" : {
508498 Type : schema .TypeString ,
509499 Computed : true ,
510500 },
501+ "source_id" : {
502+ Type : schema .TypeString ,
503+ Computed : true ,
504+ },
511505 "status" : {
512506 Type : schema .TypeString ,
513507 Computed : true ,
@@ -520,12 +514,20 @@ func resourceOracleDsource() *schema.Resource {
520514 Type : schema .TypeString ,
521515 Computed : true ,
522516 },
523- "previous_timeflow_id" : {
517+ "is_appdata" : {
518+ Type : schema .TypeBool ,
519+ Computed : true ,
520+ },
521+ "sync_policy_id" : {
524522 Type : schema .TypeString ,
525523 Computed : true ,
526524 },
527- "is_appdata" : {
528- Type : schema .TypeBool ,
525+ "retention_policy_id" : {
526+ Type : schema .TypeString ,
527+ Computed : true ,
528+ },
529+ "exported_data_directory" : {
530+ Type : schema .TypeString ,
529531 Computed : true ,
530532 },
531533 "wait_time" : {
@@ -710,7 +712,6 @@ func resourceOracleDsourceCreate(ctx context.Context, d *schema.ResourceData, me
710712 if v , has_v := d .GetOk ("ops_pre_log_sync" ); has_v {
711713 oracleDSourceLinkSourceParameters .SetOpsPreLogSync (toSourceOperationArray (v ))
712714 }
713-
714715 req := client .DSourcesAPI .LinkOracleDatabase (ctx )
715716
716717 apiRes , httpRes , err := req .OracleDSourceLinkSourceParameters (* oracleDSourceLinkSourceParameters ).Execute ()
@@ -744,10 +745,111 @@ func resourceOracleDsourceCreate(ctx context.Context, d *schema.ResourceData, me
744745 return diags
745746}
746747
747- func toIntArray (array interface {}) []int32 {
748- items := []int32 {}
749- for _ , item := range array .([]interface {}) {
750- items = append (items , int32 (item .(int )))
748+ func resourceOracleDsourceRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
749+
750+ client := meta .(* apiClient ).client
751+
752+ dsource_id := d .Id ()
753+
754+ res , diags := PollForObjectExistence (ctx , func () (interface {}, * http.Response , error ) {
755+ return client .DSourcesAPI .GetDsourceById (ctx , dsource_id ).Execute ()
756+ })
757+
758+ if res == nil {
759+ tflog .Error (ctx , DLPX + ERROR + "Dsource not found: " + dsource_id + ", removing from state. " )
760+ d .SetId ("" )
761+ return nil
762+ }
763+
764+ if diags != nil {
765+ _ , diags := PollForObjectDeletion (ctx , func () (interface {}, * http.Response , error ) {
766+ return client .DSourcesAPI .GetDsourceById (ctx , dsource_id ).Execute ()
767+ })
768+ // This would imply error in poll for deletion so we just log and exit.
769+ if diags != nil {
770+ tflog .Error (ctx , DLPX + ERROR + "Error in polling of dSource for deletion." )
771+ } else {
772+ // diags will be nil in case of successful poll for deletion logic aka 404
773+ tflog .Error (ctx , DLPX + ERROR + "Error reading the dSource " + dsource_id + ", removing from state." )
774+ d .SetId ("" )
775+ }
776+
777+ return nil
778+ }
779+
780+ result , ok := res .(* dctapi.DSource )
781+ if ! ok {
782+ return diag .Errorf ("Error occured in type casting." )
783+ }
784+
785+ d .Set ("id" , result .GetId ())
786+ d .Set ("database_type" , result .GetDatabaseType ())
787+ d .Set ("name" , result .GetName ())
788+ d .Set ("is_replica" , result .GetIsReplica ())
789+ d .Set ("database_version" , result .GetDatabaseVersion ())
790+ d .Set ("content_type" , result .GetContentType ())
791+ d .Set ("data_uuid" , result .GetDataUuid ())
792+ d .Set ("creation_date" , result .GetCreationDate ().String ())
793+ d .Set ("group_name" , result .GetGroupName ())
794+ d .Set ("enabled" , result .GetEnabled ())
795+ d .Set ("is_detached" , result .GetIsDetached ())
796+ d .Set ("engine_id" , result .GetEngineId ())
797+ d .Set ("source_id" , result .GetSourceId ())
798+ d .Set ("status" , result .GetStatus ())
799+ d .Set ("engine_name" , result .GetEngineName ())
800+ d .Set ("current_timeflow_id" , result .GetCurrentTimeflowId ())
801+ d .Set ("is_appdata" , result .GetIsAppdata ())
802+ d .Set ("hooks" , result .GetHooks ())
803+ d .Set ("sync_policy_id" , result .GetSyncPolicyId ())
804+ d .Set ("retention_policy_id" , result .GetReplicaRetentionPolicyId ())
805+ d .Set ("log_sync_enabled" , result .GetLogsyncEnabled ())
806+ d .Set ("exported_data_directory" , result .GetExportedDataDirectory ())
807+ return diags
808+ }
809+
810+ func resourceOracleDsourceUpdate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
811+ // get the changed keys
812+ changedKeys := make ([]string , 0 , len (d .State ().Attributes ))
813+ for k := range d .State ().Attributes {
814+ if d .HasChange (k ) {
815+ changedKeys = append (changedKeys , k )
816+ }
817+ }
818+ // revert and set the old value to the changed keys
819+ for _ , key := range changedKeys {
820+ old , _ := d .GetChange (key )
821+ d .Set (key , old )
751822 }
752- return items
823+
824+ return diag .Errorf ("Action update not implemented for resource : dSource" )
825+ }
826+
827+ func resourceOracleDsourceDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
828+ client := meta .(* apiClient ).client
829+
830+ dsourceId := d .Id ()
831+
832+ deleteDsourceParams := dctapi .NewDeleteDSourceRequest (dsourceId )
833+ deleteDsourceParams .SetForce (false )
834+
835+ res , httpRes , err := client .DSourcesAPI .DeleteDsource (ctx ).DeleteDSourceRequest (* deleteDsourceParams ).Execute ()
836+
837+ if diags := apiErrorResponseHelper (ctx , res , httpRes , err ); diags != nil {
838+ return diags
839+ }
840+
841+ job_status , job_err := PollJobStatus (* res .Id , ctx , client )
842+ if job_err != "" {
843+ tflog .Warn (ctx , DLPX + WARN + "Job Polling failed but continuing with deletion. Error :" + job_err )
844+ }
845+ tflog .Info (ctx , DLPX + INFO + "Job result is " + job_status )
846+ if isJobTerminalFailure (job_status ) {
847+ return diag .Errorf ("[NOT OK] dSource-Delete %s. JobId: %s / Error: %s" , job_status , * res .Id , job_err )
848+ }
849+
850+ _ , diags := PollForObjectDeletion (ctx , func () (interface {}, * http.Response , error ) {
851+ return client .DSourcesAPI .GetDsourceById (ctx , dsourceId ).Execute ()
852+ })
853+
854+ return diags
753855}
0 commit comments