|
| 1 | +// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package oci |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 14 | + |
| 15 | + oci_common "github.com/oracle/oci-go-sdk/v47/common" |
| 16 | + oci_data_safe "github.com/oracle/oci-go-sdk/v47/datasafe" |
| 17 | +) |
| 18 | + |
| 19 | +func init() { |
| 20 | + RegisterResource("oci_data_safe_compare_security_assessment", DataSafeCompareSecurityAssessmentResource()) |
| 21 | +} |
| 22 | + |
| 23 | +func DataSafeCompareSecurityAssessmentResource() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Importer: &schema.ResourceImporter{ |
| 26 | + State: schema.ImportStatePassthrough, |
| 27 | + }, |
| 28 | + Timeouts: DefaultTimeout, |
| 29 | + Create: createDataSafeCompareSecurityAssessment, |
| 30 | + Read: readDataSafeCompareSecurityAssessment, |
| 31 | + Delete: deleteDataSafeCompareSecurityAssessment, |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + // Required |
| 34 | + "comparison_security_assessment_id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + "security_assessment_id": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + }, |
| 44 | + |
| 45 | + // Optional |
| 46 | + |
| 47 | + // Computed |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func createDataSafeCompareSecurityAssessment(d *schema.ResourceData, m interface{}) error { |
| 53 | + sync := &DataSafeCompareSecurityAssessmentResourceCrud{} |
| 54 | + sync.D = d |
| 55 | + sync.Client = m.(*OracleClients).dataSafeClient() |
| 56 | + |
| 57 | + return CreateResource(d, sync) |
| 58 | +} |
| 59 | + |
| 60 | +func readDataSafeCompareSecurityAssessment(d *schema.ResourceData, m interface{}) error { |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func deleteDataSafeCompareSecurityAssessment(d *schema.ResourceData, m interface{}) error { |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +type DataSafeCompareSecurityAssessmentResourceCrud struct { |
| 69 | + BaseCrud |
| 70 | + Client *oci_data_safe.DataSafeClient |
| 71 | + Res *oci_data_safe.GetSecurityAssessmentComparisonResponse |
| 72 | + DisableNotFoundRetries bool |
| 73 | +} |
| 74 | + |
| 75 | +func (s *DataSafeCompareSecurityAssessmentResourceCrud) ID() string { |
| 76 | + return *s.Res.OpcRequestId |
| 77 | +} |
| 78 | + |
| 79 | +func (s *DataSafeCompareSecurityAssessmentResourceCrud) Get() error { |
| 80 | + request := oci_data_safe.GetSecurityAssessmentComparisonRequest{} |
| 81 | + |
| 82 | + if comparisonSecurityAssessmentId, ok := s.D.GetOkExists("comparison_security_assessment_id"); ok { |
| 83 | + tmp := comparisonSecurityAssessmentId.(string) |
| 84 | + request.ComparisonSecurityAssessmentId = &tmp |
| 85 | + } |
| 86 | + |
| 87 | + if securityAssessmentId, ok := s.D.GetOkExists("security_assessment_id"); ok { |
| 88 | + tmp := securityAssessmentId.(string) |
| 89 | + request.SecurityAssessmentId = &tmp |
| 90 | + } |
| 91 | + |
| 92 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "data_safe") |
| 93 | + |
| 94 | + response, err := s.Client.GetSecurityAssessmentComparison(context.Background(), request) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + s.Res = &response |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (s *DataSafeCompareSecurityAssessmentResourceCrud) Create() error { |
| 104 | + request := oci_data_safe.CompareSecurityAssessmentRequest{} |
| 105 | + |
| 106 | + if comparisonSecurityAssessmentId, ok := s.D.GetOkExists("comparison_security_assessment_id"); ok { |
| 107 | + tmp := comparisonSecurityAssessmentId.(string) |
| 108 | + request.ComparisonSecurityAssessmentId = &tmp |
| 109 | + } |
| 110 | + |
| 111 | + if securityAssessmentId, ok := s.D.GetOkExists("security_assessment_id"); ok { |
| 112 | + tmp := securityAssessmentId.(string) |
| 113 | + request.SecurityAssessmentId = &tmp |
| 114 | + } |
| 115 | + |
| 116 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "data_safe") |
| 117 | + |
| 118 | + response, err := s.Client.CompareSecurityAssessment(context.Background(), request) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + workId := response.OpcWorkRequestId |
| 124 | + return s.getCompareSecurityAssessmentFromWorkRequest(workId, getRetryPolicy(s.DisableNotFoundRetries, "data_safe"), oci_data_safe.WorkRequestResourceActionTypeUpdated, s.D.Timeout(schema.TimeoutCreate)) |
| 125 | +} |
| 126 | + |
| 127 | +func (s *DataSafeCompareSecurityAssessmentResourceCrud) getCompareSecurityAssessmentFromWorkRequest(workId *string, retryPolicy *oci_common.RetryPolicy, |
| 128 | + actionTypeEnum oci_data_safe.WorkRequestResourceActionTypeEnum, timeout time.Duration) error { |
| 129 | + |
| 130 | + // Wait until it finishes |
| 131 | + compareSecurityAssessmentId, err := compareSecurityAssessmentWaitForWorkRequest(workId, "securityassessment", |
| 132 | + actionTypeEnum, timeout, s.DisableNotFoundRetries, s.Client) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + s.D.SetId(*compareSecurityAssessmentId) |
| 138 | + |
| 139 | + return s.Get() |
| 140 | +} |
| 141 | + |
| 142 | +func compareSecurityAssessmentWorkRequestShouldRetryFunc(timeout time.Duration) func(response oci_common.OCIOperationResponse) bool { |
| 143 | + startTime := time.Now() |
| 144 | + stopTime := startTime.Add(timeout) |
| 145 | + return func(response oci_common.OCIOperationResponse) bool { |
| 146 | + |
| 147 | + // Stop after timeout has elapsed |
| 148 | + if time.Now().After(stopTime) { |
| 149 | + return false |
| 150 | + } |
| 151 | + |
| 152 | + // Make sure we stop on default rules |
| 153 | + if shouldRetry(response, false, "data_safe", startTime) { |
| 154 | + return true |
| 155 | + } |
| 156 | + |
| 157 | + // Only stop if the time Finished is set |
| 158 | + if workRequestResponse, ok := response.Response.(oci_data_safe.GetWorkRequestResponse); ok { |
| 159 | + return workRequestResponse.TimeFinished == nil |
| 160 | + } |
| 161 | + return false |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func compareSecurityAssessmentWaitForWorkRequest(wId *string, entityType string, action oci_data_safe.WorkRequestResourceActionTypeEnum, |
| 166 | + timeout time.Duration, disableFoundRetries bool, client *oci_data_safe.DataSafeClient) (*string, error) { |
| 167 | + retryPolicy := getRetryPolicy(disableFoundRetries, "data_safe") |
| 168 | + retryPolicy.ShouldRetryOperation = compareSecurityAssessmentWorkRequestShouldRetryFunc(timeout) |
| 169 | + |
| 170 | + response := oci_data_safe.GetWorkRequestResponse{} |
| 171 | + stateConf := &resource.StateChangeConf{ |
| 172 | + Pending: []string{ |
| 173 | + string(oci_data_safe.WorkRequestStatusInProgress), |
| 174 | + string(oci_data_safe.WorkRequestStatusAccepted), |
| 175 | + }, |
| 176 | + Target: []string{ |
| 177 | + string(oci_data_safe.WorkRequestStatusSucceeded), |
| 178 | + string(oci_data_safe.WorkRequestStatusFailed), |
| 179 | + }, |
| 180 | + Refresh: func() (interface{}, string, error) { |
| 181 | + var err error |
| 182 | + response, err = client.GetWorkRequest(context.Background(), |
| 183 | + oci_data_safe.GetWorkRequestRequest{ |
| 184 | + WorkRequestId: wId, |
| 185 | + RequestMetadata: oci_common.RequestMetadata{ |
| 186 | + RetryPolicy: retryPolicy, |
| 187 | + }, |
| 188 | + }) |
| 189 | + wr := &response.WorkRequest |
| 190 | + return wr, string(wr.Status), err |
| 191 | + }, |
| 192 | + Timeout: timeout, |
| 193 | + } |
| 194 | + if _, e := stateConf.WaitForState(); e != nil { |
| 195 | + return nil, e |
| 196 | + } |
| 197 | + |
| 198 | + var identifier *string |
| 199 | + // The work request response contains an array of objects that finished the operation |
| 200 | + for _, res := range response.Resources { |
| 201 | + if strings.Contains(strings.ToLower(*res.EntityType), entityType) { |
| 202 | + if res.ActionType == action { |
| 203 | + identifier = res.Identifier |
| 204 | + break |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + // The workrequest may have failed, check for errors if identifier is not found or work failed or got cancelled |
| 210 | + if identifier == nil || response.Status == oci_data_safe.WorkRequestStatusFailed { |
| 211 | + return nil, getErrorFromDataSafeCompareSecurityAssessmentWorkRequest(client, wId, retryPolicy, entityType, action) |
| 212 | + } |
| 213 | + |
| 214 | + return identifier, nil |
| 215 | +} |
| 216 | + |
| 217 | +func getErrorFromDataSafeCompareSecurityAssessmentWorkRequest(client *oci_data_safe.DataSafeClient, workId *string, retryPolicy *oci_common.RetryPolicy, entityType string, action oci_data_safe.WorkRequestResourceActionTypeEnum) error { |
| 218 | + response, err := client.ListWorkRequestErrors(context.Background(), |
| 219 | + oci_data_safe.ListWorkRequestErrorsRequest{ |
| 220 | + WorkRequestId: workId, |
| 221 | + RequestMetadata: oci_common.RequestMetadata{ |
| 222 | + RetryPolicy: retryPolicy, |
| 223 | + }, |
| 224 | + }) |
| 225 | + if err != nil { |
| 226 | + return err |
| 227 | + } |
| 228 | + |
| 229 | + allErrs := make([]string, 0) |
| 230 | + for _, wrkErr := range response.Items { |
| 231 | + allErrs = append(allErrs, *wrkErr.Message) |
| 232 | + } |
| 233 | + errorMessage := strings.Join(allErrs, "\n") |
| 234 | + |
| 235 | + workRequestErr := fmt.Errorf("work request did not succeed, workId: %s, entity: %s, action: %s. Message: %s", *workId, entityType, action, errorMessage) |
| 236 | + |
| 237 | + return workRequestErr |
| 238 | +} |
| 239 | + |
| 240 | +func (s *DataSafeCompareSecurityAssessmentResourceCrud) SetData() error { |
| 241 | + return nil |
| 242 | +} |
0 commit comments