-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-instance.go
178 lines (157 loc) · 4.45 KB
/
multi-instance.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Plugin "multi-instance" to obtain status from multiple Cloud Foundry
// instances, e.g., from several Bluemix Public regions.
//
// (C) 2017 IBM - Licensed under Apache License, Version 2.0
// Author: data-henrik (Henrik Loeser)
//
//
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"gopkg.in/yaml.v2"
"code.cloudfoundry.org/cli/plugin"
cfclient "github.com/cloudfoundry-community/go-cfclient"
"github.com/cloudfoundry/cli/cf/configuration/confighelpers"
)
type instanceConfig struct {
Name string `yaml:"Name"`
Identifier int `yaml:"Identifier"`
APIs []string `yaml:"APIs"`
}
// MultiInstance holds info for this plugin
type MultiInstance struct{
iConfig instanceConfig
clients [5]*cfclient.Client
}
// Run an invocation of this plugin
func (c *MultiInstance) Run(cliConnection plugin.CliConnection, args []string) {
if args[0] == "multi-instance" || args[0] == "mi" {
cfConfigFile, err := confighelpers.DefaultFilePath()
if err != nil {
panic(err)
}
// We assume a config "miconfig.yml" in the default cf cli config path
filename := filepath.Join(filepath.Dir(cfConfigFile),"miconfig.yml")
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Error: Expected miconfig.yml in default cf configuration path!")
return
}
err = yaml.Unmarshal(yamlFile, &c.iConfig)
if err != nil {
panic(err)
}
li, err := cliConnection.IsLoggedIn()
if err != nil {
fmt.Println("Error: ", err)
} else {
if li != true {
fmt.Println("You are not logged in to a Cloud Foundry instance!")
fmt.Println("Please log in and retry.")
return
}
}
endpoint, err := cliConnection.ApiEndpoint()
if err != nil {
panic(err)
}
// Check that the configured and current endpoint match
if c.iConfig.APIs[c.iConfig.Identifier-1]!=endpoint{
fmt.Println("Expected endpoint does not match current endpoint!")
fmt.Println("Configured: ",c.iConfig.APIs[c.iConfig.Identifier-1])
fmt.Println("Current: ", endpoint)
return
}
un, err := cliConnection.Username()
if err != nil {
panic(err)
}
//fmt.Println("You are ", un)
// Get and prepare the access token
token, err := cliConnection.AccessToken()
if err != nil {
panic(err)
}
// Cut of the "bearer" prefix
stoken := token[7:len(token)]
// Now initialize the clients for each configured API endpoint
for i:=0; i<len(c.iConfig.APIs);i++{
configTemp := &cfclient.Config{
ApiAddress: c.iConfig.APIs[i],
Username: un,
Token: stoken,
ClientSecret: "",
ClientID: "cf"}
if c.clients[i], err = cfclient.NewClient(configTemp); err != nil {
fmt.Println(err)
panic(err)
}
}
// Individual actions based on Command
// - they should be moved to functions
// - and fetching results from each endpoint parallized
// - could sort results
switch {
case len(args) > 1 && args[1] == "orgs":
for i:=0; i<len(c.iConfig.APIs); i++ {
orgs, err := c.clients[i].ListOrgs()
if err != nil {
panic(err)
}
fmt.Println("Endpoint: ",c.iConfig.APIs[i])
for j := 0; j < len(orgs); j++ {
fmt.Printf("Org %d: %v\n", j, orgs[j].Name)
}
}
case len(args) > 1 && args[1] == "apps":
for i:=0; i<len(c.iConfig.APIs); i++ {
apps, err := c.clients[i].ListApps()
if err != nil {
panic(err)
}
fmt.Println("Endpoint: ",c.iConfig.APIs[i])
for j := 0; j < len(apps); j++ {
fmt.Printf("App %d: %v %v\n", j, apps[j].State, apps[j].Name)
}
}
default:
fmt.Println("You have the following endpoints configured:")
for i:=0; i<len(c.iConfig.APIs); i++ {
fmt.Println("Endpoint: ",c.iConfig.APIs[i])
}
}
}
}
// GetMetadata provides info on our plugin
func (c *MultiInstance) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "multi-instance",
Version: plugin.VersionType{
Major: 0,
Minor: 2,
Build: 1,
},
Commands: []plugin.Command{
{
Name: "multi-instance",
HelpText: "Get status from multiple instances",
UsageDetails: plugin.Usage{
Usage: "multi-instance \n cf multi-instance [apps | orgs]",
},
},
{
Name: "mi",
HelpText: "Get status from multiple instances",
UsageDetails: plugin.Usage{
Usage: "mi - multi-instance\n cf mi [apps | orgs]",
},
},
},
}
}
// Initialize this plugin
func main() {
plugin.Start(new(MultiInstance))
}