Skip to content

Commit

Permalink
Add GetAllSchemas command
Browse files Browse the repository at this point in the history
  • Loading branch information
calindima committed Sep 24, 2024
1 parent 630d5f8 commit 46525a5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pulsaradmin/pkg/admin/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Schema interface {
// GetSchemaInfoByVersion retrieves the schema of a topic at a given <tt>version</tt>
GetSchemaInfoByVersion(topic string, version int64) (*utils.SchemaInfo, error)

// GetAllSchemas retrieves the latest schema of a topic
GetAllSchemas(topic string) ([]*utils.SchemaInfoWithVersion, error)

// DeleteSchema deletes the schema associated with a given <tt>topic</tt>
DeleteSchema(topic string) error

Expand Down Expand Up @@ -130,6 +133,25 @@ func (s *schemas) GetSchemaInfoByVersion(topic string, version int64) (*utils.Sc
return info, nil
}

func (s *schemas) GetAllSchemas(topic string) ([]*utils.SchemaInfoWithVersion, error) {
topicName, err := utils.GetTopicName(topic)
if err != nil {
return nil, err
}
var response utils.GetAllSchemasResponse
endpoint := s.pulsar.endpoint(s.basePath, topicName.GetTenant(), topicName.GetNamespace(),
topicName.GetLocalName(), "schemas")

err = s.pulsar.Client.Get(endpoint, &response)
if err != nil {
fmt.Println("err:", err.Error())
return nil, err
}

infos := utils.ConvertGetAllSchemasResponseToSchemaInfosWithVersion(topicName, response)
return infos, nil
}

func (s *schemas) DeleteSchema(topic string) error {
return s.delete(topic, false)
}
Expand Down
25 changes: 25 additions & 0 deletions pulsaradmin/pkg/admin/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSchemas_GetAllSchemas(t *testing.T) {
cfg := &config.Config{}
admin, err := New(cfg)
assert.NoError(t, err)
assert.NotNil(t, admin)

topic := fmt.Sprintf("my-topic-%v", time.Now().Nanosecond())
schemaPayload := utils.PostSchemaPayload{
SchemaType: "STRING",
Schema: "",
}
err = admin.Schemas().CreateSchemaByPayload(topic, schemaPayload)
assert.NoError(t, err)

infos, err := admin.Schemas().GetAllSchemas(topic)
assert.NoError(t, err)
assert.Len(t, infos, 2)

err = admin.Schemas().ForceDeleteSchema(topic)
assert.NoError(t, err)

_, err = admin.Schemas().GetSchemaInfo(topic)
assert.Errorf(t, err, "Schema not found")
}

func TestSchemas_DeleteSchema(t *testing.T) {
cfg := &config.Config{}
admin, err := New(cfg)
Expand Down
14 changes: 14 additions & 0 deletions pulsaradmin/pkg/utils/schema_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type GetSchemaResponse struct {
Properties map[string]string `json:"properties"`
}

type GetAllSchemasResponse struct {
Schemas []GetSchemaResponse `json:"getSchemaResponses"`
}

type IsCompatibility struct {
IsCompatibility bool `json:"compatibility"`
SchemaCompatibilityStrategy SchemaCompatibilityStrategy `json:"schemaCompatibilityStrategy"`
Expand Down Expand Up @@ -90,3 +94,13 @@ func ConvertGetSchemaResponseToSchemaInfoWithVersion(tn *TopicName, response Get
info.Version = response.Version
return info
}

func ConvertGetAllSchemasResponseToSchemaInfosWithVersion(tn *TopicName, response GetAllSchemasResponse) []*SchemaInfoWithVersion {
infos := make([]*SchemaInfoWithVersion, len(response.Schemas))

for i, schema := range response.Schemas {
infos[i] = ConvertGetSchemaResponseToSchemaInfoWithVersion(tn, schema)
}

return infos
}

0 comments on commit 46525a5

Please sign in to comment.