Skip to content

Commit

Permalink
Add dataResidencyRegion option for sending to portal (#1752)
Browse files Browse the repository at this point in the history
Co-authored-by: AnatolyPristensky <[email protected]>
  • Loading branch information
MikhailSuendukov and AnatolyPristensky authored Sep 22, 2023
1 parent e70fc9b commit e79284d
Show file tree
Hide file tree
Showing 37 changed files with 232 additions and 27 deletions.
1 change: 1 addition & 0 deletions Apps/Contoso.MAUI.Demo/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private void StartAppCenter()
{
AppCenter.SetMaxStorageSizeAsync(Preferences.Get(Constants.StorageMaxSize, 0));
}
AppCenter.PlatformSetDataResidencyRegion(Preferences.Get(Constants.DataResidencyRegion, null));

var appSecret = GetTokensString();
AppCenter.Start(appSecret, typeof(Analytics), typeof(Crashes), typeof(Distribute));
Expand Down
1 change: 1 addition & 0 deletions Apps/Contoso.MAUI.Demo/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public static class Constants
public const string StorageMaxSize = "storage-max-size";
public const string EnableManualSessionTracker = "enable-manual-session-tracker";
public const string CountryCode = "country-code";
public const string DataResidencyRegion = "data-residency-region";
public const string EnabledForDebuggableBuild = "enabled-for-debuggable-build";
}
7 changes: 7 additions & 0 deletions Apps/Contoso.MAUI.Demo/ModulePages/AppCenterContentPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@
<StackLayout Margin="0, 8, 0, 0">
<Button Text="Save Storage Size" Padding="16, 8, 16, 8" Clicked="SaveStorageSize_Clicked" VerticalOptions="Center" />
</StackLayout>
<StackLayout Margin="0, 8, 0, 0" Orientation="Horizontal">
<Label FontSize="Small" Text="Data Residency Region" HorizontalOptions="FillAndExpand" VerticalOptions="Center" />
<Entry FontSize="Small" BackgroundColor="LightGrey" x:Name="DataResidencyRegion" Margin="4, 0, 4, 0" HorizontalOptions="FillAndExpand" VerticalOptions="Center" HorizontalTextAlignment="End" />
</StackLayout>
<StackLayout Margin="0, 8, 0, 0">
<Button Text="Save Data Residency Region" Padding="16, 8, 16, 8" Clicked="SaveDataResidencyRegion_Clicked" VerticalOptions="Center" />
</StackLayout>
</StackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public AppCenterContentPage()
StorageMaxSize.Text = Preferences.Get(Constants.StorageMaxSize, 0).ToString();
}

DataResidencyRegion.Text = Preferences.Get(Constants.DataResidencyRegion, "").ToString();

// Setup start type dropdown choices.
foreach (var startType in StartTypeUtils.GetStartTypeChoiceStrings())
{
Expand Down Expand Up @@ -77,6 +79,13 @@ private void SaveStorageSize_Clicked(object sender, System.EventArgs e)
}
}

private void SaveDataResidencyRegion_Clicked(object sender, System.EventArgs e)
{
var inputText = DataResidencyRegion.Text;
AppCenter.PlatformSetDataResidencyRegion(inputText);
Preferences.Set(Constants.DataResidencyRegion, inputText);
}

