Skip to content

Commit fb99aa7

Browse files
Jay PatelNagaRajuPasunuri
authored andcommitted
Added - Support for Data Flow CascadeDeleteApplication API
1 parent 8de04c2 commit fb99aa7

File tree

4 files changed

+162
-7
lines changed

4 files changed

+162
-7
lines changed

examples/dataflow/main.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,19 @@ resource "oci_dataflow_pool" "test_pool" {
8282
freeform_tags = {
8383
"Department" = "Finance"
8484
}
85-
configurations = [{shape: "VM.Standard2.1", shapeConfig: {ocpus: 1, memoryInGBs: 15}, min: 0, max: 1}]
86-
schedules = [{dayOfWeek: "SUNDAY", startTime: 3}]
85+
configurations {
86+
shape = "VM.Standard2.1"
87+
shape_config {
88+
ocpus = 1
89+
memory_in_gbs = 15
90+
}
91+
min = 0
92+
max = 1
93+
}
94+
schedules {
95+
day_of_week = "SUNDAY"
96+
start_time = 3
97+
}
8798
}
8899

89100
resource "oci_dataflow_application" "tf_application" {
@@ -116,6 +127,7 @@ resource "oci_dataflow_application" "tf_application" {
116127

117128
#warehouse_bucket_uri = var.application_warehouse_bucket_uri}"
118129
metastore_id = var.metastore_id
130+
terminate_runs_on_deletion = true
119131
}
120132

121133
data "oci_dataflow_applications" "tf_applications" {

internal/service/dataflow/dataflow_application_resource.go

Lines changed: 145 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import (
77
"context"
88
"fmt"
99
"strconv"
10+
"strings"
11+
"time"
12+
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14+
oci_common "github.com/oracle/oci-go-sdk/v65/common"
1015

1116
"github.com/oracle/terraform-provider-oci/internal/client"
1217
"github.com/oracle/terraform-provider-oci/internal/tfresource"
@@ -273,6 +278,11 @@ func DataflowApplicationResource() *schema.Resource {
273278
Type: schema.TypeString,
274279
Computed: true,
275280
},
281+
"terminate_runs_on_deletion": {
282+
Type: schema.TypeBool,
283+
Optional: true,
284+
Default: false,
285+
},
276286
},
277287
}
278288
}
@@ -332,7 +342,9 @@ func (s *DataflowApplicationResourceCrud) CreatedTarget() []string {
332342
}
333343

334344
func (s *DataflowApplicationResourceCrud) DeletedPending() []string {
335-
return []string{}
345+
return []string{
346+
string(oci_dataflow.ApplicationLifecycleStateDeleting),
347+
}
336348
}
337349

