Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cc/api3 #129

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
image: Visual Studio 2017

# Versioning
version: '1.0.{build}'
version: '1.1.{build}'

assembly_info:
patch: true
Expand Down
15 changes: 11 additions & 4 deletions scripts/Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ $testProjects | ForEach-Object {
$dotnetArgs = @('test', "`"$($_.FullName)`"")
$dotnetArgs += $commonDotnetArgs

$logFileName = "$($_.BaseName)_$(Get-Date -Format 'yyyyMMdd_HHmmss').trx"
# Due to this issue, timestamp is being auto added by dotnet test which breaks AppVeyor from finding tests - https://github.com/Microsoft/vstest/issues/1951
$logBaseName = $_.BaseName
$logFileName = "$($logBaseName).trx" # Depending how above issue is fixed, may need to add this back in _$(Get-Date -Format 'yyyyMMdd_HHmmss')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation.

if($Logger) {
$dotnetArgs += @('--logger', "`"$Logger;LogFileName=$logFileName`"")
}
Expand All @@ -115,9 +117,14 @@ $testProjects | ForEach-Object {
$exitCode = $LASTEXITCODE # Save exit code in case upload results fails

if($ResultsDirectory -and $Logger -and $UploadResultsToAppVeyor -and $env:APPVEYOR_JOB_ID) {
$resultsLog = Join-Path -Path $ResultsDirectory -ChildPath $logFileName
if(Test-Path -Path $resultsLog) {
Write-Host "Publishing test results..."
#Write-Host "ResultsDirectory: $ResultsDirectory"
#Write-Host "LogFileName: $logFileName"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section of code is doubly indented. I would recommend removing these commented out console logs if you feel they aren't needed anymore.

$resultsFileName = Join-Path -Path $ResultsDirectory -ChildPath "$($logBaseName)*.trx"
$resultsLog = Get-ChildItem -Path $resultsFileName | Select-Object -First 1 | % { $_.FullName }
if($resultsLog -and (Test-Path -Path $resultsLog)) {
# https://www.appveyor.com/docs/running-tests/
# https://www.appveyor.com/docs/running-tests/#pushing-real-time-test-results-to-build-console
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation.

try {
Write-Output "Uploading file to AppVeyor (job ID $($env:APPVEYOR_JOB_ID)): $resultsLog"
$wc = New-Object 'System.Net.WebClient'
Expand All @@ -128,7 +135,7 @@ $testProjects | ForEach-Object {
}
}
else {
Write-Warning "No results file found at: $resultsLog"
Write-Warning "No results file found at: $resultsFileName"
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Common/Commands.Common.Test/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public static void AssertExpectedUnitTestResults(object expectedResponse, Mock<I
client.VerifyAll();
}

public static void AssertExpectedNoOutputForUnitTestResults(Mock<IPowerBIApiClient> client, TestPowerBICmdletInitFactory initFactory)
{
Assert.IsFalse(initFactory.Logger.ErrorRecords.Any());
var results = initFactory.Logger.Output?.ToList();
if(results != null)
{
Assert.AreEqual(0, results.Count, "No output results were expected");
}

client.VerifyAll();
}

public static string GetRandomString()
{
return Path.GetRandomFileName().Replace(".", "");
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Common.Api/Common.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -9,8 +9,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.7" />
<PackageReference Include="Microsoft.PowerBI.Api" Version="2.0.121817003" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Microsoft.PowerBI.Api" Version="3.1.0-beta" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
</ItemGroup>

Expand Down
31 changes: 13 additions & 18 deletions src/Common/Common.Api/Datasets/Dataset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.PowerBI.Common.Api.Helpers;

namespace Microsoft.PowerBI.Common.Api.Datasets
{
Expand All @@ -14,7 +15,7 @@ public class Dataset
public Guid Id { get; set; }
public string Name { get; set; }
public string ConfiguredBy { get; set; }
public string DefaultRetentionPolicy { get; set; }
public DefaultRetentionPolicy? DefaultRetentionPolicy { get; set; }
public bool AddRowsApiEnabled { get; set; }
public IEnumerable<Table> Tables { get; set; }
public string WebUrl { get; set; }
Expand All @@ -35,46 +36,40 @@ public static implicit operator Dataset(PowerBI.Api.V2.Models.Dataset dataset)

return new Dataset
{
Id = new Guid(dataset.Id),
Id = dataset.Id,
Name = dataset.Name,
ConfiguredBy = dataset.ConfiguredBy,
DefaultRetentionPolicy = dataset.DefaultRetentionPolicy,
AddRowsApiEnabled = dataset.AddRowsAPIEnabled.GetValueOrDefault(),
Tables = dataset.Tables?.Select(t => (Table)t),
WebUrl = dataset.WebUrl,
Relationships = dataset.Relationships?.Select(r => (Relationship)r),
Datasources = dataset.Datasources?.Select(d => (Datasource)d),
DefaultMode = ConvertDefaultMode(dataset.DefaultMode),
IsRefreshable = dataset.IsRefreshable.GetValueOrDefault(),
IsEffectiveIdentityRequired = dataset.IsEffectiveIdentityRequired.GetValueOrDefault(),
IsOnPremGatewayRequired = dataset.IsOnPremGatewayRequired.GetValueOrDefault()
};
}

public static PowerBI.Api.V2.Models.Dataset ConvertToDatasetV2Model(Dataset dataset)
public static PowerBI.Api.V2.Models.CreateDatasetRequest ConvertToDatasetRequest(Dataset dataset)
{
if (dataset == null)
{
return null;
}

return new PowerBI.Api.V2.Models.Dataset
return new PowerBI.Api.V2.Models.CreateDatasetRequest
{
Id = dataset.Id == Guid.Empty ? null : dataset.Id.ToString(),
Name = dataset.Name,
Tables = dataset.Tables?.Select(t => (PowerBI.Api.V2.Models.Table)t).ToList(),
Datasources = dataset.Datasources?.Select(d => (PowerBI.Api.V2.Models.Datasource)d).ToList(),
Relationships = dataset.Relationships?.Select(r => (PowerBI.Api.V2.Models.Relationship)r).ToList(),
DefaultMode = EnumTypeConverter.ConvertTo<PowerBI.Api.V2.Models.DatasetMode, DatasetMode>(dataset.DefaultMode)
};
}

private static DatasetMode? ConvertDefaultMode(string defaultMode)
{
if(string.IsNullOrEmpty(defaultMode))
{
return null;
}
}

return (DatasetMode)Enum.Parse(typeof(DatasetMode), defaultMode, true);
}
public enum DefaultRetentionPolicy
{
None,
BasicFIFO
}

public enum DatasetMode
Expand Down
63 changes: 39 additions & 24 deletions src/Common/Common.Api/Datasets/DatasetsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Management.Automation;
using Microsoft.PowerBI.Api.V2;
using Microsoft.PowerBI.Common.Api.Helpers;

namespace Microsoft.PowerBI.Common.Api.Datasets
{
Expand All @@ -21,8 +22,8 @@ public DatasetsClient(IPowerBIClient client) : base(client)
public object AddDataset(Dataset dataset, Guid? workspaceId = default)
{
var result = workspaceId.HasValue && workspaceId.Value != default ?
this.Client.Datasets.PostDatasetInGroup(groupId: workspaceId.Value.ToString(), dataset: Dataset.ConvertToDatasetV2Model(dataset)) :
this.Client.Datasets.PostDataset(dataset: Dataset.ConvertToDatasetV2Model(dataset));
this.Client.Datasets.PostDatasetInGroup(groupId: workspaceId.Value, dataset: Dataset.ConvertToDatasetRequest(dataset), EnumTypeConverter.ConvertTo<Microsoft.PowerBI.Api.V2.Models.DefaultRetentionPolicy, DefaultRetentionPolicy>(dataset.DefaultRetentionPolicy)) :
this.Client.Datasets.PostDataset(dataset: Dataset.ConvertToDatasetRequest(dataset), EnumTypeConverter.ConvertTo<Microsoft.PowerBI.Api.V2.Models.DefaultRetentionPolicy, DefaultRetentionPolicy>(dataset.DefaultRetentionPolicy));

return result;
}
Expand All @@ -34,7 +35,7 @@ public IEnumerable<Dataset> GetDatasets()

public IEnumerable<Dataset> GetDatasetsForWorkspace(Guid workspaceId)
{
return this.Client.Datasets.GetDatasets(groupId: workspaceId.ToString()).Value?.Select(x => (Dataset)x);
return this.Client.Datasets.GetDatasets(groupId: workspaceId).Value?.Select(x => (Dataset)x);
}

public IEnumerable<Dataset> GetDatasetsAsAdmin(string filter = null, int? top = null, int? skip = null)
Expand All @@ -44,40 +45,48 @@ public IEnumerable<Dataset> GetDatasetsAsAdmin(string filter = null, int? top =

public IEnumerable<Dataset> GetDatasetsAsAdminForWorkspace(Guid workspaceId, string filter = null, int? top = null, int? skip = null)
{
return this.Client.Datasets.GetDatasetsAsAdmin(groupId: workspaceId.ToString(), filter: filter, top: top, skip: skip).Value?.Select(x => (Dataset)x);
return this.Client.Datasets.GetDatasetsAsAdmin(groupId: workspaceId, filter: filter, top: top, skip: skip).Value?.Select(x => (Dataset)x);
}

public IEnumerable<Datasource> GetDatasources(Guid datasetId, Guid? workspaceId = default)
{
var result = workspaceId.HasValue && workspaceId.Value != default ?
this.Client.Datasets.GetDatasources(groupId: workspaceId.Value.ToString(), datasetKey: datasetId.ToString()) :
this.Client.Datasets.GetDatasources(datasetKey: datasetId.ToString());
this.Client.Datasets.GetDatasources(groupId: workspaceId.Value, datasetId: datasetId) :
this.Client.Datasets.GetDatasources(datasetId: datasetId);
return result.Value?.Select(x => (Datasource)x);

}

public IEnumerable<Datasource> GetDatasourcesAsAdmin(Guid datasetId)
{
return this.Client.Datasets.GetDatasourcesAsAdmin(datasetId.ToString()).Value?.Select(x => (Datasource)x);
return this.Client.Datasets.GetDatasourcesAsAdmin(datasetId).Value?.Select(x => (Datasource)x);
}

public IEnumerable<Table> GetTables(Guid datasetId, Guid? workspaceId = default)
{
var result = workspaceId.HasValue && workspaceId.Value != default ?
this.Client.Datasets.GetTables(groupId: workspaceId.Value.ToString(), datasetKey: datasetId.ToString()) :
this.Client.Datasets.GetTables(datasetKey: datasetId.ToString());
this.Client.Datasets.GetTables(groupId: workspaceId.Value, datasetId: datasetId) :
this.Client.Datasets.GetTables(datasetId: datasetId);
return result.Value?.Select(x => (Table)x);
}

public Table UpdateTable(Table table, Guid datasetId, Guid? workspaceId = null)
{
var result = workspaceId.HasValue && workspaceId.Value != default ?
this.Client.Datasets.PutTableInGroup(groupId: workspaceId.Value.ToString(), datasetKey: datasetId.ToString(), tableName: table.Name, (Microsoft.PowerBI.Api.V2.Models.Table)table) :
this.Client.Datasets.PutTable(datasetKey: datasetId.ToString(), tableName: table.Name, (Microsoft.PowerBI.Api.V2.Models.Table)table);
return result as Table;
this.Client.Datasets.PutTableInGroup(groupId: workspaceId.Value, datasetId: datasetId, tableName: table.Name, (Microsoft.PowerBI.Api.V2.Models.Table)table) :
this.Client.Datasets.PutTable(datasetId: datasetId, tableName: table.Name, (Microsoft.PowerBI.Api.V2.Models.Table)table);

if (result != null)
{
return (Table)result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could cast using as instead. I think that would help you avoid the if-else return null block.

}
else
{
return null;
}
}

public object AddRows(string datasetId, string tableName, List<PSObject> rows, Guid? workspaceId = default)
public void AddRows(Guid datasetId, string tableName, List<PSObject> rows, Guid? workspaceId = null)
{
var hashRows = new List<Hashtable>();
foreach (var row in rows)
Expand All @@ -99,21 +108,27 @@ public object AddRows(string datasetId, string tableName, List<PSObject> rows, G
}
hashRows.Add(hashtable);
}
var result = workspaceId.HasValue && workspaceId.Value != default ?
this.Client.Datasets.PostRowsInGroup(groupId: workspaceId.Value.ToString(), datasetKey: datasetId, tableName: tableName, requestMessage: hashRows) :
this.Client.Datasets.PostRows(datasetKey: datasetId, tableName: tableName, requestMessage: hashRows);

return result;
if(workspaceId.HasValue && workspaceId.Value != Guid.Empty)
{
this.Client.Datasets.PostRowsInGroup(groupId: workspaceId.Value, datasetId: datasetId, tableName: tableName, requestMessage: hashRows);
}
else
{
this.Client.Datasets.PostRows(datasetId: datasetId, tableName: tableName, requestMessage: hashRows);
}
}

public object DeleteRows(string datasetId, string tableName, Guid? workspaceId = default)
public void DeleteRows(Guid datasetId, string tableName, Guid? workspaceId = default)
{

var result = workspaceId.HasValue && workspaceId.Value != default ?
this.Client.Datasets.DeleteRowsInGroup(groupId: workspaceId.Value.ToString(), datasetKey: datasetId, tableName: tableName) :
this.Client.Datasets.DeleteRows(datasetKey: datasetId, tableName: tableName);

return result;
if(workspaceId.HasValue && workspaceId.Value != default)
{
this.Client.Datasets.DeleteRowsInGroup(groupId: workspaceId.Value, datasetId: datasetId, tableName: tableName);
}
else
{
this.Client.Datasets.DeleteRows(datasetId: datasetId, tableName: tableName);
}
}
}
}
6 changes: 4 additions & 2 deletions src/Common/Common.Api/Datasets/Datasource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Licensed under the MIT License.
*/

using System;

namespace Microsoft.PowerBI.Common.Api.Datasets
{
public class Datasource
Expand All @@ -11,8 +13,8 @@ public class Datasource
public string ConnectionString { get; set; }
public string DatasourceType { get; set; }
public DatasourceConnectionDetails ConnectionDetails { get; set; }
public string GatewayId { get; set; }
public string DatasourceId { get; set; }
public Guid? GatewayId { get; set; }
public Guid? DatasourceId { get; set; }

public static implicit operator Datasource(PowerBI.Api.V2.Models.Datasource datasource)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Common.Api/Datasets/IDatasetsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IDatasetsClient

Table UpdateTable(Table table, Guid datasetId, Guid? workspaceId = default);

object AddRows(string datasetId, string tableName, List<PSObject> rows, Guid? workspaceId = default);
object DeleteRows(string datasetId, string tableName, Guid? workspaceId = default);
void AddRows(Guid datasetId, string tableName, List<PSObject> rows, Guid? workspaceId = default);
void DeleteRows(Guid datasetId, string tableName, Guid? workspaceId = default);
}
}
20 changes: 5 additions & 15 deletions src/Common/Common.Api/Datasets/Relationship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
*/

using System;
using Microsoft.PowerBI.Common.Api.Helpers;

namespace Microsoft.PowerBI.Common.Api.Datasets
{
public class Relationship
{
public string Name { get; set; }
public CrossFilteringBehaviorEnum CrossFilteringBehavior { get; set; }
public CrossFilteringBehaviorEnum? CrossFilteringBehavior { get; set; }
public string FromTable { get; set; }
public string FromColumn { get; set; }
public string ToTable { get; set; }
Expand All @@ -26,7 +27,7 @@ public static implicit operator Relationship(PowerBI.Api.V2.Models.Relationship
return new Relationship
{
Name = relationship.Name,
CrossFilteringBehavior = ConvertCrossFilteringBehavior(relationship.CrossFilteringBehavior),
CrossFilteringBehavior = EnumTypeConverter.ConvertTo<CrossFilteringBehaviorEnum, PowerBI.Api.V2.Models.CrossFilteringBehavior>(relationship.CrossFilteringBehavior),
FromTable = relationship.FromTable,
FromColumn = relationship.FromColumn,
ToTable = relationship.ToTable,
Expand All @@ -44,30 +45,19 @@ public static implicit operator PowerBI.Api.V2.Models.Relationship(Relationship
return new PowerBI.Api.V2.Models.Relationship
{
Name = relationship.Name,
CrossFilteringBehavior = (PowerBI.Api.V2.Models.CrossFilteringBehaviorEnum)Enum.Parse(typeof(PowerBI.Api.V2.Models.CrossFilteringBehaviorEnum), relationship.CrossFilteringBehavior.ToString(), true),
CrossFilteringBehavior = EnumTypeConverter.ConvertTo<PowerBI.Api.V2.Models.CrossFilteringBehavior, CrossFilteringBehaviorEnum>(relationship.CrossFilteringBehavior),
FromTable = relationship.FromTable,
FromColumn = relationship.FromColumn,
ToTable = relationship.ToTable,
ToColumn = relationship.ToColumn
};
}

private static CrossFilteringBehaviorEnum ConvertCrossFilteringBehavior(PowerBI.Api.V2.Models.CrossFilteringBehaviorEnum? crossFilteringBehavior)
{
if(crossFilteringBehavior == null)
{
return CrossFilteringBehaviorEnum.NotAvailable;
}

return (CrossFilteringBehaviorEnum)Enum.Parse(typeof(CrossFilteringBehaviorEnum), crossFilteringBehavior.Value.ToString(), true);
}
}

public enum CrossFilteringBehaviorEnum
{
OneDirection,
BothDirections,
Automatic,
NotAvailable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional to remove NotAvailable from here?

Automatic
}
}
Loading