Skip to content

Commit 8862364

Browse files
committed
adding warning message for legacy usage of aquasec_application_scope resource on saas env
1 parent ad3a707 commit 8862364

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

aquasec/resource_application_scope.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package aquasec
22

33
import (
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

1115
func 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

391402
func 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

503517
func 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
}

aquasec/utils.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package aquasec
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/aquasecurity/terraform-provider-aquasec/client"
7-
"github.com/aquasecurity/terraform-provider-aquasec/consts"
6+
"log"
87
os "os"
98
"strings"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
12+
"github.com/aquasecurity/terraform-provider-aquasec/client"
13+
"github.com/aquasecurity/terraform-provider-aquasec/consts"
1014
)
1115

1216
func convertStringArr(ifaceArr []interface{}) []string {
@@ -240,3 +244,16 @@ func isResourceExist(response string) bool {
240244
return true
241245
}
242246
}
247+
248+
func addSaasResourceWarning(diags diag.Diagnostics, legacyResource string, newResource string) diag.Diagnostics {
249+
if isSaasEnv() {
250+
log.Println("Adding SaaS warning...")
251+
return append(diags, diag.Diagnostic{
252+
Severity: diag.Warning,
253+
Summary: "Legacy Resource Usage",
254+
Detail: fmt.Sprintf("You are using %s with an Aqua SaaS instance. Please migrate to %s, designed specifically for Aqua SaaS customers and supporting the entire SaaS platform beyond workload protection.", legacyResource, newResource),
255+
})
256+
}
257+
log.Println("No SaaS warning added (isSaasEnv is false)")
258+
return diags
259+
}

0 commit comments

Comments
 (0)