Implement Health Check Api. 94/9694/1
authornaman.gupta <naman.gupta@samsung.com>
Thu, 17 Nov 2022 12:50:03 +0000 (18:20 +0530)
committernaman.gupta <naman.gupta@samsung.com>
Thu, 17 Nov 2022 12:50:03 +0000 (18:20 +0530)
Implement Health Check Api.

Signed-off-by: naman.gupta <naman.gupta@samsung.com>
Change-Id: Id091fa42eb6df5995f9d5d3995a075a59bb827f9

a1-go/pkg/restapi/operations/a1_mediator/a1_controller_get_healthcheck_responses.go
a1-go/pkg/restful/restful.go
a1-go/pkg/resthooks/resthooks.go
a1-go/pkg/resthooks/resthooks_test.go

index 20cf0ca..84f3630 100644 (file)
@@ -55,3 +55,27 @@ func (o *A1ControllerGetHealthcheckOK) WriteResponse(rw http.ResponseWriter, pro
 
        rw.WriteHeader(200)
 }
+
+// A1ControllerGetHealthcheckInternalServerErrorCode is the HTTP code returned for type A1ControllerGetHealthcheckInternalServerError
+const A1ControllerGetHealthcheckInternalServerErrorCode int = 500
+
+/*A1ControllerGetHealthcheckInternalServerError Internal error to signal A1 is not healthy. Client should attempt to retry later.
+
+swagger:response a1ControllerGetHealthcheckInternalServerError
+*/
+type A1ControllerGetHealthcheckInternalServerError struct {
+}
+
+// NewA1ControllerGetHealthcheckInternalServerError creates A1ControllerGetHealthcheckInternalServerError with default headers values
+func NewA1ControllerGetHealthcheckInternalServerError() *A1ControllerGetHealthcheckInternalServerError {
+
+       return &A1ControllerGetHealthcheckInternalServerError{}
+}
+
+// WriteResponse to the client
+func (o *A1ControllerGetHealthcheckInternalServerError) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
+
+       rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses
+
+       rw.WriteHeader(500)
+}
index d8d1e92..44b3422 100644 (file)
@@ -50,6 +50,16 @@ func (r *Restful) setupHandler() *operations.A1API {
        }
 
        api := operations.NewA1API(swaggerSpec)
+
+       api.A1MediatorA1ControllerGetHealthcheckHandler = a1_mediator.A1ControllerGetHealthcheckHandlerFunc(func(param a1_mediator.A1ControllerGetHealthcheckParams) middleware.Responder {
+               a1.Logger.Debug("handler for get Health Check of A1")
+               resp := r.rh.GetA1Health()
+               if resp == false {
+                       return a1_mediator.NewA1ControllerGetHealthcheckInternalServerError()
+               }
+               return a1_mediator.NewA1ControllerGetHealthcheckOK()
+       })
+
        api.A1MediatorA1ControllerGetAllPolicyTypesHandler = a1_mediator.A1ControllerGetAllPolicyTypesHandlerFunc(func(param a1_mediator.A1ControllerGetAllPolicyTypesParams) middleware.Responder {
                a1.Logger.Debug("handler for get all policy type")
                return a1_mediator.NewA1ControllerGetAllPolicyTypesOK().WithPayload(r.rh.GetAllPolicyType())
index c57c2e8..8fb07df 100644 (file)
@@ -95,6 +95,15 @@ func createResthook(sdlInst iSdl, rmrSenderInst rmr.IRmrSender) *Resthook {
        }
 }
 
+func (rh *Resthook) GetA1Health() bool {
+       data, _ := rh.db.GetAll(a1MediatorNs)
+       if data != nil {
+               a1.Logger.Debug("Database connected and A1 is healthy")
+               return true
+       }
+       return false
+}
+
 func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID {
 
        var policyTypeIDs []models.PolicyTypeID
index 8c6b59a..6d7679e 100644 (file)
@@ -58,6 +58,17 @@ func TestMain(m *testing.M) {
        os.Exit(code)
 }
 
+func TestHealth(t *testing.T) {
+       resp := rh.GetA1Health()
+       if resp == true {
+               a1.Logger.Debug("A1 is healthy ")
+               assert.Equal(t, true, resp)
+       } else {
+               a1.Logger.Debug("A1 is unhealthy")
+               assert.Equal(t, false, resp)
+       }
+}
+
 func TestGetAllPolicyType(t *testing.T) {
        resp := rh.GetAllPolicyType()
        assert.Equal(t, 2, len(resp))