-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resource): Add sysdig_secure_macro resource
Signed-off-by: Federico Barcelona <fede_rico_94@hotmail.com>
- Loading branch information
1 parent
ee4828b
commit d118dfc
Showing
7 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package sysdig | ||
|
||
import ( | ||
"github.com/draios/terraform-provider-sysdig/sysdig/secure" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func resourceSysdigSecureMacro() *schema.Resource { | ||
timeout := 30 * time.Second | ||
|
||
return &schema.Resource{ | ||
Create: resourceSysdigMacroCreate, | ||
Update: resourceSysdigMacroUpdate, | ||
Read: resourceSysdigMacroRead, | ||
Delete: resourceSysdigMacroDelete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(timeout), | ||
Update: schema.DefaultTimeout(timeout), | ||
Read: schema.DefaultTimeout(timeout), | ||
Delete: schema.DefaultTimeout(timeout), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"append": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"condition": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"version": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceSysdigMacroCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
macro := macroFromResourceData(d) | ||
macro, err := client.CreateMacro(macro) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(strconv.Itoa(macro.ID)) | ||
d.Set("version", macro.Version) | ||
|
||
return nil | ||
} | ||
|
||
func resourceSysdigMacroUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
macro := macroFromResourceData(d) | ||
macro.Version = d.Get("version").(int) | ||
|
||
id, _ := strconv.Atoi(d.Id()) | ||
macro.ID = id | ||
|
||
_, err := client.UpdateMacro(macro) | ||
return err | ||
} | ||
|
||
func resourceSysdigMacroRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
id, _ := strconv.Atoi(d.Id()) | ||
macro, err := client.GetMacroById(id) | ||
|
||
if err != nil { | ||
d.SetId("") | ||
} | ||
|
||
d.Set("name", macro.Name) | ||
d.Set("version", macro.Version) | ||
d.Set("items", macro.Condition.Condition) | ||
d.Set("append", macro.Append) | ||
|
||
return nil | ||
} | ||
|
||
func resourceSysdigMacroDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
id, _ := strconv.Atoi(d.Id()) | ||
|
||
return client.DeleteMacro(id) | ||
} | ||
|
||
func macroFromResourceData(d *schema.ResourceData) secure.Macro { | ||
return secure.Macro{ | ||
Name: d.Get("name").(string), | ||
Append: d.Get("append").(bool), | ||
Condition: secure.MacroCondition{Condition: d.Get("condition").(string)}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package sysdig_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/draios/terraform-provider-sysdig/sysdig" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestAccMacro(t *testing.T) { | ||
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) } | ||
fixedRandomText := rText() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" { | ||
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests") | ||
} | ||
}, | ||
Providers: map[string]terraform.ResourceProvider{ | ||
"sysdig": sysdig.Provider(), | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: macroWithName(rText()), | ||
}, | ||
{ | ||
Config: macroWithName(fixedRandomText), | ||
}, | ||
{ | ||
Config: macroUpdatedWithName(fixedRandomText), | ||
}, | ||
{ | ||
Config: macroAppendToDefault(), | ||
}, | ||
{ | ||
Config: macroWithMacro(rText(), rText()), | ||
}, | ||
{ | ||
Config: macroWithMacroAndList(rText(), rText(), rText()), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func macroWithName(name string) string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_macro" "sample" { | ||
name = "terraform_test_%s" | ||
condition = "always_true" | ||
} | ||
`, name) | ||
} | ||
|
||
func macroUpdatedWithName(name string) string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_macro" "sample" { | ||
name = "terraform_test_%s" | ||
condition = "never_true" | ||
} | ||
`, name) | ||
} | ||
|
||
func macroAppendToDefault() string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_macro" "sample2" { | ||
name = "container" | ||
condition = "and always_true" | ||
append = true | ||
} | ||
`) | ||
} | ||
|
||
func macroWithMacro(name1, name2 string) string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_macro" "sample3" { | ||
name = "terraform_test_%s" | ||
condition = "always_true" | ||
} | ||
resource "sysdig_secure_macro" "sample4" { | ||
name = "terraform_test_%s" | ||
condition = "never_true and ${sysdig_secure_macro.sample3.name}" | ||
} | ||
`, name1, name2) | ||
} | ||
|
||
func macroWithMacroAndList(name1, name2, name3 string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "sysdig_secure_macro" "sample5" { | ||
name = "terraform_test_%s" | ||
condition = "fd.name in (${sysdig_secure_list.sample.name})" | ||
} | ||
resource "sysdig_secure_macro" "sample6" { | ||
name = "terraform_test_%s" | ||
condition = "never_true and ${sysdig_secure_macro.sample5.name}" | ||
} | ||
`, listWithName(name3), name1, name2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package secure | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func (client *sysdigSecureClient) CreateMacro(macroRequest Macro) (macro Macro, err error) { | ||
response, err := client.doSysdigSecureRequest(http.MethodPost, client.GetMacrosUrl(), macroRequest.ToJSON()) | ||
if err != nil { | ||
return | ||
} | ||
defer response.Body.Close() | ||
|
||
body, _ := ioutil.ReadAll(response.Body) | ||
|
||
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { | ||
err = errors.New(response.Status) | ||
return | ||
} | ||
|
||
macro, err = MacroFromJSON(body) | ||
return | ||
} | ||
|
||
func (client *sysdigSecureClient) GetMacroById(id int) (macro Macro, err error) { | ||
response, err := client.doSysdigSecureRequest(http.MethodGet, client.GetMacroUrl(id), nil) | ||
if err != nil { | ||
return | ||
} | ||
defer response.Body.Close() | ||
|
||
body, _ := ioutil.ReadAll(response.Body) | ||
|
||
if response.StatusCode != http.StatusOK { | ||
err = errors.New(response.Status) | ||
return | ||
} | ||
|
||
macro, err = MacroFromJSON(body) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if macro.Version == 0 { | ||
err = fmt.Errorf("Macro with ID: %d does not exists", id) | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (client *sysdigSecureClient) UpdateMacro(macroRequest Macro) (macro Macro, err error) { | ||
response, err := client.doSysdigSecureRequest(http.MethodPut, client.GetMacroUrl(macroRequest.ID), macroRequest.ToJSON()) | ||
if err != nil { | ||
return | ||
} | ||
defer response.Body.Close() | ||
|
||
body, _ := ioutil.ReadAll(response.Body) | ||
|
||
if response.StatusCode != http.StatusOK { | ||
err = errors.New(response.Status) | ||
return | ||
} | ||
|
||
return MacroFromJSON(body) | ||
} | ||
|
||
func (client *sysdigSecureClient) DeleteMacro(id int) error { | ||
response, err := client.doSysdigSecureRequest(http.MethodDelete, client.GetMacroUrl(id), nil) | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { | ||
return errors.New(response.Status) | ||
} | ||
return nil | ||
} | ||
|
||
func (client *sysdigSecureClient) GetMacrosUrl() string { | ||
return fmt.Sprintf("%s/api/secure/falco/macros", client.URL) | ||
} | ||
|
||
func (client *sysdigSecureClient) GetMacroUrl(id int) string { | ||
return fmt.Sprintf("%s/api/secure/falco/macros/%d", client.URL, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters