Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grafana contact point #114

Merged
merged 20 commits into from
Nov 16, 2023
25 changes: 25 additions & 0 deletions grafana_contact_points/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Grafana Contact Point

To create a Grafana Contact Point:

```go
createGrafanaContactPoint := grafana_contact_points.GrafanaContactPoint{
Name: "my-contact-point",
Type: "email",
Settings: map[string]interface{}{
"addresses": "[email protected];[email protected]",
"singleEmail": false,
},
DisableResolveMessage: false,
}
client, err := grafana_contact_points.New("some-token", "some-url")
contactPoint, err := client.CreateGrafanaContactPoint(createGrafanaContactPoint)
```

| function | func name |
|--------------------------|---------------------------------------------------------------------------------------------------------------------------|
| create contact point | `func (c *GrafanaContactPointClient) CreateGrafanaContactPoint(payload GrafanaContactPoint) (GrafanaContactPoint, error)` |
| delete contact point | `func (c *GrafanaContactPointClient) DeleteGrafanaContactPoint(uid string) error` |
| get all contact points | `func (c *GrafanaContactPointClient) GetAllGrafanaContactPoints() ([]GrafanaContactPoint, error)` |
| get contact point by uid | `func (c *GrafanaContactPointClient) GetGrafanaContactPointByUid(uid string) (GrafanaContactPoint, error)` |
| update contact point | `func (c *GrafanaContactPointClient) UpdateContactPoint(contactPoint GrafanaContactPoint) error` |
74 changes: 74 additions & 0 deletions grafana_contact_points/client_grafana_contact_point.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package grafana_contact_points

import (
"fmt"
"github.com/logzio/logzio_terraform_client/client"
)

const (
grafanaContactPointServiceEndpoint = "%s/v1/grafana/api/v1/provisioning/contact-points"

operationCreateGrafanaContactPoint = "CreateGrafanaContactPoint"
operationGetAllGrafanaContactPoint = "GetAllGrafanaContactPoint"
operationGetByUidGrafanaContactPoint = "GetByUidGrafanaContactPoint"
operationUpdateGrafanaContactPoint = "UpdateGrafanaContactPoint"
operationDeleteGrafanaContactPoint = "DeleteGrafanaContactPoint"

grafanaContactPointResourceName = "grafana contact point"

GrafanaContactPointTypeEmail ContactPointType = "email"
GrafanaContactPointTypeGoogleChat ContactPointType = "googlechat"
GrafanaContactPointTypeOpsgenie ContactPointType = "opsgenie"
GrafanaContactPointTypePagerduty ContactPointType = "pagerduty"
GrafanaContactPointTypeSlack ContactPointType = "slack"
GrafanaContactPointTypeMicrosoftTeams ContactPointType = "teams"
GrafanaContactPointTypeVictorps ContactPointType = "victorops"
GrafanaContactPointTypeWebhook ContactPointType = "webhook"
)

type GrafanaContactPointClient struct {
*client.Client
}

type GrafanaContactPoint struct {
Uid string `json:"uid"`
Name string `json:"name"`
Type string `json:"type"`
Settings map[string]interface{} `json:"settings"`
DisableResolveMessage bool `json:"disableResolveMessage"`
Provenance string `json:"provenance"`
}

func New(apiToken string, baseUrl string) (*GrafanaContactPointClient, error) {
if len(apiToken) == 0 {
return nil, fmt.Errorf("API token not defined")
}
if len(baseUrl) == 0 {
return nil, fmt.Errorf("Base URL not defined")
}

grafanaContactPointClient := &GrafanaContactPointClient{
Client: client.New(apiToken, baseUrl),
}

return grafanaContactPointClient, nil
}

type ContactPointType string

func (cpt ContactPointType) String() string {
return string(cpt)
}

func GetSupportedContactPointTypes() []ContactPointType {
return []ContactPointType{
GrafanaContactPointTypeEmail,
GrafanaContactPointTypeGoogleChat,
GrafanaContactPointTypeOpsgenie,
GrafanaContactPointTypePagerduty,
GrafanaContactPointTypeSlack,
GrafanaContactPointTypeMicrosoftTeams,
GrafanaContactPointTypeVictorps,
GrafanaContactPointTypeWebhook,
}
}
67 changes: 67 additions & 0 deletions grafana_contact_points/client_grafana_contact_point_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package grafana_contact_points

import (
"encoding/json"
"fmt"
logzio_client "github.com/logzio/logzio_terraform_client"
"net/http"
)

const (
createGrafanaContactPointServiceUrl = grafanaContactPointServiceEndpoint
createGrafanaContactPointServiceMethod = http.MethodPost
createGrafanaContactPointMethodAccepted = http.StatusAccepted
createGrafanaContactPointStatusNotFound = http.StatusNotFound
)

