func (c *SvcClient) CreateService(data *Data) (string, error) {
	escalationPolicy, err := c.PdClient.GetEscalationPolicy(string(data.EscalationPolicyID), nil)
	if err != nil {
		return "", errors.New("Escalation policy not found in PagerDuty")
	}
	clusterService := pdApi.Service{
		Name:                   data.ServicePrefix + "-" + data.ClusterID + "." + data.BaseDomain + "-hive-cluster",
		Description:            data.ClusterID + " - A managed hive created cluster",
		EscalationPolicy:       *escalationPolicy,
		AutoResolveTimeout:     &data.AutoResolveTimeout,
		AcknowledgementTimeout: &data.AcknowledgeTimeOut,
		AlertCreation:          "create_alerts_and_incidents",
		IncidentUrgencyRule: &pdApi.IncidentUrgencyRule{
			Type:    "constant",
			Urgency: config.PagerDutyUrgencyRule,
		},
	}
	var newSvc *pdApi.Service
	newSvc, err = c.PdClient.CreateService(clusterService)
	if err != nil {
		if !strings.Contains(err.Error(), "Name has already been taken") {
			return "", err
		}
		lso := pdApi.ListServiceOptions{}
		lso.Query = clusterService.Name
		currentSvcs, newerr := c.PdClient.ListServices(lso)
		if newerr != nil {
			return "", err
		}
		if len(currentSvcs.Services) > 0 {
			for _, svc := range currentSvcs.Services {
				if svc.Name == clusterService.Name {
					newSvc = &svc
					break
				}
			}
		}
		if newSvc == nil {
			return "", err
		}
	}
	data.ServiceID = newSvc.ID
	data.IntegrationID, err = c.createIntegration(newSvc.ID, "V4 Alertmanager", "events_api_v2_inbound_integration")
	if err != nil {
		return "", err
	}
	return data.IntegrationID, err
}
CreateService creates a service in pagerduty for the specified clusterid and returns the service key
package pagerduty
import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"strconv"
	"strings"
	"github.com/openshift/pagerduty-operator/config"
	"github.com/openshift/pagerduty-operator/pkg/localmetrics"
	"time"
	pdApi "github.com/PagerDuty/go-pagerduty"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"
)
func getConfigMapKey(data map[string]string, key string) (string, error) {
	if _, ok := data[key]; !ok {
		errorStr := fmt.Sprintf("%v does not exist", key)
		return "", errors.New(errorStr)
	}
	retString := data[key]
	if len(retString) <= 0 {
		errorStr := fmt.Sprintf("%v is empty", key)
		return "", errors.New(errorStr)
	}
	return retString, nil
}
func GetSecretKey(data map[string][]byte, key string) (string, error) {
	if _, ok := data[key]; !ok {
		errorStr := fmt.Sprintf("%v does not exist", key)
		return "", errors.New(errorStr)
	}
	retString := string(data[key])
	if len(retString) <= 0 {
		errorStr := fmt.Sprintf("%v is empty", key)
		return "", errors.New(errorStr)
	}
	return retString, nil
}
type Client interface {
	GetService(data *Data) (*pdApi.Service, error)
	GetIntegrationKey(data *Data) (string, error)
	CreateService(data *Data) (string, error)
	DeleteService(data *Data) error
	EnableService(data *Data) error
	DisableService(data *Data) error
}
type PdClient interface {
	GetService(string, *pdApi.GetServiceOptions) (*pdApi.Service, error)
	GetEscalationPolicy(string, *pdApi.GetEscalationPolicyOptions) (*pdApi.EscalationPolicy, error)
	GetIntegration(string, string, pdApi.GetIntegrationOptions) (*pdApi.Integration, error)
	CreateService(service pdApi.Service) (*pdApi.Service, error)
	DeleteService(id string) error
	CreateIntegration(serviceID string, integration pdApi.Integration) (*pdApi.Integration, error)
	ListServices(pdApi.ListServiceOptions) (*pdApi.ListServiceResponse, error)
	ListIncidents(pdApi.ListIncidentsOptions) (*pdApi.ListIncidentsResponse, error)
	ListIncidentAlerts(incidentId string) (*pdApi.ListAlertsResponse, error)
	UpdateService(service pdApi.Service)  (*pdApi.Service, error)
}
type ManageEventFunc func(pdApi.V2Event) (*pdApi.V2EventResponse, error)
type DelayFunc func(time.Duration)
type SvcClient struct {
	APIKey      string
	PdClient    PdClient
	ManageEvent ManageEventFunc
	Delay       DelayFunc
}
type customHTTPClient struct {
	pdApi.HTTPClient
	controller string
}
func (c customHTTPClient) Do(req *http.Request) (*http.Response, error) {
	start := time.Now()
	resp, err := c.HTTPClient.Do(req)
	if err == nil {
		localmetrics.AddAPICall(c.controller, req, resp, time.Since(start).Seconds())
	}
	return resp, err
}
func WithCustomHTTPClient(controllerName string) pdApi.ClientOptions {
	return func(c *pdApi.Client) {
		c.HTTPClient = customHTTPClient{
			HTTPClient: c.HTTPClient,
			controller: controllerName,
		}
	}
}
func NewClient(APIKey string, controllerName string) Client {
	return &SvcClient{
		APIKey:      APIKey,
		PdClient:    pdApi.NewClient(APIKey, WithCustomHTTPClient(controllerName)),
		ManageEvent: pdApi.ManageEvent,
		Delay:       time.Sleep,
	}
}
type Data struct {
	EscalationPolicyID string
	AutoResolveTimeout uint
	AcknowledgeTimeOut uint
	ServicePrefix      string
	APIKey             string
	ClusterID          string
	BaseDomain         string
	ServiceID     string
	IntegrationID string
	Hibernating   bool
}
func (data *Data) ParseClusterConfig(osc client.Client, namespace string, cmName string) error {
	pdAPIConfigMap := &corev1.ConfigMap{}
	err := osc.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: cmName}, pdAPIConfigMap)
	if err != nil {
		return err
	}
	data.ServiceID, err = getConfigMapKey(pdAPIConfigMap.Data, "SERVICE_ID")
	if err != nil {
		return err
	}
	data.IntegrationID, err = getConfigMapKey(pdAPIConfigMap.Data, "INTEGRATION_ID")
	if err != nil {
		return err
	}
	val := pdAPIConfigMap.Data["HIBERNATING"]
	data.Hibernating = val == "true"
	return nil
}
func (data *Data) SetClusterConfig(osc client.Client, namespace string, cmName string) error {
	pdAPIConfigMap := &corev1.ConfigMap{}
	if err := osc.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: cmName}, pdAPIConfigMap); err != nil {
		return err
	}
	pdAPIConfigMap.Data["SERVICE_ID"] = data.ServiceID
	pdAPIConfigMap.Data["INTEGRATION_ID"] = data.IntegrationID
	pdAPIConfigMap.Data["HIBERNATING"] = strconv.FormatBool(data.Hibernating)
	if err := osc.Update(context.TODO(), pdAPIConfigMap); err != nil {
		return err
	}
	return nil
}
func (c *SvcClient) GetService(data *Data) (*pdApi.Service, error) {
	service, err := c.PdClient.GetService(data.ServiceID, nil)
	if err != nil {
		return nil, err
	}
	return service, nil
}
func (c *SvcClient) GetIntegrationKey(data *Data) (string, error) {
	integration, err := c.PdClient.GetIntegration(data.ServiceID, data.IntegrationID, pdApi.GetIntegrationOptions{})
	if err != nil {
		return "", err
	}
	return integration.IntegrationKey, nil
}