Skip to content

Commit

Permalink
adding warning message for legacy usage of aquasec_application_scope …
Browse files Browse the repository at this point in the history
…resource on saas env
  • Loading branch information
Tal Mosenzon committed Jan 27, 2025
1 parent ad3a707 commit 8862364
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
58 changes: 36 additions & 22 deletions aquasec/resource_application_scope.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package aquasec

import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/aquasecurity/terraform-provider-aquasec/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceApplicationScope() *schema.Resource {
return &schema.Resource{
Create: resourceApplicationScopeCreate,
Read: resourceApplicationScopeRead,
Update: resourceApplicationScopeUpdate,
Delete: resourceApplicationScopeDelete,
CreateContext: resourceApplicationScopeCreate,
ReadContext: resourceApplicationScopeRead,
UpdateContext: resourceApplicationScopeUpdate,
DeleteContext: resourceApplicationScopeDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -370,22 +374,29 @@ func resourceApplicationScope() *schema.Resource {
}
}

func resourceApplicationScopeCreate(d *schema.ResourceData, m interface{}) error {
func resourceApplicationScopeCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
result := isSaasEnv()
log.Printf("is SAAS envvvvvv %v", result) // TODO: DELETE
var diags diag.Diagnostics
diags = addSaasResourceWarning(diags, "aquasec_application_scope", "aquasec_application_scope_saas")

ac := m.(*client.Client)
name := d.Get("name").(string)
iap, err1 := expandApplicationScope(d)
if err1 != nil {
return fmt.Errorf("expanding applications is failed with error: %v", err1)
return diag.FromErr(fmt.Errorf("expanding applications failed with error: %w", err1))
}
err := ac.CreateApplicationScope(iap)

if err == nil {
d.SetId(name)
} else {
return fmt.Errorf("application scope resource create is failed with error: %v", err)
return diag.FromErr(fmt.Errorf("application scope resource create is failed with error: %w", err1))
}
readDiags := resourceApplicationScopeRead(ctx, d, m)
diags = append(diags, readDiags...)

return resourceApplicationScopeRead(d, m)
return diags
}

func expandApplicationScope(d *schema.ResourceData) (*client.ApplicationScope, error) {
Expand Down Expand Up @@ -439,7 +450,7 @@ func expandApplicationScope(d *schema.ResourceData) (*client.ApplicationScope, e

}

func resourceApplicationScopeRead(d *schema.ResourceData, m interface{}) error {
func resourceApplicationScopeRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
ac := m.(*client.Client)

iap, err := ac.GetApplicationScope(d.Id())
Expand All @@ -448,38 +459,41 @@ func resourceApplicationScopeRead(d *schema.ResourceData, m interface{}) error {
d.SetId("")
return nil
}
return err
return diag.FromErr(err)
}

err = d.Set("name", iap.Name)
if err != nil {
return err
return diag.FromErr(err)
}
err = d.Set("description", iap.Description)
if err != nil {
return err
return diag.FromErr(err)
}
err = d.Set("author", iap.Author)
if err != nil {
return err
return diag.FromErr(err)
}
err = d.Set("owner_email", iap.OwnerEmail)
if err != nil {
return err
return diag.FromErr(err)
}

err = d.Set("categories", flattenCategories(iap.Categories))

if err != nil {
return err
return diag.FromErr(err)
}

d.SetId(iap.Name)

return nil
}

func resourceApplicationScopeUpdate(d *schema.ResourceData, m interface{}) error {
func resourceApplicationScopeUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
diags = addSaasResourceWarning(diags, "aquasec_application_scope", "aquasec_application_scope_saas")

ac := m.(*client.Client)
name := d.Get("name").(string)

Expand All @@ -488,16 +502,16 @@ func resourceApplicationScopeUpdate(d *schema.ResourceData, m interface{}) error

iap, err1 := expandApplicationScope(d)
if err1 != nil {
return err1
return diag.FromErr(err1)
}
err = ac.UpdateApplicationScope(iap, name)
if err != nil {
return err
return diag.FromErr(err)
}
return resourceApplicationScopeRead(d, m)
return resourceApplicationScopeRead(ctx, d, m)

}
return nil
return diags
}

func createCategory(a map[string]interface{}, w map[string]interface{}, i map[string]interface{}) client.Category {
Expand Down Expand Up @@ -613,15 +627,15 @@ func createEmptyCommonStruct() client.CommonStruct {
return commonStruct1
}

func resourceApplicationScopeDelete(d *schema.ResourceData, m interface{}) error {
func resourceApplicationScopeDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
ac := m.(*client.Client)
name := d.Get("name").(string)
err := ac.DeleteApplicationScope(name)

if err == nil {
d.SetId("")
} else {
return err
return diag.FromErr(err)
}
return nil
}
Expand Down
21 changes: 19 additions & 2 deletions aquasec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package aquasec
import (
"encoding/json"
"fmt"
"github.com/aquasecurity/terraform-provider-aquasec/client"
"github.com/aquasecurity/terraform-provider-aquasec/consts"
"log"
os "os"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/aquasecurity/terraform-provider-aquasec/client"
"github.com/aquasecurity/terraform-provider-aquasec/consts"
)

func convertStringArr(ifaceArr []interface{}) []string {
Expand Down Expand Up @@ -240,3 +244,16 @@ func isResourceExist(response string) bool {
return true
}
}

func addSaasResourceWarning(diags diag.Diagnostics, legacyResource string, newResource string) diag.Diagnostics {
if isSaasEnv() {
log.Println("Adding SaaS warning...")
return append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Legacy Resource Usage",
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),
})
}
log.Println("No SaaS warning added (isSaasEnv is false)")
return diags
}

0 comments on commit 8862364

Please sign in to comment.