Skip to content

Commit 1776250

Browse files
authored
refactor(macro): refactor macro to use new v2 client (#311)
1 parent b1d4bea commit 1776250

File tree

8 files changed

+138
-132
lines changed

8 files changed

+138
-132
lines changed

sysdig/internal/client/secure/client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ type SysdigSecureClient interface {
2727
DeleteList(context.Context, int) error
2828
UpdateList(context.Context, List) (List, error)
2929

30-
CreateMacro(context.Context, Macro) (Macro, error)
31-
GetMacroById(context.Context, int) (Macro, error)
32-
DeleteMacro(context.Context, int) error
33-
UpdateMacro(context.Context, Macro) (Macro, error)
34-
3530
CreateVulnerabilityExceptionList(context.Context, *VulnerabilityExceptionList) (*VulnerabilityExceptionList, error)
3631
GetVulnerabilityExceptionListByID(context.Context, string) (*VulnerabilityExceptionList, error)
3732
DeleteVulnerabilityExceptionList(context.Context, string) error

sysdig/internal/client/secure/macros.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

sysdig/internal/client/secure/models.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -163,30 +163,6 @@ func ListFromJSON(body []byte) (list List, err error) {
163163
return
164164
}
165165

166-
// -------- Macro -------
167-
168-
type Macro struct {
169-
ID int `json:"id,omitempty"`
170-
Version int `json:"version,omitempty"`
171-
Name string `json:"name"`
172-
Condition MacroCondition `json:"condition"`
173-
Append bool `json:"append"`
174-
}
175-
176-
type MacroCondition struct {
177-
Condition string `json:"condition"`
178-
}
179-
180-
func (l *Macro) ToJSON() io.Reader {
181-
payload, _ := json.Marshal(l)
182-
return bytes.NewBuffer(payload)
183-
}
184-
185-
func MacroFromJSON(body []byte) (macro Macro, err error) {
186-
err = json.Unmarshal(body, &macro)
187-
return
188-
}
189-
190166
// -------- VulnerabilityExceptionList --------
191167

192168
type VulnerabilityExceptionList struct {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const (
10+
CreateMacroPath = "%s/api/secure/falco/macros"
11+
GetMacroByIDPath = "%s/api/secure/falco/macros/%d"
12+
UpdateMacroPath = "%s/api/secure/falco/macros/%d"
13+
DeleteMacroPath = "%s/api/secure/falco/macros/%d"
14+
)
15+
16+
type MacroInterface interface {
17+
CreateMacro(ctx context.Context, macro Macro) (Macro, error)
18+
GetMacroByID(ctx context.Context, id int) (Macro, error)
19+
UpdateMacro(ctx context.Context, macro Macro) (Macro, error)
20+
DeleteMacro(ctx context.Context, id int) error
21+
}
22+
23+
func (client *Client) CreateMacro(ctx context.Context, macro Macro) (Macro, error) {
24+
payload, err := Marshal(macro)
25+
if err != nil {
26+
return Macro{}, err
27+
}
28+
29+
response, err := client.requester.Request(ctx, http.MethodPost, client.CreateMacroURL(), payload)
30+
if err != nil {
31+
return Macro{}, err
32+
}
33+
defer response.Body.Close()
34+
35+
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
36+
return Macro{}, client.ErrorFromResponse(response)
37+
}
38+
39+
return Unmarshal[Macro](response.Body)
40+
}
41+
42+
func (client *Client) GetMacroByID(ctx context.Context, id int) (Macro, error) {
43+
response, err := client.requester.Request(ctx, http.MethodGet, client.GetMacroByIDURL(id), nil)
44+
if err != nil {
45+
return Macro{}, err
46+
}
47+
defer response.Body.Close()
48+
49+
if response.StatusCode != http.StatusOK {
50+
return Macro{}, client.ErrorFromResponse(response)
51+
}
52+
53+
macro, err := Unmarshal[Macro](response.Body)
54+
if err != nil {
55+
return Macro{}, err
56+
}
57+
58+
if macro.Version == 0 {
59+
return Macro{}, fmt.Errorf("macro with ID: %d does not exists", id)
60+
}
61+
62+
return macro, nil
63+
}
64+
65+
func (client *Client) UpdateMacro(ctx context.Context, macro Macro) (Macro, error) {
66+
payload, err := Marshal(macro)
67+
if err != nil {
68+
return Macro{}, err
69+
}
70+
71+
response, err := client.requester.Request(ctx, http.MethodPut, client.UpdateMacroURL(macro.ID), payload)
72+
if err != nil {
73+
return Macro{}, err
74+
}
75+
defer response.Body.Close()
76+
77+
if response.StatusCode != http.StatusOK {
78+
return Macro{}, client.ErrorFromResponse(response)
79+
}
80+
81+
return Unmarshal[Macro](response.Body)
82+
}
83+
84+
func (client *Client) DeleteMacro(ctx context.Context, id int) error {
85+
response, err := client.requester.Request(ctx, http.MethodDelete, client.DeleteMacroURL(id), nil)
86+
if err != nil {
87+
return err
88+
}
89+
defer response.Body.Close()
90+
91+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
92+
return client.ErrorFromResponse(response)
93+
}
94+
return nil
95+
}
96+
97+
func (client *Client) CreateMacroURL() string {
98+
return fmt.Sprintf(CreateMacroPath, client.config.url)
99+
}
100+
101+
func (client *Client) GetMacroByIDURL(id int) string {
102+
return fmt.Sprintf(GetMacroByIDPath, client.config.url, id)
103+
}
104+
105+
func (client *Client) UpdateMacroURL(id int) string {
106+
return fmt.Sprintf(UpdateMacroPath, client.config.url, id)
107+
}
108+
109+
func (client *Client) DeleteMacroURL(id int) string {
110+
return fmt.Sprintf(DeleteMacroPath, client.config.url, id)
111+
}

sysdig/internal/client/v2/model.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,15 @@ type Monitor struct {
176176
Metric string `json:"metric"`
177177
StdDevFactor float64 `json:"stdDevFactor"`
178178
}
179+
180+
type Macro struct {
181+
ID int `json:"id,omitempty"`
182+
Version int `json:"version,omitempty"`
183+
Name string `json:"name"`
184+
Condition MacroCondition `json:"condition"`
185+
Append bool `json:"append"`
186+
}
187+
188+
type MacroCondition struct {
189+
Condition string `json:"condition"`
190+
}

sysdig/internal/client/v2/sysdig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type SysdigMonitor interface {
2828

2929
type SysdigSecure interface {
3030
SysdigCommon
31+
MacroInterface
3132
}
3233

3334
func (sr *SysdigRequest) Request(ctx context.Context, method string, url string, payload io.Reader) (*http.Response, error) {

sysdig/resource_sysdig_secure_macro.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package sysdig
22

33
import (
44
"context"
5+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
56
"strconv"
67
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10-
11-
"github.com/draios/terraform-provider-sysdig/sysdig/internal/client/secure"
1211
)
1312

1413
func resourceSysdigSecureMacro() *schema.Resource {
@@ -53,8 +52,12 @@ func resourceSysdigSecureMacro() *schema.Resource {
5352
}
5453
}
5554

55+
func getSecureMacroClient(c SysdigClients) (v2.MacroInterface, error) {
56+
return c.sysdigSecureClientV2()
57+
}
58+
5659
func resourceSysdigMacroCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
57-
client, err := meta.(SysdigClients).sysdigSecureClient()
60+
client, err := getSecureMacroClient(meta.(SysdigClients))
5861
if err != nil {
5962
return diag.FromErr(err)
6063
}
@@ -72,7 +75,7 @@ func resourceSysdigMacroCreate(ctx context.Context, d *schema.ResourceData, meta
7275
}
7376

7477
func resourceSysdigMacroUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
75-
client, err := meta.(SysdigClients).sysdigSecureClient()
78+
client, err := getSecureMacroClient(meta.(SysdigClients))
7679
if err != nil {
7780
return diag.FromErr(err)
7881
}
@@ -91,13 +94,13 @@ func resourceSysdigMacroUpdate(ctx context.Context, d *schema.ResourceData, meta
9194
}
9295

9396
func resourceSysdigMacroRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
94-
client, err := meta.(SysdigClients).sysdigSecureClient()
97+
client, err := getSecureMacroClient(meta.(SysdigClients))
9598
if err != nil {
9699
return diag.FromErr(err)
97100
}
98101

99102
id, _ := strconv.Atoi(d.Id())
100-
macro, err := client.GetMacroById(ctx, id)
103+
macro, err := client.GetMacroByID(ctx, id)
101104

102105
if err != nil {
103106
d.SetId("")
@@ -112,7 +115,7 @@ func resourceSysdigMacroRead(ctx context.Context, d *schema.ResourceData, meta i
112115
}
113116

114117
func resourceSysdigMacroDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
115-
client, err := meta.(SysdigClients).sysdigSecureClient()
118+
client, err := getSecureMacroClient(meta.(SysdigClients))
116119
if err != nil {
117120
return diag.FromErr(err)
118121
}
@@ -126,10 +129,10 @@ func resourceSysdigMacroDelete(ctx context.Context, d *schema.ResourceData, meta
126129
return nil
127130
}
128131

129-
func macroFromResourceData(d *schema.ResourceData) secure.Macro {
130-
return secure.Macro{
132+
func macroFromResourceData(d *schema.ResourceData) v2.Macro {
133+
return v2.Macro{
131134
Name: d.Get("name").(string),
132135
Append: d.Get("append").(bool),
133-
Condition: secure.MacroCondition{Condition: d.Get("condition").(string)},
136+
Condition: v2.MacroCondition{Condition: d.Get("condition").(string)},
134137
}
135138
}

sysdig/resource_sysdig_secure_macro_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package sysdig_test
44

55
import (
66
"fmt"
7-
"os"
87
"testing"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -19,11 +18,7 @@ func TestAccMacro(t *testing.T) {
1918
fixedRandomText := rText()
2019

2120
resource.ParallelTest(t, resource.TestCase{
22-
PreCheck: func() {
23-
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
24-
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
25-
}
26-
},
21+
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv),
2722
ProviderFactories: map[string]func() (*schema.Provider, error){
2823
"sysdig": func() (*schema.Provider, error) {
2924
return sysdig.Provider(), nil

0 commit comments

Comments
 (0)