Skip to content

Commit

Permalink
add delete api (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
hila-krut-sysdig authored May 23, 2024
1 parent dd66925 commit a50ee6a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
20 changes: 20 additions & 0 deletions sysdig/internal/client/v2/posture_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ const (
PosturePolicyListPath = "%s/api/cspm/v1/policy/policies/list"
PosturePolicyCreatePath = "%s/api/cspm/v1/policy"
PosturePolicyGetPath = "%s/api/cspm/v1/policy/posture/policies/%d?include_controls=true"
PosturePolicyDeletePath = "%s/api/cspm/v1/policy/policies/%d"
)

type PosturePolicyInterface interface {
Base
ListPosturePolicies(ctx context.Context) ([]PosturePolicy, error)
CreateOrUpdatePosturePolicy(ctx context.Context, p *CreatePosturePolicy) (*FullPosturePolicy, string, error)
GetPosturePolicy(ctx context.Context, id int64) (*FullPosturePolicy, error)
DeletePosturePolicy(ctx context.Context, id int64) error
}

func (client *Client) ListPosturePolicies(ctx context.Context) ([]PosturePolicy, error) {
Expand Down Expand Up @@ -69,10 +71,28 @@ func (client *Client) GetPosturePolicy(ctx context.Context, id int64) (*FullPost
return &wrapper.Data, nil
}

func (client *Client) DeletePosturePolicy(ctx context.Context, id int64) error {
response, err := client.requester.Request(ctx, http.MethodDelete, client.deletePolicyUrl(id), nil)
if err != nil {
return err
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return client.ErrorFromResponse(response)
}

return nil
}

func (client *Client) getPolicyUrl(id int64) string {
return fmt.Sprintf(PosturePolicyGetPath, client.config.url, id)
}

func (client *Client) deletePolicyUrl(id int64) string {
return fmt.Sprintf(PosturePolicyDeletePath, client.config.url, id)
}

func (client *Client) getPosturePolicyURL(path string) string {
return fmt.Sprintf(path, client.config.url)
}
23 changes: 19 additions & 4 deletions sysdig/resource_sysdig_secure_posture_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,25 @@ func resourceSysdigSecurePosturePolicyRead(ctx context.Context, d *schema.Resour
return nil
}

func resourceSysdigSecurePosturePolicyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := getPosturePolicyClient(meta.(SysdigClients))
if err != nil {
return diag.FromErr(err)
}

id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
}

err = client.DeletePosturePolicy(ctx, id)
if err != nil {
return diag.FromErr(err)
}

return nil
}

func setGroups(d *schema.ResourceData, groups []v2.RequirementsGroup) error {
var groupsData []interface{}
for _, group := range groups {
Expand Down Expand Up @@ -343,10 +362,6 @@ func setControls(controls []v2.Control) []interface{} {
}
return controlsData
}
func resourceSysdigSecurePosturePolicyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// TODO: implement deletion
return nil
}

// Helper function to retrieve string value from ResourceData and handle nil case
func getStringValue(d *schema.ResourceData, key string) string {
Expand Down

0 comments on commit a50ee6a

Please sign in to comment.