Skip to content

Commit

Permalink
refactor: move resourcestore into core and internal packages
Browse files Browse the repository at this point in the history
In preparation for moving the resource store into service factory, move
the current implementation into core and internal. Core can hold the
interface, details go into internal. A few new types were necessary to
avoid importing the application domain.
  • Loading branch information
hmlanigan committed Dec 2, 2024
1 parent 85925cf commit 005f371
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 105 deletions.
19 changes: 19 additions & 0 deletions apiserver/facades/client/modelupgrader/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package modelupgrader

import (
"context"

"github.com/juju/version/v2"
)

// ModelAgentService provides access to the Juju agent version for the model.
type ModelAgentService interface {
// GetModelTargetAgentVersion returns the target agent version for the
// entire model. The following errors can be returned:
// - [github.com/juju/juju/domain/model/errors.NotFound] - When the model does
// not exist.
GetModelTargetAgentVersion(context.Context) (version.Number, error)
}
54 changes: 54 additions & 0 deletions core/resource/store/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package store

import (
"context"
"io"

"github.com/juju/utils/v4/hash"
)

// ResourceStore provides a list of methods necessary for interacting with
// a store for the resource.
type ResourceStore interface {
// Get returns an io.ReadCloser for a resource in the resource store.
Get(
ctx context.Context,
storageKey string,
) (r io.ReadCloser, size int64, err error)

// Put stores data from io.Reader in the resource store using the storage
// key.
Put(
ctx context.Context,
storageKey string,
r io.Reader,
size int64,
fingerprint Fingerprint,
) (UUID, error)

// Remove removes a resource from storage.
Remove(
ctx context.Context,
storageKey string,
) error
}

// Fingerprint represents the unique fingerprint value of a resource's data.
type Fingerprint struct {
hash.Fingerprint
}

// NewFingerprint returns a resource store Fingerprint for the given
// hash Fingerprint.
func NewFingerprint(f hash.Fingerprint) Fingerprint {
return Fingerprint{f}
}

// UUID is the UUID of the stored blob in the database, this can
// be used for adding referential integrity from the resource to the stored
// blob. This can be an object store metadata UUID or a container image metadata
// storage key.
type UUID string
6 changes: 0 additions & 6 deletions domain/application/resource/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,3 @@ type SetRepositoryResourcesArgs struct {
// LastPolled indicates when the resource data was last polled.
LastPolled time.Time
}

// ResourceStorageUUID is the UUID of the stored blob in the database, this can
// be used for adding referential integrity from the resource to the stored
// blob. This can be an object store metadata UUID or a container image metadata
// storage key.
type ResourceStorageUUID string
17 changes: 9 additions & 8 deletions domain/application/service/package_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions domain/application/service/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

