Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api support to update target subscriptions directly #461

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion docs/user_guide/api/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,43 @@ Returns an empty body if successful.
"Error Text"
]
}
```
```

## `PATCH /api/v1/targets/{id}/subscriptions`

Updates existing subscriptions for the target ID

Returns an empty body if successful.

=== "Request"
```bash
curl --request PATCH gnmic-api-address:port/api/v1/targets/192.168.1.131:57400/subscriptions -d '{"subscriptions": ["sub1", "sub2"]}'
```
=== "200 OK"
```json
```
=== "404 Not found"
```json
{
"errors": [
"target $target not found"
]
}
```
=== "400 Bad Request"
```json
{
"errors": [
"subscriptions not found",
"subscription $subscription does not exist"
]
karimra marked this conversation as resolved.
Show resolved Hide resolved
}
```
=== "500 Internal Server Error"
```json
{
"errors": [
"Error Text"
]
}
```
37 changes: 37 additions & 0 deletions pkg/app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,43 @@ func (a *App) handleConfigTargetsPost(w http.ResponseWriter, r *http.Request) {
a.AddTargetConfig(tc)
}

func (a *App) handleConfigTargetsSubscriptions(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
if !a.targetConfigExists(id) {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{fmt.Sprintf("target %q not found", id)}})
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{err.Error()}})
return
}
defer r.Body.Close()

var data map[string][]string
err = json.Unmarshal(body, &data)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{err.Error()}})
return
}
subs, ok := data["subscriptions"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{"subscriptions not found"}})
return
}
err = a.UpdateTargetSubscription(a.ctx, id, subs)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{err.Error()}})
return
}
}

func (a *App) handleConfigTargetsDelete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
Expand Down
1 change: 1 addition & 0 deletions pkg/app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (a *App) configRoutes(r *mux.Router) {
r.HandleFunc("/config/targets/{id}", a.handleConfigTargetsGet).Methods(http.MethodGet)
r.HandleFunc("/config/targets", a.handleConfigTargetsPost).Methods(http.MethodPost)
r.HandleFunc("/config/targets/{id}", a.handleConfigTargetsDelete).Methods(http.MethodDelete)
r.HandleFunc("/config/targets/{id}/subscriptions", a.handleConfigTargetsSubscriptions).Methods(http.MethodPatch)
// config/subscriptions
r.HandleFunc("/config/subscriptions", a.handleConfigSubscriptions).Methods(http.MethodGet)
// config/outputs
Expand Down
21 changes: 20 additions & 1 deletion pkg/app/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (a *App) initTarget(tc *types.TargetConfig) (*target.Target, error) {
return t, nil
}
return t, nil

}

func (a *App) stopTarget(ctx context.Context, name string) error {
Expand Down Expand Up @@ -96,6 +95,26 @@ func (a *App) DeleteTarget(ctx context.Context, name string) error {
return nil
}

// UpdateTargetConfig updates the subscriptions for an existing target
func (a *App) UpdateTargetSubscription(ctx context.Context, name string, subs []string) error {
a.configLock.Lock()
for _, subName := range subs {
if _, ok := a.Config.Subscriptions[subName]; !ok {
return fmt.Errorf("subscription %q does not exist", subName)
jarrodb marked this conversation as resolved.
Show resolved Hide resolved
}
}
targetConfig := a.Config.Targets[name]
targetConfig.Subscriptions = subs
a.configLock.Unlock()

if err := a.stopTarget(ctx, name); err != nil {
return err
}

go a.TargetSubscribeStream(ctx, targetConfig)
return nil
}

// AddTargetConfig adds a *TargetConfig to the configuration map
func (a *App) AddTargetConfig(tc *types.TargetConfig) {
a.Logger.Printf("adding target %s", tc)
Expand Down