forked from Scalingo/scalingo-addon-api-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
70 lines (67 loc) · 1.74 KB
/
update.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/codegangsta/cli"
)
var (
updateCommand = cli.Command{
Name: "update",
Usage: "Request addon plan change",
Flags: []cli.Flag{
cli.StringFlag{Name: "plan", Usage: "Choose the plan to change to"},
},
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("Usage:", os.Args[0], "<addon id> --plan <plan>")
os.Exit(-127)
}
id := c.Args()[0]
plan := c.String("plan")
if plan == "" {
log.Fatalln("no plan specified")
} else if !manifest.PlanExist(plan) {
fmt.Println("Plan", plan, "is not defined in manifest")
os.Exit(1)
}
options := manifest.PlanOptions(plan)
res, err := doRequest("PUT", manifest.Test.BaseURL+"/"+id, map[string]interface{}{
"plan": plan,
"options": options,
})
if err != nil {
log.Fatalln("Fail to change plan of addon:", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalln("Addon returned bad status:", res.Status, "expected 200")
}
var updRes UpdateResponse
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln("Failed to read body from addon:", err)
}
err = json.Unmarshal(body, &updRes)
if err != nil {
log.Fatalln("Addon returned bad JSON:", err, "-", string(body))
}
if updRes.Message == "" {
log.Println("Notice: no message received")
}
if updRes.Config == nil || len(updRes.Config) == 0 {
log.Println("Notice: no configuration received")
}
if err = manifest.CheckAddonConfig(updRes.Config); err != nil {
log.Fatalln(err)
}
err = saveAddonRef(id, plan)
if err != nil {
log.Println("fail to save addon reference:", err)
}
fmt.Println("→ OK")
},
}
)