@@ -5,7 +5,9 @@ package opensearch
55
66import (
77 "context"
8+ "fmt"
89 "log"
10+ "time"
911
1012 "github.com/YakDriver/regexache"
1113 "github.com/aws/aws-sdk-go-v2/aws"
@@ -117,6 +119,9 @@ func resourcePackageCreate(ctx context.Context, d *schema.ResourceData, meta any
117119
118120 d .SetId (aws .ToString (output .PackageDetails .PackageID ))
119121
122+ if _ , err := waitPackageValidationCompleted (ctx , conn , d .Id ()); err != nil {
123+ return sdkdiag .AppendErrorf (diags , "waiting for package validation (%s) completed: %s" , d .Id (), err )
124+ }
120125 return append (diags , resourcePackageRead (ctx , d , meta )... )
121126}
122127
@@ -242,3 +247,46 @@ func expandPackageSource(v any) *awstypes.PackageSource {
242247 S3Key : aws .String (v .(map [string ]any )["s3_key" ].(string )),
243248 }
244249}
250+
251+ func waitPackageValidationCompleted (ctx context.Context , conn * opensearch.Client , id string ) (* opensearch.DescribePackagesOutput , error ) {
252+ stateConf := & retry.StateChangeConf {
253+ Pending : []string {"COPYING" , "VALIDATING" },
254+ Target : []string {"AVAILABLE" },
255+ Refresh : statusPackageValidation (ctx , conn , id ),
256+ Timeout : 20 * time .Minute ,
257+ MinTimeout : 15 * time .Second ,
258+ Delay : 30 * time .Second ,
259+ }
260+
261+ outputRaw , err := stateConf .WaitForStateContext (ctx )
262+
263+ if output , ok := outputRaw .(* opensearch.DescribePackagesOutput ); ok {
264+ return output , err
265+ }
266+
267+ return nil , err
268+ }
269+
270+ func statusPackageValidation (ctx context.Context , conn * opensearch.Client , id string ) retry.StateRefreshFunc {
271+ return func () (any , string , error ) {
272+ output , err := findPackageByID (ctx , conn , id )
273+
274+ if tfresource .NotFound (err ) {
275+ return nil , "" , nil
276+ }
277+
278+ if err != nil {
279+ return nil , "" , err
280+ }
281+
282+ if output == nil {
283+ return nil , "" , nil
284+ }
285+
286+ if output .ErrorDetails != nil {
287+ return nil , string (output .PackageStatus ), fmt .Errorf ("package validation failed: %s, %s, %s" , string (output .PackageStatus ), aws .ToString (output .ErrorDetails .ErrorType ), aws .ToString (output .ErrorDetails .ErrorMessage ))
288+ }
289+
290+ return output , string (output .PackageStatus ), nil
291+ }
292+ }
0 commit comments