Skip to content

Commit

Permalink
Merge pull request #461 from jarrodb/config-targets-subscriptions
Browse files Browse the repository at this point in the history
api support to update target subscriptions directly
  • Loading branch information
karimra authored Jun 21, 2024
2 parents 5f1fdc5 + b104276 commit a677013
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
40 changes: 39 additions & 1 deletion docs/user_guide/api/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,42 @@ 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": [
"subscription $subscription does not exist"
]
}
```
=== "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
22 changes: 21 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,27 @@ 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 {
a.configLock.Unlock()
return fmt.Errorf("subscription %q does not exist", subName)
}
}
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

0 comments on commit a677013

Please sign in to comment.