Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
Finish refactoring mapper package
Browse files Browse the repository at this point in the history
Fix bugs, add docstrings and let each mapper give us full url since it doesn't handle any http requests now (it just maps text to objects now)
  • Loading branch information
prymitive committed Jan 21, 2018
1 parent fe98ef1 commit fcf9026
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
4 changes: 4 additions & 0 deletions internal/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ var (
silenceMappers = []SilenceMapper{}
)

// Mapper converts Alertmanager response body and maps to unsee data structures
type Mapper interface {
IsSupported(version string) bool
AbsoluteURL(baseURI string) (string, error)
}

// AlertMapper handles mapping of Alertmanager alert information to unsee AlertGroup models
type AlertMapper interface {
Mapper
Decode(io.ReadCloser) ([]models.AlertGroup, error)
}

// SilenceMapper handles mapping of Alertmanager silence information to unsee Silence models
type SilenceMapper interface {
Mapper
Decode(io.ReadCloser) ([]models.Silence, error)
Expand Down
9 changes: 8 additions & 1 deletion internal/mapper/v04/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/blang/semver"
"github.com/cloudflare/unsee/internal/mapper"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/transport"
)

type alert struct {
Expand Down Expand Up @@ -53,19 +54,25 @@ type AlertMapper struct {
mapper.AlertMapper
}

// AbsoluteURL for alerts API endpoint this mapper supports
func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return transport.JoinURL(baseURI, "api/v1/alerts/groups")
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.4.0 <0.5.0")
return versionRange(semver.MustParse(version))
}

// Decode Alertmanager API response body and return unsee model instances
func (m AlertMapper) Decode(source io.ReadCloser) ([]models.AlertGroup, error) {
groups := []models.AlertGroup{}
receivers := map[string]alertsGroupReceiver{}
resp := alertsGroupsAPISchema{}

defer source.Close()
err := json.NewDecoder(source).Decode(resp)
err := json.NewDecoder(source).Decode(&resp)
if err != nil {
return groups, err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/mapper/v04/silences.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/blang/semver"
"github.com/cloudflare/unsee/internal/mapper"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/transport"
)

// Alertmanager 0.4 silence format
Expand Down Expand Up @@ -46,18 +47,24 @@ type SilenceMapper struct {
mapper.SilenceMapper
}

// AbsoluteURL for silences API endpoint this mapper supports
func (m SilenceMapper) AbsoluteURL(baseURI string) (string, error) {
return transport.JoinURL(baseURI, "api/v1/silences")
}

// IsSupported returns true if given version string is supported
func (m SilenceMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.4.0 <0.5.0")
return versionRange(semver.MustParse(version))
}

// Decode Alertmanager API response body and return unsee model instances
func (m SilenceMapper) Decode(source io.ReadCloser) ([]models.Silence, error) {
silences := []models.Silence{}
resp := silenceAPISchema{}

defer source.Close()
err := json.NewDecoder(source).Decode(resp)
err := json.NewDecoder(source).Decode(&resp)
if err != nil {
return silences, err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/mapper/v05/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/blang/semver"
"github.com/cloudflare/unsee/internal/mapper"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/transport"
)

type alert struct {
Expand Down Expand Up @@ -52,19 +53,25 @@ type AlertMapper struct {
mapper.AlertMapper
}

// AbsoluteURL for alerts API endpoint this mapper supports
func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return transport.JoinURL(baseURI, "api/v1/alerts/groups")
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.5.0 <=0.6.0")
return versionRange(semver.MustParse(version))
}

// Decode Alertmanager API response body and return unsee model instances
func (m AlertMapper) Decode(source io.ReadCloser) ([]models.AlertGroup, error) {
groups := []models.AlertGroup{}
receivers := map[string]alertsGroupReceiver{}
resp := alertsGroupsAPISchema{}

defer source.Close()
err := json.NewDecoder(source).Decode(resp)
err := json.NewDecoder(source).Decode(&resp)
if err != nil {
return groups, err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/mapper/v05/silences.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/blang/semver"
"github.com/cloudflare/unsee/internal/mapper"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/transport"
)

type silence struct {
Expand Down Expand Up @@ -40,18 +41,24 @@ type SilenceMapper struct {
mapper.SilenceMapper
}

// AbsoluteURL for silences API endpoint this mapper supports
func (m SilenceMapper) AbsoluteURL(baseURI string) (string, error) {
return transport.JoinURL(baseURI, "api/v1/silences")
}

// IsSupported returns true if given version string is supported
func (m SilenceMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.5.0")
return versionRange(semver.MustParse(version))
}

// Decode Alertmanager API response body and return unsee model instances
func (m SilenceMapper) Decode(source io.ReadCloser) ([]models.Silence, error) {
silences := []models.Silence{}
resp := silenceAPISchema{}

defer source.Close()
err := json.NewDecoder(source).Decode(resp)
err := json.NewDecoder(source).Decode(&resp)
if err != nil {
return silences, err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/mapper/v061/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/blang/semver"
"github.com/cloudflare/unsee/internal/mapper"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/transport"
)

type alert struct {
Expand Down Expand Up @@ -54,19 +55,25 @@ type AlertMapper struct {
mapper.AlertMapper
}

// AbsoluteURL for alerts API endpoint this mapper supports
func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return transport.JoinURL(baseURI, "api/v1/alerts/groups")
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange("=0.6.1")
return versionRange(semver.MustParse(version))
}

// Decode Alertmanager API response body and return unsee model instances
func (m AlertMapper) Decode(source io.ReadCloser) ([]models.AlertGroup, error) {
groups := []models.AlertGroup{}
receivers := map[string]alertsGroupReceiver{}
resp := alertsGroupsAPISchema{}

defer source.Close()
err := json.NewDecoder(source).Decode(resp)
err := json.NewDecoder(source).Decode(&resp)
if err != nil {
return groups, err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/mapper/v062/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/blang/semver"
"github.com/cloudflare/unsee/internal/mapper"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/transport"
)

type alertStatus struct {
Expand Down Expand Up @@ -58,19 +59,25 @@ type AlertMapper struct {
mapper.AlertMapper
}

// AbsoluteURL for alerts API endpoint this mapper supports
func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return transport.JoinURL(baseURI, "api/v1/alerts/groups")
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.6.2")
return versionRange(semver.MustParse(version))
}

// Decode Alertmanager API response body and return unsee model instances
func (m AlertMapper) Decode(source io.ReadCloser) ([]models.AlertGroup, error) {
groups := []models.AlertGroup{}
receivers := map[string]alertsGroupReceiver{}
resp := alertsGroupsAPISchema{}

defer source.Close()
err := json.NewDecoder(source).Decode(resp)
err := json.NewDecoder(source).Decode(&resp)
if err != nil {
return groups, err
}
Expand Down

0 comments on commit fcf9026

Please sign in to comment.