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

Added remove dataset command #345

Open
wants to merge 1 commit into
base: dev
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
17 changes: 17 additions & 0 deletions src/Common/Common.Api/Datasets/DatasetsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ public void PatchDataset(Guid datasetId, PatchDatasetRequest patchDatasetRequest
}
}

public object DeleteDataset(Guid datasetId, Guid? workspaceId = default)
{
if (workspaceId.HasValue && workspaceId.Value != default)
{
return this.Client.Datasets.DeleteDatasetByIdInGroup(groupId: workspaceId.Value.ToString(), datasetKey: datasetId.ToString());
}
else
{
return this.Client.Datasets.DeleteDatasetById(datasetKey: datasetId.ToString());
}
}

public object DeleteDataset(Guid datasetId)
{
return this.Client.Datasets.DeleteDatasetById(datasetKey: datasetId.ToString());
}

public IEnumerable<Datasource> GetDatasources(Guid datasetId, Guid? workspaceId = default)
{
var result = workspaceId.HasValue && workspaceId.Value != default ?
Expand Down
3 changes: 3 additions & 0 deletions src/Common/Common.Api/Datasets/IDatasetsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public interface IDatasetsClient

void PatchDataset(Guid datasetId, PatchDatasetRequest patchDatasetRequest, Guid? workspaceId = default);

object DeleteDataset(Guid datasetId);
object DeleteDataset(Guid datasetId, Guid? workspaceId = default);

IEnumerable<Datasource> GetDatasources(Guid datasetId, Guid? workspaceId = default);
IEnumerable<Datasource> GetDatasourcesAsAdmin(Guid datasetId);

Expand Down
66 changes: 66 additions & 0 deletions src/Modules/Data/Commands.Data.Test/RemovePowerBIDatasetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.PowerBI.Commands.Common.Test;
using Microsoft.PowerBI.Commands.Datasets;
using Microsoft.PowerBI.Commands.Profile.Test;
using Microsoft.PowerBI.Common.Abstractions;
using Microsoft.PowerBI.Common.Api;
using Microsoft.PowerBI.Common.Api.Datasets;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Microsoft.PowerBI.Commands.Data.Test
{
[TestClass]
public class RemovePowerBIDatasetTests
{
[TestMethod]
[TestCategory("Interactive")]
[TestCategory("SkipWhenLiveUnitTesting")] // Ignore for Live Unit Testing
public void EndToEndRemovePowerBIDataset()
{
using (var ps = System.Management.Automation.PowerShell.Create())
{
ProfileTestUtilities.ConnectToPowerBI(ps, PowerBIEnvironmentType.Public); // If login is needed
ps.AddCommand(new CmdletInfo($"{RemovePowerBIDataset.CmdletVerb}-{RemovePowerBIDataset.CmdletName}", typeof(RemovePowerBIDataset)));
ps.AddParameter("Id", "fce8abb5-192b-4be2-b75e-b43bb93d8943");
ps.AddParameter("WorkspaceId", "kjsdfjs;sf");
var result = ps.Invoke();

// Add asserts to verify

TestUtilities.AssertNoCmdletErrors(ps);
}
}

[TestMethod]
public void RemovePowerBIDatasetTest()
{
// Arrange
var datasetID = Guid.NewGuid();
object expectedResponse = null;
var client = new Mock<IPowerBIApiClient>();
client.Setup(x => x.Datasets
.DeleteDataset(datasetID))
.Returns(expectedResponse);
var initFactory = new TestPowerBICmdletInitFactory(client.Object);
var cmdlet = new RemovePowerBIDataset(initFactory)
{
Id = datasetID
};

// Act
cmdlet.InvokePowerBICmdlet();

// Assert
TestUtilities.AssertExpectedUnitTestResults(expectedResponse, client, initFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ FormatsToProcess = @('Microsoft.PowerBI.Commands.Data.format.ps1xml')
FunctionsToExport = '*'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = 'Add-PowerBIDataset','Set-PowerBITable','New-PowerBIDataset','New-PowerBITable','New-PowerBIColumn','Get-PowerBIDataset','Get-PowerBIDatasource','Get-PowerBITable','Add-PowerBIRow','Remove-PowerBIRow','Set-PowerBIDataset', 'Get-PowerBIDataflow','Export-PowerBIDataflow','Get-PowerBIDataflowDatasource'
CmdletsToExport = 'Add-PowerBIDataset','Set-PowerBITable','New-PowerBIDataset','New-PowerBITable','New-PowerBIColumn','Get-PowerBIDataset','Get-PowerBIDatasource','Get-PowerBITable','Add-PowerBIRow','Remove-PowerBIRow','Set-PowerBIDataset','Remove-PowerBIDataset', 'Get-PowerBIDataflow','Export-PowerBIDataflow','Get-PowerBIDataflowDatasource'

# Variables to export from this module
VariablesToExport = '*'
Expand Down
72 changes: 72 additions & 0 deletions src/Modules/Data/Commands.Data/RemovePowerBIDataset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

using System;
using System.Management.Automation;
using Microsoft.PowerBI.Common.Api.Workspaces;
using Microsoft.PowerBI.Common.Client;

namespace Microsoft.PowerBI.Commands.Datasets
{
[Cmdlet(CmdletVerb, CmdletName, DefaultParameterSetName = MyWorkspaceParameterSetName)]
public class RemovePowerBIDataset : PowerBIClientCmdlet
{
public const string CmdletName = "PowerBIDataset";
public const string CmdletVerb = VerbsCommon.Remove;

#region ParameterSets

private const string MyWorkspaceParameterSetName = "MyWorkspace";
private const string WorkspaceIdParameterSetName = "WorkspaceId";
private const string WorkspaceParameterSetName = "Workspace";

#endregion

#region ParameterSets

[Parameter(Mandatory = true)]
[Alias("DatasetId")]
public Guid Id { get; set; }

[Parameter(Mandatory = true, ParameterSetName = WorkspaceIdParameterSetName)]
[Alias("GroupId")]
public Guid WorkspaceId { get; set; }

[Parameter(Mandatory = true, ParameterSetName = WorkspaceParameterSetName)]
[Alias("Group")]
public Workspace Workspace { get; set; }

#endregion

#region Constructors
public RemovePowerBIDataset() : base() { }

public RemovePowerBIDataset(IPowerBIClientCmdletInitFactory init) : base(init) { }
#endregion

public override void ExecuteCmdlet()
{
if (this.Workspace != default)
{
this.WorkspaceId = this.Workspace.Id;
}

using (var client = this.CreateClient())
{
object result = null;
if (this.WorkspaceId != default)
{
result = client.Datasets.DeleteDataset(this.WorkspaceId, this.Id);
}
else
{
result = client.Datasets.DeleteDataset(this.Id);
}

this.Logger.WriteObject(result, true);
}
}
}
}