Skip to content

Commit

Permalink
add support to return list of all instances
Browse files Browse the repository at this point in the history
  • Loading branch information
GIC-de committed May 23, 2024
1 parent 6627677 commit 0e1f71d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ paths:
text/plain:
schema:
type: string
/api/v1/instances:
get:
summary: List of all instances.
description: >-
Get list of all instances.
responses:
200:
description: ok
content:
application/json:
schema:
type: list
/api/v1/instances/{instance_name}:
get:
summary: Status information of an instance.
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package controller
type Repository interface {
// ConfigFolder returns the config folder of this controller.
ConfigFolder() string
// Instances returns a list of all bngblaster instances.
Instances() []string
// Create a bngblaster instance on the file system.
// Does not start an instance.
Create(name string, config []byte) error
Expand Down
15 changes: 15 additions & 0 deletions pkg/controller/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ func (r *DefaultRepository) cleanupRunFiles(name string) error {
return nil
}

// Instances implements Repository.
func (r *DefaultRepository) Instances() []string {
instances := []string{}
entries, err := os.ReadDir(r.configFolder)
if err != nil {
return instances // Return the empty slice if there's an error.
}
for _, entry := range entries {
if entry.IsDir() {
instances = append(instances, entry.Name())
}
}
return instances
}

// Exists implements Repository.
func (r *DefaultRepository) Exists(name string) bool {
folder := path.Join(r.configFolder, name)
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/repositorymock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *Server) routes() {
EnableOpenMetrics: true,
},
))
s.router.Path("/api/v1/instances").Methods(http.MethodGet).Handler(s.instances())
s.router.
Path(
fmt.Sprintf("%s/{file_name:%s|%s|%s|%s|%s|%s|%s}",
Expand Down Expand Up @@ -102,6 +103,15 @@ func (s *Server) fileServing(directory string) http.HandlerFunc {
}
}

func (s *Server) instances() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
instances := s.repository.Instances()
w.Header().Set(contentType, applicationJSON)
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(instances)
}
}

func (s *Server) create() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
instanceVariable := mux.Vars(r)[instanceNameParameter]
Expand Down

0 comments on commit 0e1f71d

Please sign in to comment.