-
Notifications
You must be signed in to change notification settings - Fork 25
/
updater.go
130 lines (106 loc) · 2.77 KB
/
updater.go
1
2
3
4
5
6
7
8
9
10
11
12
13
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
package ec2instancesinfo
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
const (
defaultAPIHost = "api.leanercloud.com"
defaultAPIPath = "/instance-type-info/"
defaultRefreshInterval = 7
)
func getDataURL(apiHost, apiKey string) (*string, error) {
url := fmt.Sprintf("https://%s%s", apiHost, defaultAPIPath)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %s", err)
}
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received unexpected HTTP status: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %s", err)
}
ret := string(body)
return &ret, nil
}
func UpdateData(apiHost, apiKey *string) error {
if apiHost == nil {
s := defaultAPIHost
apiHost = &s
}
log.Printf("Dynamic data size before: %d, downloading new instance type data.", len(dataBody))
url, err := getDataURL(*apiHost, *apiKey)
if err != nil {
return fmt.Errorf("error getting download URL file: %s", err)
}
client := &http.Client{}
req, err := http.NewRequest("GET", *url, nil)
if err != nil {
return fmt.Errorf("error creating request: %s", err)
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body: %s", err)
}
if len(dataBody) > 0 {
backupDataBody = dataBody
} else {
backupDataBody = staticDataBody
}
dataBody = body
log.Println("Data size after:", len(dataBody))
out, err := os.Create("instances_dump.json")
if err != nil {
panic("Couldn't create dump file")
}
defer out.Close()
out.Write(body)
return nil
}
func Updater(refreshDays int, apiHost, apiKey *string) error {
if apiKey == nil {
log.Println("API key is missing")
return fmt.Errorf("API key is missing")
}
if apiHost == nil {
host := defaultAPIHost
apiHost = &host
}
if refreshDays <= 0 {
refreshDays = defaultRefreshInterval
}
refreshInterval := time.Duration(refreshDays) * 24 * time.Hour
if err := UpdateData(apiHost, apiKey); err != nil {
log.Printf("Failed to download updated data: %s", err.Error())
return fmt.Errorf("error downloading new data: %s", err)
}
// refresh the file every refreshInterval
ticker := time.NewTicker(refreshInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := UpdateData(apiHost, apiKey)
if err != nil {
log.Println("Error refreshing data:", err)
}
}
}
}