Skip to content

Commit 0b3a639

Browse files
committed
refactor: update sdk and api docs
Signed-off-by: Felix Gateru <felix.gateru@gmail.com>
1 parent 7dc68fc commit 0b3a639

File tree

11 files changed

+62
-47
lines changed

11 files changed

+62
-47
lines changed

api/http/common.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
233233
errors.Contains(err, apiutil.ErrMissingRoleMembers),
234234
errors.Contains(err, apiutil.ErrMissingDescription),
235235
errors.Contains(err, apiutil.ErrMissingEntityID),
236-
errors.Contains(err, apiutil.ErrInvalidRouteFormat):
236+
errors.Contains(err, apiutil.ErrInvalidRouteFormat),
237+
errors.Contains(err, apiutil.ErrMissingRoute):
237238
err = unwrap(err)
238239
w.WriteHeader(http.StatusBadRequest)
239240

api/http/util/errors.go

+3
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,7 @@ var (
265265

266266
// ErrInvalidRouteFormat indicates invalid route format.
267267
ErrInvalidRouteFormat = errors.New("invalid route format")
268+
269+
// ErrMissingRoute indicates missing route.
270+
ErrMissingRoute = errors.New("missing route")
268271
)

apidocs/openapi/channels.yaml

+6-11
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,10 @@ components:
538538
type: string
539539
example: bb7edb32-2eac-4aad-aebe-ed96fe073879
540540
description: Id of parent channel, it must be existing channel.
541-
topic:
541+
route:
542542
type: string
543-
example: channelTopic
544-
description: Channel topic.
543+
example: channelRoute
544+
description: Channel route.
545545
metadata:
546546
type: object
547547
example: { "location": "example" }
@@ -587,10 +587,10 @@ components:
587587
format: uuid
588588
example: bb7edb32-2eac-4aad-aebe-ed96fe073879
589589
description: Channel parent identifier.
590-
topic:
590+
route:
591591
type: string
592-
example: channelTopic
593-
description: Channel topic.
592+
example: channelRoute
593+
description: Channel route.
594594
metadata:
595595
type: object
596596
example: { "role": "general" }
@@ -655,17 +655,12 @@ components:
655655
type: string
656656
example: channelName
657657
description: Free-form channel name. Channel name is unique on the given hierarchy level.
658-
topic:
659-
type: string
660-
example: channelTopic
661-
description: Channel topic.
662658
metadata:
663659
type: object
664660
example: { "role": "general" }
665661
description: Arbitrary, object-encoded channels's data.
666662
required:
667663
- name
668-
- topic
669664
- metadata
670665

671666
ChannelUpdateTags:

channels/api/http/endpoint_test.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestCreateChannelEndpoint(t *testing.T) {
7272
Metadata: map[string]interface{}{
7373
"name": "test",
7474
},
75+
Route: valid,
7576
}
7677

7778
cases := []struct {
@@ -217,6 +218,7 @@ func TestCreateChannelsEndpoint(t *testing.T) {
217218
Metadata: map[string]interface{}{
218219
"name": "test",
219220
},
221+
Route: valid,
220222
},
221223
}
222224

