Add agent

This commit is contained in:
2023-07-03 18:43:27 -04:00
parent b1b010deee
commit f82d3f18c6
6 changed files with 137 additions and 15 deletions

View File

@@ -26,10 +26,11 @@ func OptionLog(l logger) option {
}
}
func NewApprovalListener(ps svc.PubSub, requester svc.ApprovalRequester, options ...option) ApprovalListener {
func NewApprovalListener(ps svc.PubSub, requester svc.ApprovalRequester, repo svc.AppRepo, options ...option) ApprovalListener {
out := ApprovalListener{
PubSub: ps,
ApprovalRequester: requester,
AppRepo: repo,
}
for _, opt := range options {

View File

@@ -44,10 +44,9 @@ func (r Registrar) HandleRegistration(resp http.ResponseWriter, req *http.Reques
cert := req.TLS.PeerCertificates[0]
name := cert.DNSNames[0]
if r.repo.IsRegistered(name) {
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(`{"status": "registered"}`))
} else {
app, err := r.repo.GetApp(name)
switch err {
case svc.ErrApplicationNotFound:
id := r.repo.StartAppRegistration(name)
event := map[string]string{
"id": id.String(),
@@ -59,6 +58,12 @@ func (r Registrar) HandleRegistration(resp http.ResponseWriter, req *http.Reques
resp.WriteHeader(http.StatusCreated)
fmt.Fprintf(resp, `{"status": "pending_approval"}`)
case nil:
resp.WriteHeader(http.StatusOK)
fmt.Fprintf(resp, `{"status": "%s"}`, app.State)
default:
resp.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(resp, `{"errors": ["%s"]}`, err)
}
}

View File

@@ -23,6 +23,16 @@ func (repo FakeRepo) IsRegistered(name string) bool {
return false
}
func (repo FakeRepo) GetApp(name string) (svc.Application, error) {
for _, app := range repo.apps {
if name == app.Name {
return *app, nil
}
}
return svc.Application{}, svc.ErrApplicationNotFound
}
func (repo FakeRepo) PendingApprovalCount() int {
count := 0
for _, app := range repo.apps {
@@ -34,26 +44,23 @@ func (repo FakeRepo) PendingApprovalCount() int {
}
func (repo *FakeRepo) StartAppRegistration(name string) uuid.UUID {
repo.apps = append(repo.apps, &svc.Application{
newApp := &svc.Application{
ID: uuid.New(),
Name: name,
State: "pending_approval",
})
return uuid.New()
}
repo.apps = append(repo.apps, newApp)
return newApp.ID
}
func (repo *FakeRepo) ApproveApp(id uuid.UUID) (svc.Application, error) {
var app *svc.Application
for _, application := range repo.apps {
if application.ID == id {
application.State = "registered"
return *application, nil
}
}
if app == nil {
return svc.Application{}, svc.ErrApplicationNotFound
}
return *app, nil
return svc.Application{}, svc.ErrApplicationNotFound
}

View File

@@ -21,6 +21,7 @@ type Application struct {
type AppRepo interface {
IsRegistered(name string) bool
PendingApprovalCount() int
GetApp(name string) (Application, error)
StartAppRegistration(name string) uuid.UUID
ApproveApp(id uuid.UUID) (Application, error)
}