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
135
136
137
138
139
140
141
142
143
144
|
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/go-ini/ini"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
type jwt struct {
Scope string
Access_token string
}
type projects struct {
Projects []project
}
type project struct {
Project string
}
type config struct {
idp string
id string
secret string
}
func main() {
var config string
flag.StringVar(&config, "config", "", "what config file to read?")
flag.Parse()
if config == "" {
fmt.Fprintf(os.Stderr, "MUST give config file\n")
os.Exit(1)
}
cfg, err := get_creds(config)
if err != nil {
os.Exit(1)
}
token, err := get_access_token(cfg)
if err != nil {
os.Exit(1)
}
// fmt.Fprintf(os.Stdout, "%s\n", token)
projects, err := get_projects(cfg, token)
if err != nil {
os.Exit(1)
}
if projects == nil {
os.Exit(1)
}
for _, p := range projects.Projects {
fmt.Printf("project: %v\n", p.Project)
}
}
func NewConfig(idp string, id string, secret string) *config {
new := config{idp: idp, id: id, secret: secret}
return &new
}
func get_creds(filename string) (*config, error) {
cfg, err := ini.Load(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "reading config %s: %v\n", filename, err)
return nil, err
}
idp := cfg.Section("").Key("idp").String()
id := cfg.Section("").Key("id").String()
secret := cfg.Section("").Key("secret").String()
return NewConfig(idp, id, secret), nil
}
func get_access_token(cfg *config) (string, error) {
data := url.Values{}
data.Add("grant_type", "client_credentials")
data.Add("scope", "uapi_projects_get")
url := fmt.Sprintf("%s/token", cfg.idp)
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(data.Encode()))
req.SetBasicAuth(cfg.id, cfg.secret)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "POST %s: %v\n", cfg.idp, err)
return "", err
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "reading HTTP response: %v\n", err)
return "", err
}
s := string(bodyText)
res := jwt{}
json.Unmarshal([]byte(s), &res)
return res.Access_token, nil
}
func get_projects(cfg *config, token string) (*projects, error) {
url := fmt.Sprintf("%s/projects", cfg.idp)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", fmt.Sprintf("bearer %s", token))
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "GET %s: %v\n", url, err)
return nil, err
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "reading HTTP response: %v\n", err)
return nil, err
}
// s := string(bodyText)
// fmt.Printf("%v", s)
res := projects{}
json.Unmarshal(bodyText, &res)
return &res, nil
}
|