coreapplication "github.com/juju/juju/core/application"
coreresource "github.com/juju/juju/core/resource"
"github.com/juju/juju/core/resource/store"
coreunit "github.com/juju/juju/core/unit"
applicationerrors "github.com/juju/juju/domain/application/errors"
"github.com/juju/juju/domain/application/resource"
Expand Down Expand Up @@ -51,11 +52,11 @@ type ResourceState interface {

type ResourceStoreGetter interface {
// AddStore injects a ResourceStore for the given type into the ResourceStoreFactory.
AddStore(t charmresource.Type, store resource.ResourceStore)
AddStore(t charmresource.Type, store store.ResourceStore)

// GetResourceStore returns the appropriate ResourceStore for the
// given resource type.
GetResourceStore(context.Context, charmresource.Type) (resource.ResourceStore, error)
GetResourceStore(context.Context, charmresource.Type) (store.ResourceStore, error)
}

// GetApplicationResourceID returns the ID of the application resource specified by
Expand Down
4 changes: 2 additions & 2 deletions domain/application/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
"github.com/juju/juju/domain"
"github.com/juju/juju/domain/application"
applicationerrors "github.com/juju/juju/domain/application/errors"
"github.com/juju/juju/domain/application/resource"
"github.com/juju/juju/domain/application/service"
"github.com/juju/juju/domain/application/state"
"github.com/juju/juju/domain/ipaddress"
"github.com/juju/juju/domain/schema/testing"
domainsecret "github.com/juju/juju/domain/secret"
secretstate "github.com/juju/juju/domain/secret/state"
loggertesting "github.com/juju/juju/internal/logger/testing"
"github.com/juju/juju/internal/resource/resourcestore"
"github.com/juju/juju/internal/storage"
"github.com/juju/juju/internal/storage/provider"
coretesting "github.com/juju/juju/internal/testing"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (s *serviceSuite) SetUpTest(c *gc.C) {
return provider.CommonStorageProviders()
}),
nil,
resource.NewResourceStoreFactory(nil),
store.NewResourceStoreFactory(nil),
clock.WallClock,
loggertesting.WrapCheckLog(c),
)
Expand Down
4 changes: 2 additions & 2 deletions domain/application/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"github.com/juju/juju/core/watcher/watchertest"
"github.com/juju/juju/domain"
"github.com/juju/juju/domain/application/charm"
"github.com/juju/juju/domain/application/resource"
"github.com/juju/juju/domain/application/service"
"github.com/juju/juju/domain/application/state"
secretstate "github.com/juju/juju/domain/secret/state"
changestreamtesting "github.com/juju/juju/internal/changestream/testing"
internalcharm "github.com/juju/juju/internal/charm"
loggertesting "github.com/juju/juju/internal/logger/testing"
"github.com/juju/juju/internal/resource/resourcestore"
"github.com/juju/juju/internal/storage"
"github.com/juju/juju/internal/storage/provider"
coretesting "github.com/juju/juju/internal/testing"
Expand Down Expand Up @@ -461,7 +461,7 @@ func (s *watcherSuite) setupService(c *gc.C, factory domain.WatchableDBFactory)
corestorage.ConstModelStorageRegistry(func() storage.ProviderRegistry {
return provider.CommonStorageProviders()
}),
resource.NewResourceStoreFactory(nil),
store.NewResourceStoreFactory(nil),
"",
domain.NewWatcherFactory(factory, loggertesting.WrapCheckLog(c)),
nil, nil, nil,
Expand Down
12 changes: 6 additions & 6 deletions domain/containerimageresourcestore/service/package_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions domain/containerimageresourcestore/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"encoding/json"
"io"

