Skip to content

Commit

Permalink
Allow managing ConfigurationVersion backing data
Browse files Browse the repository at this point in the history
  • Loading branch information
mwudka committed Nov 1, 2023
1 parent c954357 commit c80cc10
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
35 changes: 35 additions & 0 deletions configuration_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ type ConfigurationVersions interface {

// Download a configuration version. Only configuration versions in the uploaded state may be downloaded.
Download(ctx context.Context, cvID string) ([]byte, error)

// SoftDeleteBackingData soft deletes the configuration version's backing data
SoftDeleteBackingData(ctx context.Context, svID string) error

// RestoreBackingData restores a soft deleted configuration version's backing data
RestoreBackingData(ctx context.Context, svID string) error

// PermanentlyDeleteBackingData permanently deletes a soft deleted configuration version's backing data
PermanentlyDeleteBackingData(ctx context.Context, svID string) error
}

// configurationVersions implements ConfigurationVersions.
Expand Down Expand Up @@ -356,3 +365,29 @@ func (s *configurationVersions) Download(ctx context.Context, cvID string) ([]by

return buf.Bytes(), nil
}

func (s *configurationVersions) SoftDeleteBackingData(ctx context.Context, cvID string) error {
return s.manageBackingData(ctx, cvID, "soft_delete_backing_data")
}

func (s *configurationVersions) RestoreBackingData(ctx context.Context, cvID string) error {
return s.manageBackingData(ctx, cvID, "restore_backing_data")
}

func (s *configurationVersions) PermanentlyDeleteBackingData(ctx context.Context, cvID string) error {
return s.manageBackingData(ctx, cvID, "permanently_delete_backing_data")
}

func (s *configurationVersions) manageBackingData(ctx context.Context, cvID, action string) error {
if !validStringID(&cvID) {
return ErrInvalidStateVerID
}

u := fmt.Sprintf("configuration-versions/%s/actions/%s", cvID, action)
req, err := s.client.NewRequest("POST", u, nil)
if err != nil {
return err
}

return req.Do(ctx, nil)
}
44 changes: 44 additions & 0 deletions configuration_version_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,47 @@ func TestConfigurationVersions_Unmarshal(t *testing.T) {
assert.Equal(t, cv.Provisional, true)
assert.Equal(t, cv.Speculative, true)
}

func TestConfigurationVersions_ManageBackingData(t *testing.T) {
client := testClient(t)
ctx := context.Background()

workspace, workspaceCleanup := createWorkspace(t, client, nil)
t.Cleanup(workspaceCleanup)

nonCurrentCv, uploadedCvCleanup := createUploadedConfigurationVersion(t, client, workspace)
defer uploadedCvCleanup()

_, uploadedCvCleanup = createUploadedConfigurationVersion(t, client, workspace)
defer uploadedCvCleanup()

t.Run("soft delete backing data", func(t *testing.T) {
err := client.ConfigurationVersions.SoftDeleteBackingData(ctx, nonCurrentCv.ID)
require.NoError(t, err)

_, err = client.ConfigurationVersions.Download(ctx, nonCurrentCv.ID)
assert.Equal(t, ErrResourceNotFound, err)
})

t.Run("restore backing data", func(t *testing.T) {
err := client.ConfigurationVersions.RestoreBackingData(ctx, nonCurrentCv.ID)
require.NoError(t, err)

_, err = client.ConfigurationVersions.Download(ctx, nonCurrentCv.ID)
require.NoError(t, err)
})

t.Run("permanently delete backing data", func(t *testing.T) {
err := client.ConfigurationVersions.SoftDeleteBackingData(ctx, nonCurrentCv.ID)
require.NoError(t, err)

err = client.ConfigurationVersions.PermanentlyDeleteBackingData(ctx, nonCurrentCv.ID)
require.NoError(t, err)

err = client.ConfigurationVersions.RestoreBackingData(ctx, nonCurrentCv.ID)
require.ErrorContainsf(t, err, "transition not allowed", "Restore backing data should fail")

_, err = client.ConfigurationVersions.Download(ctx, nonCurrentCv.ID)
assert.Equal(t, ErrResourceNotFound, err)
})
}

0 comments on commit c80cc10

Please sign in to comment.