func (c *GrafanaContactPointClient) CreateGrafanaContactPoint(payload GrafanaContactPoint) (GrafanaContactPoint, error) {
err := validateContactPoint(payload)
if err != nil {
return GrafanaContactPoint{}, err
}

grafanaContactPointJson, err := json.Marshal(payload)
if err != nil {
return GrafanaContactPoint{}, err
}

res, err := logzio_client.CallLogzioApi(logzio_client.LogzioApiCallDetails{
ApiToken: c.ApiToken,
HttpMethod: createGrafanaContactPointServiceMethod,
Url: fmt.Sprintf(createGrafanaContactPointServiceUrl, c.BaseUrl),
Body: grafanaContactPointJson,
SuccessCodes: []int{createGrafanaContactPointMethodAccepted},
NotFoundCode: createGrafanaContactPointStatusNotFound,
ResourceId: nil,
ApiAction: operationCreateGrafanaContactPoint,
ResourceName: grafanaContactPointResourceName,
})

if err != nil {
return GrafanaContactPoint{}, err
}

var grafanaContactPoint GrafanaContactPoint
err = json.Unmarshal(res, &grafanaContactPoint)
if err != nil {
return GrafanaContactPoint{}, err
}

return grafanaContactPoint, nil
}

func validateContactPoint(payload GrafanaContactPoint) error {
if len(payload.Name) == 0 {
return fmt.Errorf("name must be set!")
}

if len(payload.Type) == 0 {
return fmt.Errorf("type must be set!")
}

if payload.Settings == nil || len(payload.Settings) == 0 {
return fmt.Errorf("settings must be set!")
}

return nil
}
33 changes: 33 additions & 0 deletions grafana_contact_points/client_grafana_contact_point_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package grafana_contact_points

import (
"fmt"
logzio_client "github.com/logzio/logzio_terraform_client"
"net/http"
)

const (
deleteGrafanaContactPointServiceUrl = grafanaContactPointServiceEndpoint + "/%s"
deleteGrafanaContactPointServiceMethod = http.MethodDelete
deleteGrafanaContactPointMethodSuccessNoContent = http.StatusNoContent
deleteGrafanaContactPointMethodSuccessAccepted = http.StatusAccepted
// The status not found part is just for compatability with the CallLogzioApi function,
// The actual API doesn't return an error if you try to delete a contact point that doesn't exist
deleteGrafanaContactPointMethodNotFound = http.StatusNotFound
)

func (c *GrafanaContactPointClient) DeleteGrafanaContactPoint(uid string) error {
_, err := logzio_client.CallLogzioApi(logzio_client.LogzioApiCallDetails{
ApiToken: c.ApiToken,
HttpMethod: deleteGrafanaContactPointServiceMethod,
Url: fmt.Sprintf(deleteGrafanaContactPointServiceUrl, c.BaseUrl, uid),
Body: nil,
SuccessCodes: []int{deleteGrafanaContactPointMethodSuccessNoContent, deleteGrafanaContactPointMethodSuccessAccepted},
NotFoundCode: deleteGrafanaContactPointMethodNotFound,
ResourceId: uid,
ApiAction: operationDeleteGrafanaContactPoint,
ResourceName: grafanaContactPointResourceName,
})

return err
}
63 changes: 63 additions & 0 deletions grafana_contact_points/client_grafana_contact_point_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package grafana_contact_points

import (
"encoding/json"
"fmt"
logzio_client "github.com/logzio/logzio_terraform_client"
"net/http"
)

const (
getAllGrafanaContactPointServiceUrl = grafanaContactPointServiceEndpoint
getAllGrafanaContactPointServiceMethod = http.MethodGet
getAllGrafanaContactPointServiceSuccess = http.StatusOK
getAllGrafanaContactPointServiceNotFound = http.StatusNotFound
)

func (c *GrafanaContactPointClient) GetAllGrafanaContactPoints() ([]GrafanaContactPoint, error) {
res, err := logzio_client.CallLogzioApi(logzio_client.LogzioApiCallDetails{
ApiToken: c.ApiToken,
HttpMethod: getAllGrafanaContactPointServiceMethod,
Url: fmt.Sprintf(getAllGrafanaContactPointServiceUrl, c.BaseUrl),
Body: nil,
SuccessCodes: []int{getAllGrafanaContactPointServiceSuccess},
NotFoundCode: getAllGrafanaContactPointServiceNotFound,
ResourceId: nil,
ApiAction: operationGetAllGrafanaContactPoint,
ResourceName: grafanaContactPointResourceName,
})

if err != nil {
return nil, err
}

var grafanaContactPoints []GrafanaContactPoint
err = json.Unmarshal(res, &grafanaContactPoints)
if err != nil {
return nil, err
}

return grafanaContactPoints, nil
}

