Skip to content

Commit 935ab44

Browse files
committed
move types to a dedicated package
1 parent c0683a2 commit 935ab44

9 files changed

+88
-48
lines changed

internal/outputs/file.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"path/filepath"
77

88
yaml "gopkg.in/yaml.v1"
9+
10+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
911
)
1012

1113
// OutputFile stores values needed by the File output
@@ -20,10 +22,10 @@ func setupOutputFile(path string) (*OutputFile, error) {
2022
}, err
2123
}
2224

23-
// WriteOutput writes data to a file
24-
func (o *OutputFile) WriteOutput(data interface{}) (err error) {
25+
// WriteOutput writes static configs to a file
26+
func (o *OutputFile) WriteOutput(staticConfigs []types.StaticConfig) (err error) {
2527
os.MkdirAll(filepath.Dir(o.path), 0755)
26-
c, err := yaml.Marshal(&data)
28+
c, err := yaml.Marshal(&staticConfigs)
2729
if err != nil {
2830
return
2931
}

internal/outputs/file_test.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/assert"
10+
11+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
1012
)
1113

1214
func TestFileSetupSuccess(t *testing.T) {
@@ -31,9 +33,17 @@ func TestFileWriteOutputSuccess(t *testing.T) {
3133
defer os.RemoveAll(dir)
3234
tmpfn := filepath.Join(dir, "output.yaml")
3335

34-
data := map[string]string{
35-
"foo": "bar",
36+
data := []types.StaticConfig{
37+
{
38+
Targets: []string{
39+
"127.0.0.1:9103",
40+
},
41+
Labels: map[string]string{
42+
"foo": "bar",
43+
},
44+
},
3645
}
46+
3747
o := &OutputFile{
3848
path: tmpfn,
3949
}
@@ -45,5 +55,5 @@ func TestFileWriteOutputSuccess(t *testing.T) {
4555
if err != nil {
4656
assert.FailNow(t, "failed to read output file content", err.Error())
4757
}
48-
assert.Equal(t, "foo: bar\n", string(fileContent))
58+
assert.Equal(t, "- targets:\n - 127.0.0.1:9103\n labels:\n foo: bar\n", string(fileContent))
4959
}

internal/outputs/k8s-configmap.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"k8s.io/client-go/kubernetes"
1010
"k8s.io/client-go/rest"
1111
"k8s.io/client-go/tools/clientcmd"
12+
13+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
1214
)
1315

1416
// OutputK8SConfigMap stores data needed to fill a Kubernetes ConfigMap
@@ -49,8 +51,8 @@ func setupOutputK8SConfigMap(namespace, configMapName string) (*OutputK8SConfigM
4951
return o, nil
5052
}
5153

52-
// WriteOutput writes data to a Kubernetes ConfigMap
53-
func (o *OutputK8SConfigMap) WriteOutput(data interface{}) (err error) {
54+
// WriteOutput writes static configs to a Kubernetes ConfigMap
55+
func (o *OutputK8SConfigMap) WriteOutput(staticConfigs []types.StaticConfig) (err error) {
5456
var configMap *v1.ConfigMap
5557
_, err = o.k8sClient.CoreV1().ConfigMaps(o.namespace).Get(o.configMapName, metav1.GetOptions{})
5658
if err != nil {
@@ -72,7 +74,7 @@ func (o *OutputK8SConfigMap) WriteOutput(data interface{}) (err error) {
7274
}
7375
}
7476

75-
c, err := yaml.Marshal(&data)
77+
c, err := yaml.Marshal(&staticConfigs)
7678
if err != nil {
7779
return
7880
}

internal/outputs/k8s-configmap_test.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ import (
66
"github.com/stretchr/testify/assert"
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
testclient "k8s.io/client-go/kubernetes/fake"
9+
10+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
911
)
1012

1113
func TestK8SConfigMapWriteOutputSuccess(t *testing.T) {
12-
data := map[string]string{
13-
"foo": "bar",
14+
data := []types.StaticConfig{
15+
{
16+
Targets: []string{
17+
"127.0.0.1:9103",
18+
},
19+
Labels: map[string]string{
20+
"foo": "bar",
21+
},
22+
},
1423
}
24+
1525
o := &OutputK8SConfigMap{
1626
namespace: "foo",
1727
configMapName: "bar",
@@ -28,7 +38,7 @@ func TestK8SConfigMapWriteOutputSuccess(t *testing.T) {
2838
assert.Equal(
2939
t,
3040
map[string]string{
31-
"targets.yml": "foo: bar\n",
41+
"targets.yml": "- targets:\n - 127.0.0.1:9103\n labels:\n foo: bar\n",
3242
},
3343
cm.Data,
3444
)

internal/outputs/outputs.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package outputs
22

33
import (
44
"fmt"
5+
6+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
57
)
68

79
// Options stores options that might be used by the different output types
@@ -16,7 +18,7 @@ type Options struct {
1618

1719
// Output is an abstraction to the different output types
1820
type Output interface {
19-
WriteOutput(data interface{}) (err error)
21+
WriteOutput(staticConfigs []types.StaticConfig) (err error)
2022
}
2123

2224
// Setup returns an output type

internal/outputs/stdout.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import (
44
"fmt"
55

66
yaml "gopkg.in/yaml.v1"
7+
8+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
79
)
810

911
// OutputStdout stores values needed to print output to stdout
1012
type OutputStdout struct{}
1113

12-
// WriteOutput writes data to stdout
13-
func (o *OutputStdout) WriteOutput(data interface{}) (err error) {
14-
c, err := yaml.Marshal(&data)
14+
// WriteOutput writes static configs to stdout
15+
func (o *OutputStdout) WriteOutput(staticConfigs []types.StaticConfig) (err error) {
16+
c, err := yaml.Marshal(&staticConfigs)
1517
if err != nil {
1618
return
1719
}

internal/outputs/stdout_test.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/assert"
10+
11+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
1012
)
1113

1214
func TestStdoutWriteOutputSuccess(t *testing.T) {
13-
data := map[string]string{
14-
"foo": "bar",
15+
data := []types.StaticConfig{
16+
{
17+
Targets: []string{
18+
"127.0.0.1:9103",
19+
},
20+
Labels: map[string]string{
21+
"foo": "bar",
22+
},
23+
},
1524
}
25+
1626
o := &OutputStdout{}
1727

1828
old := os.Stdout
@@ -33,5 +43,5 @@ func TestStdoutWriteOutputSuccess(t *testing.T) {
3343
out := <-outC
3444

3545
assert.Nil(t, err)
36-
assert.Equal(t, "foo: bar\n", out)
46+
assert.Equal(t, "- targets:\n - 127.0.0.1:9103\n labels:\n foo: bar\n", out)
3747
}

internal/types/types.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package types
2+
3+
// Exporter contains exporter targets and labels
4+
type Exporter struct {
5+
URL string
6+
Labels map[string]string
7+
}
8+
9+
// Node contains Puppet node informations
10+
type Node struct {
11+
Certname string `json:"certname"`
12+
Exporters map[string]interface{} `json:"value"`
13+
}
14+
15+
// StaticConfig contains Prometheus static targets
16+
type StaticConfig struct {
17+
Targets []string `yaml:"targets"`
18+
Labels map[string]string `yaml:"labels"`
19+
}

main.go

+12-29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
log "github.com/sirupsen/logrus"
1818

1919
"github.com/camptocamp/prometheus-puppetdb/internal/outputs"
20+
"github.com/camptocamp/prometheus-puppetdb/internal/types"
2021
)
2122

2223
var version = "undefined"
@@ -39,24 +40,6 @@ type Config struct {
3940
Manpage bool `short:"m" long:"manpage" description:"Output manpage."`
4041
}
4142

42-
// Exporter contains exporter targets and labels
43-
type Exporter struct {
44-
URL string
45-
Labels map[string]string
46-
}
47-
48-
// Node contains Puppet node informations
49-
type Node struct {
50-
Certname string `json:"certname"`
51-
Exporters map[string]interface{} `json:"value"`
52-
}
53-
54-
// StaticConfig contains Prometheus static targets
55-
type StaticConfig struct {
56-
Targets []string `yaml:"targets"`
57-
Labels map[string]string `yaml:"labels"`
58-
}
59-
6043
func loadConfig(version string) (c Config, err error) {
6144
parser := flags.NewParser(&c, flags.Default)
6245
_, err = parser.Parse()
@@ -79,7 +62,7 @@ func loadConfig(version string) (c Config, err error) {
7962
return
8063
}
8164

82-
func getNodes(client *http.Client, puppetdb string, query string) (nodes []Node, err error) {
65+
func getNodes(client *http.Client, puppetdb string, query string) (nodes []types.Node, err error) {
8366
form := strings.NewReader(fmt.Sprintf("{\"query\":\"%s\"}", query))
8467
puppetdbURL := fmt.Sprintf("%s/pdb/query/v4", puppetdb)
8568
req, err := http.NewRequest("POST", puppetdbURL, form)
@@ -102,8 +85,8 @@ func getNodes(client *http.Client, puppetdb string, query string) (nodes []Node,
10285
return
10386
}
10487

105-
func getTargets() (staticConfigs []StaticConfig, err error) {
106-
staticConfigs = []StaticConfig{}
88+
func getTargets() (staticConfigs []types.StaticConfig, err error) {
89+
staticConfigs = []types.StaticConfig{}
10790

10891
nodes, err := getNodes(client, cfg.PuppetDBURL, cfg.Query)
10992
if err != nil {
@@ -140,7 +123,7 @@ func getTargets() (staticConfigs []StaticConfig, err error) {
140123
for k, v := range vt.Labels {
141124
labels[k] = v
142125
}
143-
staticConfig := StaticConfig{
126+
staticConfig := types.StaticConfig{
144127
Targets: []string{url.Host},
145128
Labels: labels,
146129
}
@@ -159,31 +142,31 @@ func getTargets() (staticConfigs []StaticConfig, err error) {
159142
}
160143

161144
// Allow backward compatibility (to remove)
162-
func extractTargets(targets interface{}) (t []Exporter, err error) {
145+
func extractTargets(targets interface{}) (t []types.Exporter, err error) {
163146
switch v := targets.(type) {
164147
case string:
165148
log.Warningf("Deprecated: target should be a struct Exporter, not a String: %v", v)
166-
e := Exporter{
149+
e := types.Exporter{
167150
URL: v,
168151
Labels: make(map[string]string),
169152
}
170-
t = []Exporter{e}
153+
t = []types.Exporter{e}
171154
case []interface{}:
172155
switch v[0].(type) {
173156
case string:
174157
log.Warningf("Deprecated: target should be a struct Exporter, not an Array of Strings: %v", v)
175-
t = make([]Exporter, len(v))
158+
t = make([]types.Exporter, len(v))
176159
for i := range v {
177-
t[i] = Exporter{
160+
t[i] = types.Exporter{
178161
URL: v[i].(string),
179162
Labels: make(map[string]string),
180163
}
181164
}
182165
case map[string]interface{}:
183-
t = make([]Exporter, len(v))
166+
t = make([]types.Exporter, len(v))
184167
for i := range v {
185168
a := v[i].(map[string]interface{})
186-
t[i] = Exporter{
169+
t[i] = types.Exporter{
187170
URL: a["url"].(string),
188171
Labels: make(map[string]string),
189172
}

0 commit comments

Comments
 (0)