yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/events.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package aliyun 16 17 import ( 18 "strings" 19 "time" 20 21 "github.com/pkg/errors" 22 23 "yunion.io/x/jsonutils" 24 "yunion.io/x/pkg/utils" 25 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 ) 28 29 const ( 30 EVENT_REGION_HANGZHOU = "cn-hangzhou" 31 ) 32 33 type SAttributes struct { 34 CreationDate time.Time 35 MfaAuthenticated bool 36 } 37 38 type SSessionContext struct { 39 Attributes SAttributes 40 } 41 42 type SUserIdentity struct { 43 AccessKeyId string 44 AccountId string 45 PrincipalId string 46 SessionContext SSessionContext 47 Type string 48 UserName string 49 } 50 51 type SEvent struct { 52 region *SRegion 53 AdditionalEventData map[string]string 54 ApiVersion string 55 EventId string 56 EventName string 57 EventSource string 58 EventTime time.Time 59 EventType string 60 EventVersion string 61 RequestId string 62 RequestParameters map[string]string 63 ServiceName string 64 SourceIpAddress string 65 UserAgent string 66 UserIdentity SUserIdentity 67 ResponseElements map[string]string 68 IsGlobal bool 69 } 70 71 func (event *SEvent) GetCreatedAt() time.Time { 72 return event.EventTime 73 } 74 75 func (event *SEvent) GetName() string { 76 return event.EventName 77 } 78 79 func (event *SEvent) GetAction() string { 80 return event.ServiceName 81 } 82 83 func (event *SEvent) GetResourceType() string { 84 return "" 85 } 86 87 func (event *SEvent) GetRequestId() string { 88 return event.RequestId 89 } 90 91 func (event *SEvent) GetRequest() jsonutils.JSONObject { 92 return jsonutils.Marshal(event) 93 } 94 95 func (event *SEvent) GetAccount() string { 96 if account, ok := event.AdditionalEventData["loginAccount"]; ok { 97 return account 98 } 99 if len(event.UserIdentity.AccessKeyId) > 0 { 100 return event.UserIdentity.AccessKeyId 101 } 102 return event.UserIdentity.UserName 103 } 104 105 func (event *SEvent) GetService() string { 106 return event.ServiceName 107 } 108 109 func (event *SEvent) IsSuccess() bool { 110 return true 111 } 112 113 func (region *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) { 114 var ( 115 events []SEvent 116 err error 117 token string 118 _events []SEvent 119 iEvents []cloudprovider.ICloudEvent 120 eventRW string 121 ) 122 123 eventRW = "Write" 124 if withReadEvent { 125 eventRW = "" 126 } 127 128 for { 129 _events, token, err = region.GetEvents(start, end, token, eventRW, "") 130 if err != nil { 131 return nil, errors.Wrap(err, "region.GetEvents") 132 } 133 events = append(events, _events...) 134 if len(token) == 0 || len(_events) == 0 { 135 break 136 } 137 } 138 139 for i := range events { 140 if events[i].IsGlobal && region.RegionId != ALIYUN_DEFAULT_REGION { 141 continue 142 } 143 if !withReadEvent && strings.HasPrefix(events[i].EventName, "Query") { // QueryInstanceBill QueryBill 144 continue 145 } 146 iEvents = append(iEvents, &events[i]) 147 } 148 return iEvents, nil 149 } 150 151 func (region *SRegion) GetEvents(start time.Time, end time.Time, token string, eventRW string, requestId string) ([]SEvent, string, error) { 152 params := map[string]string{ 153 "RegionId": region.RegionId, 154 } 155 if !start.IsZero() { 156 params["StartTime"] = start.Format("2006-01-02T15:04:05Z") 157 } 158 if !end.IsZero() { 159 params["EndTime"] = end.Format("2006-01-02T15:04:05Z") 160 } 161 if utils.IsInStringArray(eventRW, []string{"Read", "Write"}) { 162 params["LookupAttribute.1.Key"] = "EventRW" 163 params["LookupAttribute.1.Value"] = eventRW 164 } 165 if len(token) > 0 { 166 params["NextToken"] = token 167 } 168 if len(requestId) > 0 { 169 params["Request"] = requestId 170 } 171 resp, err := region.trialRequest("LookupEvents", params) 172 if err != nil { 173 if strings.Contains(err.Error(), "no such host") { // cn-wulanchabu not support 174 return []SEvent{}, "", nil 175 } 176 return nil, "", err 177 } 178 179 events := []SEvent{} 180 err = resp.Unmarshal(&events, "Events") 181 if err != nil { 182 return nil, "", err 183 } 184 nextToken, _ := resp.GetString("NextToken") 185 return events, nextToken, nil 186 }