"github.com/juju/juju/domain/application/resource"
"github.com/juju/juju/core/resource/store"
"github.com/juju/juju/domain/containerimageresourcestore"
charmresource "github.com/juju/juju/internal/charm/resource"
"github.com/juju/juju/internal/docker"
Expand All @@ -31,7 +31,7 @@ type State interface {
ctx context.Context,
storageKey string,
registryPath, userName, password string,
) (resource.ResourceStorageUUID, error)
) (store.UUID, error)
// GetContainerImageMetadata gets a container image resources metadata from
// the container image metadata resource store.
GetContainerImageMetadata(
Expand Down Expand Up @@ -85,7 +85,7 @@ func (s Service) Put(
r io.Reader,
size int64,
fingerprint charmresource.Fingerprint,
) (resource.ResourceStorageUUID, error) {
) (store.UUID, error) {
respBuf := new(bytes.Buffer)
bytesRead, err := respBuf.ReadFrom(r)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions domain/containerimageresourcestore/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"go.uber.org/mock/gomock"
gc "gopkg.in/check.v1"

resourcestore "github.com/juju/juju/core/resource/store"
resourcetesting "github.com/juju/juju/core/resource/testing"
"github.com/juju/juju/domain/application/resource"
"github.com/juju/juju/domain/containerimageresourcestore"
charmresource "github.com/juju/juju/internal/charm/resource"
"github.com/juju/juju/internal/docker"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (s *containerImageResourceStoreSuite) TestContainerImageResourceStorePut(c
store := NewService(s.containerImageResourceState)

storageKey := resourcetesting.GenResourceUUID(c).String()
expectedUUID := resource.ResourceStorageUUID("expected-uuid")
expectedUUID := resourcestore.UUID("expected-uuid")
s.containerImageResourceState.EXPECT().PutContainerImageMetadata(
gomock.Any(),
storageKey,
Expand Down
6 changes: 3 additions & 3 deletions domain/containerimageresourcestore/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/juju/juju/core/database"
"github.com/juju/juju/core/logger"
"github.com/juju/juju/core/resource/store"
"github.com/juju/juju/domain"
"github.com/juju/juju/domain/application/resource"
"github.com/juju/juju/domain/containerimageresourcestore"
containerimageresourcestoreerrors "github.com/juju/juju/domain/containerimageresourcestore/errors"
"github.com/juju/juju/internal/errors"
Expand Down Expand Up @@ -81,7 +81,7 @@ func (s *State) PutContainerImageMetadata(
ctx context.Context,
storageKey string,
registryPath, userName, password string,
) (resource.ResourceStorageUUID, error) {
) (store.UUID, error) {
db, err := s.DB()
if err != nil {
return "", errors.Capture(err)
Expand Down Expand Up @@ -129,7 +129,7 @@ WHERE storage_key = excluded.storage_key
if err != nil {
return "", err
}
return resource.ResourceStorageUUID(storageKey), nil
return store.UUID(storageKey), nil
}

// RemoveContainerImageMetadata removes a container image metadata resource from
Expand Down
4 changes: 2 additions & 2 deletions domain/containerimageresourcestore/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

"github.com/juju/juju/core/resource/store"
coreresourcetesting "github.com/juju/juju/core/resource/testing"
"github.com/juju/juju/domain/application/resource"
"github.com/juju/juju/domain/containerimageresourcestore"
"github.com/juju/juju/domain/containerimageresourcestore/errors"
schematesting "github.com/juju/juju/domain/schema/testing"
Expand Down Expand Up @@ -195,7 +195,7 @@ func (s *containerImageMetadataSuite) TestContainerImageMetadataRemoveBadUUID(c
c.Assert(err, jc.ErrorIs, errors.ContainerImageMetadataNotFound)
}

func (s *containerImageMetadataSuite) getContainerImageMetadata(c *gc.C, storageKey resource.ResourceStorageUUID) (string, string, string) {
func (s *containerImageMetadataSuite) getContainerImageMetadata(c *gc.C, storageKey store.UUID) (string, string, string) {
var retrievedRegistryPath, retrievedUsername, retrievedPassword string
err := s.TxnRunner().StdTxn(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
return tx.QueryRow(`
Expand Down
6 changes: 3 additions & 3 deletions domain/secret/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
corecharm "github.com/juju/juju/core/charm"
"github.com/juju/juju/core/database"
coremodel "github.com/juju/juju/core/model"
"github.com/juju/juju/core/resource/store"
coresecrets "github.com/juju/juju/core/secrets"
corestorage "github.com/juju/juju/core/storage"
jujuversion "github.com/juju/juju/core/version"
"github.com/juju/juju/domain"
"github.com/juju/juju/domain/application/resource"
applicationservice "github.com/juju/juju/domain/application/service"
applicationstate "github.com/juju/juju/domain/application/state"
modeltesting "github.com/juju/juju/domain/model/state/testing"
Expand Down Expand Up @@ -86,9 +86,9 @@ func (noopSecretDeleter) DeleteSecret(ctx domain.AtomicContext, uri *coresecrets

type noopResourceStoreGetter struct{}

func (noopResourceStoreGetter) AddStore(t charmresource.Type, store resource.ResourceStore) {}
func (noopResourceStoreGetter) AddStore(t charmresource.Type, store store.ResourceStore) {}

func (noopResourceStoreGetter) GetResourceStore(context.Context, charmresource.Type) (resource.ResourceStore, error) {
func (noopResourceStoreGetter) GetResourceStore(context.Context, charmresource.Type) (store.ResourceStore, error) {
return nil, nil
}

Expand Down
Loading

0 comments on commit 005f371

Please sign in to comment.