Skip to content

Commit

Permalink
Refactor get_resource and get_resource_version for multiple hub support
Browse files Browse the repository at this point in the history
No functionality change in this PR. Prior to this change, the Hub CLI supports only 1 type of Hub (Tekton Hub).

This commit 1) refactors the `ResourceResult` and `ResourceVersionResult` to interfaces;
2) creates THResourceResult and THResourceVersionResult structs implementing the interfaces, keeping original support for Tekton Hub;
3) creates AHResourceResult and AHResourceVersionResult for Artifact Hub, implementations will be added in later PRs.
  • Loading branch information
QuanZhang-William committed Jan 9, 2023
1 parent 0762ee1 commit b3b8ff3
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 89 deletions.
204 changes: 129 additions & 75 deletions api/pkg/cli/hub/get_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ type ResourceOption struct {
PipelineVersion string
}

type ResourceResult interface {
RawURL() (string, error)
Manifest() ([]byte, error)
Resource() (interface{}, error)
ResourceYaml() (string, error)
ResourceVersion() (string, error)
MinPipelinesVersion() (string, error)
UnmarshalData() error
}

// ResourceResult defines API response
type ResourceResult struct {
type THResourceResult struct {
data []byte
yaml []byte
status int
Expand All @@ -49,6 +59,9 @@ type ResourceResult struct {
ResourceContent *ResourceContent
}

type AHResourceResult struct {
}

type ResourceVersionOptions struct {
hubResVersionsRes ResourceVersionResult
hubResVersions *ResVersions
Expand All @@ -75,14 +88,14 @@ type resourceYaml = rclient.ByCatalogKindNameVersionYamlResponseBody
// GetResource queries the data using Artifact Hub Endpoint
func (a *artifactHubClient) GetResource(opt ResourceOption) ResourceResult {
// Todo: implement GetResource for Artifact Hub
return ResourceResult{}
return nil
}

// GetResource queries the data using Tekton Hub Endpoint
func (t *tektonHubclient) GetResource(opt ResourceOption) ResourceResult {
data, status, err := t.Get(opt.Endpoint())

return ResourceResult{
return &THResourceResult{
data: data,
version: opt.Version,
status: status,
Expand All @@ -94,7 +107,7 @@ func (t *tektonHubclient) GetResource(opt ResourceOption) ResourceResult {
// GetResource queries the data using Artifact Hub Endpoint
func (a *artifactHubClient) GetResourceYaml(opt ResourceOption) ResourceResult {
// Todo: implement GetResourceYaml for Artifact Hub
return ResourceResult{}
return nil
}

// GetResource queries the data using Tekton Hub Endpoint
Expand All @@ -103,7 +116,7 @@ func (t *tektonHubclient) GetResourceYaml(opt ResourceOption) ResourceResult {
yaml, yamlStatus, yamlErr := t.Get(fmt.Sprintf("/v1/resource/%s/%s/%s/%s/yaml", opt.Catalog, opt.Kind, opt.Name, opt.Version))
data, status, err := t.Get(opt.Endpoint())

return ResourceResult{
return &THResourceResult{
data: data,
yaml: yaml,
version: opt.Version,
Expand All @@ -115,6 +128,67 @@ func (t *tektonHubclient) GetResourceYaml(opt ResourceOption) ResourceResult {
}
}

func (a *artifactHubClient) GetResourcesList(so SearchOption) ([]string, error) {
// Todo: implement GetResourcesList for Artifact Hub
return []string{}, nil
}

func (t *tektonHubclient) GetResourcesList(so SearchOption) ([]string, error) {
// Get all resources
result := t.Search(SearchOption{
Kinds: so.Kinds,
Catalog: so.Catalog,
})

typed, err := result.Typed()
if err != nil {
return nil, err
}

var data = struct {
Resources SearchResponse
}{
Resources: typed,
}

// Get all resource names
var resources []string
for i := range data.Resources {
resources = append(resources, *data.Resources[i].Name)
}

return resources, nil
}

func (a *artifactHubClient) GetResourceVersionslist(r ResourceOption) ([]string, error) {
// Todo: implement GetResourceVersionslist for Artifact Hub
return []string{}, nil
}

func (t *tektonHubclient) GetResourceVersionslist(r ResourceOption) ([]string, error) {
opts := &ResourceVersionOptions{}
// Get the resource versions
opts.hubResVersionsRes = t.GetResourceVersions(ResourceOption{
Name: r.Name,
Catalog: r.Catalog,
Kind: r.Kind,
})

var err error
opts.hubResVersions, err = opts.hubResVersionsRes.ResourceVersions()
if err != nil {
return nil, err
}

var ver []string
for i := range opts.hubResVersions.Versions {
ver = append(ver, *opts.hubResVersions.Versions[i].Version)
}
sort.Sort(sort.Reverse(sort.StringSlice(ver)))

return ver, nil
}

// Endpoint computes the endpoint url using input provided
func (opt ResourceOption) Endpoint() string {
if opt.Version != "" {
Expand All @@ -130,7 +204,7 @@ func (opt ResourceOption) Endpoint() string {
return fmt.Sprintf("/v1/resource/%s/%s/%s", opt.Catalog, opt.Kind, opt.Name)
}

func (rr *ResourceResult) unmarshalData() error {
func (rr *THResourceResult) UnmarshalData() error {
if rr.err != nil {
return rr.err
}
Expand Down Expand Up @@ -164,9 +238,14 @@ func (rr *ResourceResult) unmarshalData() error {
return nil
}

func (rr *AHResourceResult) UnmarshalData() error {
// TODO
return nil
}

// RawURL returns the raw url of the resource yaml file
func (rr *ResourceResult) RawURL() (string, error) {
if err := rr.unmarshalData(); err != nil {
func (rr *THResourceResult) RawURL() (string, error) {
if err := rr.UnmarshalData(); err != nil {
return "", err
}

Expand All @@ -176,8 +255,14 @@ func (rr *ResourceResult) RawURL() (string, error) {
return *rr.resourceData.LatestVersion.RawURL, nil
}

// RawURL returns the raw url of the resource yaml file
func (rr *AHResourceResult) RawURL() (string, error) {
// TODO
return "", nil
}

// Manifest gets the resource from catalog
func (rr *ResourceResult) Manifest() ([]byte, error) {
func (rr *THResourceResult) Manifest() ([]byte, error) {
rawURL, err := rr.RawURL()
if err != nil {
return nil, err
Expand All @@ -196,9 +281,15 @@ func (rr *ResourceResult) Manifest() ([]byte, error) {
return data, nil
}

// Manifest gets the resource from catalog
func (rr *AHResourceResult) Manifest() ([]byte, error) {
// TODO
return []byte{}, nil
}

// Resource returns the resource found
func (rr *ResourceResult) Resource() (interface{}, error) {
if err := rr.unmarshalData(); err != nil {
func (rr *THResourceResult) Resource() (interface{}, error) {
if err := rr.UnmarshalData(); err != nil {
return "", err
}

Expand All @@ -209,7 +300,13 @@ func (rr *ResourceResult) Resource() (interface{}, error) {
}

// Resource returns the resource found
func (rr *ResourceResult) ResourceYaml() (string, error) {
func (rr *AHResourceResult) Resource() (interface{}, error) {
// TODO
return "", nil
}

// Resource returns the resource found
func (rr *THResourceResult) ResourceYaml() (string, error) {

if rr.yamlErr != nil {
return "", rr.err
Expand All @@ -231,9 +328,15 @@ func (rr *ResourceResult) ResourceYaml() (string, error) {
return *rr.ResourceContent.Yaml, nil
}

// Resource returns the resource found
func (rr *AHResourceResult) ResourceYaml() (string, error) {
// TODO
return "", nil
}

// ResourceVersion returns the resource version found
func (rr *ResourceResult) ResourceVersion() (string, error) {
if err := rr.unmarshalData(); err != nil {
func (rr *THResourceResult) ResourceVersion() (string, error) {
if err := rr.UnmarshalData(); err != nil {
return "", err
}

Expand All @@ -243,9 +346,15 @@ func (rr *ResourceResult) ResourceVersion() (string, error) {
return *rr.resourceData.LatestVersion.Version, nil
}

// ResourceVersion returns the resource version found
func (rr *AHResourceResult) ResourceVersion() (string, error) {
//TODO
return "", nil
}

// MinPipelinesVersion returns the minimum pipeline version the resource is compatible
func (rr *ResourceResult) MinPipelinesVersion() (string, error) {
if err := rr.unmarshalData(); err != nil {
func (rr *THResourceResult) MinPipelinesVersion() (string, error) {
if err := rr.UnmarshalData(); err != nil {
return "", err
}

Expand All @@ -255,63 +364,8 @@ func (rr *ResourceResult) MinPipelinesVersion() (string, error) {
return *rr.resourceData.LatestVersion.MinPipelinesVersion, nil
}

func (a *artifactHubClient) GetResourcesList(so SearchOption) ([]string, error) {
// Todo: implement GetResourcesList for Artifact Hub
return []string{}, nil
}

func (t *tektonHubclient) GetResourcesList(so SearchOption) ([]string, error) {
// Get all resources
result := t.Search(SearchOption{
Kinds: so.Kinds,
Catalog: so.Catalog,
})

typed, err := result.Typed()
if err != nil {
return nil, err
}

var data = struct {
Resources SearchResponse
}{
Resources: typed,
}

// Get all resource names
var resources []string
for i := range data.Resources {
resources = append(resources, *data.Resources[i].Name)
}

return resources, nil
}

func (a *artifactHubClient) GetResourceVersionslist(r ResourceOption) ([]string, error) {
// Todo: implement GetResourceVersionslist for Artifact Hub
return []string{}, nil
}

func (t *tektonHubclient) GetResourceVersionslist(r ResourceOption) ([]string, error) {
opts := &ResourceVersionOptions{}
// Get the resource versions
opts.hubResVersionsRes = t.GetResourceVersions(ResourceOption{
Name: r.Name,
Catalog: r.Catalog,
Kind: r.Kind,
})

var err error
opts.hubResVersions, err = opts.hubResVersionsRes.ResourceVersions()
if err != nil {
return nil, err
}

var ver []string
for i := range opts.hubResVersions.Versions {
ver = append(ver, *opts.hubResVersions.Versions[i].Version)
}
sort.Sort(sort.Reverse(sort.StringSlice(ver)))

return ver, nil
// MinPipelinesVersion returns the minimum pipeline version the resource is compatible
func (rr *AHResourceResult) MinPipelinesVersion() (string, error) {
//TODO
return "", nil
}
33 changes: 19 additions & 14 deletions api/pkg/cli/hub/get_resource_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ type resVersionsResponse = rclient.VersionsByIDResponseBody
// ResVersions is the data in API response consisting of list of versions
type ResVersions = rclient.VersionsResponseBody

type ResourceVersionResult interface {
ResourceVersions() (*ResVersions, error)
UnmarshalData() error
}

// ResourceVersionResult defines API response
type ResourceVersionResult struct {
type THResourceVersionResult struct {
rr ResourceResult
data []byte
status int
Expand All @@ -41,37 +46,37 @@ type ResourceVersionResult struct {
// GetResourceVersion queries the data using Artifact Hub Endpoint
func (a *artifactHubClient) GetResourceVersions(opt ResourceOption) ResourceVersionResult {
// Todo: implement GetResourceVersions for Artifact Hub
return ResourceVersionResult{}
return nil
}

// GetResourceVersion queries the data using Tekton Hub Endpoint
func (t *tektonHubclient) GetResourceVersions(opt ResourceOption) ResourceVersionResult {

rvr := ResourceVersionResult{set: false}
rvr := THResourceVersionResult{set: false}

rvr.rr = t.GetResource(opt)
if rvr.err = rvr.rr.unmarshalData(); rvr.err != nil {
return rvr
rr := t.GetResource(opt).(*THResourceResult)
rvr.rr = rr
if rvr.err = rvr.rr.UnmarshalData(); rvr.err != nil {
return &rvr
}

var resID uint
if rvr.rr.version != "" {
resID = *rvr.rr.resourceWithVersionData.Resource.ID
if rr.version != "" {
resID = *rr.resourceWithVersionData.Resource.ID
} else {
resID = *rvr.rr.resourceData.ID
resID = *rr.resourceData.ID
}

rvr.data, rvr.status, rvr.err = t.Get(resVersionsEndpoint(resID))

return rvr
return &rvr
}

// Endpoint computes the endpoint url using input provided
func resVersionsEndpoint(rID uint) string {
return fmt.Sprintf("/v1/resource/%s/versions", strconv.FormatUint(uint64(rID), 10))
}

func (rvr *ResourceVersionResult) unmarshalData() error {
func (rvr *THResourceVersionResult) UnmarshalData() error {
if rvr.err != nil {
return rvr.err
}
Expand All @@ -89,9 +94,9 @@ func (rvr *ResourceVersionResult) unmarshalData() error {
}

// ResourceVersions returns list of all versions of the resource
func (rvr *ResourceVersionResult) ResourceVersions() (*ResVersions, error) {
func (rvr *THResourceVersionResult) ResourceVersions() (*ResVersions, error) {

if err := rvr.unmarshalData(); err != nil {
if err := rvr.UnmarshalData(); err != nil {
return nil, err
}

Expand Down

0 comments on commit b3b8ff3

Please sign in to comment.