-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for cleanup policies
- Loading branch information
1 parent
89cc312
commit 52307b0
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cleanup | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/client" | ||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" | ||
"github.com/datadrivers/go-nexus-client/nexus3/schema/cleanuppolicies" | ||
) | ||
|
||
const ( | ||
cleanupAPIEndpoint = client.BasePath + "v1/cleanup-policies" | ||
) | ||
|
||
type CleanupPolicyService client.Service | ||
|
||
func NewCleanupPolicyService(c *client.Client) *CleanupPolicyService { | ||
return &CleanupPolicyService{Client: c} | ||
} | ||
|
||
func (s *CleanupPolicyService) Create(policy *cleanuppolicies.CleanupPolicy) error { | ||
data, err := tools.JsonMarshalInterfaceToIOReader(policy) | ||
if err != nil { | ||
return err | ||
} | ||
body, resp, err := s.Client.Post(cleanupAPIEndpoint, data) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { | ||
return fmt.Errorf("could not create cleanup policy '%s': HTTP: %d, %s", policy.Name, resp.StatusCode, string(body)) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *CleanupPolicyService) Get(name string) (*cleanuppolicies.CleanupPolicy, error) { | ||
|
||
body, resp, err := s.Client.Get(fmt.Sprintf("%s/%s", cleanupAPIEndpoint, name), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("could not get cleanup policy '%s': HTTP: %d, %s", name, resp.StatusCode, string(body)) | ||
} | ||
policy := &cleanuppolicies.CleanupPolicy{} | ||
if err := json.Unmarshal(body, policy); err != nil { | ||
return nil, fmt.Errorf("could not unmarshal repository: %v", err) | ||
} | ||
|
||
return policy, nil | ||
} | ||
|
||
func (s *CleanupPolicyService) Update(policy *cleanuppolicies.CleanupPolicy) error { | ||
data, err := tools.JsonMarshalInterfaceToIOReader(policy) | ||
if err != nil { | ||
return err | ||
} | ||
body, resp, err := s.Client.Put(fmt.Sprintf("%s/%s", cleanupAPIEndpoint, policy.Name), data) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusNoContent { | ||
return fmt.Errorf("could not update cleanup policy '%s': HTTP: %d, %s", policy.Name, resp.StatusCode, string(body)) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *CleanupPolicyService) Delete(name string) error { | ||
endpoint := cleanupAPIEndpoint + "/" + name | ||
body, resp, err := s.Client.Delete(endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusNoContent { | ||
return fmt.Errorf("could not delete cleanup policy '%s': HTTP: %d, %s", name, resp.StatusCode, string(body)) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *CleanupPolicyService) List() ([]*cleanuppolicies.CleanupPolicy, error) { | ||
body, resp, err := s.Client.Get(cleanupAPIEndpoint, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("could not list cleanup policies: HTTP: %d, %s", resp.StatusCode, string(body)) | ||
} | ||
var policies []*cleanuppolicies.CleanupPolicy | ||
if err := json.Unmarshal(body, &policies); err != nil { | ||
return nil, fmt.Errorf("could not unmarshal cleanup policies: %v", err) | ||
} | ||
return policies, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cleanup_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/cleanup" | ||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/client" | ||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" | ||
"github.com/datadrivers/go-nexus-client/nexus3/schema/cleanuppolicies" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// https://help.sonatype.com/en/cleanup-policies-api.html | ||
|
||
var ( | ||
testClient *client.Client = nil | ||
) | ||
|
||
func getTestClient() *client.Client { | ||
if testClient != nil { | ||
return testClient | ||
} | ||
return client.NewClient(getDefaultConfig()) | ||
} | ||
|
||
func getTestService() *cleanup.CleanupPolicyService { | ||
return cleanup.NewCleanupPolicyService(getTestClient()) | ||
} | ||
|
||
func getDefaultConfig() client.Config { | ||
return client.Config{ | ||
Insecure: tools.GetEnv("NEXUS_INSECURE_SKIP_VERIFY", true).(bool), | ||
Password: tools.GetEnv("NEXUS_PASSWORD", "admin123").(string), | ||
URL: tools.GetEnv("NEXUS_URL", "http://127.0.0.1:8081").(string), | ||
Username: tools.GetEnv("NEXUS_USRNAME", "admin").(string), | ||
} | ||
} | ||
|
||
func TestNewCleanupService(t *testing.T) { | ||
s := getTestService() | ||
|
||
assert.NotNil(t, s, "NewCleanupService() must not return nil") | ||
} | ||
|
||
func TestCreateCleanupPolicy(t *testing.T) { | ||
s := getTestService() | ||
|
||
policy := &cleanuppolicies.CleanupPolicy{ | ||
Notes: tools.GetStringPointer("Test"), | ||
CriteriaAssetRegex: tools.GetStringPointer("*"), | ||
Name: "Test", | ||
Format: "go", | ||
} | ||
|
||
policy2 := &cleanuppolicies.CleanupPolicy{ | ||
Notes: tools.GetStringPointer("Test2"), | ||
CriteriaAssetRegex: tools.GetStringPointer("*"), | ||
Name: "Test2", | ||
Format: "go", | ||
} | ||
|
||
err := s.Create(policy) | ||
assert.Nil(t, err, "Create() must not return an error") | ||
|
||
err = s.Create(policy2) | ||
assert.Nil(t, err, "Second Create() must not return an error") | ||
|
||
policy, err = s.Get(policy.Name) | ||
assert.Nil(t, err, "Get() must not return an error") | ||
assert.Equal(t, policy.Name, "Test") | ||
|
||
policies, err := s.List() | ||
assert.Nil(t, err, "List() must not return an error") | ||
assert.Equal(t, len(policies), 2) | ||
|
||
policy.CriteriaLastBlobUpdated = tools.GetIntPointer(1) | ||
err = s.Update(policy) | ||
assert.Nil(t, err, "Update() must not return an error") | ||
assert.Equal(t, policy.CriteriaLastBlobUpdated, tools.GetIntPointer(1)) | ||
|
||
err = s.Delete(policy.Name) | ||
assert.Nil(t, err, "Delete() must not return an error") | ||
|
||
err = s.Delete(policy2.Name) | ||
assert.Nil(t, err, "Second Delete() must not return an error") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cleanuppolicies | ||
|
||
type CleanupPolicy struct { | ||
Notes *string `json:"notes,omitempty"` // any details on the specific cleanup policy. | ||
CriteriaLastBlobUpdated *int `json:"criteriaLastBlobUpdated,omitempty"` // the age of the component in days. | ||
CriteriaLastDownloaded *int `json:"criteriaLastDownloaded,omitempty"` // the last time the component had been downloaded in days. | ||
CriteriaReleaseType *string `json:"criteriaReleaseType,omitempty"` // is one of: RELEASES_AND_PRERELEASES, PRERELEASES, RELEASES], Only maven2, npm and yum repositories support this field. | ||
CriteriaAssetRegex *string `json:"criteriaAssetRegex,omitempty"` // a regex string to filter for specific asset paths. Not for gitlfs or * | ||
Retain int `json:"retain,omitempty"` // number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments. | ||
Name string `json:"name"` // the name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot. | ||
Format string `json:"format"` // one of ["*", "apt", "bower", "cocoapods", "conan", "conda", "docker", "gitlfs", "go", "helm", "maven2", "npm", "nuget", "p2", "pypi", "r", "raw", "rubygems", "yum"] | ||
} |