void AllowedNetworkRequestEnabled(System.Object sender, ToggledEventArgs e)
{
AppCenter.IsNetworkRequestsAllowed = e.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public App()
{
AppCenter.SetCountryCode(countryCode);
}
var dataResidencyRegion = localSettings.Values[Constants.KeyDataResidencyRegion] as string;
AppCenter.PlatformSetDataResidencyRegion(dataResidencyRegion);
var storageSize = localSettings.Values[Constants.KeyStorageMaxSize] as long?;
if (storageSize != null && storageSize > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Constants
public static string KeyStorageMaxSize = "StorageMaxSize";
public static string KeyUserId = "UserId";
public static string KeyCountryCode = "CountryCode";
public static string KeyDataResidencyRegion = "DataResidencyRegion";
public static string KeyFileErrorAttachments = "FileErrorAttachments";
public static string KeyTextErrorAttachments = "TextErrorAttachments";
public static string KeyEnableManualSessionTracker = "EnableManualSessionTrackerKey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
<TextBlock Visibility="Collapsed" Text="Country code has been updated. This value will only be applied to the following sessions." Foreground="Red" Name="CountryCodeNotice" TextWrapping="Wrap"/>
</StackPanel>
</StackPanel>

<!-- Set data residency region -->
<StackPanel Orientation="Horizontal" Height="1" Background="Gray"/>
<StackPanel Margin="0, 16, 0, 16">
<StackPanel Name="DataResidencyRegionPanel">
<TextBlock Text="Data residency region" Name="DataResidencyRegionLabel"/>
<TextBox Name="DataResidencyRegionText" VerticalAlignment="Center"/>
<Button Name="SaveDataResidencyRegionButton" Content="Save" Margin="0, 8, 0, 8" Click="SaveDataResidencyRegionButton_Click"/>
<TextBlock Visibility="Collapsed" Text="Data residency region has been updated." Foreground="Red" Name="DataResidencyRegionNotice" TextWrapping="Wrap"/>
</StackPanel>
</StackPanel>
</StackPanel>
</muxc:TabViewItem>
<muxc:TabViewItem Header="Analytics" IsClosable="False">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ private void SaveCountryCode()
AppCenter.SetCountryCode(CountryCodeText.Text.Length > 0 ? CountryCodeText.Text : null);
}

private void SaveDataResidencyRegionButton_Click(object sender, RoutedEventArgs e)
{
SaveDataResidencyRegion();
}

private void SaveDataResidencyRegion()
{
DataResidencyRegionNotice.Visibility = Visibility.Visible;
localSettings.Values[Constants.KeyDataResidencyRegion] = DataResidencyRegionText.Text;
AppCenter.PlatformSetDataResidencyRegion(!string.IsNullOrEmpty(DataResidencyRegionText.Text) ? DataResidencyRegionText.Text : null);
}

private void SaveStorageSize_Click(object sender, RoutedEventArgs e)
{
var storageSize = StorageMaxSize.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public App()
{
AppCenter.SetCountryCode(countryCode);
}
var dataResidencyRegion = localSettings.Values[Constants.KeyDataResidencyRegion] as string;
AppCenter.PlatformSetDataResidencyRegion(dataResidencyRegion);
var storageSize = localSettings.Values[Constants.KeyStorageMaxSize] as long?;
if (storageSize != null && storageSize > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Constants
public static string KeyStorageMaxSize = "StorageMaxSize";
public static string KeyUserId = "UserId";
public static string KeyCountryCode = "CountryCode";
public static string KeyDataResidencyRegion = "DataResidencyRegion";
public static string KeyFileErrorAttachments = "FileErrorAttachments";
public static string KeyTextErrorAttachments = "TextErrorAttachments";
public static string KeyEnableManualSessionTracker = "EnableManualSessionTrackerKey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
<TextBlock Visibility="Collapsed" Text="Country code has been updated. This value will only be applied to the following sessions." Foreground="Red" Name="CountryCodeNotice" TextWrapping="Wrap"/>
</StackPanel>
</StackPanel>

<!-- Set data residency region -->
<StackPanel Orientation="Horizontal" Height="1" Background="Gray"/>
<StackPanel Margin="0, 16, 0, 16">
<StackPanel Name="DataResidencyRegionPanel">
<TextBlock Text="Data residency region" Name="DataResidencyRegionLabel"/>
<TextBox Name="DataResidencyRegionText" VerticalAlignment="Center"/>
<Button Name="SaveDataResidencyRegionButton" Content="Save" Margin="0, 8, 0, 8" Click="SaveDataResidencyRegionButton_Click"/>
<TextBlock Visibility="Collapsed" Text="Data residency region has been updated." Foreground="Red" Name="DataResidencyRegionNotice" TextWrapping="Wrap"/>
</StackPanel>
</StackPanel>
</StackPanel>
</muxc:TabViewItem>
<muxc:TabViewItem Header="Analytics" IsClosable="False">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ private void SaveCountryCode()
AppCenter.SetCountryCode(CountryCodeText.Text.Length > 0 ? CountryCodeText.Text : null);
}

private void SaveDataResidencyRegionButton_Click(object sender, RoutedEventArgs e)
{
SaveDataResidencyRegion();
}

private void SaveDataResidencyRegion()
{
DataResidencyRegionNotice.Visibility = Visibility.Visible;
localSettings.Values[Constants.KeyDataResidencyRegion] = DataResidencyRegionText.Text;
AppCenter.PlatformSetDataResidencyRegion(!string.IsNullOrEmpty(DataResidencyRegionText.Text) ? DataResidencyRegionText.Text : null);
}

private void SaveStorageSize_Click(object sender, RoutedEventArgs e)
{
var storageSize = StorageMaxSize.Text;
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# App Center SDK for .NET Change Log

## Version 5.0.3 (Under development)
## Version 5.0.3

### App Center

* **[Internal]** Add `dataResidencyRegion` option.

#### Windows

* **[Improvement]** Update SQLitePCLRaw.bundle_green to version 2.1.5
Expand Down
10 changes: 10 additions & 0 deletions SDK/AppCenter/Microsoft.AppCenter.Android/AppCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ static void PlatformSetCountryCode(string countryCode)
Android.AppCenter.SetCountryCode(countryCode);
}

static void PlatformSetDataResidencyRegion(string dataResidencyRegion)
{
Android.AppCenter.DataResidencyRegion = dataResidencyRegion;
}

static string PlatformGetDataResidencyRegion()
{
return Android.AppCenter.DataResidencyRegion;
}

static bool PlatformConfigured
{
get
Expand Down
10 changes: 10 additions & 0 deletions SDK/AppCenter/Microsoft.AppCenter.Apple/ApiDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ interface MSACAppCenter
[Export("setCountryCode:")]
void SetCountryCode(string countryCode);

//+ (void)setDataResidencyRegion:(NSString *)dataResidencyRegion
[Static]
[Export("setDataResidencyRegion:")]
void SetDataResidencyRegion([NullAllowed] string dataResidencyRegion);

//+ (NSString *)dataResidencyRegion;
[Static]
[Export("dataResidencyRegion")]
NSString GetDataResidencyRegion();

// + (void)setNetworkRequestsAllowed:(BOOL)isAllowed;
[Static]
[Export("setNetworkRequestsAllowed:")]
Expand Down
10 changes: 10 additions & 0 deletions SDK/AppCenter/Microsoft.AppCenter.Apple/AppCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ static void PlatformSetCountryCode(string countryCode)
MSACAppCenter.SetCountryCode(countryCode);
}

static void PlatformSetDataResidencyRegion(string dataResidencyRegion)
{
MSACAppCenter.SetDataResidencyRegion(dataResidencyRegion);
}

static string PlatformGetDataResidencyRegion()
{
return MSACAppCenter.GetDataResidencyRegion();
}

static bool PlatformConfigured
{
get
Expand Down
27 changes: 25 additions & 2 deletions SDK/AppCenter/Microsoft.AppCenter.Shared.Windows/AppCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public partial class AppCenter
private bool _instanceConfigured;
private string _appSecret;
private long _storageMaxSize;
private string _dataResidencyResion;
private TaskCompletionSource<bool> _storageTaskCompletionSource;

#region static
Expand Down Expand Up @@ -132,6 +133,28 @@ public static void PlatformSetCountryCode(string countryCode)
DeviceInformationHelper.SetCountryCode(countryCode);
}

/// <summary>
/// Sets the data residency region to send to the backend.
/// </summary>
/// <param name="dataResidencyRegion">The data residency region code.
/// Verify list of supported regions on <link>. Value outside of supported range is treated as ANY</param>
public static void PlatformSetDataResidencyRegion(string dataResidencyRegion)
{
lock (AppCenterLock)
{
Instance._dataResidencyResion = dataResidencyRegion;
}
}

/// <summary>
/// Get the data residency region.
/// </summary>
/// <returns>Data residency region code.</returns>
public static string PlatformGetDataResidencyRegion()
{
return Instance._dataResidencyResion;
}

// This method must be called *before* instance of AppCenter has been created
// for a custom application settings to be used.
[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down Expand Up @@ -337,7 +360,7 @@ private Task SetInstanceEnabled(bool value)
// Send started services.
if (_startedServiceNames != null && value)
{
var startServiceLog = new StartServiceLog { Services = _startedServiceNames };
var startServiceLog = new StartServiceLog { Services = _startedServiceNames, DataResidencyRegion = PlatformGetDataResidencyRegion() };
_startedServiceNames = null;
return _channel.EnqueueAsync(startServiceLog);
}
Expand Down Expand Up @@ -454,7 +477,7 @@ internal void StartInstance(params Type[] services)
{
if (InstanceEnabled)
{
_channel.EnqueueAsync(new StartServiceLog { Services = serviceNames }).ConfigureAwait(false);
_channel.EnqueueAsync(new StartServiceLog { Services = serviceNames, DataResidencyRegion = PlatformGetDataResidencyRegion() }).ConfigureAwait(false);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ private async Task PrepareLogAsync(Log log, State state)
}
log.Device = log.Device ?? _device;
log.Timestamp = log.Timestamp ?? DateTime.Now;
log.DataResidencyRegion = log.DataResidencyRegion ?? AppCenter.PlatformGetDataResidencyRegion();
}

private async Task PersistLogAsync(Log log, State state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ public Log()
/// <param name="userId">optional string used for associating logs with
/// users.
/// </param>
public Log(Device device, System.DateTime? timestamp = default(System.DateTime?), System.Guid? sid = default(System.Guid?), string userId = default(string))
/// <param name="dataResidencyRegion">optional string used for specify data residency region.
/// </param>
public Log(Device device, System.DateTime? timestamp = default(System.DateTime?), System.Guid? sid = default(System.Guid?), string userId = default(string), string dataResidencyRegion = default(string))
{
Timestamp = timestamp;
Sid = sid;
UserId = userId;
Device = device;
DataResidencyRegion = dataResidencyRegion;
CustomInit();
}

Expand Down Expand Up @@ -78,6 +81,13 @@ public Log()
[JsonProperty(PropertyName = "device")]
public Device Device { get; set; }

/// <summary>
/// Gets or sets optional string used for specify data residency region.
///
/// </summary>
[JsonProperty(PropertyName = "dataResidencyRegion")]
public string DataResidencyRegion { get; set; }

/// <summary>
/// Validate the object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public LogWithProperties()
/// <param name="userId">optional string used for associating logs with
/// users.
/// </param>
/// <param name="dataResidencyRegion">The data residency region code.</param>
/// <param name="properties">Additional key/value pair parameters.
/// </param>
public LogWithProperties(Device device, System.DateTime? timestamp = default(System.DateTime?), System.Guid? sid = default(System.Guid?), string userId = default(string), IDictionary<string, string> properties = default(IDictionary<string, string>))
: base(device, timestamp, sid, userId)
public LogWithProperties(Device device, System.DateTime? timestamp = default(System.DateTime?), System.Guid? sid = default(System.Guid?), string userId = default(string), string dataResidencyRegion = default(string), IDictionary<string, string> properties = default(IDictionary<string, string>))
: base(device, timestamp, sid, userId, dataResidencyRegion)
{
Properties = properties;
CustomInit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public StartServiceLog()
/// </param>
/// <param name="services">The list of services of the MobileCenter
/// Start API call.</param>
public StartServiceLog(Device device, DateTime? timestamp = default(DateTime?), Guid? sid = default(Guid?), IList<string> services = default(IList<string>))
: base(device, timestamp, sid)
/// <param name="dataResidencyRegion">The data residency region code.</param>
public StartServiceLog(Device device, DateTime? timestamp = default(DateTime?), Guid? sid = default(Guid?), IList<string> services = default(IList<string>), string dataResidencyRegion = default(string))
: base(device, timestamp, sid, null, dataResidencyRegion)
{
Services = services;
}
Expand Down
20 changes: 20 additions & 0 deletions SDK/AppCenter/Microsoft.AppCenter.Shared/AppCenter.Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ public static void SetCountryCode(string countryCode)
PlatformSetCountryCode(countryCode);
}

/// <summary>
/// Sets the data residency region to send to the backend.
/// </summary>
/// <param> name="dataResidencyRegion">The data residency region code.
/// Verify list of supported regions on [link]. Value outside of supported range is treated as ANY.
/// </param>
public static void SetDataResidencyRegion(string dataResidencyRegion)
{
PlatformSetDataResidencyRegion(dataResidencyRegion);
}

/// <summary>
/// Return the data residency region.
/// </summary>
/// <returns>Data residency region code if set, otherwise null.</returns>
public static string GetDataResidencyRegion()
{
return PlatformGetDataResidencyRegion();
}

/// <summary>
/// Set the custom user id.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions SDK/AppCenter/Microsoft.AppCenter/AppCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ static void PlatformSetCountryCode(string countryCode)
{
}

static void PlatformSetDataResidencyRegion(string dataResidencyRegion)
{
}

static string PlatformGetDataResidencyRegion()
{
return null;
}

static bool PlatformConfigured { get; }

static void PlatformConfigure(string appSecret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void InstanceTrackEvent(string name, IDictionary<string, string> propert
if (ValidateName(ref name, type))
{
properties = PropertyValidator.ValidateProperties(properties, $"{type} '{name}'");
var log = new EventLog(null, Guid.NewGuid(), name, null, null, null, properties);
var log = new EventLog(null, Guid.NewGuid(), name, null, null, null, AppCenter.PlatformGetDataResidencyRegion(), properties);
Channel.EnqueueAsync(log);
}
}
Expand Down
Loading

0 comments on commit e79284d

Please sign in to comment.