forked from gravitational/teleport-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugindata.go
105 lines (87 loc) · 2.73 KB
/
plugindata.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
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"strings"
"time"
)
// PluginData is a data associated with access request that we store in Teleport using UpdatePluginData API.
type PluginData struct {
RequestData
JiraData
}
type Resolution struct {
Tag ResolutionTag
Reason string
}
type ResolutionTag string
const Unresolved = ResolutionTag("")
const ResolvedApproved = ResolutionTag("approved")
const ResolvedDenied = ResolutionTag("denied")
const ResolvedExpired = ResolutionTag("expired")
type RequestData struct {
User string
Roles []string
Created time.Time
RequestReason string
ReviewsCount int
Resolution Resolution
}
type JiraData struct {
IssueID string
IssueKey string
}
// DecodePluginData deserializes a string map to PluginData struct.
func DecodePluginData(dataMap map[string]string) (data PluginData) {
data.User = dataMap["user"]
if str := dataMap["roles"]; str != "" {
data.Roles = strings.Split(str, ",")
}
if str := dataMap["created"]; str != "" {
var created int64
fmt.Sscanf(str, "%d", &created)
data.Created = time.Unix(created, 0)
}
data.RequestReason = dataMap["request_reason"]
if str := dataMap["reviews_count"]; str != "" {
fmt.Sscanf(str, "%d", &data.ReviewsCount)
}
data.Resolution.Tag = ResolutionTag(dataMap["resolution"])
data.Resolution.Reason = dataMap["resolve_reason"]
data.IssueID = dataMap["issue_id"]
data.IssueKey = dataMap["issue_key"]
return
}
// EncodePluginData serializes a PluginData struct into a string map.
func EncodePluginData(data PluginData) map[string]string {
result := make(map[string]string)
result["issue_id"] = data.IssueID
result["issue_key"] = data.IssueKey
result["user"] = data.User
result["roles"] = strings.Join(data.Roles, ",")
var createdStr string
if !data.Created.IsZero() {
createdStr = fmt.Sprintf("%d", data.Created.Unix())
}
result["created"] = createdStr
result["request_reason"] = data.RequestReason
var reviewsCountStr string
if data.ReviewsCount != 0 {
reviewsCountStr = fmt.Sprintf("%d", data.ReviewsCount)
}
result["reviews_count"] = reviewsCountStr
result["resolution"] = string(data.Resolution.Tag)
result["resolve_reason"] = data.Resolution.Reason
return result
}