-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgithubcontracts.go
134 lines (106 loc) · 3.85 KB
/
githubcontracts.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
131
132
133
134
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
log "github.com/sirupsen/logrus"
)
type GitHubContentItem struct {
Name string `json:"name"`
Type string `json:"type"`
DownloadUrl string `json:"download_url"`
Url string `json:"url"`
}
type GitHubDashboardTemplate struct {
Name string
Contents string
}
const (
GitHubGrafanaTemplateRootUrl = "https://api.github.com/repos/asheniam/azure-grafana-dashboard-templates/contents/"
)
func getGitHubGrafanaTemplates(resourceType string, resourceKind string, subResourceType string) []GitHubDashboardTemplate {
var httpClient *http.Client = &http.Client{}
// Generate the root URL for the given ARM resource type. This is the folder that contains the Grafana dashboard templates
// for this ARM resource type.
encodedResourceType := resourceType
if len(subResourceType) > 0 {
encodedResourceType = resourceType + "/" + subResourceType
}
if len(resourceKind) > 0 {
resourceType = encodedResourceType + "/kind/" + resourceKind
}
encodedResourceType = strings.Replace(encodedResourceType, ".", "-", -1)
encodedResourceType = strings.Replace(encodedResourceType, "/", "-", -1)
githubUrl := fmt.Sprintf("%s%s?ref=master", GitHubGrafanaTemplateRootUrl, encodedResourceType)
// Get all the Grafana dashboard template subfolders
githubContentItems := httpGetGitHubContentItems(httpClient, githubUrl)
gitHubDashboardTemplates := make([]GitHubDashboardTemplate, 0)
// For each sub-folder, find the template.json
for _, githubContentItem := range githubContentItems {
if strings.EqualFold(githubContentItem.Type, "dir") &&
len(githubContentItem.Url) > 0 {
githubContentDashboardFolderItems := httpGetGitHubContentItems(httpClient, githubContentItem.Url)
for _, githubContentDashboardFolderItem := range githubContentDashboardFolderItems {
if strings.EqualFold(githubContentDashboardFolderItem.Name, "template.json") {
// Read the template.json
templateJson := httpGetGitHubDashboardTemplateJson(httpClient, githubContentDashboardFolderItem.DownloadUrl)
dashboardTemplate := GitHubDashboardTemplate{
Name: githubContentItem.Name,
Contents: templateJson,
}
gitHubDashboardTemplates = append(gitHubDashboardTemplates, dashboardTemplate)
}
}
}
}
return gitHubDashboardTemplates
}
func httpGetGitHubContentItems(httpClient *http.Client, targetUrl string) []GitHubContentItem {
request, err := http.NewRequest("GET", targetUrl, nil)
if err != nil {
log.Fatalf("Error creating HTTP request: %v", err)
}
log.Debugf("Executing GET %s\n", targetUrl)
response, err := httpClient.Do(request)
if err != nil {
log.Fatalf("Error sending HTTP request: %v", err)
}
log.Debugf("Status code: %d\n", response.StatusCode)
defer response.Body.Close()
githubContentResponse := make([]GitHubContentItem, 0)
if response.StatusCode == 200 {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("Error reading body of response: %v", err)
}
err = json.Unmarshal(body, &githubContentResponse)
if err != nil {
log.Fatalf("Error unmarshalling ARM reource response body: %v", err)
}
}
return githubContentResponse
}
func httpGetGitHubDashboardTemplateJson(httpClient *http.Client, targetUrl string) string {
request, err := http.NewRequest("GET", targetUrl, nil)
if err != nil {
log.Fatalf("Error creating HTTP request: %v", err)
}
log.Debugf("Executing GET %s\n", targetUrl)
response, err := httpClient.Do(request)
if err != nil {
log.Fatalf("Error sending HTTP request: %v", err)
}
log.Debugf("Status code: %d\n", response.StatusCode)
defer response.Body.Close()
templateJson := ""
if response.StatusCode == 200 {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("Error reading body of response: %v", err)
}
templateJson = string(body)
}
return templateJson
}