Commit a4c73332 authored by Pablo Carranza's avatar Pablo Carranza 🦄

Build up configuration to be yaml, add PD wrapper

2 things here:
1. Don't use json for configuration, please
2. Wrap the objects you are accessing to so they don't leak
parent 8baead57
Pipeline #13315973 passed with stage
in 53 seconds
......@@ -3,10 +3,28 @@ package oncall
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
// PagerDutyConfig keeps the configuration for the pagerduty service
type PagerDutyConfig struct {
Token string `yaml:"token"`
Schedules []string `yaml:"schedules"`
PrimarySchedule string `yaml:"primary"`
SecondarySchedule string `yaml:"secondary"`
}
// RealConfig the real deal
type RealConfig struct {
PagerDuty PagerDutyConfig `yaml:"pagerduty"`
GitLabToken string `yaml:"gitlab-token"`
}
// Config : map the json fields on the config file
type Config struct {
GitLabToken string `json:"gitlab_token"`
......@@ -18,6 +36,20 @@ type Config struct {
OncallEndTime string `json:"oncall_end_time"`
}
// Parses the yaml configuration from a reader
func ParseConfig(r io.Reader) (RealConfig, error) {
configBytes, err := ioutil.ReadAll(r)
config := RealConfig{}
if err != nil {
return config, fmt.Errorf("could not read configuration: %s", err)
}
err = yaml.Unmarshal(configBytes, &config)
if err != nil {
return config, fmt.Errorf("could not parse configuration yaml: %s", err)
}
return config, nil
}
// readConfig will attempt to read the different configuration
// parameters from a JSON formatted file.
func ReadConfig(f string) (*Config, error) {
......
package oncall
import (
"fmt"
"os"
"reflect"
"strings"
"testing"
)
......@@ -17,3 +21,33 @@ func TestReadConfig(t *testing.T) {
t.Fatalf("wrong configuration error, got %s; expected %s", got, want)
}
}
func TestParseConfigWrong(t *testing.T) {
_, err := ParseConfig(strings.NewReader("invalid yaml"))
expected := "could not parse configuration yaml: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into oncall.RealConfig"
if fmt.Sprintf("%s", err) != expected {
t.Fatalf("unexpected error, got %s", err)
}
}
func TestParseConfigCorrectly(t *testing.T) {
f, err := os.Open("../test-fixtures/settings.yaml")
defer f.Close()
if err != nil {
t.Fatalf("failed to open the configuration file: %s", err)
}
c, err := ParseConfig(f)
if err != nil {
t.Fatalf("failed to parse the configuration: %s", err)
}
assertEquals(t, "gitlab token", "ZZXX", c.GitLabToken)
assertEquals(t, "pagerduty token", "XXYY", c.PagerDuty.Token)
assertEquals(t, "schedules", []string{"PrimaryID", "SecondaryID"}, c.PagerDuty.Schedules)
}
func assertEquals(t *testing.T, name string, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("%s is not as expected: %+v; got %+v", name, expected, actual)
}
}
......@@ -7,11 +7,24 @@ import (
pagerduty "github.com/PagerDuty/go-pagerduty"
)
// PagerDuty helper
type PagerDuty struct {
token string
}
// NewPagerDuty returns a new PagerDuty object
func NewPagerDuty(token string) PagerDuty {
return PagerDuty{
token: token,
}
}
// ListOncallPeople returns a list of the people that is going to be oncall for the next X days
func ListOncallPeople(c *Config, days int) ([]pagerduty.OnCall, error) {
client := pagerduty.NewClient(c.PDToken)
func (pd PagerDuty) ListOncallPeople(ScheduleID string, days int) ([]pagerduty.OnCall, error) {
client := pagerduty.NewClient(pd.token)
resp, err := client.ListOnCalls(pagerduty.ListOnCallOptions{
ScheduleIDs: []string{c.OncallPriID, c.OncallSecID},
ScheduleIDs: []string{ScheduleID},
Since: fmt.Sprintf("%sT04:00:00Z", time.Now().Add(-24*time.Duration(days)*time.Hour).Format("2006-01-02")),
Until: fmt.Sprintf("%sT16:00:00Z", time.Now().Add(24*time.Duration(days)*time.Hour).Format("2006-01-02")),
})
if err != nil {
......
......@@ -48,13 +48,8 @@ func TestOncallDates(t *testing.T) {
}
func TestPagerDutyStuff(t *testing.T) {
c := &Config{
PDToken: os.Getenv("PD_API_KEY"),
OncallPriID: os.Getenv("PD_PRIMARY_SCHEDULE"),
OncallSecID: os.Getenv("PD_SECONDARY_SCHEDULE"),
}
people, err := ListOncallPeople(c, 7)
pd := NewPagerDuty(os.Getenv("PD_API_KEY"))
people, err := pd.ListOncallPeople(os.Getenv("PD_PRIMARY_SCHEDULE"), 7)
if err != nil {
t.Fatalf("could not list the oncall people: %s", err)
}
......
---
pagerduty:
token: XXYY
schedules:
- PrimaryID
- SecondaryID
gitlab-token: ZZXX
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment