summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lwirzenius@wikimedia.org>2020-01-02 18:15:29 +0200
committerLars Wirzenius <lwirzenius@wikimedia.org>2020-01-02 18:15:29 +0200
commitb05ab7ddfba7552d4a35e345882e0fcf3d8b3dea (patch)
treed9ea8bedaaeb4a53f3d8afef9f643faccfbb84e9
parented64333c59670e2851c351d4b0d4979737bbccc6 (diff)
downloadickadmin-b05ab7ddfba7552d4a35e345882e0fcf3d8b3dea.tar.gz
Change: fetch list of all projects, print names to stdout
-rw-r--r--ickadmin.go53
1 files changed, 51 insertions, 2 deletions
diff --git a/ickadmin.go b/ickadmin.go
index 8519f27..f3ca9a8 100644
--- a/ickadmin.go
+++ b/ickadmin.go
@@ -18,6 +18,14 @@ type jwt struct {
Access_token string
}
+type projects struct {
+ Projects []project
+}
+
+type project struct {
+ Project string
+}
+
type config struct {
idp string
id string
@@ -44,7 +52,18 @@ func main() {
if err != nil {
os.Exit(1)
}
- fmt.Fprintf(os.Stdout, "%s\n", token)
+// 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 {
@@ -70,8 +89,10 @@ func get_access_token(cfg *config) (string, error) {
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", cfg.idp, strings.NewReader(data.Encode()))
+ 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())))
@@ -93,3 +114,31 @@ func get_access_token(cfg *config) (string, error) {
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
+}