lablup / lablup/backend.ai

Add GraphQL and REST APIs for Notification Channel and Rule Management

Open
#6,439 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
670
Forks
183
Avg merge
17h 7m
Merged PRs (30d)
358

Description

## User Story

As a Backend.AI administrator, I want GraphQL and REST APIs to manage notification channels and rules, so that I can configure notification routing through the admin interface or API clients.

## Context

This story implements user-facing APIs (GraphQL mutations/queries and REST endpoints) for managing notification channels, rules, and viewing notification history.

## Functional Requirements

### 1. GraphQL Schema

Define GraphQL types and operations:

```
# Types
type NotificationChannel {
id: UUID!
name: String!
description: String
channelType: String!
config: JSONObject!
enabled: Boolean!
createdAt: DateTime!
updatedAt: DateTime
}

type NotificationRule {
id: UUID!
name: String!
description: String
eventType: String!
eventFilter: JSONObject
channel: NotificationChannel!
messageTemplate: String!
priority: Int!
enabled: Boolean!
createdAt: DateTime!
updatedAt: DateTime
}

# Input types
input CreateNotificationChannelInput {
name: String!
description: String
channelType: String!
config: JSONObject!
}

input UpdateNotificationChannelInput {
name: String
description: String
config: JSONObject
enabled: Boolean
}

input CreateNotificationRuleInput {
name: String!
description: String
eventType: String!
eventFilter: JSONObject
channelId: UUID!
messageTemplate: String!
priority: Int
}

input UpdateNotificationRuleInput {
name: String
description: String
eventFilter: JSONObject
channelId: UUID
messageTemplate: String
priority: Int
enabled: Boolean
}

# Queries
type Query {
notificationChannel(id: UUID!): NotificationChannel
notificationChannels(enabled: Boolean): [NotificationChannel!]!

notificationRule(id: UUID!): NotificationRule
notificationRules(eventType: String, enabled: Boolean): [NotificationRule!]!
}

# Mutations
type Mutation {
createNotificationChannel(input: CreateNotificationChannelInput!): NotificationChannel!
updateNotificationChannel(id: UUID!, input: UpdateNotificationChannelInput!): NotificationChannel!
deleteNotificationChannel(id: UUID!): Boolean!

createNotificationRule(input: CreateNotificationRuleInput!): NotificationRule!
updateNotificationRule(id: UUID!, input: UpdateNotificationRuleInput!): NotificationRule!
deleteNotificationRule(id: UUID!): Boolean!

testNotificationChannel(id: UUID!): Boolean!
}
```

### 2. GraphQL Resolvers

Implement resolvers with proper RBAC checks:

```python
# src/ai/backend/manager/api/notification_gql.py

@strawberry.type
class NotificationChannelMutations:
@strawberry.mutation(
permission_classes=[SuperAdminRequired]
)
async def create_notification_channel(
self,
info: Info,
input: CreateNotificationChannelInput,
) -> NotificationChannel:
# Validation
# Create in DB
# Return result

@strawberry.mutation(
permission_classes=[SuperAdminRequired]
)
async def update_notification_channel(
self,
info: Info,
id: UUID,
input: UpdateNotificationChannelInput,
) -> NotificationChannel:
# Update in DB
# Return result

@strawberry.mutation(
permission_classes=[SuperAdminRequired]
)
async def test_notification_channel(
self,
info: Info,
id: UUID,
) -> bool:
# Send test notification
# Return success status
```

### 3. REST API Endpoints

Provide REST API as alternative to GraphQL:

```python
# src/ai/backend/manager/api/notification_rest.py

# Channels
POST /notification/channels
GET /notification/channels
GET /notification/channels/{channel_id}
PATCH /notification/channels/{channel_id}
DELETE /notification/channels/{channel_id}
POST /notification/channels/{channel_id}/test

# Rules
POST /notification/rules
GET /notification/rules
GET /notification/rules/{rule_id}
PATCH /notification/rules/{rule_id}
DELETE /notification/rules/{rule_id}
```

### 4. RBAC Integration

Permissions required:

- **SuperAdmin**: Full access to channels and rules
- **DomainAdmin**: Read-only access to view rules
- **User**: No access (future: user preferences)

### 5. Validation

Implement validation for:

- Channel config schema based on channel_type
- Message template syntax (Jinja2)
- Event type format
- Required fields

### 6. Test Channel Functionality

Implement test notification feature:

```python
async def test_notification_channel(channel_id: UUID):
# Create test event
test_event = NotificationEvent(
event_type="test.notification",
subject="Test Notification",
body="This is a test notification from Backend.AI",
severity="info",
context={"test": True},
...
)

# Send through specific channel
# Return result
```

## Acceptance Criteria

- [ ] GraphQL schema defined with all types
- [ ] GraphQL mutations implemented with RBAC
- [ ] GraphQL queries with filtering
- [ ] REST endpoints for channels and rules
- [ ] Channel config validation by type
- [ ] Message template validation (Jinja2)
- [ ] Test notification functionality
- [ ] Error handling and proper error messages
- [ ] Unit tests for resolvers
- [ ] Integration tests for API endpoints
- [ ] API documentation

## Related Issues

Epic: BA-302
Depends on: BA-2862 (Database models)

JIRA Issue: BA-2864

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.