-
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_list resource
Signed-off-by: Federico Barcelona <fede_rico_94@hotmail.com>
- Loading branch information
1 parent
61ad471
commit ee4828b
Showing
7 changed files
with
327 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package sysdig | ||
|
||
import ( | ||
"github.com/draios/terraform-provider-sysdig/sysdig/secure" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func resourceSysdigSecureList() *schema.Resource { | ||
timeout := 30 * time.Second | ||
|
||
return &schema.Resource{ | ||
Create: resourceSysdigListCreate, | ||
Update: resourceSysdigListUpdate, | ||
Read: resourceSysdigListRead, | ||
Delete: resourceSysdigListDelete, | ||
|
||
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, | ||
}, | ||
"items": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"append": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
"version": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceSysdigListCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
list := listFromResourceData(d) | ||
list, err := client.CreateList(list) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(strconv.Itoa(list.ID)) | ||
d.Set("version", list.Version) | ||
|
||
return nil | ||
} | ||
|
||
func resourceSysdigListUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
list := listFromResourceData(d) | ||
list.Version = d.Get("version").(int) | ||
|
||
id, _ := strconv.Atoi(d.Id()) | ||
list.ID = id | ||
|
||
_, err := client.UpdateList(list) | ||
return err | ||
} | ||
|
||
func resourceSysdigListRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
id, _ := strconv.Atoi(d.Id()) | ||
list, err := client.GetListById(id) | ||
|
||
if err != nil { | ||
d.SetId("") | ||
} | ||
|
||
d.Set("name", list.Name) | ||
d.Set("version", list.Version) | ||
d.Set("items", list.Items.Items) | ||
d.Set("append", list.Append) | ||
|
||
return nil | ||
} | ||
|
||
func resourceSysdigListDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*SysdigClients).sysdigSecureClient | ||
|
||
id, _ := strconv.Atoi(d.Id()) | ||
|
||
return client.DeleteList(id) | ||
} | ||
|
||
func listFromResourceData(d *schema.ResourceData) secure.List { | ||
list := secure.List{ | ||
Name: d.Get("name").(string), | ||
Append: d.Get("append").(bool), | ||
Items: secure.Items{Items: []string{}}, | ||
} | ||
|
||
items := d.Get("items").([]interface{}) | ||
for _, item := range items { | ||
if item_str, ok := item.(string); ok { | ||
item_str = strings.TrimSpace(item_str) | ||
list.Items.Items = append(list.Items.Items, item_str) | ||
} | ||
} | ||
|
||
return list | ||
} |
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,86 @@ | ||
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 TestAccList(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: listWithName(rText()), | ||
}, | ||
{ | ||
Config: listWithName(fixedRandomText), | ||
}, | ||
{ | ||
Config: listUpdatedWithName(fixedRandomText), | ||
}, | ||
{ | ||
Config: listAppendToDefault(), | ||
}, | ||
{ | ||
Config: listWithList(rText(), rText()), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func listWithName(name string) string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_list" "sample" { | ||
name = "terraform_test_%s" | ||
items = ["foo", "bar"] | ||
} | ||
`, name) | ||
} | ||
|
||
func listUpdatedWithName(name string) string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_list" "sample" { | ||
name = "terraform_test_%s" | ||
items = ["foo", "bar", "baz"] | ||
} | ||
`, name) | ||
} | ||
|
||
func listAppendToDefault() string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_list" "sample2" { | ||
name = "allowed_k8s_nodes" | ||
items = ["foo", "bar"] | ||
append = true | ||
} | ||
`) | ||
} | ||
|
||
func listWithList(name1, name2 string) string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_secure_list" "sample3" { | ||
name = "terraform_test_%s" | ||
items = ["foo", "bar"] | ||
} | ||
resource "sysdig_secure_list" "sample4" { | ||
name = "terraform_test_%s" | ||
items = [sysdig_secure_list.sample3.name] | ||
} | ||
`, 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) CreateList(listRequest List) (list List, err error) { | ||
response, err := client.doSysdigSecureRequest(http.MethodPost, client.GetListsUrl(), listRequest.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 | ||
} | ||
|
||
list, err = ListFromJSON(body) | ||
return | ||
} | ||
|
||
func (client *sysdigSecureClient) GetListById(id int) (list List, err error) { | ||
response, err := client.doSysdigSecureRequest(http.MethodGet, client.GetListUrl(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 | ||
} | ||
|
||
list, err = ListFromJSON(body) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if list.Version == 0 { | ||
err = fmt.Errorf("List with ID: %d does not exists", id) | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (client *sysdigSecureClient) UpdateList(listRequest List) (list List, err error) { | ||
response, err := client.doSysdigSecureRequest(http.MethodPut, client.GetListUrl(listRequest.ID), listRequest.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 ListFromJSON(body) | ||
} | ||
|
||
func (client *sysdigSecureClient) DeleteList(id int) error { | ||
response, err := client.doSysdigSecureRequest(http.MethodDelete, client.GetListUrl(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) GetListsUrl() string { | ||
return fmt.Sprintf("%s/api/secure/falco/lists", client.URL) | ||
} | ||
|
||
func (client *sysdigSecureClient) GetListUrl(id int) string { | ||
return fmt.Sprintf("%s/api/secure/falco/lists/%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
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