11package aquasec
22
33import (
4+ "context"
45 "fmt"
6+ "log"
57 "strings"
68
9+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
711 "github.com/aquasecurity/terraform-provider-aquasec/client"
812 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
913)
1014
1115func resourceApplicationScope () * schema.Resource {
1216 return & schema.Resource {
13- Create : resourceApplicationScopeCreate ,
14- Read : resourceApplicationScopeRead ,
15- Update : resourceApplicationScopeUpdate ,
16- Delete : resourceApplicationScopeDelete ,
17+ CreateContext : resourceApplicationScopeCreate ,
18+ ReadContext : resourceApplicationScopeRead ,
19+ UpdateContext : resourceApplicationScopeUpdate ,
20+ DeleteContext : resourceApplicationScopeDelete ,
1721 Importer : & schema.ResourceImporter {
1822 StateContext : schema .ImportStatePassthroughContext ,
1923 },
@@ -370,22 +374,29 @@ func resourceApplicationScope() *schema.Resource {
370374 }
371375}
372376
373- func resourceApplicationScopeCreate (d * schema.ResourceData , m interface {}) error {
377+ func resourceApplicationScopeCreate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
378+ result := isSaasEnv ()
379+ log .Printf ("is SAAS envvvvvv %v" , result ) // TODO: DELETE
380+ var diags diag.Diagnostics
381+ diags = addSaasResourceWarning (diags , "aquasec_application_scope" , "aquasec_application_scope_saas" )
382+
374383 ac := m .(* client.Client )
375384 name := d .Get ("name" ).(string )
376385 iap , err1 := expandApplicationScope (d )
377386 if err1 != nil {
378- return fmt .Errorf ("expanding applications is failed with error: %v " , err1 )
387+ return diag . FromErr ( fmt .Errorf ("expanding applications failed with error: %w " , err1 ) )
379388 }
380389 err := ac .CreateApplicationScope (iap )
381390
382391 if err == nil {
383392 d .SetId (name )
384393 } else {
385- return fmt .Errorf ("application scope resource create is failed with error: %v " , err )
394+ return diag . FromErr ( fmt .Errorf ("application scope resource create is failed with error: %w " , err1 ) )
386395 }
396+ readDiags := resourceApplicationScopeRead (ctx , d , m )
397+ diags = append (diags , readDiags ... )
387398
388- return resourceApplicationScopeRead ( d , m )
399+ return diags
389400}
390401
391402func expandApplicationScope (d * schema.ResourceData ) (* client.ApplicationScope , error ) {
@@ -439,7 +450,7 @@ func expandApplicationScope(d *schema.ResourceData) (*client.ApplicationScope, e
439450
440451}
441452
442- func resourceApplicationScopeRead (d * schema.ResourceData , m interface {}) error {
453+ func resourceApplicationScopeRead (ctx context. Context , d * schema.ResourceData , m interface {}) diag. Diagnostics {
443454 ac := m .(* client.Client )
444455
445456 iap , err := ac .GetApplicationScope (d .Id ())
@@ -448,38 +459,41 @@ func resourceApplicationScopeRead(d *schema.ResourceData, m interface{}) error {
448459 d .SetId ("" )
449460 return nil
450461 }
451- return err
462+ return diag . FromErr ( err )
452463 }
453464
454465 err = d .Set ("name" , iap .Name )
455466 if err != nil {
456- return err
467+ return diag . FromErr ( err )
457468 }
458469 err = d .Set ("description" , iap .Description )
459470 if err != nil {
460- return err
471+ return diag . FromErr ( err )
461472 }
462473 err = d .Set ("author" , iap .Author )
463474 if err != nil {
464- return err
475+ return diag . FromErr ( err )
465476 }
466477 err = d .Set ("owner_email" , iap .OwnerEmail )
467478 if err != nil {
468- return err
479+ return diag . FromErr ( err )
469480 }
470481
471482 err = d .Set ("categories" , flattenCategories (iap .Categories ))
472483
473484 if err != nil {
474- return err
485+ return diag . FromErr ( err )
475486 }
476487
477488 d .SetId (iap .Name )
478489
479490 return nil
480491}
481492
482- func resourceApplicationScopeUpdate (d * schema.ResourceData , m interface {}) error {
493+ func resourceApplicationScopeUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
494+ var diags diag.Diagnostics
495+ diags = addSaasResourceWarning (diags , "aquasec_application_scope" , "aquasec_application_scope_saas" )
496+
483497 ac := m .(* client.Client )
484498 name := d .Get ("name" ).(string )
485499
@@ -488,16 +502,16 @@ func resourceApplicationScopeUpdate(d *schema.ResourceData, m interface{}) error
488502
489503 iap , err1 := expandApplicationScope (d )
490504 if err1 != nil {
491- return err1
505+ return diag . FromErr ( err1 )
492506 }
493507 err = ac .UpdateApplicationScope (iap , name )
494508 if err != nil {
495- return err
509+ return diag . FromErr ( err )
496510 }
497- return resourceApplicationScopeRead (d , m )
511+ return resourceApplicationScopeRead (ctx , d , m )
498512
499513 }
500- return nil
514+ return diags
501515}
502516
503517func createCategory (a map [string ]interface {}, w map [string ]interface {}, i map [string ]interface {}) client.Category {
@@ -613,15 +627,15 @@ func createEmptyCommonStruct() client.CommonStruct {
613627 return commonStruct1
614628}
615629
616- func resourceApplicationScopeDelete (d * schema.ResourceData , m interface {}) error {
630+ func resourceApplicationScopeDelete (ctx context. Context , d * schema.ResourceData , m interface {}) diag. Diagnostics {
617631 ac := m .(* client.Client )
618632 name := d .Get ("name" ).(string )
619633 err := ac .DeleteApplicationScope (name )
620634
621635 if err == nil {
622636 d .SetId ("" )
623637 } else {
624- return err
638+ return diag . FromErr ( err )
625639 }
626640 return nil
627641}
0 commit comments