338350
func (s *DataflowApplicationResourceCrud) DeletedTarget() []string {
@@ -760,17 +772,146 @@ func (s *DataflowApplicationResourceCrud) Update() error {
760772
}
761773

762774
func (s *DataflowApplicationResourceCrud) Delete() error {
763-
request := oci_dataflow.DeleteApplicationRequest{}
764-
765775
tmp := s.D.Id()
766-
request.ApplicationId = &tmp
767776

777+
// Execute cascading deletion of application and related runs if terminate_runs_on_deletion flag is set to true
778+
if s.D.Get("terminate_runs_on_deletion").(bool) {
779+
780+
request := oci_dataflow.CascadingDeleteApplicationRequest{}
781+
request.ApplicationId = &tmp
782+
request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(s.DisableNotFoundRetries, "dataflow")
783+
784+
response, err := s.Client.CascadingDeleteApplication(context.Background(), request)
785+
786+
if err != nil {
787+
return err
788+
}
789+
790+
workRequestId := response.OpcWorkRequestId
791+
792+
// Wait until the work-request completes
793+
_, delWorkRequestErr := cascadingDeleteApplicationWaitForWorkRequest(workRequestId, "application",
794+
oci_dataflow.WorkRequestResourceActionTypeDeleted, s.D.Timeout(schema.TimeoutDelete),
795+
s.DisableNotFoundRetries, s.Client)
796+
797+
return delWorkRequestErr
798+
}
799+
800+
// Normal deletion
801+
request := oci_dataflow.DeleteApplicationRequest{}
802+
request.ApplicationId = &tmp
768803
request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(s.DisableNotFoundRetries, "dataflow")
769804

770805
_, err := s.Client.DeleteApplication(context.Background(), request)
771806
return err
772807
}
773808

809+
func cascadingDeleteApplicationWaitForWorkRequest(wId *string, entityType string,
810+
action oci_dataflow.WorkRequestResourceActionTypeEnum,
811+
timeout time.Duration, disableFoundRetries bool, client *oci_dataflow.DataFlowClient) (*string, error) {
812+
813+
retryPolicy := tfresource.GetRetryPolicy(disableFoundRetries, "dataflow")
814+
retryPolicy.ShouldRetryOperation = applicationWorkRequestShouldRetryFunc(timeout)
815+
816+
response := oci_dataflow.GetWorkRequestResponse{}
817+
stateConf := &resource.StateChangeConf{
818+
Pending: []string{
819+
string(oci_dataflow.WorkRequestStatusInprogress),
820+
string(oci_dataflow.WorkRequestStatusAccepted),
821+
string(oci_dataflow.WorkRequestStatusCancelling),
822+
},
823+
Target: []string{
824+
string(oci_dataflow.WorkRequestStatusSucceeded),
825+
string(oci_dataflow.WorkRequestStatusFailed),
826+
string(oci_dataflow.WorkRequestStatusCancelled),
827+
},
828+
Refresh: func() (interface{}, string, error) {
829+
var err error
830+
response, err = client.GetWorkRequest(context.Background(),
831+
oci_dataflow.GetWorkRequestRequest{
832+
WorkRequestId: wId,
833+
RequestMetadata: oci_common.RequestMetadata{
834+
RetryPolicy: retryPolicy,
835+
},
836+
})
837+
wr := &response.WorkRequest
838+
return wr, string(wr.Status), err
839+
},
840+
Timeout: timeout,
841+
}
842+
if _, e := stateConf.WaitForState(); e != nil {
843+
return nil, e
844+
}
845+
846+
var identifier *string
847+
// The work request response contains an array of objects that finished the operation
848+
for _, res := range response.Resources {
849+
if strings.Contains(strings.ToLower(*res.ResourceType), entityType) {
850+
if res.ActionType == action {
851+
identifier = res.ResourceId
852+
break
853+
}
854+
}
855+
}
856+
857+
// The workrequest may have failed, check for errors if identifier is not found or work failed or got cancelled
858+
if identifier == nil ||
859+
response.Status == oci_dataflow.WorkRequestStatusFailed ||
860+
response.Status == oci_dataflow.WorkRequestStatusCancelled {
861+
return nil, getErrorFromDataflowApplicationWorkRequest(client, wId, retryPolicy, entityType, action)
862+
}
863+
864+
return identifier, nil
865+
}
866+
867+
func applicationWorkRequestShouldRetryFunc(timeout time.Duration) func(response oci_common.OCIOperationResponse) bool {
868+
startTime := time.Now()
869+
stopTime := startTime.Add(timeout)
870+
return func(response oci_common.OCIOperationResponse) bool {
871+
872+
// Stop after timeout has elapsed
873+
if time.Now().After(stopTime) {
874+
return false
875+
}
876+
877+
// Make sure we stop on default rules
878+
if tfresource.ShouldRetry(response, false, "dataflow", startTime) {
879+
return true
880+
}
881+
882+
// Only stop if the time Finished is set
883+
if workRequestResponse, ok := response.Response.(oci_dataflow.GetWorkRequestResponse); ok {
884+
return workRequestResponse.TimeFinished == nil
885+
}
886+
return false
887+
}
888+
}
889+
890+
func getErrorFromDataflowApplicationWorkRequest(client *oci_dataflow.DataFlowClient, workId *string,
891+
retryPolicy *oci_common.RetryPolicy, entityType string, action oci_dataflow.WorkRequestResourceActionTypeEnum) error {
892+
response, err := client.ListWorkRequestErrors(context.Background(),
893+
oci_dataflow.ListWorkRequestErrorsRequest{
894+
WorkRequestId: workId,
895+
RequestMetadata: oci_common.RequestMetadata{
896+
RetryPolicy: retryPolicy,
897+
},
898+
})
899+
if err != nil {
900+
return err
901+
}
902+
903+
allErrs := make([]string, 0)
904+
for _, wrkErr := range response.Items {
905+
allErrs = append(allErrs, *wrkErr.Message)
906+
}
907+
errorMessage := strings.Join(allErrs, "\n")
908+
909+
workRequestErr := fmt.Errorf("work request did not succeed, workId: %s, entity: %s, action: %s. Message: %s",
910+
*workId, entityType, action, errorMessage)
911+
912+
return workRequestErr
913+
}
914+
774915
func (s *DataflowApplicationResourceCrud) SetData() error {
775916
if s.Res.ApplicationLogConfig != nil {
776917
s.D.Set("application_log_config", []interface{}{ApplicationLogConfigToMap(s.Res.ApplicationLogConfig)})

website/docs/d/dataflow_application.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ The following attributes are exported:
7575
* `time_updated` - The date and time the resource was updated, expressed in [RFC 3339](https://tools.ietf.org/html/rfc3339) timestamp format. Example: `2018-04-03T21:10:29.600Z`
7676
* `type` - The Spark application processing type.
7777
* `warehouse_bucket_uri` - An Oracle Cloud Infrastructure URI of the bucket to be used as default warehouse directory for BATCH SQL runs. See https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/hdfsconnector.htm#uriformat.
78+
* `terminate_runs_on_deletion` - A boolean flag which indicates whether related non-terminal Run(s) for the Application should be terminated along with Application deletion or not.
7879

website/docs/r/dataflow_application.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ resource "oci_dataflow_application" "test_application" {
6666
private_endpoint_id = oci_dataflow_private_endpoint.test_private_endpoint.id
6767
type = var.application_type
6868
warehouse_bucket_uri = var.application_warehouse_bucket_uri
69+
terminate_runs_on_deletion = true
6970
}
7071
```
7172

@@ -109,7 +110,7 @@ The following arguments are supported:
109110
* `spark_version` - (Required) (Updatable) The Spark version utilized to run the application.
110111
* `type` - (Optional) The Spark application processing type.
111112
* `warehouse_bucket_uri` - (Optional) (Updatable) An Oracle Cloud Infrastructure URI of the bucket to be used as default warehouse directory for BATCH SQL runs. See https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/hdfsconnector.htm#uriformat.
112-
113+
* `terminate_runs_on_deletion` - (Optional) A boolean flag which indicates whether related non-terminal Run(s) for the Application should be terminated along with Application deletion or not.
113114

114115
** IMPORTANT **
115116
Any change to a property that does not support update will force the destruction and recreation of the resource with the new property values

0 commit comments

Comments
 (0)