-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Grafana notification policy (#112) * client initial * set * get * reset * refactor * setup integration tests * setup tests * get tests * reset tests * readme * main readme * match type * add function * add string method to match type * Grafana contact point (#114) * client, create * start of get all * get * update * delete * integration test create * rename * create tests * get integration test * get tests * update integration test, rename get by uid * update tests * delete integration tests, fix delete * delete tests, add note * readme, add note to get by uid * contact point types * make contact points public * improve get by uid error * add get by name * get by name
- Loading branch information
Showing
31 changed files
with
1,395 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 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)` | | ||
| get contact points by name | `func (c *GrafanaContactPointClient) GetGrafanaContactPointsByName(name string) ([]GrafanaContactPoint, error) ` | | ||
| update contact point | `func (c *GrafanaContactPointClient) UpdateContactPoint(contactPoint GrafanaContactPoint) error` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
grafana_contact_points/client_grafana_contact_point_create.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
grafana_contact_points/client_grafana_contact_point_delete.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
87 changes: 87 additions & 0 deletions
87
grafana_contact_points/client_grafana_contact_point_get.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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, "") | ||
} | ||
|
||
func (c *GrafanaContactPointClient) GetGrafanaContactPointsByName(name string) ([]GrafanaContactPoint, error) { | ||
var contactPoints []GrafanaContactPoint | ||
if len(name) == 0 { | ||
return nil, fmt.Errorf("uid must be set") | ||
} | ||
|
||
getContactPoints, err := c.GetAllGrafanaContactPoints() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, cp := range getContactPoints { | ||
if cp.Name == name { | ||
contactPoints = append(contactPoints, cp) | ||
} | ||
} | ||
|
||
if len(contactPoints) == 0 { | ||
return nil, fmt.Errorf("could not find grafana contact points with name %s", name) | ||
} | ||
|
||
return contactPoints, nil | ||
} |
40 changes: 40 additions & 0 deletions
40
grafana_contact_points/client_grafana_contact_point_update.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.