Debian Codegraph @

order/order.go

raw symbols SCIP
1 // Package order manages the bookkeeping and utilies required
2 // for users to create an 'order' meaning they have requested
3 // delegations for a certian resource.
4 //
5 // Copyright (c) 2016 CloudFlare, Inc.
6 package order
7
8 import (
9 "crypto/rand"
10 "encoding/hex"
11 "fmt"
12 "net/url"
13 "strconv"
14 "time"
15
16 "github.com/cloudflare/redoctober/hipchat"
17 )
18
19 const (
20 NewOrder = "%s has created an order for the label %s. requesting %d delegations for %s"
21 NewOrderLink = "@%s - https://%s?%s"
22 OrderFulfilled = "%s has had order %s fulfilled."
23 NewDelegation = "%s has delegated the label %s to %s (per order %s) for %s"
24 )
25
26 type Order struct {
27 Creator string
28 Users []string
29 Num string
30
31 TimeRequested time.Time
32 DurationRequested time.Duration
33 Delegated int
34 OwnersDelegated []string
35 Owners []string
36 Labels []string
37 }
38
39 type OrderIndex struct {
40 OrderFor string
41
42 OrderId string
43 OrderOwners []string
44 }
45
46 // Orders represents a mapping of Order IDs to Orders. This structure
47 // is useful for looking up information about individual Orders and
48 // whether or not an order has been fulfilled. Orders that have been
49 // fulfilled will be removed from the structure.
50 type Orderer struct {
51 Orders map[string]Order
52 Hipchat hipchat.HipchatClient
53 AlternateName string
54 }
55
56 func CreateOrder(name, orderNum string, time time.Time, duration time.Duration, adminsDelegated, contacts, users, labels []string, numDelegated int) (ord Order) {
57 ord.Creator = name
58 ord.Num = orderNum
59 ord.Labels = labels
60 ord.TimeRequested = time
61 ord.DurationRequested = duration
62 ord.OwnersDelegated = adminsDelegated
63 ord.Owners = contacts
64 ord.Delegated = numDelegated
65 ord.Users = users
66 return
67 }
68
69 func GenerateNum() (num string) {
70 b := make([]byte, 12)
71 rand.Read(b)
72 return hex.EncodeToString(b)
73 }
74
75 // NewOrder will create a new map of Orders
76 func NewOrderer(hipchatClient hipchat.HipchatClient) (o Orderer) {
77 o.Orders = make(map[string]Order)
78 o.Hipchat = hipchatClient
79 o.AlternateName = "HipchatName"
80 return
81 }
82
83 // notify is a generic function for using a notifier, but it checks to make
84 // sure that there is a notifier available, since there won't always be.
85 func notify(o *Orderer, msg, color string) {
86 o.Hipchat.Notify(msg, color)
87 }
88 func (o *Orderer) NotifyNewOrder(duration, orderNum string, names, labels []string, uses int, owners map[string]string) {
89 labelList := ""
90 for i, label := range labels {
91 if i == 0 {
92 labelList += label
93 } else {
94 // Never include spaces in something go URI encodes. Go will
95 // add a + to the string, instead of a %20
96 labelList += "," + label
97 }
98 }
99 nameList := ""
100 for i, name := range names {
101 if i == 0 {
102 nameList += name
103 } else {
104 // Never include spaces in something go URI encodes. Go will
105 // add a + to the string, instead of a %20
106 nameList += "," + name
107 }
108 }
109
110 n := fmt.Sprintf(NewOrder, nameList, labelList, uses, duration)
111 notify(o, n, hipchat.RedBackground)
112 for owner, hipchatName := range owners {
113 queryParams := url.Values{
114 "delegator": {owner},
115 "label": {labelList},
116 "duration": {duration},
117 "uses": {strconv.Itoa(uses)},
118 "ordernum": {orderNum},
119 "delegatee": {nameList},
120 }.Encode()
121 notify(o, fmt.Sprintf(NewOrderLink, hipchatName, o.Hipchat.RoHost, queryParams), hipchat.GreenBackground)
122 }
123 }
124
125 func (o *Orderer) NotifyDelegation(delegator, delegatee, orderNum, duration string, labels []string) {
126 labelList := ""
127 for i, label := range labels {
128 if i == 0 {
129 labelList += label
130 } else {
131 labelList += ", " + label
132 }
133 }
134 n := fmt.Sprintf(NewDelegation, delegator, labelList, delegatee, orderNum, duration)
135 notify(o, n, hipchat.YellowBackground)
136 }
137 func (o *Orderer) NotifyOrderFulfilled(name, orderNum string) {
138 n := fmt.Sprintf(OrderFulfilled, name, orderNum)
139 notify(o, n, hipchat.PurpleBackground)
140 }
141
142 func (o *Orderer) FindOrder(user string, labels []string) (string, bool) {
143 for key, order := range o.Orders {
144 foundLabel := false
145 foundUser := false
146 for _, orderUser := range order.Users {
147 if orderUser == user {
148 foundUser = true
149 }
150 }
151 if !foundUser {
152 continue
153 }
154 for _, ol := range order.Labels {
155 foundLabel = false
156 for _, il := range labels {
157 if il == ol {
158 foundLabel = true
159 }
160 }
161 }
162 if !foundLabel {
163 continue
164 }
165 return key, true
166 }
167 return "", false
168 }