Compare commits
2 commits
b0c73b2952
...
6dd21a465d
| Author | SHA1 | Date | |
|---|---|---|---|
|
6dd21a465d |
|||
|
3955fb2112 |
4 changed files with 31 additions and 10 deletions
|
|
@ -35,7 +35,7 @@ func main() {
|
|||
apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port)
|
||||
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
|
||||
|
||||
go agentapi.New(q).Start(apiAddr)
|
||||
go agentapi.New(q, db).Start(apiAddr)
|
||||
go promserver.Start(promAddr, registry)
|
||||
|
||||
select {}
|
||||
|
|
|
|||
|
|
@ -5,14 +5,16 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/worker"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
queue *worker.Queue
|
||||
db *badger.DB
|
||||
}
|
||||
|
||||
func New(queue *worker.Queue) *Server {
|
||||
return &Server{queue: queue}
|
||||
func New(queue *worker.Queue, db *badger.DB) *Server {
|
||||
return &Server{queue: queue, db: db}
|
||||
}
|
||||
|
||||
func (s *Server) Start(address string) {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,13 @@ package agentapi
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -25,14 +30,23 @@ func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Server) getVpc(w http.ResponseWriter, r *http.Request, name string) {
|
||||
func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "created"})
|
||||
}
|
||||
|
||||
func (s *Server) deleteVpc(w http.ResponseWriter, r *http.Request, name string) {
|
||||
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
s.queue.Submit(func() {
|
||||
destroyVpc(name)
|
||||
kv.AddInDB(s.db, "vpc/"+name+"/state", "deleting")
|
||||
if err := vpc.DeleteVPC(s.db, name); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
if state, err := kv.GetFromDB(s.db, "vpc/"+name+"/state"); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
} else if state == "deleted" {
|
||||
kv.DeleteInDB(s.db, "vpc/"+name)
|
||||
}
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
|
||||
|
|
|
|||
|
|
@ -2,7 +2,11 @@ package agentapi
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -17,7 +21,7 @@ func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Server) listVpcs(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) listVpcs(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode([]VPC{})
|
||||
}
|
||||
|
|
@ -35,10 +39,11 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
s.queue.Submit(func() {
|
||||
createVpc(req.Name)
|
||||
kv.AddInDB(s.db, "vpc/"+req.Name+"/state", "creating")
|
||||
if err := vpc.CreateVPC(s.db, req.Name); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"})
|
||||
}
|
||||
|
||||
func createVpc(name string) {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue