rest.go 2.77 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
4
5
6
7
8
9
10
11
12
package rest

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"git.scc.kit.edu/feudal/feudalClient/config"
Lukas Burgey's avatar
Lukas Burgey committed
13
	"git.scc.kit.edu/feudal/feudalClient/deployments"
Lukas Burgey's avatar
Lukas Burgey committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
)

type (
	// Source implements the DepSource interface for http
	Source struct {
		Config *config.Config
		Ticker *time.Ticker
	}
)

func (src Source) fetchDeps(sink chan<- deps.Dep) (err error) {
	if len(src.Config.EntitlementToServiceIDs) == 0 && len(src.Config.GroupToServiceIDs) == 0 {
		log.Printf("[HTTP] Not fetching because the are no services to fetch")
		return
	}
	if src.Config.Debug.Backend {
		log.Printf("[HTTP] Fetching")
	}

	// construct a request
	uri := "https://" + src.Config.Hostname + "/backend/clientapi/deployments"
	req, err := http.NewRequest("GET", uri, nil)
	if err != nil {
		return
	}

	// add query params for the services
	q := req.URL.Query()
	for groupName := range src.Config.GroupToServiceIDs {
		q.Add("g", groupName)
	}
	for entitlementName := range src.Config.EntitlementToServiceIDs {
		q.Add("e", entitlementName)
	}
	req.URL.RawQuery = q.Encode()

	req.SetBasicAuth(src.Config.Username, src.Config.Password)

	// execute the request
	resp, err := (&http.Client{}).Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		err = fmt.Errorf("[HTTP] Unable to fetch tasks (response: %v)", resp.Status)
		return
	}

	var newDeps []deps.Dep
	body, err := ioutil.ReadAll(resp.Body)

	err = json.Unmarshal(body, &newDeps)
	if err != nil {
		err = fmt.Errorf("[HTTP] Error unmarshaling: %s\n%s", err, body)
		return
	}

	if src.Config.Debug.Backend {
		indented := new(bytes.Buffer)
		if err := json.Indent(indented, body, "", "  "); err != nil {
			log.Printf("[HTTP] Fetched (unable to indent: %v):\n%s", err, body)
		} else {
			log.Printf("[HTTP] Fetched:\n%s", indented)
		}
	}

	for _, newDep := range newDeps {
		sink <- newDep
	}
	return
}

// Init initializes the http source
func (src *Source) Init(conf *config.Config) {
	src.Config = conf
}

// Connect starts fetching using the rest interface.
func (src *Source) Connect() (sink <-chan deps.Dep, err error) {
	sinkPipe := make(chan deps.Dep)
	sink = sinkPipe

	// start ticker
	src.Ticker = time.NewTicker(src.Config.FetchInterval)

	// fetch when the ticker ticks
	go func() {
		var err error
		for {
			select {
			case _, ok := <-src.Ticker.C:
				if !ok {
					log.Printf("[HTTP] Source stopping")
					return
				}
				err = src.fetchDeps(sinkPipe)
				if err != nil {
					log.Printf("[HTTP] error fetching: %s", err)
				}
			}
		}
	}()

	// start one fetch right now
	go func() {
		if src.fetchDeps(sinkPipe) != nil {
			log.Printf("[HTTP] error fetching: %s", err)
		}
	}()

	log.Printf("[HTTP] Source connected")
	return
}

// Close stop the fetching
func (src *Source) Close() {
	src.Ticker.Stop()
}