This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
forked from lacework/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_channels_email_user.go
123 lines (107 loc) · 3.36 KB
/
alert_channels_email_user.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
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2021, Lacework Inc.
// License:: Apache License, Version 2.0
//
// 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 api
import (
"encoding/json"
"strings"
)
// GetEmailUser gets a single EmailUser alert channel matching the
// provided integration guid
func (svc *AlertChannelsService) GetEmailUser(guid string) (
response EmailUserAlertChannelResponse,
err error,
) {
// by default, expect the correct response, if not, try the workaround
err = svc.get(guid, &response)
if err == nil {
return
}
// Workaround from APIv2
// Bug: https://lacework.atlassian.net/browse/RAIN-20070
//
// This means that the response.Data.Data.ChannelProps.Recipients is a 'string'
// instead of '[]string'. We will try to deserialize and cast to correct response
var getResponse emailUserGetAlertChannelResponse
err = svc.get(guid, &getResponse)
if err != nil {
return
}
// convert GET response to a consistent response
response, err = convertGetEmailUserAlertChannelResponse(getResponse)
return
}
// UpdateEmailUser updates a single EmailUser integration on the Lacework Server
func (svc *AlertChannelsService) UpdateEmailUser(data AlertChannel) (
response EmailUserAlertChannelResponse,
err error,
) {
err = svc.update(data.ID(), data, &response)
return
}
type EmailUserAlertChannelResponse struct {
Data EmailUserIntegration `json:"data"`
}
type EmailUserIntegration struct {
v2CommonIntegrationData
Data EmailUserData `json:"data"`
}
type EmailUserData struct {
ChannelProps EmailUserChannelProps `json:"channelProps"`
NotificationTypes struct {
Properties interface{} `json:"properties,omitempty"`
} `json:"notificationTypes"`
}
type EmailUserChannelProps struct {
Recipients []string `json:"recipients"`
}
// Workaround from APIv2
// Bug: https://lacework.atlassian.net/browse/RAIN-20070
type emailUserGetData struct {
ChannelProps struct {
Recipients interface{} `json:"recipients"`
} `json:"channelProps"`
NotificationTypes struct {
Properties interface{} `json:"properties,omitempty"`
} `json:"notificationTypes"`
}
type emailUserGetIntegration struct {
v2CommonIntegrationData
Data emailUserGetData `json:"data"`
}
type emailUserGetAlertChannelResponse struct {
Data emailUserGetIntegration `json:"data"`
}
func convertGetEmailUserAlertChannelResponse(
res emailUserGetAlertChannelResponse) (EmailUserAlertChannelResponse, error) {
recipientsString, ok := res.Data.Data.ChannelProps.Recipients.(string)
if ok {
// deserialize string
res.Data.Data.ChannelProps.Recipients = strings.Split(recipientsString, ",")
}
return castEmailUserAlertChannelResponse(res)
}
func castEmailUserAlertChannelResponse(
res interface{}) (r EmailUserAlertChannelResponse, err error) {
var j []byte
j, err = json.Marshal(res)
if err != nil {
return
}
err = json.Unmarshal(j, &r)
return
}