Skip to content

Commit

Permalink
feat: implement reboot and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
c100k committed Mar 21, 2024
1 parent c80ed93 commit a679dcd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions impl/http-server-go/data.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package main

const Err401 = "Authentication required"
const Err404Runnable = "Runnable Not found"
68 changes: 58 additions & 10 deletions impl/http-server-go/service_file_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"openapi"
"os"
"slices"
)

type ServiceFileJson struct {
Expand All @@ -16,6 +17,49 @@ type ServiceFileJson struct {
func (service ServiceFileJson) list(params *openapi.ListRunnablesQueryParams) (*ServiceError, *openapi.ListResRunnable) {
config := service.config

err, items := findItems(config)
if err != nil {
return err, nil
}

total := int32(len(items))

res := openapi.NewListResRunnable(items, total)

return nil, res
}

func (service ServiceFileJson) reboot(id string) (*ServiceError, *openapi.RunnableOperationRes) {
config := service.config
logger := service.logger

err, item := findItem(config, id)
if err != nil {
return err, nil
}

// In a real world, we would probably SSH into the instance to perform the command
logger.Info("Faking reboot", "id", item.Id, "name", item.Name)

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
}

func (service ServiceFileJson) stop(id string) (*ServiceError, *openapi.RunnableOperationRes) {
config := service.config
logger := service.logger

err, item := findItem(config, id)
if err != nil {
return err, nil
}

// In a real world, we would probably SSH into the instance to perform the command
logger.Info("Faking stop", "id", item.Id, "name", item.Name)

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
}

func findItems(config *Config) (*ServiceError, []openapi.Runnable) {
file, err := os.Open(*config.serviceFileJsonFilePath)
if err != nil {
return &ServiceError{HttpStatus: 500, Message: err.Error()}, nil
Expand All @@ -34,17 +78,21 @@ func (service ServiceFileJson) list(params *openapi.ListRunnablesQueryParams) (*
return &ServiceError{HttpStatus: 500, Message: "Fix your JSON file to respect the schema"}, nil
}

total := int32(len(items))

res := openapi.NewListResRunnable(items, total)

return nil, res
return nil, items
}

func (service ServiceFileJson) reboot(id string) (*ServiceError, *openapi.RunnableOperationRes) {
return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
}
func findItem(config *Config, id string) (*ServiceError, *openapi.Runnable) {
err, items := findItems(config)
if err != nil {
return err, nil
}

func (service ServiceFileJson) stop(id string) (*ServiceError, *openapi.RunnableOperationRes) {
return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
// If this happens to be called lots of times and we know the file is not changed after starting the server,
// this can be optimized by creating a Map to search faster
idx := slices.IndexFunc(items, func(i openapi.Runnable) bool { return i.Id == id })
if idx == -1 {
return &ServiceError{HttpStatus: 404, Message: Err404Runnable}, nil
}

return nil, &items[idx]
}
2 changes: 1 addition & 1 deletion impl/http-server-go/service_self.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (service ServiceSelf) stop(id string) (*ServiceError, *openapi.RunnableOper

func checkThatRunnableExists(config *Config, id string) *ServiceError {
if id != config.runnableId {
return &ServiceError{HttpStatus: 404, Message: "Runnable not found"}
return &ServiceError{HttpStatus: 404, Message: Err404Runnable}
}
return nil
}
Expand Down

0 comments on commit a679dcd

Please sign in to comment.