Skip to content

Commit

Permalink
[pulsaradmin] Add GetAllSchemas command (#1289)
Browse files Browse the repository at this point in the history
* Add GetAllSchemas command

* Fix doc comment

* Fix typo

* Fix linter error: line too long

* Remove unnecessary print
  • Loading branch information
calindima authored Sep 30, 2024
1 parent 8f3334e commit f0518c0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
21 changes: 21 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 all schemas 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,24 @@ 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 {
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, 1)

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
17 changes: 17 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,16 @@ 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 f0518c0

Please sign in to comment.