Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions api/handlers/v1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ func (server APIServer) GetResource(ctx context.Context, request *entropyv1beta1
return &response, nil
}

func (server APIServer) ListResources(ctx context.Context, request *entropyv1beta1.ListResourcesRequest) (*entropyv1beta1.ListResourcesResponse, error) {
var responseResources []*entropyv1beta1.Resource
resources, err := server.resourceService.ListResources(ctx, request.Parent, request.Kind)
if err != nil {
return nil, status.Error(codes.Internal, "failed to fetch resources from db")
}
for _, res := range resources {
responseResource, err := resourceToProto(res)
if err != nil {
return nil, status.Error(codes.Internal, "failed to serialize resource")
}
responseResources = append(responseResources, responseResource)
}
if err != nil {
return nil, status.Error(codes.Internal, "failed to serialize resource")
}
response := entropyv1beta1.ListResourcesResponse{
Resources: responseResources,
}
return &response, nil
}

func (server APIServer) syncResource(ctx context.Context, updatedResource *domain.Resource) (*domain.Resource, error) {
syncedResource, err := server.moduleService.Sync(ctx, updatedResource)
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions api/handlers/v1/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,46 @@ func TestAPIServer_GetResource(t *testing.T) {
}
})
}

func TestAPIServer_ListResource(t *testing.T) {
t.Run("test list resource", func(t *testing.T) {
r := &domain.Resource{
Urn: "p-testdata-gl-testname-mock",
Name: "testname",
Parent: "p-testdata-gl",
Kind: "mock",
Configs: map[string]interface{}{},
Labels: map[string]string{},
Status: domain.ResourceStatusCompleted,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
rProto, _ := resourceToProto(r)
argsRequest := &entropyv1beta1.ListResourcesRequest{
Parent: "p-testdata-gl",
Kind: "mock",
}
want := &entropyv1beta1.ListResourcesResponse{
Resources: []*entropyv1beta1.Resource{rProto},
}
wantErr := error(nil)

mockResourceService := &mocks.ResourceService{}
mockResourceService.EXPECT().ListResources(mock.Anything, r.Parent, r.Kind).Return([]*domain.Resource{r}, nil).Once()

mockModuleService := &mocks.ModuleService{}

server := APIServer{
resourceService: mockResourceService,
moduleService: mockModuleService,
}
got, err := server.ListResources(context.TODO(), argsRequest)
if !errors.Is(err, wantErr) {
t.Errorf("ListResource() error = %v, wantErr %v", err, wantErr)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("ListResource() got = %v, want %v", got, want)
}
})
}
46 changes: 46 additions & 0 deletions mocks/resource_repository.go

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

48 changes: 48 additions & 0 deletions mocks/resource_service.go

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

12 changes: 12 additions & 0 deletions pkg/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ServiceInterface interface {
CreateResource(ctx context.Context, res *domain.Resource) (*domain.Resource, error)
UpdateResource(ctx context.Context, res *domain.Resource) (*domain.Resource, error)
GetResource(ctx context.Context, urn string) (*domain.Resource, error)
ListResources(ctx context.Context, parent string, kind string) ([]*domain.Resource, error)
}

type Service struct {
Expand Down Expand Up @@ -50,3 +51,14 @@ func (s *Service) UpdateResource(ctx context.Context, res *domain.Resource) (*do
func (s *Service) GetResource(ctx context.Context, urn string) (*domain.Resource, error) {
return s.resourceRepository.GetByURN(urn)
}

func (s *Service) ListResources(ctx context.Context, parent string, kind string) ([]*domain.Resource, error) {
filter := map[string]string{}
if kind != "" {
filter["kind"] = kind
}
if parent != "" {
filter["parent"] = parent
}
return s.resourceRepository.List(filter)
}
46 changes: 46 additions & 0 deletions pkg/resource/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,49 @@ func TestService_GetResource(t *testing.T) {
}
})
}

func TestService_ListResources(t *testing.T) {
t.Run("test list resource", func(t *testing.T) {
mockRepo := &mocks.ResourceRepository{}
currentTime := time.Now()
updatedTime := time.Now()
want := []*domain.Resource{{
Urn: "p-testdata-gl-testname-log",
Name: "testname",
Parent: "p-testdata-gl",
Kind: "log",
Configs: map[string]interface{}{
"replicas": "10",
},
Labels: map[string]string{},
Status: domain.ResourceStatusCompleted,
CreatedAt: currentTime,
UpdatedAt: updatedTime,
}}
wantErr := error(nil)

mockRepo.EXPECT().List(map[string]string{"parent": "p-testdata-gl", "kind": "log"}).Return([]*domain.Resource{{
Urn: "p-testdata-gl-testname-log",
Name: "testname",
Parent: "p-testdata-gl",
Kind: "log",
Configs: map[string]interface{}{
"replicas": "10",
},
Labels: map[string]string{},
Status: domain.ResourceStatusCompleted,
CreatedAt: currentTime,
UpdatedAt: updatedTime,
}}, nil).Once()

s := NewService(mockRepo)
got, err := s.ListResources(context.Background(), "p-testdata-gl", "log")
if !errors.Is(err, wantErr) {
t.Errorf("ListResources() error = %v, wantErr %v", err, wantErr)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("ListResources() got = %v, want %v", got, want)
}
})
}
16 changes: 16 additions & 0 deletions store/mongodb/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,19 @@ func (rc *ResourceRepository) GetByURN(urn string) (*domain.Resource, error) {
}
return res, nil
}

func (rc *ResourceRepository) List(filter map[string]string) ([]*domain.Resource, error) {
var res []*domain.Resource
cur, err := rc.collection.Find(context.TODO(), filter)
if err != nil {
return nil, err
}
err = cur.All(context.TODO(), &res)
if err != nil {
if err == mongo.ErrNoDocuments {
return res, nil
}
return nil, err
}
return res, nil
}
1 change: 1 addition & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ResourceRepository interface {
Update(r *domain.Resource) error
GetByURN(urn string) (*domain.Resource, error)
Migrate() error
List(filter map[string]string) ([]*domain.Resource, error)
}

type ModuleRepository interface {
Expand Down