2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2016-09-04 05:13:42 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ApplyPackSpecs(ctx context.Context, specs []*fleet.PackSpec) error {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
return svc.ds.ApplyPackSpecs(specs)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) GetPackSpecs(ctx context.Context) ([]*fleet.PackSpec, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
return svc.ds.GetPackSpecs()
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) GetPackSpec(ctx context.Context, name string) (*fleet.PackSpec, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-08 01:54:29 +00:00
|
|
|
return svc.ds.GetPackSpec(name)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListPacks(ctx context.Context, opt fleet.ListOptions) ([]*fleet.Pack, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
return svc.ds.ListPacks(opt)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) GetPack(ctx context.Context, id uint) (*fleet.Pack, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
return svc.ds.Pack(id)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) NewPack(ctx context.Context, p fleet.PackPayload) (*fleet.Pack, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
var pack fleet.Pack
|
2018-06-15 14:13:11 +00:00
|
|
|
|
|
|
|
if p.Name != nil {
|
|
|
|
pack.Name = *p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Description != nil {
|
|
|
|
pack.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Platform != nil {
|
|
|
|
pack.Platform = *p.Platform
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Disabled != nil {
|
|
|
|
pack.Disabled = *p.Disabled
|
|
|
|
}
|
|
|
|
|
2021-07-12 13:48:50 +00:00
|
|
|
if p.HostIDs != nil {
|
|
|
|
pack.HostIDs = *p.HostIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.LabelIDs != nil {
|
|
|
|
pack.LabelIDs = *p.LabelIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.TeamIDs != nil {
|
|
|
|
pack.TeamIDs = *p.TeamIDs
|
|
|
|
}
|
|
|
|
|
2018-06-15 14:13:11 +00:00
|
|
|
_, err := svc.ds.NewPack(&pack)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pack, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ModifyPack(ctx context.Context, id uint, p fleet.PackPayload) (*fleet.Pack, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-15 14:13:11 +00:00
|
|
|
pack, err := svc.ds.Pack(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Name != nil {
|
|
|
|
pack.Name = *p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Description != nil {
|
|
|
|
pack.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Platform != nil {
|
|
|
|
pack.Platform = *p.Platform
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Disabled != nil {
|
|
|
|
pack.Disabled = *p.Disabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.HostIDs != nil {
|
2021-06-18 16:43:16 +00:00
|
|
|
pack.HostIDs = *p.HostIDs
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.LabelIDs != nil {
|
2021-06-18 16:43:16 +00:00
|
|
|
pack.LabelIDs = *p.LabelIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.TeamIDs != nil {
|
|
|
|
pack.TeamIDs = *p.TeamIDs
|
|
|
|
}
|
2018-06-15 14:13:11 +00:00
|
|
|
|
2021-06-18 16:43:16 +00:00
|
|
|
err = svc.ds.SavePack(pack)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pack, err
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
func (svc *Service) DeletePack(ctx context.Context, name string) error {
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-04 18:05:55 +00:00
|
|
|
return svc.ds.DeletePack(name)
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
func (svc *Service) DeletePackByID(ctx context.Context, id uint) error {
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-15 14:13:11 +00:00
|
|
|
pack, err := svc.ds.Pack(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return svc.ds.DeletePack(pack.Name)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListPacksForHost(ctx context.Context, hid uint) ([]*fleet.Pack, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Pack{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
return svc.ds.ListPacksForHost(hid)
|
2016-10-17 19:30:47 +00:00
|
|
|
}
|