-
Notifications
You must be signed in to change notification settings - Fork 1
/
account.go
142 lines (122 loc) · 4.32 KB
/
account.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
package lib
import (
"bytes"
"io"
"github.com/BytemarkHosting/bytemark-client/lib/billing"
"github.com/BytemarkHosting/bytemark-client/lib/brain"
"github.com/BytemarkHosting/bytemark-client/lib/output"
"github.com/BytemarkHosting/bytemark-client/lib/output/prettyprint"
)
// Account represents both the BigV and bmbilling parts of an account.
type Account struct {
Name string `json:"name"`
Owner billing.Person `json:"owner"`
TechnicalContact billing.Person `json:"technical_contact"`
BillingID int `json:"billing_id"`
BrainID int `json:"brain_id"`
CardReference string `json:"card_reference"`
Groups brain.Groups `json:"groups"`
Suspended bool `json:"suspended"`
IsDefaultAccount bool `json:"-"`
}
func (a *Account) fillBrain(b brain.Account) {
a.BrainID = b.ID
a.Groups = b.Groups
a.Suspended = b.Suspended
a.Name = b.Name
}
func (a *Account) fillBilling(b billing.Account) {
a.BillingID = b.ID
a.Owner = b.Owner
a.TechnicalContact = b.TechnicalContact
a.CardReference = b.CardReference
a.Name = b.Name
}
// CountVirtualMachines returns the number of virtual machines across all the Account's Groups.
func (a Account) CountVirtualMachines() (servers int) {
for _, g := range a.Groups {
servers += len(g.VirtualMachines)
}
return
}
// DefaultFields returns the list of default fields to feed to github.com/BytemarkHosting/row.From for this type.
func (a Account) DefaultFields(f output.Format) string {
switch f {
case output.List:
return "BillingID, Name, Suspended"
}
return "BillingID, Name, Suspended, Groups"
}
// billingAccount copies all the billing parts of the account into a new billingAccount.
func (a Account) billingAccount() (b *billing.Account) {
b = new(billing.Account)
b.ID = a.BillingID
b.Owner = a.Owner
b.TechnicalContact = a.TechnicalContact
b.CardReference = a.CardReference
b.Name = a.Name
return
}
// IsValid returns true if this account has been populated with data from the brain or billing endpoints.
func (a Account) IsValid() bool {
return a.BillingID != 0 || a.BrainID != 0
}
// PrettyPrint writes an overview of this account out to the given writer.
func (a Account) PrettyPrint(wr io.Writer, detail prettyprint.DetailLevel) error {
const accountsTemplate = `{{ define "account_name" }}{{ if .BillingID }}{{ .BillingID }} - {{ end }}{{ if .Name }}{{ .Name }}{{ else }}[no bigv account]{{ end }}{{ end }}
{{ define "account_sgl" }}• {{ template "account_name" . -}}
{{- if .IsDefaultAccount }} (this is your default account){{ end -}}
{{- end }}
{{ define "group_overview" }} • {{ .Name }} - {{ pluralize "server" "servers" ( len .VirtualMachines ) -}}
{{- if len .VirtualMachines | gt 6 -}}
{{- range .VirtualMachines }}
{{ prettysprint . "_sgl" -}}
{{- end -}}
{{- end }}
{{ end -}}
{{ define "account_medium" }}
{{- if .IsDefaultAccount -}}
Your default account ({{ template "account_name" . }})
{{- else -}}
{{- template "account_name" . -}}
{{- end -}}
{{ end }}
{{ define "account_full" -}}
{{- template "account_medium" . }}
{{ range .Groups -}}
{{ template "group_overview" . -}}
{{- end -}}
{{- end }}`
return prettyprint.Run(wr, accountsTemplate, "account"+string(detail), a)
}
// String formats this account as a string - the same format as prettyprint.SingleLine
func (a Account) String() string {
buf := bytes.Buffer{}
err := a.PrettyPrint(&buf, prettyprint.SingleLine)
if err != nil {
return "ERROR"
}
return buf.String()
}
// Accounts represents more than one account in output.Outputtable form.
type Accounts []Account
// DefaultFields returns the list of default fields to feed to github.com/BytemarkHosting/row.From for this type.
func (as Accounts) DefaultFields(f output.Format) string {
return (Account{}).DefaultFields(f)
}
// PrettyPrint writes a human-readable summary of the accounts to writer at the given detail level.
func (as Accounts) PrettyPrint(wr io.Writer, detail prettyprint.DetailLevel) error {
accountsTpl := `
{{ define "accounts_sgl" -}}
{{- range . -}}
{{- .Name }}, {{ end -}}
{{- end }}
{{ define "accounts_medium" }}{{ template "accounts_sgl" . }}{{ end }}
{{ define "accounts_full" }}
Accounts:
{{ range . -}}
{{- prettysprint . "_sgl" }}
{{ end -}}
{{- end }}`
return prettyprint.Run(wr, accountsTpl, "accounts"+string(detail), as)
}