yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud 16 17 import ( 18 "fmt" 19 "strings" 20 "time" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/pkg/errors" 24 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 ) 27 28 type SEvent struct { 29 region *SRegion 30 AccountID int64 31 CloudAuditEvent string 32 ErrorCode int 33 EventId string 34 EventName string 35 EventNameCn string 36 EventRegion string 37 EventSource string 38 EventTime int64 39 RequestID string 40 ResourceRegion string 41 ResourceTypeCn string 42 Resources map[string]string 43 SecretId string 44 SourceIPAddress string 45 Username string 46 } 47 48 func (event *SEvent) GetName() string { 49 if resourceName, ok := event.Resources["ResourceName"]; ok && len(resourceName) > 0 { 50 return resourceName 51 } 52 return event.EventName 53 } 54 55 func (event *SEvent) GetService() string { 56 return event.ResourceTypeCn 57 } 58 59 func (event *SEvent) GetAction() string { 60 return event.EventName 61 } 62 63 func (event *SEvent) GetResourceType() string { 64 if resourceType, ok := event.Resources["ResourceType"]; ok { 65 return resourceType 66 } 67 return "" 68 } 69 70 func (event *SEvent) GetRequest() jsonutils.JSONObject { 71 return jsonutils.Marshal(event) 72 } 73 74 func (event *SEvent) GetRequestId() string { 75 return event.RequestID 76 } 77 78 func (event *SEvent) GetAccount() string { 79 if len(event.SecretId) > 0 { 80 return event.SecretId 81 } 82 return event.Username 83 } 84 85 func (event *SEvent) IsSuccess() bool { 86 return event.ErrorCode == 0 87 } 88 89 func (event *SEvent) GetCreatedAt() time.Time { 90 return time.Unix(event.EventTime, 0) 91 } 92 93 func (region *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) { 94 events, err := region.GetEvents(start, end) 95 if err != nil { 96 return nil, err 97 } 98 iEvents := []cloudprovider.ICloudEvent{} 99 for i := range events { 100 if withReadEvent || !strings.Contains(events[i].CloudAuditEvent, `"Read"`) { 101 iEvents = append(iEvents, &events[i]) 102 } 103 } 104 return iEvents, nil 105 } 106 107 func (region *SRegion) GetEvents(start time.Time, end time.Time) ([]SEvent, error) { 108 var ( 109 events []SEvent 110 nextToken string 111 err error 112 _events []SEvent 113 ) 114 for { 115 _events, nextToken, err = region.getEvents(start, end, nextToken) 116 if err != nil { 117 return nil, err 118 } 119 if len(nextToken) == 0 { 120 break 121 } 122 events = append(events, _events...) 123 } 124 return events, nil 125 } 126 127 func (region *SRegion) getEvents(start time.Time, end time.Time, nextToken string) ([]SEvent, string, error) { 128 params := map[string]string{} 129 if start.IsZero() { 130 start = time.Now().AddDate(0, 0, -7) 131 } 132 if end.IsZero() { 133 end = time.Now() 134 } 135 params["StartTime"] = fmt.Sprintf("%d", start.Unix()) 136 params["EndTime"] = fmt.Sprintf("%d", end.Unix()) 137 params["MaxResults"] = "50" 138 if len(nextToken) > 0 { 139 params["NextToken"] = nextToken 140 } 141 142 body, err := region.auditRequest("LookUpEvents", params) 143 if err != nil { 144 return nil, "", err 145 } 146 147 events := []SEvent{} 148 149 err = body.Unmarshal(&events, "Events") 150 if err != nil { 151 return nil, "", errors.Wrap(err, "body.Unmarshal") 152 } 153 nextToken, _ = body.GetString("NextToken") 154 if over, _ := body.Bool("ListOver"); over { 155 nextToken = "" 156 } 157 return events, nextToken, nil 158 }