yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/event.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 azure
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  	"time"
    22  
    23  	"yunion.io/x/jsonutils"
    24  
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  )
    27  
    28  type SAuthorization struct {
    29  	Action string
    30  	Scope  string
    31  }
    32  
    33  type SClaims struct {
    34  	Aud      string
    35  	Iss      string
    36  	Iat      string
    37  	Nbf      string
    38  	Exp      string
    39  	Aio      string
    40  	Appid    string
    41  	Appidacr string
    42  	Uti      string
    43  	Ver      string
    44  }
    45  
    46  type SLocalized struct {
    47  	Value          string
    48  	LocalizedValue string
    49  }
    50  
    51  type SEvent struct {
    52  	region *SRegion
    53  
    54  	Authorization        SAuthorization
    55  	Channels             string
    56  	Claims               SClaims
    57  	CorrelationId        string
    58  	Description          string
    59  	EventDataId          string
    60  	EventName            SLocalized
    61  	Category             SLocalized
    62  	Level                string
    63  	ResourceGroupName    string
    64  	ResourceProviderName SLocalized
    65  	ResourceId           string
    66  	ResourceType         SLocalized
    67  	OperationId          string
    68  	OperationName        SLocalized
    69  	Properties           string
    70  	Status               SLocalized
    71  	SubStatus            SLocalized
    72  	Caller               string
    73  	EventTimestamp       time.Time
    74  	SubmissionTimestamp  time.Time
    75  	SubscriptionId       string
    76  	TenantId             string
    77  	ID                   string
    78  	Name                 string
    79  }
    80  
    81  func (event *SEvent) GetName() string {
    82  	return event.ResourceId
    83  }
    84  
    85  func (event *SEvent) GetService() string {
    86  	return event.ResourceProviderName.Value
    87  }
    88  
    89  func (event *SEvent) GetAction() string {
    90  	return event.OperationName.Value
    91  }
    92  
    93  func (event *SEvent) GetResourceType() string {
    94  	return event.ResourceType.Value
    95  }
    96  
    97  func (event *SEvent) GetRequestId() string {
    98  	return event.CorrelationId
    99  }
   100  
   101  func (event *SEvent) GetRequest() jsonutils.JSONObject {
   102  	return jsonutils.Marshal(event)
   103  }
   104  
   105  func (event *SEvent) GetAccount() string {
   106  	return event.Claims.Appid
   107  }
   108  
   109  func (event *SEvent) IsSuccess() bool {
   110  	return event.Status.Value != "Failed"
   111  }
   112  
   113  func (event *SEvent) GetCreatedAt() time.Time {
   114  	return event.EventTimestamp
   115  }
   116  
   117  func (region *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) {
   118  	events, err := region.GetEvents(start, end)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	iEvents := []cloudprovider.ICloudEvent{}
   123  	for i := range events {
   124  		read := false
   125  		for _, k := range []string{"read", "listKeys"} {
   126  			if strings.Contains(events[i].Authorization.Action, k) {
   127  				read = true
   128  				break
   129  			}
   130  		}
   131  		if withReadEvent || !read {
   132  			iEvents = append(iEvents, &events[i])
   133  		}
   134  	}
   135  	return iEvents, nil
   136  }
   137  
   138  func (region *SRegion) GetEvents(start time.Time, end time.Time) ([]SEvent, error) {
   139  	events := []SEvent{}
   140  	if start.IsZero() {
   141  		start = time.Now().AddDate(0, 0, -7)
   142  	}
   143  	if end.IsZero() {
   144  		end = time.Now()
   145  	}
   146  	params := url.Values{}
   147  	params.Set("$filter", fmt.Sprintf("eventTimestamp ge '%s' and eventTimestamp le '%s' and eventChannels eq 'Admin, Operation' and levels eq 'Critical,Error,Warning,Informational'", start.Format("2006-01-02T15:04:05Z"), end.Format("2006-01-02T15:04:05Z")))
   148  	resource := fmt.Sprintf("microsoft.insights/eventtypes/management/values")
   149  	err := region.client.list(resource, params, &events)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  	return events, nil
   154  }