mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-07 07:16:54 +00:00
feat: Add ListInstances endpoint (#9452)
This commit is contained in:
@@ -2,11 +2,22 @@ package instance
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/cmd/build"
|
||||
filter "github.com/zitadel/zitadel/internal/api/grpc/filter/v2beta"
|
||||
"github.com/zitadel/zitadel/internal/api/grpc/object/v2"
|
||||
"github.com/zitadel/zitadel/internal/config/systemdefaults"
|
||||
"github.com/zitadel/zitadel/internal/query"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/instance/v2"
|
||||
)
|
||||
|
||||
func InstancesToPb(instances []*query.Instance) []*instance.Instance {
|
||||
list := []*instance.Instance{}
|
||||
for i, instance := range instances {
|
||||
list[i] = ToProtoObject(instance)
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func ToProtoObject(inst *query.Instance) *instance.Instance {
|
||||
return &instance.Instance{
|
||||
Id: inst.ID,
|
||||
@@ -39,3 +50,61 @@ func DomainToPb(d *query.InstanceDomain) *instance.Domain {
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func ListInstancesRequestToModel(req *instance.ListInstancesRequest, sysDefaults systemdefaults.SystemDefaults) (*query.InstanceSearchQueries, error) {
|
||||
offset, limit, asc, err := filter.PaginationPbToQuery(sysDefaults, req.GetPagination())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
queries, err := instanceQueriesToModel(req.Queries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &query.InstanceSearchQueries{
|
||||
SearchRequest: query.SearchRequest{
|
||||
Offset: offset,
|
||||
Limit: limit,
|
||||
Asc: asc,
|
||||
SortingColumn: fieldNameToInstanceColumn(req.SortingColumn),
|
||||
},
|
||||
Queries: queries,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func fieldNameToInstanceColumn(fieldName instance.FieldName) query.Column {
|
||||
switch fieldName {
|
||||
case instance.FieldName_FIELD_NAME_ID:
|
||||
return query.InstanceColumnID
|
||||
case instance.FieldName_FIELD_NAME_NAME:
|
||||
return query.InstanceColumnName
|
||||
case instance.FieldName_FIELD_NAME_CREATION_DATE:
|
||||
return query.InstanceColumnCreationDate
|
||||
default:
|
||||
return query.Column{}
|
||||
}
|
||||
}
|
||||
|
||||
func instanceQueriesToModel(queries []*instance.Query) (_ []query.SearchQuery, err error) {
|
||||
q := make([]query.SearchQuery, len(queries))
|
||||
for i, query := range queries {
|
||||
q[i], err = instanceQueryToModel(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func instanceQueryToModel(searchQuery *instance.Query) (query.SearchQuery, error) {
|
||||
switch q := searchQuery.Query.(type) {
|
||||
case *instance.Query_IdQuery:
|
||||
return query.NewInstanceIDsListSearchQuery(q.IdQuery.Ids...)
|
||||
case *instance.Query_DomainQuery:
|
||||
return query.NewInstanceDomainsListSearchQuery(q.DomainQuery.Domains...)
|
||||
default:
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "INST-3m0se", "List.Query.Invalid")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package instance
|
||||
import (
|
||||
"context"
|
||||
|
||||
filter "github.com/zitadel/zitadel/pkg/grpc/filter/v2beta"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/instance/v2"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
@@ -17,3 +18,23 @@ func (s *Server) GetInstance(ctx context.Context, _ *emptypb.Empty) (*instance.G
|
||||
Instance: ToProtoObject(inst),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) ListInstances(ctx context.Context, req *instance.ListInstancesRequest) (*instance.ListInstancesResponse, error) {
|
||||
queries, err := ListInstancesRequestToModel(req, s.systemDefaults)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
instances, err := s.query.SearchInstances(ctx, queries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &instance.ListInstancesResponse{
|
||||
Instances: InstancesToPb(instances.Instances),
|
||||
Pagination: &filter.PaginationResponse{
|
||||
TotalResult: instances.Count,
|
||||
AppliedLimit: uint64(req.GetPagination().GetLimit()),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/api/grpc/server"
|
||||
"github.com/zitadel/zitadel/internal/command"
|
||||
"github.com/zitadel/zitadel/internal/config/systemdefaults"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/query"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/instance/v2"
|
||||
@@ -18,6 +19,7 @@ type Server struct {
|
||||
command *command.Commands
|
||||
query *query.Queries
|
||||
checkPermission domain.PermissionCheck
|
||||
systemDefaults systemdefaults.SystemDefaults
|
||||
}
|
||||
|
||||
type Config struct{}
|
||||
@@ -26,11 +28,13 @@ func CreateServer(
|
||||
command *command.Commands,
|
||||
query *query.Queries,
|
||||
checkPermission domain.PermissionCheck,
|
||||
systemDefaults systemdefaults.SystemDefaults,
|
||||
) *Server {
|
||||
return &Server{
|
||||
command: command,
|
||||
query: query,
|
||||
checkPermission: checkPermission,
|
||||
systemDefaults: systemDefaults,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user