Skip to content

Commit

Permalink
Fixes 4293: add support for upload only repos
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill committed Jul 8, 2024
1 parent ae4410d commit 6d264f2
Show file tree
Hide file tree
Showing 20 changed files with 411 additions and 95 deletions.
52 changes: 51 additions & 1 deletion api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.RepositoryRequest"
"$ref": "#/definitions/api.RepositoryUpdateRequest"
}
}
],
Expand Down Expand Up @@ -3342,6 +3342,11 @@ const docTemplate = `{
"description": "Name of the remote yum repository",
"type": "string"
},
"origin": {
"description": "Origin of the repository",
"type": "string",
"readOnly": true
},
"snapshot": {
"description": "Enable snapshotting and hosting of this repository",
"type": "boolean"
Expand Down Expand Up @@ -3529,6 +3534,51 @@ const docTemplate = `{
}
}
},
"api.RepositoryUpdateRequest": {
"type": "object",
"properties": {
"distribution_arch": {
"description": "Architecture to restrict client usage to",
"type": "string",
"example": "x86_64"
},
"distribution_versions": {
"description": "Versions to restrict client usage to",
"type": "array",
"items": {
"type": "string"
},
"example": [
"7",
"8"
]
},
"gpg_key": {
"description": "GPG key for repository",
"type": "string"
},
"metadata_verification": {
"description": "Verify packages",
"type": "boolean"
},
"module_hotfixes": {
"description": "Disable modularity filtering on this repository",
"type": "boolean"
},
"name": {
"description": "Name of the remote yum repository",
"type": "string"
},
"snapshot": {
"description": "Enable snapshotting and hosting of this repository",
"type": "boolean"
},
"url": {
"description": "URL of the remote yum repository",
"type": "string"
}
}
},
"api.RepositoryValidationRequest": {
"type": "object",
"properties": {
Expand Down
52 changes: 51 additions & 1 deletion api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@
"description": "Name of the remote yum repository",
"type": "string"
},
"origin": {
"description": "Origin of the repository",
"readOnly": true,
"type": "string"
},
"snapshot": {
"description": "Enable snapshotting and hosting of this repository",
"type": "boolean"
Expand Down Expand Up @@ -645,6 +650,51 @@
},
"type": "object"
},
"api.RepositoryUpdateRequest": {
"properties": {
"distribution_arch": {
"description": "Architecture to restrict client usage to",
"example": "x86_64",
"type": "string"
},
"distribution_versions": {
"description": "Versions to restrict client usage to",
"example": [
"7",
"8"
],
"items": {
"type": "string"
},
"type": "array"
},
"gpg_key": {
"description": "GPG key for repository",
"type": "string"
},
"metadata_verification": {
"description": "Verify packages",
"type": "boolean"
},
"module_hotfixes": {
"description": "Disable modularity filtering on this repository",
"type": "boolean"
},
"name": {
"description": "Name of the remote yum repository",
"type": "string"
},
"snapshot": {
"description": "Enable snapshotting and hosting of this repository",
"type": "boolean"
},
"url": {
"description": "URL of the remote yum repository",
"type": "string"
}
},
"type": "object"
},
"api.RepositoryValidationRequest": {
"properties": {
"gpg_key": {
Expand Down Expand Up @@ -2233,7 +2283,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/api.RepositoryRequest"
"$ref": "#/components/schemas/api.RepositoryUpdateRequest"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion db/migrations.latest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20240610112037
20240627171711
3 changes: 3 additions & 0 deletions db/migrations/20240627171711_allow_null_url.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
alter table repositories alter column URL set not null;
COMMIT;
5 changes: 5 additions & 0 deletions db/migrations/20240627171711_allow_null_url.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

alter table repositories alter column URL drop not null;

COMMIT;
126 changes: 95 additions & 31 deletions pkg/api/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,56 +36,112 @@ type RepositoryResponse struct {
LastSnapshotTask *TaskInfoResponse `json:"last_snapshot_task,omitempty"` // Last snapshot task response (contains last snapshot status)
}

// RepositoryRequest holds data received from request to create/update repository
// RepositoryRequest holds data received from request to create repository
type RepositoryRequest struct {
UUID *string `json:"uuid" readonly:"true" swaggerignore:"true"`
Name *string `json:"name"` // Name of the remote yum repository
URL *string `json:"url"` // URL of the remote yum repository
DistributionVersions *[]string `json:"distribution_versions" example:"7,8"` // Versions to restrict client usage to
DistributionArch *string `json:"distribution_arch" example:"x86_64"` // Architecture to restrict client usage to
GpgKey *string `json:"gpg_key"` // GPG key for repository
MetadataVerification *bool `json:"metadata_verification"` // Verify packages
ModuleHotfixes *bool `json:"module_hotfixes"` // Disable modularity filtering on this repository
Snapshot *bool `json:"snapshot"` // Enable snapshotting and hosting of this repository
AccountID *string `json:"account_id" readonly:"true" swaggerignore:"true"` // Account ID of the owner
OrgID *string `json:"org_id" readonly:"true" swaggerignore:"true"` // Organization ID of the owner
Origin *string `json:"origin" readonly:"true" swaggerignore:"true"` // Origin of the repository
ContentType *string `json:"content_type" readonly:"true" swaggerignore:"true"` // Content Type (rpm) of the repository
UUID *string `json:"uuid" readonly:"true" swaggerignore:"true"`
AccountID *string `json:"account_id" readonly:"true" swaggerignore:"true"` // Account ID of the owner
OrgID *string `json:"org_id" readonly:"true" swaggerignore:"true"` // Organization ID of the owner
Origin *string `json:"origin" readonly:"true"` // Origin of the repository
ContentType *string `json:"content_type" readonly:"true" swaggerignore:"true"` // Content Type (rpm) of the repository

Name *string `json:"name"` // Name of the remote yum repository
URL *string `json:"url"` // URL of the remote yum repository
DistributionVersions *[]string `json:"distribution_versions" example:"7,8"` // Versions to restrict client usage to
DistributionArch *string `json:"distribution_arch" example:"x86_64"` // Architecture to restrict client usage to
GpgKey *string `json:"gpg_key"` // GPG key for repository
MetadataVerification *bool `json:"metadata_verification"` // Verify packages
ModuleHotfixes *bool `json:"module_hotfixes"` // Disable modularity filtering on this repository
Snapshot *bool `json:"snapshot"` // Enable snapshotting and hosting of this repository
}

type RepositoryUpdateRequest struct {
Name *string `json:"name"` // Name of the remote yum repository
URL *string `json:"url"` // URL of the remote yum repository
DistributionVersions *[]string `json:"distribution_versions" example:"7,8"` // Versions to restrict client usage to
DistributionArch *string `json:"distribution_arch" example:"x86_64"` // Architecture to restrict client usage to
GpgKey *string `json:"gpg_key"` // GPG key for repository
MetadataVerification *bool `json:"metadata_verification"` // Verify packages
ModuleHotfixes *bool `json:"module_hotfixes"` // Disable modularity filtering on this repository
Snapshot *bool `json:"snapshot"` // Enable snapshotting and hosting of this repository
}

func (r *RepositoryRequest) ToRepositoryUpdateRequest() RepositoryUpdateRequest {
return RepositoryUpdateRequest{
Name: r.Name,
URL: r.URL,
DistributionVersions: r.DistributionVersions,
DistributionArch: r.DistributionArch,
GpgKey: r.GpgKey,
MetadataVerification: r.MetadataVerification,
ModuleHotfixes: r.ModuleHotfixes,
Snapshot: r.Snapshot,
}
}

var defaultRepoValues = RepositoryUpdateRequest{
Name: pointy.Pointer(""),
URL: pointy.Pointer(""),
DistributionVersions: pointy.Pointer([]string{"any"}),
DistributionArch: pointy.Pointer("any"),
GpgKey: pointy.Pointer(""),
MetadataVerification: pointy.Pointer(false),
ModuleHotfixes: pointy.Pointer(false),
Snapshot: pointy.Pointer(false),
}

func (r *RepositoryUpdateRequest) FillDefaults() {
if r.Name == nil {
r.Name = defaultRepoValues.Name
}
if r.URL == nil {
r.URL = defaultRepoValues.URL
}
if r.DistributionVersions == nil {
r.DistributionVersions = defaultRepoValues.DistributionVersions
}
if r.DistributionArch == nil {
r.DistributionArch = defaultRepoValues.DistributionArch
}
if r.GpgKey == nil {
r.GpgKey = defaultRepoValues.GpgKey
}
if r.MetadataVerification == nil {
r.MetadataVerification = defaultRepoValues.MetadataVerification
}
if r.ModuleHotfixes == nil {
r.ModuleHotfixes = defaultRepoValues.ModuleHotfixes
}
}

func (r *RepositoryRequest) FillDefaults() {
// Currently the user cannot change these
r.Origin = pointy.Pointer(config.OriginExternal)
// Currently the user cannot change these, only set at creation
r.ContentType = pointy.Pointer(config.ContentTypeRpm)
// Fill in default values in case of PUT request, doesn't have to be valid, let the db validate that
defaultName := ""
defaultUrl := ""
defaultVersions := []string{"any"}
defaultArch := "any"
defaultGpgKey := ""
defaultMetadataVerification := false
defaultModuleHotfixes := false

if r.Origin == nil {
r.Origin = pointy.Pointer(config.OriginExternal)
}

// copied from RepositoryUpdateRequest FillDefaults
if r.Name == nil {
r.Name = &defaultName
r.Name = defaultRepoValues.Name
}
if r.URL == nil {
r.URL = &defaultUrl
r.URL = defaultRepoValues.URL
}
if r.DistributionVersions == nil {
r.DistributionVersions = &defaultVersions
r.DistributionVersions = defaultRepoValues.DistributionVersions
}
if r.DistributionArch == nil {
r.DistributionArch = &defaultArch
r.DistributionArch = defaultRepoValues.DistributionArch
}
if r.GpgKey == nil {
r.GpgKey = &defaultGpgKey
r.GpgKey = defaultRepoValues.GpgKey
}
if r.MetadataVerification == nil {
r.MetadataVerification = &defaultMetadataVerification
r.MetadataVerification = defaultRepoValues.MetadataVerification
}
if r.ModuleHotfixes == nil {
r.ModuleHotfixes = &defaultModuleHotfixes
r.ModuleHotfixes = defaultRepoValues.ModuleHotfixes
}
}

Expand All @@ -103,3 +159,11 @@ func (r *RepositoryCollectionResponse) SetMetadata(meta ResponseMetadata, links
r.Meta = meta
r.Links = links
}

func (r *RepositoryResponse) Snapshottable() bool {
return r.Origin != config.OriginUpload && r.Snapshot
}

func (r *RepositoryResponse) Introspectable() bool {
return r.Origin != config.OriginUpload
}
1 change: 1 addition & 0 deletions pkg/config/value_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
const (
OriginExternal = "external"
OriginRedHat = "red_hat"
OriginUpload = "upload"
)

const RedHatOrg = "-1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/dao/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func SetupGormTableOrFail(db *gorm.DB) {
type RepositoryConfigDao interface {
Create(ctx context.Context, newRepo api.RepositoryRequest) (api.RepositoryResponse, error)
BulkCreate(ctx context.Context, newRepositories []api.RepositoryRequest) ([]api.RepositoryResponse, []error)
Update(ctx context.Context, orgID, uuid string, repoParams api.RepositoryRequest) (bool, error)
Update(ctx context.Context, orgID, uuid string, repoParams api.RepositoryUpdateRequest) (bool, error)
Fetch(ctx context.Context, orgID string, uuid string) (api.RepositoryResponse, error)
InternalOnly_ListReposToSnapshot(ctx context.Context, filter *ListRepoFilter) ([]models.RepositoryConfiguration, error)
List(ctx context.Context, orgID string, paginationData api.PaginationData, filterData api.FilterData) (api.RepositoryCollectionResponse, int64, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/dao/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (p repositoryDaoImpl) ListForIntrospection(ctx context.Context, urls *[]str
db = db.Where("url in ?", *urls)
}

result := db.Find(&dbRepos)
result := db.Where("origin in ?", []string{config.OriginExternal, config.OriginRedHat}).Find(&dbRepos)
if result.Error != nil {
return repos, result.Error
}
Expand Down
Loading

0 comments on commit 6d264f2

Please sign in to comment.