[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add health check of CMPv2 provisioner (cert-service...
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / certserviceclient / cert_service_client.go
index 870a3ed..15b9062 100644 (file)
@@ -23,6 +23,7 @@ package certserviceclient
 import (
        "encoding/base64"
        "encoding/json"
+       "fmt"
        "net/http"
 )
 
@@ -33,9 +34,11 @@ const (
 
 type CertServiceClient interface {
        GetCertificates(csr []byte, key []byte) (*CertificatesResponse, error)
+       CheckHealth() error
 }
 
 type CertServiceClientImpl struct {
+       healthUrl string
        certificationUrl string
        httpClient       HTTPClient
 }
@@ -49,6 +52,25 @@ type CertificatesResponse struct {
        TrustedCertificates []string `json:"trustedCertificates"`
 }
 
+func (client *CertServiceClientImpl) CheckHealth() error {
+       request, err := http.NewRequest("GET", client.healthUrl, nil)
+       if err != nil {
+               return err
+       }
+
+       response, err := client.httpClient.Do(request)
+       if err != nil {
+               return err
+       }
+
+       if response.StatusCode != 200 {
+               return fmt.Errorf("health check retured status code [%d]", response.StatusCode)
+       }
+
+       return nil
+}
+
+
 func (client *CertServiceClientImpl) GetCertificates(csr []byte, key []byte) (*CertificatesResponse, error) {
 
        request, err := http.NewRequest("GET", client.certificationUrl, nil)