Skip to content

Commit

Permalink
element support
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Sep 2, 2024
1 parent 7392c34 commit f3de7b2
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 0 deletions.
38 changes: 38 additions & 0 deletions api/ocm/elements/artifactaccess/ocmaccess/forward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ocmaccess

import (
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/ocm/selectors/rscsel"
"ocm.software/ocm/api/utils/blobaccess/ocm"
)

////////////////////////////////////////////////////////////////////////////////
// Component Version

func ByComponentVersion(cv cpi.ComponentVersionAccess) ocm.ComponentVersionProvider {
return ocm.ByComponentVersion(cv)
}

func ByResolverAndName(resolver cpi.ComponentVersionResolver, comp, vers string) ocm.ComponentVersionProvider {
return ocm.ByResolverAndName(resolver, comp, vers)
}

func ByRepositorySpecAndName(ctx cpi.ContextProvider, spec cpi.RepositorySpec, comp, vers string) ocm.ComponentVersionProvider {
return ocm.ByRepositorySpecAndName(ctx, spec, comp, vers)
}

////////////////////////////////////////////////////////////////////////////////
// Resource

func ByResourceId(id metav1.Identity) ocm.ResourceProvider {
return ocm.ByResourceId(id)
}

func ByResourcePath(id metav1.Identity, path ...metav1.Identity) ocm.ResourceProvider {
return ocm.ByResourcePath(id, path...)
}

func ByResourceSelector(sel ...rscsel.Selector) ocm.ResourceProvider {
return ocm.ByResourceSelector(sel...)
}
27 changes: 27 additions & 0 deletions api/ocm/elements/artifactaccess/ocmaccess/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ocmaccess

import (
"ocm.software/ocm/api/ocm"
"ocm.software/ocm/api/ocm/compdesc"
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/ocm/elements/artifactaccess/genericaccess"
access "ocm.software/ocm/api/ocm/extensions/accessmethods/ocm"
)

func Access[M any, P compdesc.ArtifactMetaPointer[M]](ctx ocm.Context, meta P, comp, vers string, repo cpi.RepositorySpec, id metav1.Identity, path ...metav1.Identity) (cpi.ArtifactAccess[M], error) {
spec, err := access.New(comp, vers, repo, id, path...)
if err != nil {
return nil, err
}
// is global access, must work, otherwise there is an error in the lib.
return genericaccess.MustAccess(ctx, meta, spec), nil
}

func ResourceAccess(ctx ocm.Context, meta *cpi.ResourceMeta, comp, vers string, repo cpi.RepositorySpec, id metav1.Identity, path ...metav1.Identity) (cpi.ResourceAccess, error) {
return Access(ctx, meta, comp, vers, repo, id, path...)
}

func SourceAccess(ctx ocm.Context, meta *cpi.SourceMeta, comp, vers string, repo cpi.RepositorySpec, id metav1.Identity, path ...metav1.Identity) (cpi.SourceAccess, error) {
return Access(ctx, meta, comp, vers, repo, id, path...)
}
71 changes: 71 additions & 0 deletions api/ocm/elements/artifactblob/ocmblob/access_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ocmblob_test

import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "ocm.software/ocm/api/helper/builder"

"ocm.software/ocm/api/ocm/elements"
me "ocm.software/ocm/api/ocm/elements/artifactblob/mavenblob"
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/repositories/composition"
"ocm.software/ocm/api/tech/maven"
"ocm.software/ocm/api/tech/maven/maventest"
)

const (
MAVEN_PATH = "/testdata/.m2/repository"
FAIL_PATH = "/testdata/.m2/fail"
MAVEN_CENTRAL_ADDRESS = "repo.maven.apache.org:443"
MAVEN_CENTRAL = "https://repo.maven.apache.org/maven2/"
MAVEN_GROUP_ID = "maven"
MAVEN_ARTIFACT_ID = "maven"
MAVEN_VERSION = "1.1"
)

var _ = Describe("blobaccess for maven", func() {
Context("maven filesystem repository", func() {
var env *Builder
var repo *maven.Repository

BeforeEach(func() {
env = NewBuilder(maventest.TestData())
repo = maven.NewFileRepository(MAVEN_PATH, env.FileSystem())
})

AfterEach(func() {
MustBeSuccessful(env.Cleanup())
})

It("blobaccess for a single file with classifier and extension", func() {
cv := composition.NewComponentVersion(env.OCMContext(), "acme.org/test", "1.0.0")
defer Close(cv)

coords := maven.NewCoordinates("com.sap.cloud.sdk", "sdk-modules-bom", "5.7.0",
maven.WithClassifier("random-content"), maven.WithExtension("json"))

a := me.ResourceAccessForMavenCoords(env.OCMContext(), Must(elements.ResourceMeta("mavenblob", resourcetypes.OCM_JSON, elements.WithLocalRelation())), repo, coords, me.WithCachingFileSystem(env.FileSystem()))
Expect(a.ReferenceHint()).To(Equal(""))
b := Must(a.BlobAccess())
defer Close(b)
Expect(string(Must(b.Get()))).To(Equal(`{"some": "test content"}`))

MustBeSuccessful(cv.SetResourceByAccess(a))
r := Must(cv.GetResourceByIndex(0))
m := Must(r.AccessMethod())
defer Close(m)
Expect(string(Must(m.Get()))).To(Equal(`{"some": "test content"}`))
})

It("blobaccess for package", func() {
cv := composition.NewComponentVersion(env.OCMContext(), "acme.org/test", "1.0.0")
defer Close(cv)

coords := maven.NewCoordinates("com.sap.cloud.sdk", "sdk-modules-bom", "5.7.0")

a := me.ResourceAccessForMavenCoords(env.OCMContext(), Must(elements.ResourceMeta("mavenblob", resourcetypes.OCM_JSON, elements.WithLocalRelation())), repo, coords, me.WithCachingFileSystem(env.FileSystem()))
Expect(a.ReferenceHint()).To(Equal(coords.GAV()))
})
})
})
38 changes: 38 additions & 0 deletions api/ocm/elements/artifactblob/ocmblob/forward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ocmblob

