|  | 
|  | 1 | +package v2 | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"errors" | 
|  | 6 | +	"fmt" | 
|  | 7 | +	"net/http" | 
|  | 8 | +	"strconv" | 
|  | 9 | +) | 
|  | 10 | + | 
|  | 11 | +const ( | 
|  | 12 | +	vulnerabilityRuleBundlesPath = "%s/secure/vulnerability/v1/bundles" | 
|  | 13 | +	vulnerabilityRuleBundlePath  = "%s/secure/vulnerability/v1/bundles/%s" | 
|  | 14 | +) | 
|  | 15 | + | 
|  | 16 | +type VulnerabilityRuleBundleClient interface { | 
|  | 17 | +	CreateVulnerabilityRuleBundle(ctx context.Context, vulnerabilityRuleBundle VulnerabilityRuleBundle) (VulnerabilityRuleBundle, error) | 
|  | 18 | +	GetVulnerabilityRuleBundleByID(ctx context.Context, vulnerabilityRuleBundleID string) (VulnerabilityRuleBundle, error) | 
|  | 19 | +	UpdateVulnerabilityRuleBundle(ctx context.Context, vulnerabilityRuleBundle VulnerabilityRuleBundle) (VulnerabilityRuleBundle, error) | 
|  | 20 | +	DeleteVulnerabilityRuleBundleByID(ctx context.Context, vulnerabilityRuleBundleID string) error | 
|  | 21 | +} | 
|  | 22 | + | 
|  | 23 | +func (c *Client) CreateVulnerabilityRuleBundle(ctx context.Context, vulnerabilityRuleBundle VulnerabilityRuleBundle) (ruleBundle VulnerabilityRuleBundle, err error) { | 
|  | 24 | +	payload, err := Marshal(vulnerabilityRuleBundle) | 
|  | 25 | +	if err != nil { | 
|  | 26 | +		return VulnerabilityRuleBundle{}, err | 
|  | 27 | +	} | 
|  | 28 | + | 
|  | 29 | +	response, err := c.requester.Request(ctx, http.MethodPost, c.vulnerabilityRuleBundlesURL(), payload) | 
|  | 30 | +	if err != nil { | 
|  | 31 | +		return VulnerabilityRuleBundle{}, err | 
|  | 32 | +	} | 
|  | 33 | +	defer func() { | 
|  | 34 | +		if dErr := response.Body.Close(); dErr != nil { | 
|  | 35 | +			err = fmt.Errorf("unable to close response body: %w", dErr) | 
|  | 36 | +		} | 
|  | 37 | +	}() | 
|  | 38 | + | 
|  | 39 | +	if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { | 
|  | 40 | +		return VulnerabilityRuleBundle{}, c.ErrorFromResponse(response) | 
|  | 41 | +	} | 
|  | 42 | + | 
|  | 43 | +	return Unmarshal[VulnerabilityRuleBundle](response.Body) | 
|  | 44 | +} | 
|  | 45 | + | 
|  | 46 | +func (c *Client) GetVulnerabilityRuleBundleByID(ctx context.Context, vulnerabilityRuleBundleID string) (ruleBundle VulnerabilityRuleBundle, err error) { | 
|  | 47 | +	response, err := c.requester.Request(ctx, http.MethodGet, c.vulnerabilityRuleBundleURL(vulnerabilityRuleBundleID), nil) | 
|  | 48 | +	if err != nil { | 
|  | 49 | +		return VulnerabilityRuleBundle{}, err | 
|  | 50 | +	} | 
|  | 51 | +	defer func() { | 
|  | 52 | +		if dErr := response.Body.Close(); dErr != nil { | 
|  | 53 | +			err = fmt.Errorf("unable to close response body: %w", dErr) | 
|  | 54 | +		} | 
|  | 55 | +	}() | 
|  | 56 | + | 
|  | 57 | +	if response.StatusCode != http.StatusOK { | 
|  | 58 | +		return VulnerabilityRuleBundle{}, c.ErrorFromResponse(response) | 
|  | 59 | +	} | 
|  | 60 | + | 
|  | 61 | +	return Unmarshal[VulnerabilityRuleBundle](response.Body) | 
|  | 62 | +} | 
|  | 63 | + | 
|  | 64 | +func (c *Client) UpdateVulnerabilityRuleBundle(ctx context.Context, vulnerabilityRuleBundle VulnerabilityRuleBundle) (ruleBundle VulnerabilityRuleBundle, err error) { | 
|  | 65 | +	if vulnerabilityRuleBundle.ID == nil { | 
|  | 66 | +		return VulnerabilityRuleBundle{}, errors.New("rule bundle id was null") | 
|  | 67 | +	} | 
|  | 68 | + | 
|  | 69 | +	payload, err := Marshal(vulnerabilityRuleBundle) | 
|  | 70 | +	if err != nil { | 
|  | 71 | +		return VulnerabilityRuleBundle{}, err | 
|  | 72 | +	} | 
|  | 73 | + | 
|  | 74 | +	idAsStr := strconv.Itoa(int(*vulnerabilityRuleBundle.ID)) | 
|  | 75 | +	response, err := c.requester.Request(ctx, http.MethodPut, c.vulnerabilityRuleBundleURL(idAsStr), payload) | 
|  | 76 | +	if err != nil { | 
|  | 77 | +		return VulnerabilityRuleBundle{}, err | 
|  | 78 | +	} | 
|  | 79 | +	defer func() { | 
|  | 80 | +		if dErr := response.Body.Close(); dErr != nil { | 
|  | 81 | +			err = fmt.Errorf("unable to close response body: %w", dErr) | 
|  | 82 | +		} | 
|  | 83 | +	}() | 
|  | 84 | + | 
|  | 85 | +	if response.StatusCode != http.StatusOK { | 
|  | 86 | +		return VulnerabilityRuleBundle{}, c.ErrorFromResponse(response) | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +	return Unmarshal[VulnerabilityRuleBundle](response.Body) | 
|  | 90 | +} | 
|  | 91 | + | 
|  | 92 | +func (c *Client) DeleteVulnerabilityRuleBundleByID(ctx context.Context, vulnerabilityRuleBundleID string) (err error) { | 
|  | 93 | +	response, err := c.requester.Request(ctx, http.MethodDelete, c.vulnerabilityRuleBundleURL(vulnerabilityRuleBundleID), nil) | 
|  | 94 | +	if err != nil { | 
|  | 95 | +		return err | 
|  | 96 | +	} | 
|  | 97 | +	defer func() { | 
|  | 98 | +		if dErr := response.Body.Close(); dErr != nil { | 
|  | 99 | +			err = fmt.Errorf("unable to close response body: %w", dErr) | 
|  | 100 | +		} | 
|  | 101 | +	}() | 
|  | 102 | + | 
|  | 103 | +	if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { | 
|  | 104 | +		return c.ErrorFromResponse(response) | 
|  | 105 | +	} | 
|  | 106 | + | 
|  | 107 | +	return err | 
|  | 108 | +} | 
|  | 109 | + | 
|  | 110 | +func (c *Client) vulnerabilityRuleBundlesURL() string { | 
|  | 111 | +	return fmt.Sprintf(vulnerabilityRuleBundlesPath, c.config.url) | 
|  | 112 | +} | 
|  | 113 | + | 
|  | 114 | +func (c *Client) vulnerabilityRuleBundleURL(vulnerabilityRuleBundleID string) string { | 
|  | 115 | +	return fmt.Sprintf(vulnerabilityRuleBundlePath, c.config.url, vulnerabilityRuleBundleID) | 
|  | 116 | +} | 
0 commit comments