Skip to content

Commit

Permalink
Fixes 2310: fix 404 err on delete task (#353)
Browse files Browse the repository at this point in the history
* Fixes 2310: fix 404 err on delete task

if no domain was ever created
  • Loading branch information
jlsherrill authored Aug 9, 2023
1 parent 1d9ef38 commit 8e90514
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 19 deletions.
2 changes: 2 additions & 0 deletions pkg/pulp_client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import zest "github.com/content-services/zest/release/v2023"
type PulpGlobalClient interface {
// Domains
LookupOrCreateDomain(name string) (*string, error)
LookupDomain(name string) (*string, error)

// Tasks
GetTask(taskHref string) (zest.TaskResponse, error)
Expand Down Expand Up @@ -47,6 +48,7 @@ type PulpClient interface {

// Domains
LookupOrCreateDomain(name string) (*string, error)
LookupDomain(name string) (*string, error)

// Status
Status() (*zest.StatusResponse, error)
Expand Down
26 changes: 26 additions & 0 deletions pkg/pulp_client/pulp_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions pkg/pulp_client/pulp_global_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 41 additions & 16 deletions pkg/tasks/delete_repository_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,46 @@ type DeleteRepositorySnapshotsPayload struct {

type DeleteRepositorySnapshots struct {
daoReg *dao.DaoRegistry
pulpClient pulp_client.PulpClient
pulpClient *pulp_client.PulpClient
payload *DeleteRepositorySnapshotsPayload
task *models.TaskInfo
ctx context.Context
}

// This org may or may not have a domain created in pulp, so make sure the domain exists and if not, return a nil pulpClient
func lookupOptionalPulpClient(ctx context.Context, globalClient pulp_client.PulpGlobalClient, task *models.TaskInfo, daoReg *dao.DaoRegistry) (*pulp_client.PulpClient, error) {
domainName, err := daoReg.Domain.FetchOrCreateDomain(task.OrgId)
if err != nil {
return nil, err
}

// If we are deleting a repo from an org that's never done snapshotting,
// it will not have a domain created
domainFound, err := globalClient.LookupDomain(domainName)
if err != nil {
return nil, err
}
if domainFound != nil {
client := pulp_client.GetPulpClientWithDomain(ctx, domainName)
return &client, nil
}
return nil, nil
}

func DeleteSnapshotHandler(ctx context.Context, task *models.TaskInfo, _ *queue.Queue) error {
opts := DeleteRepositorySnapshotsPayload{}
if err := json.Unmarshal(task.Payload, &opts); err != nil {
return fmt.Errorf("payload incorrect type for " + config.DeleteRepositorySnapshotsTask)
}
daoReg := dao.GetDaoRegistry(db.DB)
domainName, err := daoReg.Domain.FetchOrCreateDomain(task.OrgId)
logger := LogForTask(task.Id.String(), task.Typename, task.RequestID)
ctxWithLogger := logger.WithContext(ctx)
globalPulpClient := pulp_client.GetGlobalPulpClient(ctxWithLogger)

pulpClient, err := lookupOptionalPulpClient(ctxWithLogger, globalPulpClient, task, daoReg)
if err != nil {
return err
}

logger := LogForTask(task.Id.String(), task.Typename, task.RequestID)
ctxWithLogger := logger.WithContext(context.Background())
pulpClient := pulp_client.GetPulpClientWithDomain(ctxWithLogger, domainName)

ds := DeleteRepositorySnapshots{
daoReg: daoReg,
pulpClient: pulpClient,
Expand All @@ -53,7 +72,9 @@ func DeleteSnapshotHandler(ctx context.Context, task *models.TaskInfo, _ *queue.

func (d *DeleteRepositorySnapshots) Run() error {
var err error
if config.PulpConfigured() {

// If pulp client is deleted, the org never had a domain created
if config.PulpConfigured() && d.pulpClient != nil {
snaps, _ := d.fetchSnapshots()
for _, snap := range snaps {
_, err := d.deleteRpmDistribution(snap)
Expand All @@ -78,50 +99,54 @@ func (d *DeleteRepositorySnapshots) Run() error {
return nil
}

func (d *DeleteRepositorySnapshots) getPulpClient() pulp_client.PulpClient {
return *d.pulpClient
}

func (d *DeleteRepositorySnapshots) fetchSnapshots() ([]models.Snapshot, error) {
return d.daoReg.Snapshot.FetchForRepoConfigUUID(d.payload.RepoConfigUUID)
}

func (d *DeleteRepositorySnapshots) deleteRpmDistribution(snap models.Snapshot) (*zest.TaskResponse, error) {
deleteDistributionHref, err := d.pulpClient.DeleteRpmDistribution(snap.DistributionHref)
deleteDistributionHref, err := d.getPulpClient().DeleteRpmDistribution(snap.DistributionHref)
if err != nil {
return nil, err
}
task, err := d.pulpClient.PollTask(deleteDistributionHref)
task, err := d.getPulpClient().PollTask(deleteDistributionHref)
if err != nil {
return task, err
}
return task, nil
}

func (d *DeleteRepositorySnapshots) deleteRpmRepoAndRemote() (taskRepo, taskRemote *zest.TaskResponse, err error) {
remoteResp, err := d.pulpClient.GetRpmRemoteByName(d.payload.RepoConfigUUID)
remoteResp, err := d.getPulpClient().GetRpmRemoteByName(d.payload.RepoConfigUUID)
if err != nil {
return nil, nil, err
}
if remoteResp != nil {
remoteHref := remoteResp.PulpHref
deleteRemoteHref, err := d.pulpClient.DeleteRpmRemote(*remoteHref)
deleteRemoteHref, err := d.getPulpClient().DeleteRpmRemote(*remoteHref)
if err != nil {
return taskRepo, nil, err
}
taskRemote, err = d.pulpClient.PollTask(deleteRemoteHref)
taskRemote, err = d.getPulpClient().PollTask(deleteRemoteHref)
if err != nil {
return taskRepo, taskRemote, err
}
}

repoResp, err := d.pulpClient.GetRpmRepositoryByName(d.payload.RepoConfigUUID)
repoResp, err := d.getPulpClient().GetRpmRepositoryByName(d.payload.RepoConfigUUID)
if err != nil {
return nil, nil, err
}
if repoResp != nil {
repoHref := repoResp.PulpHref
deleteRepoHref, err := d.pulpClient.DeleteRpmRepository(*repoHref)
deleteRepoHref, err := d.getPulpClient().DeleteRpmRepository(*repoHref)
if err != nil {
return nil, nil, err
}
taskRepo, err = d.pulpClient.PollTask(deleteRepoHref)
taskRepo, err = d.getPulpClient().PollTask(deleteRepoHref)
if err != nil {
return taskRepo, nil, err
}
Expand Down
68 changes: 65 additions & 3 deletions pkg/tasks/delete_repository_snapshots_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tasks

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -28,14 +29,73 @@ func TestDeleteSnapshotSuite(t *testing.T) {
suite.Run(t, new(DeleteRepositorySnapshotsSuite))
}

func (s *DeleteRepositorySnapshotsSuite) pulpClient() pulp_client.PulpClient {
return &s.MockPulpClient
}

func (s *DeleteRepositorySnapshotsSuite) SetupTest() {
s.mockDaoRegistry = dao.GetMockDaoRegistry(s.T())
s.MockPulpClient = *pulp_client.NewMockPulpClient(s.T())
s.MockQueue = *queue.NewMockQueue(s.T())
s.Queue = &s.MockQueue
}

func (s *DeleteRepositorySnapshotsSuite) TestDeleteNoSnapshots() {
func (s *DeleteRepositorySnapshotsSuite) TestLookupOptionalPulpClient() {
task := models.TaskInfo{
Id: uuid.UUID{},
OrgId: uuid.NewString(),
RepositoryUUID: uuid.New(),
}

s.mockDaoRegistry.Domain.On("FetchOrCreateDomain", task.OrgId).Return("myDomain", nil)
s.MockPulpClient.On("LookupDomain", "myDomain").Return(pointy.Pointer("somepath"), nil)
found, err := lookupOptionalPulpClient(context.Background(), s.pulpClient(), &task, s.mockDaoRegistry.ToDaoRegistry())
assert.NoError(s.T(), err)
assert.NotNil(s.T(), found)
}

func (s *DeleteRepositorySnapshotsSuite) TestLookupOptionalPulpClientNil() {
task := models.TaskInfo{
Id: uuid.UUID{},
OrgId: uuid.NewString(),
RepositoryUUID: uuid.New(),
}

s.mockDaoRegistry.Domain.On("FetchOrCreateDomain", task.OrgId).Return("myDomain", nil)
s.MockPulpClient.On("LookupDomain", "myDomain").Return(nil, nil)
found, err := lookupOptionalPulpClient(context.Background(), s.pulpClient(), &task, s.mockDaoRegistry.ToDaoRegistry())
assert.NoError(s.T(), err)
assert.Nil(s.T(), found)
}

func (s *DeleteRepositorySnapshotsSuite) TestDeleteNoSnapshotsWithoutClient() {
repoUuid := uuid.New()
repo := dao.Repository{UUID: repoUuid.String(), URL: "http://random.example.com/thing"}
repoConfig := api.RepositoryResponse{OrgID: "OrgId", UUID: uuid.NewString(), URL: repo.URL}
task := models.TaskInfo{
Id: uuid.UUID{},
OrgId: repoConfig.OrgID,
RepositoryUUID: repoUuid,
}

s.mockDaoRegistry.Snapshot.On("FetchForRepoConfigUUID", repoConfig.UUID).Return([]models.Snapshot{}, nil).Once()
s.mockDaoRegistry.RepositoryConfig.On("Delete", repoConfig.OrgID, repoConfig.UUID).Return(nil).Once()

payload := DeleteRepositorySnapshotsPayload{
RepoConfigUUID: repoConfig.UUID,
}
snap := DeleteRepositorySnapshots{
daoReg: s.mockDaoRegistry.ToDaoRegistry(),
pulpClient: nil,
payload: &payload,
task: &task,
ctx: nil,
}
snapErr := snap.Run()
assert.NoError(s.T(), snapErr)
}

func (s *DeleteRepositorySnapshotsSuite) TestDeleteNoSnapshotsWithClient() {
repoUuid := uuid.New()
repo := dao.Repository{UUID: repoUuid.String(), URL: "http://random.example.com/thing"}
repoConfig := api.RepositoryResponse{OrgID: "OrgId", UUID: uuid.NewString(), URL: repo.URL}
Expand All @@ -54,9 +114,10 @@ func (s *DeleteRepositorySnapshotsSuite) TestDeleteNoSnapshots() {
payload := DeleteRepositorySnapshotsPayload{
RepoConfigUUID: repoConfig.UUID,
}
pulpClient := s.pulpClient()
snap := DeleteRepositorySnapshots{
daoReg: s.mockDaoRegistry.ToDaoRegistry(),
pulpClient: &s.MockPulpClient,
pulpClient: &pulpClient,
payload: &payload,
task: &task,
ctx: nil,
Expand Down Expand Up @@ -106,9 +167,10 @@ func (s *DeleteRepositorySnapshotsSuite) TestDeleteSnapshotFull() {
payload := DeleteRepositorySnapshotsPayload{
RepoConfigUUID: repoConfig.UUID,
}
pulpClient := s.pulpClient()
snap := DeleteRepositorySnapshots{
daoReg: s.mockDaoRegistry.ToDaoRegistry(),
pulpClient: &s.MockPulpClient,
pulpClient: &pulpClient,
payload: &payload,
task: &task,
ctx: nil,
Expand Down
Loading

0 comments on commit 8e90514

Please sign in to comment.