@@ -288,6 +290,20 @@ func TestCreateChannelsEndpoint(t *testing.T) {
288290
status: http.StatusBadRequest,
289291
err: apiutil.ErrNameSize,
290292
},
293+
{
294+
desc: "create channels with invalid route format",
295+
token: validToken,
296+
domainID: validID,
297+
req: []channels.Channel{
298+
{
299+
Name: valid,
300+
Route: "__invalid",
301+
},
302+
},
303+
contentType: contentType,
304+
status: http.StatusBadRequest,
305+
err: apiutil.ErrInvalidRouteFormat,
306+
},
291307
{
292308
desc: "create channels with invalid content type",
293309
token: validToken,
@@ -770,8 +786,8 @@ func TestUpdateChannelEndpoint(t *testing.T) {
770786
defer gs.Close()
771787

772788
updateChannelReq := channels.Channel{
773-
ID: validID,
774-
Name: valid,
789+
ID: validID,
790+
Name: valid,
775791
Metadata: map[string]interface{}{
776792
"name": "test",
777793
},

channels/api/http/requests.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ func (req createChannelReq) validate() error {
2525
return apiutil.ErrMissingChannelID
2626
}
2727
}
28-
if req.Channel.Route != "" {
29-
if err := validateRoute(req.Channel.Route); err != nil {
30-
return err
31-
}
28+
if req.Channel.Route == "" {
29+
return apiutil.ErrMissingRoute
30+
}
31+
if err := validateRoute(req.Channel.Route); err != nil {
32+
return err
3233
}
3334

3435
return nil
@@ -51,6 +52,12 @@ func (req createChannelsReq) validate() error {
5152
if len(channel.Name) > api.MaxNameSize {
5253
return apiutil.ErrNameSize
5354
}
55+
if channel.Route == "" {
56+
return apiutil.ErrMissingRoute
57+
}
58+
if err := validateRoute(channel.Route); err != nil {
59+
return err
60+
}
5461
}
5562

5663
return nil

channels/api/http/requests_test.go

+8-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ func TestCreateChannelReqValidation(t *testing.T) {
2626
desc: "valid request",
2727
req: createChannelReq{
2828
Channel: channels.Channel{
29-
Name: valid,
29+
Name: valid,
30+
Route: valid,
3031
},
3132
},
3233
err: nil,
@@ -35,20 +36,11 @@ func TestCreateChannelReqValidation(t *testing.T) {
3536
desc: "long name",
3637
req: createChannelReq{
3738
Channel: channels.Channel{
38-
Name: strings.Repeat("a", api.MaxNameSize+1),
39-
},
40-
},
41-
err: apiutil.ErrNameSize,
42-
},
43-
{
44-
desc: "valid request with route",
45-
req: createChannelReq{
46-
Channel: channels.Channel{
47-
Name: valid,
39+
Name: strings.Repeat("a", api.MaxNameSize+1),
4840
Route: valid,
4941
},
5042
},
51-
err: nil,
43+
err: apiutil.ErrNameSize,
5244
},
5345
{
5446
desc: "invalid route",
@@ -88,7 +80,8 @@ func TestCreateChannelsReqValidation(t *testing.T) {
8880
req: createChannelsReq{
8981
Channels: []channels.Channel{
9082
{
91-
Name: valid,
83+
Name: valid,
84+
Route: valid,
9285
},
9386
},
9487
},
@@ -99,7 +92,8 @@ func TestCreateChannelsReqValidation(t *testing.T) {
9992
req: createChannelsReq{
10093
Channels: []channels.Channel{
10194
{
102-
Name: strings.Repeat("a", api.MaxNameSize+1),
95+
Name: strings.Repeat("a", api.MaxNameSize+1),
96+
Route: valid,
10397
},
10498
},
10599
},

channels/service.go

-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ func (svc service) CreateChannels(ctx context.Context, session authn.Session, ch
6565
}
6666
c.ID = clientID
6767
}
68-
if c.Route == "" {
69-
c.Route = c.ID
70-
}
7168

7269
if c.Status != DisabledStatus && c.Status != EnabledStatus {
7370
return []Channel{}, []roles.RoleProvision{}, svcerr.ErrInvalidStatus

channels/service_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,6 @@ func TestCreateChannel(t *testing.T) {
117117
}},
118118
err: nil,
119119
},
120-
{
121-
desc: "create channel with empty route",
122-
channel: etChan,
123-
saveResp: []channels.Channel{{
124-
ID: testsutil.GenerateUUID(t),
125-
CreatedAt: time.Now(),
126-
Domain: validID,
127-
}},
128-
err: nil,
129-
},
130120
{
131121
desc: "create channel with invalid status",
132122
channel: channels.Channel{

pkg/sdk/channels.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Channel struct {
2424
ID string `json:"id,omitempty"`
2525
Name string `json:"name,omitempty"`
2626
Tags []string `json:"tags,omitempty"`
27+
Route string `json:"route,omitempty"`
2728
ParentGroup string `json:"parent_group_id,omitempty"`
2829
DomainID string `json:"domain_id,omitempty"`
2930
Metadata Metadata `json:"metadata,omitempty"`

pkg/sdk/channels_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ func TestCreateChannel(t *testing.T) {
5353

5454
createChannelReq := channels.Channel{
5555
Name: channel.Name,
56+
Route: channel.Route,
5657
Metadata: channels.Metadata{"role": "client"},
5758
Status: channels.EnabledStatus,
5859
}
5960

6061
channelReq := sdk.Channel{
6162
Name: channel.Name,
63+
Route: channel.Route,
6264
Metadata: validMetadata,
6365
Status: channels.EnabledStatus.String(),
6466
}
@@ -133,6 +135,7 @@ func TestCreateChannel(t *testing.T) {
133135
desc: "create channel with parent group",
134136
channelReq: sdk.Channel{
135137
Name: channel.Name,
138+
Route: channel.Route,
136139
ParentGroup: parentID,
137140
Status: channels.EnabledStatus.String(),
138141
},
@@ -141,6 +144,7 @@ func TestCreateChannel(t *testing.T) {
141144
createChannelReq: channels.Channel{
142145
Name: channel.Name,
143146
ParentGroup: parentID,
147+
Route: channel.Route,
144148
Status: channels.EnabledStatus,
145149
},
146150
svcRes: []channels.Channel{convertChannel(pChannel)},
@@ -152,6 +156,7 @@ func TestCreateChannel(t *testing.T) {
152156
desc: "create channel with invalid parent",
153157
channelReq: sdk.Channel{
154158
Name: channel.Name,
159+
Route: channel.Route,
155160
ParentGroup: wrongID,
156161
Status: channels.EnabledStatus.String(),
157162
},
@@ -160,6 +165,7 @@ func TestCreateChannel(t *testing.T) {
160165
createChannelReq: channels.Channel{
161166
Name: channel.Name,
162167
ParentGroup: wrongID,
168+
Route: channel.Route,
163169
Status: channels.EnabledStatus,
164170
},
165171
svcRes: []channels.Channel{},
@@ -172,6 +178,7 @@ func TestCreateChannel(t *testing.T) {
172178
channelReq: sdk.Channel{
173179
ID: channel.ID,
174180
ParentGroup: parentID,
181+
Route: channel.Route,
175182
Name: channel.Name,
176183
Metadata: validMetadata,
177184
CreatedAt: channel.CreatedAt,
@@ -183,6 +190,7 @@ func TestCreateChannel(t *testing.T) {
183190
createChannelReq: channels.Channel{
184191
ID: channel.ID,
185192
ParentGroup: parentID,
193+
Route: channel.Route,
186194
Name: channel.Name,
187195
Metadata: channels.Metadata{"role": "client"},
188196
CreatedAt: channel.CreatedAt,
@@ -293,8 +301,9 @@ func TestCreateChannels(t *testing.T) {
293301
token: validToken,
294302
channelsReq: []sdk.Channel{
295303
{
296-
ID: generateUUID(t),
297-
Name: "channel_1",
304+
ID: generateUUID(t),
305+
Name: "channel_1",
306+
Route: valid,
298307
Metadata: map[string]interface{}{
299308
"test": make(chan int),
300309
},
@@ -2116,6 +2125,7 @@ func generateTestChannel(t *testing.T) sdk.Channel {
21162125
ID: testsutil.GenerateUUID(&testing.T{}),
21172126
DomainID: testsutil.GenerateUUID(&testing.T{}),
21182127
Name: channelName,
2128+
Route: valid,
21192129
Metadata: sdk.Metadata{"role": "client"},
21202130
CreatedAt: createdAt,
21212131
UpdatedAt: updatedAt,

pkg/sdk/setup_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ func convertChannel(g sdk.Channel) mgchannels.Channel {
212212
Name: g.Name,
213213
Tags: g.Tags,
214214
ParentGroup: g.ParentGroup,
215+
Route: g.Route,
215216
Domain: g.DomainID,
216217
Metadata: channels.Metadata(g.Metadata),
217218
CreatedAt: g.CreatedAt,

0 commit comments

Comments
 (0)