99 "encoding/json"
1010 "fmt"
1111 "net/http"
12+ "path"
1213
1314 "github.com/elastic/elastic-package/internal/packages"
1415)
@@ -244,7 +245,7 @@ func (c *Client) AddPackageDataStreamToPolicy(ctx context.Context, r PackageData
244245 return fmt .Errorf ("could not convert policy-package (request) to JSON: %w" , err )
245246 }
246247
247- statusCode , respBody , err := c .post (ctx , fmt . Sprintf ( "%s/ package_policies", FleetAPI ), reqBody )
248+ statusCode , respBody , err := c .post (ctx , path . Join ( FleetAPI , " package_policies" ), reqBody )
248249 if err != nil {
249250 return fmt .Errorf ("could not add package to policy: %w" , err )
250251 }
@@ -289,9 +290,9 @@ func (c *Client) CreatePackagePolicy(ctx context.Context, p PackagePolicy) (*Pac
289290 return nil , fmt .Errorf ("could not convert package policy (request) to JSON: %w" , err )
290291 }
291292
292- statusCode , respBody , err := c .post (ctx , fmt . Sprintf ( "%s/ package_policies", FleetAPI ), reqBody )
293+ statusCode , respBody , err := c .post (ctx , path . Join ( FleetAPI , " package_policies" ), reqBody )
293294 if err != nil {
294- return nil , fmt .Errorf ("could not create package policy (req %s): %w" , string ( reqBody ) , err )
295+ return nil , fmt .Errorf ("could not create package policy (req %s): %w" , reqBody , err )
295296 }
296297
297298 if statusCode != http .StatusOK {
@@ -315,9 +316,73 @@ func (c *Client) CreatePackagePolicy(ctx context.Context, p PackagePolicy) (*Pac
315316 return & p , nil
316317}
317318
319+ // ListRawPackagePolicies fetches all the Package Policies in Fleet.
320+ func (c * Client ) ListRawPackagePolicies (ctx context.Context ) ([]json.RawMessage , error ) {
321+ itemsRetrieved := 0
322+ currentPage := 1
323+ var items []json.RawMessage
324+ var resp struct {
325+ Items []json.RawMessage `json:"items"`
326+ Total int `json:"total"`
327+ Page int `json:"page"`
328+ PerPage int `json:"perPage"`
329+ }
330+
331+ for finished := false ; ! finished ; finished = itemsRetrieved == resp .Total {
332+ statusCode , respBody , err := c .get (ctx , fmt .Sprintf ("%s?showUpgradeable=true&page=%d" , path .Join (FleetAPI , "package_policies" ), currentPage ))
333+ if err != nil {
334+ return nil , fmt .Errorf ("could not get policies: %w" , err )
335+ }
336+
337+ if statusCode != http .StatusOK {
338+ return nil , fmt .Errorf ("could not get policies; API status code = %d; response body = %s" , statusCode , respBody )
339+ }
340+
341+ if err := json .Unmarshal (respBody , & resp ); err != nil {
342+ return nil , fmt .Errorf ("could not convert policies (response) to JSON: %w" , err )
343+ }
344+
345+ itemsRetrieved += len (resp .Items )
346+ currentPage += 1
347+ items = append (items , resp .Items ... )
348+ }
349+
350+ return items , nil
351+ }
352+
353+ // UpgradePackagePolicyToLatest upgrades the given package in Fleet to the latest available version.
354+ func (c * Client ) UpgradePackagePolicyToLatest (ctx context.Context , policyIDs ... string ) error {
355+ var req struct {
356+ PackagePolicyIds []string `json:"packagePolicyIds"`
357+ }
358+ req .PackagePolicyIds = policyIDs
359+ body , err := json .Marshal (req )
360+ if err != nil {
361+ return fmt .Errorf ("could not convert package policy (request) to JSON: %w" , err )
362+ }
363+ statusCode , respBody , err := c .post (ctx , path .Join (FleetAPI , "package_policies/upgrade" ), body )
364+ if err != nil {
365+ return fmt .Errorf ("could not create package policy (req %s): %w" , body , err )
366+ }
367+ if statusCode == http .StatusBadRequest {
368+ var resp struct {
369+ Message string `json:"message"`
370+ }
371+ err := json .Unmarshal (respBody , & resp )
372+ if err != nil {
373+ return fmt .Errorf ("could not upgrade package: %q" , respBody )
374+ }
375+ return fmt .Errorf ("could not upgrade package: %s" , resp .Message )
376+ }
377+ if statusCode != http .StatusOK {
378+ return fmt .Errorf ("could not create package policy (req %s); API status code = %d; response body = %s" , body , statusCode , respBody )
379+ }
380+ return nil
381+ }
382+
318383// DeletePackagePolicy removes the given Package Policy from Fleet.
319384func (c * Client ) DeletePackagePolicy (ctx context.Context , p PackagePolicy ) error {
320- statusCode , respBody , err := c .delete (ctx , fmt . Sprintf ( "%s/ package_policies/%s" , FleetAPI , p .ID ))
385+ statusCode , respBody , err := c .delete (ctx , path . Join ( FleetAPI , " package_policies" , p .ID ))
321386 if err != nil {
322387 return fmt .Errorf ("could not delete package policy: %w" , err )
323388 }
0 commit comments