public VMLogAutoShut GetVMAutoShutDetail() { VMLogAutoShut _return = null; try { AzureVMLogger azureVMLogger = new AzureVMLogger(); if (azureVMLogger.VMLogSpreadSheetID.IsNotNullOrEmpty() && azureVMLogger.VMLogSheetName.IsNotNullOrEmpty()) { string spreadsheetId = azureVMLogger.VMLogSpreadSheetID; string sheetName = azureVMLogger.VMLogSheetName; string _lastUpdatedCell = azureVMLogger.LastSheetUpdated; string _autoShutTimeCell = azureVMLogger.AutoShutTime; string _lastUpdatedCellValue = GetCellValue(spreadsheetId, sheetName, _lastUpdatedCell)?.ToString(); string _autoShutTimeCellValue = GetCellValue(spreadsheetId, sheetName, _autoShutTimeCell)?.ToString(); _return = new VMLogAutoShut() { AutoShutTime = _autoShutTimeCellValue.IsNullOrEmpty() ? default : TimeSpan.Parse(_autoShutTimeCellValue), LastUpdated = _lastUpdatedCellValue.IsNullOrEmpty() ? default : Convert.ToDateTime(_lastUpdatedCellValue), }; } } catch (Exception ex) { } return(_return); }
public int GetVMRowIndex(string spreadsheetId, string sheetName, string vmName) { int _return = 0; try { AzureVMLogger azureVMLogger = new AzureVMLogger(); char ServerColRowIndex = azureVMLogger.ServerColumn[1]; char ServerColIndex = azureVMLogger.ServerColumn[0]; string vmCellRange = string.Concat(azureVMLogger.ServerColumn, ":", ServerColIndex); string range = $"{ sheetName }!{vmCellRange}"; var getServerRequest = service.Spreadsheets.Values.Get(spreadsheetId, range); var getServerResponse = getServerRequest.Execute(); if (getServerResponse.IsNotNull() && getServerResponse.Values.IsNotNull() && getServerResponse.Values.Count > 0) { int count = Convert.ToInt32(ServerColRowIndex.ToString()) - 1; foreach (var item in getServerResponse.Values) { count++; if (item.Count > 0 && item[0].Equals(vmName)) { _return = count; } } } } catch (Exception ex) { } return(_return); }
public IList <VMLogSheetDetail> GetAllVMLogDetail(string spreadsheetId, string sheetName) { IList <VMLogSheetDetail> _return = null; try { AzureVMLogger azureVMLogger = new AzureVMLogger(); int initialRowIndex = int.Parse(azureVMLogger.RowIndex); if (initialRowIndex > 0) { _return = new List <VMLogSheetDetail>(); for (int _rowIndex = initialRowIndex; true; _rowIndex++) { string _resourceGroupCell = string.Concat(azureVMLogger.ResourceGroupColumn[0], _rowIndex); string _serverCell = string.Concat(azureVMLogger.ServerColumn[0], _rowIndex); string _serverDateTimeCell = string.Concat(azureVMLogger.ServerDateTimeColumn[0], _rowIndex); string _serverStatusCell = string.Concat(azureVMLogger.ServerStatusColumn[0], _rowIndex); string _userActiveCell = string.Concat(azureVMLogger.UserActiveColumn[0], _rowIndex); string _userActiveDateTimeCell = string.Concat(azureVMLogger.UserActiveDateTimeColumn[0], _rowIndex); string _serverDateTimeRawValue = GetCellValue(spreadsheetId, sheetName, _serverDateTimeCell)?.ToString(); string _userActiveDateTimeRawValue = GetCellValue(spreadsheetId, sheetName, _userActiveDateTimeCell)?.ToString(); //If vmName not exist then break the loop if (GetCellValue(spreadsheetId, sheetName, _serverCell).IsNull()) { break; } _return.Add(new VMLogSheetDetail() { rowId = _rowIndex, ResourceGroupName = GetCellValue(spreadsheetId, sheetName, _resourceGroupCell)?.ToString(), ServerName = GetCellValue(spreadsheetId, sheetName, _serverCell)?.ToString(), ServerDateTime = _serverDateTimeRawValue.IsNullOrEmpty() ? DateTime.Now : Convert.ToDateTime(_serverDateTimeRawValue), ServerStatus = GetCellValue(spreadsheetId, sheetName, _serverStatusCell)?.ToString(), UserActiveDateTime = _userActiveDateTimeRawValue.IsNullOrEmpty() ? DateTime.Now : Convert.ToDateTime(_userActiveDateTimeRawValue), UserActiveStatus = GetCellValue(spreadsheetId, sheetName, _userActiveCell)?.ToString() }); } } } catch (Exception ex) { } return(_return); }
public bool UpdateVmLogServerStatus(int rowID, string value, bool IsUpdateDate) { bool _return = false; try { AzureVMLogger azureVMLogger = new AzureVMLogger(); if (rowID > 0 && azureVMLogger.VMLogSpreadSheetID.IsNotNullOrEmpty() && azureVMLogger.VMLogSheetName.IsNotNullOrEmpty()) { string spreadsheetId = azureVMLogger.VMLogSpreadSheetID; string sheetName = azureVMLogger.VMLogSheetName; string _ServerStatusCol = azureVMLogger.ServerStatusColumn; char _ServerStatusColRowIndex = azureVMLogger.ServerStatusColumn[1]; char _ServerStatusColIndex = azureVMLogger.ServerStatusColumn[0]; string _ServerDateTimeCol = azureVMLogger.ServerDateTimeColumn; char _ServerDateTimeColRowIndex = azureVMLogger.ServerDateTimeColumn[1]; char _ServerDateTimeColIndex = azureVMLogger.ServerDateTimeColumn[0]; string ServerStatusCellName = string.Concat(_ServerStatusColIndex, rowID); string ServerDateTimeCellName = string.Concat(_ServerDateTimeColIndex, rowID); if (value.IsNotNullOrEmpty()) { _return = UpdateCellValue(spreadsheetId, sheetName, ServerStatusCellName, value); if (_return && IsUpdateDate) { string currentDateTime = DateTime.Now.ToString(CommonConstant.DateTimeFormat); UpdateCellValue(spreadsheetId, sheetName, ServerDateTimeCellName, currentDateTime); } } } } catch (Exception ex) { } return(_return); }
public VMLogSheetDetail GetVMLogDetail(string spreadsheetId, string sheetName, string vmName) { VMLogSheetDetail _return = null; try { AzureVMLogger azureVMLogger = new AzureVMLogger(); int searchedRowIndex = GetVMRowIndex(spreadsheetId, sheetName, vmName); if (searchedRowIndex > 0) { string _resourceGroupCell = string.Concat(azureVMLogger.ResourceGroupColumn[0], searchedRowIndex); string _serverCell = string.Concat(azureVMLogger.ServerColumn[0], searchedRowIndex); string _serverDateTimeCell = string.Concat(azureVMLogger.ServerDateTimeColumn[0], searchedRowIndex); string _serverStatusCell = string.Concat(azureVMLogger.ServerStatusColumn[0], searchedRowIndex); string _userActiveCell = string.Concat(azureVMLogger.UserActiveColumn[0], searchedRowIndex); string _userActiveDateTimeCell = string.Concat(azureVMLogger.UserActiveDateTimeColumn[0], searchedRowIndex); string _serverDateTimeRawValue = GetCellValue(spreadsheetId, sheetName, _serverDateTimeCell)?.ToString(); string _userActiveDateTimeRawValue = GetCellValue(spreadsheetId, sheetName, _userActiveDateTimeCell)?.ToString(); _return = new VMLogSheetDetail() { rowId = searchedRowIndex, ResourceGroupName = GetCellValue(spreadsheetId, sheetName, _resourceGroupCell)?.ToString(), ServerName = GetCellValue(spreadsheetId, sheetName, _serverCell)?.ToString(), ServerDateTime = _serverDateTimeRawValue.IsNullOrEmpty() ? DateTime.Now : Convert.ToDateTime(_serverDateTimeRawValue), ServerStatus = GetCellValue(spreadsheetId, sheetName, _serverStatusCell)?.ToString(), UserActiveDateTime = _userActiveDateTimeRawValue.IsNullOrEmpty() ? DateTime.Now : Convert.ToDateTime(_userActiveDateTimeRawValue), UserActiveStatus = GetCellValue(spreadsheetId, sheetName, _userActiveCell)?.ToString() }; } } catch (Exception ex) { } return(_return); }
public bool UpdateSheetDate() { bool _return = false; try { AzureVMLogger _prop = new AzureVMLogger(); string currentDateTime = DateTime.Now.ToString(CommonConstant.DateTimeFormat); _return = googleService.UpdateCellValue(_prop.VMLogSpreadSheetID, _prop.VMLogSheetName, _prop.LastSheetUpdated, currentDateTime); } catch (Exception ex) { _return = false; } return(_return); }
public bool PerformAzureToGoogleActionForUnlistedVM(IList <string> vmNameList) { bool _return = false; try { if (vmNameList.IsNotNull()) { AzureVMLogger _prop = new AzureVMLogger(); var _listAllVM = googleService.GetAllVMLogDetail(_prop.VMLogSpreadSheetID, _prop.VMLogSheetName); if (_listAllVM.IsNotNull()) { var autoShutDetail = googleService.GetVMAutoShutDetail(); var _filteredVMList = _listAllVM.Where(x => !vmNameList.Any(i => CommonConstant.GetActualVMName(i).Equals(x.ServerName, StringComparison.OrdinalIgnoreCase))); if (_filteredVMList.IsNotNull()) { foreach (VMLogSheetDetail vmSheetDetail in _filteredVMList) { if (vmSheetDetail.IsNotNull()) { var vmDetail = azureService.GetVMDetail(vmSheetDetail.ResourceGroupName, vmSheetDetail.ServerName); if (vmDetail.IsNotNull() && autoShutDetail.IsNotNull()) { bool _onSheetUserActive = vmSheetDetail.UserActiveStatus.Contains(UserStatus.Active, StringComparison.OrdinalIgnoreCase); bool _onSheetServerActive = vmSheetDetail.ServerStatus.Contains(ServerStatus.Running, StringComparison.OrdinalIgnoreCase); if (vmDetail.PowerState == PowerState.Deallocating || vmDetail.PowerState == PowerState.Deallocated || vmDetail.PowerState == PowerState.Stopping || vmDetail.PowerState == PowerState.Stopped) { if (_onSheetUserActive) { //Update User status to No and change datetime of user googleService.UpdateVmLogUserStatus(vmSheetDetail.rowId, UserStatus.InActive, true); } if (_onSheetServerActive) { //Update Server status to Stopped and change datetime of server googleService.UpdateVmLogServerStatus(vmSheetDetail.rowId, ServerStatus.Stopped, true); } } else if (vmDetail.PowerState == PowerState.Running || vmDetail.PowerState == PowerState.Starting) { if (!_onSheetServerActive) { googleService.UpdateVmLogServerStatus(vmSheetDetail.rowId, ServerStatus.Running, true); } else { DateTime?latestTime = vmSheetDetail.ServerDateTime > vmSheetDetail.UserActiveDateTime ? vmSheetDetail.ServerDateTime : vmSheetDetail.UserActiveDateTime; if (autoShutDetail.LastUpdated.Value.Subtract(latestTime.Value) >= autoShutDetail.AutoShutTime) { //shutdown system with server status update to Stopped azureService.StopVMByVmNameAsync(vmSheetDetail.ResourceGroupName, vmSheetDetail.ServerName); googleService.UpdateVmLogServerStatus(vmSheetDetail.rowId, ServerStatus.Stopped, true); if (_onSheetUserActive) { //Update User status to No and change datetime of user googleService.UpdateVmLogUserStatus(vmSheetDetail.rowId, UserStatus.InActive, true); } } } } } } } } } _return = true; } } catch (Exception ex) { _return = false; } return(_return); }
public bool PerformAzureToGoogleAction(IList <RunCommandRequest> requestList, string SessionHostName) { bool _return = false; try { if (requestList.IsNotNull()) { AzureVMLogger _prop = new AzureVMLogger(); bool isUserActive = requestList.Any(x => x.State.Contains("Active", StringComparison.OrdinalIgnoreCase)); string azureVMName = CommonConstant.GetActualVMName(SessionHostName); var vmSheetDetail = googleService.GetVMLogDetail(_prop.VMLogSpreadSheetID, _prop.VMLogSheetName, azureVMName); if (vmSheetDetail.IsNotNull()) { var autoShutDetail = googleService.GetVMAutoShutDetail(); var vmDetail = azureService.GetVMDetail(vmSheetDetail.ResourceGroupName, azureVMName); if (vmDetail.IsNotNull() && autoShutDetail.IsNotNull()) { bool _onSheetUserActive = vmSheetDetail.UserActiveStatus.Contains(UserStatus.Active, StringComparison.OrdinalIgnoreCase); bool _onSheetServerActive = vmSheetDetail.ServerStatus.Contains(ServerStatus.Running, StringComparison.OrdinalIgnoreCase); if (isUserActive) { if (!_onSheetUserActive) { //Update User status to Yes and change datetime of user googleService.UpdateVmLogUserStatus(vmSheetDetail.rowId, UserStatus.Active, true); } if (!_onSheetServerActive) { //Update Server status to Run and change datetime of server googleService.UpdateVmLogServerStatus(vmSheetDetail.rowId, ServerStatus.Running, true); } } else { if (_onSheetUserActive) { //Update User status to No and change datetime of user googleService.UpdateVmLogUserStatus(vmSheetDetail.rowId, UserStatus.InActive, true); } if (_onSheetServerActive) { DateTime?latestTime = vmSheetDetail.ServerDateTime > vmSheetDetail.UserActiveDateTime ? vmSheetDetail.ServerDateTime : vmSheetDetail.UserActiveDateTime; if (autoShutDetail.LastUpdated.Value.Subtract(latestTime.Value) >= autoShutDetail.AutoShutTime) { //shutdown system with server status update to Stopped azureService.StopVMByVmNameAsync(vmSheetDetail.ResourceGroupName, azureVMName); googleService.UpdateVmLogServerStatus(vmSheetDetail.rowId, ServerStatus.Stopped, true); } } } } } _return = true; } } catch (Exception ex) { _return = false; } return(_return); }