// GetGrafanaContactPointByUid - The actual API doesn't have functionality of getting specific Contact Point by uid.
// This function wraps the GetAllGrafanaContactPoints function, and looks for a match of a given uid
func (c *GrafanaContactPointClient) GetGrafanaContactPointByUid(uid string) (GrafanaContactPoint, error) {
if len(uid) == 0 {
return GrafanaContactPoint{}, fmt.Errorf("uid must be set")
}

contactPoints, err := c.GetAllGrafanaContactPoints()
if err != nil {
return GrafanaContactPoint{}, err
}

for _, cp := range contactPoints {
if cp.Uid == uid {
return cp, nil
}
}

return GrafanaContactPoint{}, fmt.Errorf("API call %s failed with missing %s %s, data: %s",
operationGetByUidGrafanaContactPoint, grafanaContactPointResourceName, uid, "")
}
40 changes: 40 additions & 0 deletions grafana_contact_points/client_grafana_contact_point_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package grafana_contact_points

import (
"encoding/json"
"fmt"
logzio_client "github.com/logzio/logzio_terraform_client"
"net/http"
)

const (
updateGrafanaContactPointServiceUrl = grafanaContactPointServiceEndpoint + "/%s"
updateGrafanaContactPointServiceMethod = http.MethodPut
updateGrafanaContactPointMethodAccepted = http.StatusAccepted
updateGrafanaContactPointMethodNotFound = http.StatusNotFound
)

func (c *GrafanaContactPointClient) UpdateContactPoint(contactPoint GrafanaContactPoint) error {
if len(contactPoint.Uid) == 0 {
return fmt.Errorf("uid must be set")
}

updateGrafanaContactPointJson, err := json.Marshal(contactPoint)
if err != nil {
return err
}

_, err = logzio_client.CallLogzioApi(logzio_client.LogzioApiCallDetails{
ApiToken: c.ApiToken,
HttpMethod: updateGrafanaContactPointServiceMethod,
Url: fmt.Sprintf(updateGrafanaContactPointServiceUrl, c.BaseUrl, contactPoint.Uid),
Body: updateGrafanaContactPointJson,
SuccessCodes: []int{updateGrafanaContactPointMethodAccepted},
NotFoundCode: updateGrafanaContactPointMethodNotFound,
ResourceId: contactPoint.Uid,
ApiAction: operationUpdateGrafanaContactPoint,
ResourceName: grafanaContactPointResourceName,
})

return err
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package grafana_contact_points_test

import (
"fmt"
"github.com/logzio/logzio_terraform_client/test_utils"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestIntegrationGrafanaContactPoint_CreateGrafanaContactPoint(t *testing.T) {
underTest, err := setupGrafanaContactPointIntegrationTest()
defer test_utils.TestDoneTimeBuffer()

if assert.NoError(t, err) {
createGrafanaContactPoint := getGrafanaContactPointObject()
createGrafanaContactPoint.Name = fmt.Sprintf("%s_%s", createGrafanaContactPoint.Name, "create")
contactPoint, err := underTest.CreateGrafanaContactPoint(createGrafanaContactPoint)
if assert.NoError(t, err) && assert.NotEmpty(t, contactPoint) {
time.Sleep(4 * time.Second)
defer underTest.DeleteGrafanaContactPoint(contactPoint.Uid)
assert.NotEmpty(t, contactPoint.Uid)
assert.Equal(t, createGrafanaContactPoint.Name, contactPoint.Name)
}
}
}

func TestIntegrationGrafanaContactPoint_CreateGrafanaContactPointNoName(t *testing.T) {
underTest, err := setupGrafanaContactPointIntegrationTest()
defer test_utils.TestDoneTimeBuffer()

if assert.NoError(t, err) {
createGrafanaContactPoint := getGrafanaContactPointObject()
createGrafanaContactPoint.Name = ""
contactPoint, err := underTest.CreateGrafanaContactPoint(createGrafanaContactPoint)
assert.Error(t, err)
assert.Contains(t, err.Error(), "name must be set")
assert.Empty(t, contactPoint)
}
}

func TestIntegrationGrafanaContactPoint_CreateGrafanaContactPointNoType(t *testing.T) {
underTest, err := setupGrafanaContactPointIntegrationTest()
defer test_utils.TestDoneTimeBuffer()

if assert.NoError(t, err) {
createGrafanaContactPoint := getGrafanaContactPointObject()
createGrafanaContactPoint.Type = ""
contactPoint, err := underTest.CreateGrafanaContactPoint(createGrafanaContactPoint)
assert.Error(t, err)
assert.Contains(t, err.Error(), "type must be set")
assert.Empty(t, contactPoint)
}
}

func TestIntegrationGrafanaContactPoint_CreateGrafanaContactPointNoSettings(t *testing.T) {
underTest, err := setupGrafanaContactPointIntegrationTest()
defer test_utils.TestDoneTimeBuffer()

if assert.NoError(t, err) {
createGrafanaContactPoint := getGrafanaContactPointObject()
createGrafanaContactPoint.Settings = nil
contactPoint, err := underTest.CreateGrafanaContactPoint(createGrafanaContactPoint)
assert.Error(t, err)
assert.Contains(t, err.Error(), "settings must be set")
assert.Empty(t, contactPoint)
}
}
Loading
Loading