import (
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/ocm/selectors/rscsel"
"ocm.software/ocm/api/utils/blobaccess/ocm"
)

////////////////////////////////////////////////////////////////////////////////
// Component Version

func ByComponentVersion(cv cpi.ComponentVersionAccess) ocm.ComponentVersionProvider {
return ocm.ByComponentVersion(cv)
}

func ByResolverAndName(resolver cpi.ComponentVersionResolver, comp, vers string) ocm.ComponentVersionProvider {
return ocm.ByResolverAndName(resolver, comp, vers)
}

func ByRepositorySpecAndName(ctx cpi.ContextProvider, spec cpi.RepositorySpec, comp, vers string) ocm.ComponentVersionProvider {
return ocm.ByRepositorySpecAndName(ctx, spec, comp, vers)
}

////////////////////////////////////////////////////////////////////////////////
// Resource

func ByResourceId(id metav1.Identity) ocm.ResourceProvider {
return ocm.ByResourceId(id)
}

func ByResourcePath(id metav1.Identity, path ...metav1.Identity) ocm.ResourceProvider {
return ocm.ByResourcePath(id, path...)
}

func ByResourceSelector(sel ...rscsel.Selector) ocm.ResourceProvider {
return ocm.ByResourceSelector(sel...)
}
21 changes: 21 additions & 0 deletions api/ocm/elements/artifactblob/ocmblob/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ocmblob

import (
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/ocm/elements/artifactblob/api"
)

type Option = api.Option

type Options = api.Options

////////////////////////////////////////////////////////////////////////////////
// General Options

func WithHint(h string) Option {
return api.WrapHint[Options](h)
}

func WithGlobalAccess(a cpi.AccessSpec) Option {
return api.WrapGlobalAccess[Options](a)
}
26 changes: 26 additions & 0 deletions api/ocm/elements/artifactblob/ocmblob/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ocmblob

import (
"github.com/mandelsoft/goutils/generics"
"github.com/mandelsoft/goutils/optionutils"

"ocm.software/ocm/api/ocm/compdesc"
"ocm.software/ocm/api/ocm/cpi"
base "ocm.software/ocm/api/utils/blobaccess/ocm"
)

func Access[M any, P compdesc.ArtifactMetaPointer[M]](ctx cpi.Context, meta P, cvp base.ComponentVersionProvider, res base.ResourceProvider, opts ...Option) cpi.ArtifactAccess[M] {
eff := optionutils.EvalOptions(opts...)
blobprov := base.Provider(cvp, res)
accprov := cpi.NewAccessProviderForBlobAccessProvider(ctx, blobprov, eff.Hint, eff.Global)
// strange type cast is required by Go compiler, meta has the correct type.
return cpi.NewArtifactAccessForProvider(generics.Cast[*M](meta), accprov)
}

func ResourceAccess(ctx cpi.Context, meta *cpi.ResourceMeta, cvp base.ComponentVersionProvider, res base.ResourceProvider, opts ...Option) cpi.ResourceAccess {
return Access(ctx, meta, cvp, res, opts...)
}

func SourceAccess(ctx cpi.Context, meta *cpi.SourceMeta, cvp base.ComponentVersionProvider, res base.ResourceProvider, opts ...Option) cpi.SourceAccess {
return Access(ctx, meta, cvp, res, opts...)
}
13 changes: 13 additions & 0 deletions api/ocm/elements/artifactblob/ocmblob/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ocmblob_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "OCM Blob Access Test Suite")
}
6 changes: 6 additions & 0 deletions api/utils/blobaccess/ocm/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ func BlobAccess(cvp ComponentVersionProvider, res ResourceProvider) (blob bpi.Bl
}
return r.BlobAccess()
}

func Provider(cvp ComponentVersionProvider, res ResourceProvider) bpi.BlobAccessProvider {
return bpi.BlobAccessProviderFunction(func() (bpi.BlobAccess, error) {
return BlobAccess(cvp, res)
})
}

0 comments on commit f3de7b2

Please sign in to comment.