Skip to content

Commit

Permalink
Added LogicMonitorClient.UpdateResourceDataSourceInstanceAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnmbond committed Jan 26, 2025
1 parent 09a306b commit 61c6c08
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 9 deletions.
99 changes: 94 additions & 5 deletions LogicMonitor.Api.Test/Resources/ResourceDataSourceInstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ await LogicMonitorClient
[Fact]
public async Task GetDataPointConfiguration()
{
var device = await GetWindowsResourceAsync(default)
var resource = await GetWindowsResourceAsync(default)
.ConfigureAwait(true);
var deviceDataSources = await LogicMonitorClient.GetAllResourceDataSourcesAsync(device.Id, new Filter<ResourceDataSource>
var resourceDataSources = await LogicMonitorClient.GetAllResourceDataSourcesAsync(resource.Id, new Filter<ResourceDataSource>
{
Skip = 0,
Take = 10,
Expand All @@ -130,16 +130,16 @@ public async Task GetDataPointConfiguration()
]
}, default).ConfigureAwait(true);

var datasourceInstances = await LogicMonitorClient
.GetAllResourceDataSourceInstancesAsync(device.Id, deviceDataSources[0].Id, new Filter<ResourceDataSourceInstance>()
var resourceDataSourceInstances = await LogicMonitorClient
.GetAllResourceDataSourceInstancesAsync(resource.Id, resourceDataSources[0].Id, new Filter<ResourceDataSourceInstance>()
{
Skip = 0,
Properties = [nameof(ResourceDataSourceInstance.Id), nameof(ResourceDataSourceInstance.DisplayName)]
}, default)
.ConfigureAwait(true);

var config = await LogicMonitorClient
.GetResourceDataSourceInstanceDataPointConfigurationsAsync(device.Id, deviceDataSources[0].Id, datasourceInstances[0].Id, default)
.GetResourceDataSourceInstanceDataPointConfigurationsAsync(resource.Id, resourceDataSources[0].Id, resourceDataSourceInstances[0].Id, default)
.ConfigureAwait(true);

config.Should().NotBeEmpty();
Expand Down Expand Up @@ -208,4 +208,93 @@ await LogicMonitorClient
}
}
}

[Fact]
public async Task SetResourceDataSourceInstanceCustomProperty()
{
var resource = await GetWindowsResourceAsync(default)
.ConfigureAwait(true);
var resourceDataSources = await LogicMonitorClient.GetAllResourceDataSourcesAsync(resource.Id, new Filter<ResourceDataSource>
{
Skip = 0,
Take = 10,
Properties =
[
nameof(ResourceDataSource.Id),
]
}, default).ConfigureAwait(true);

var datasourceInstances = await LogicMonitorClient
.GetAllResourceDataSourceInstancesAsync(
resource.Id,
resourceDataSources[0].Id,
new Filter<ResourceDataSourceInstance>()
{
Skip = 0,
}, default)
.ConfigureAwait(true);

var instance = datasourceInstances[0];

var customPropertyName = "test";

// Is it already set?
var customProperty = instance.CustomProperties.FirstOrDefault(cp => cp.Name == customPropertyName);
if (customProperty != null)
{
// Set it to "value1"
customProperty.Value = "value1";
}
else
{
instance.CustomProperties.Add(new EntityProperty
{
Name = customPropertyName,
Value = "value1"
});
}

// Update the instance
await LogicMonitorClient
.UpdateResourceDataSourceInstanceAsync(
resource.Id,
resourceDataSources[0].Id,
instance.Id,
instance,
default)
.ConfigureAwait(true);

// Re-fetch the instance
var refetchedInstance = await LogicMonitorClient
.GetResourceDataSourceInstanceAsync(resource.Id, resourceDataSources[0].Id, instance.Id, default)
.ConfigureAwait(true);

// Check that the custom property was set
customProperty = refetchedInstance.CustomProperties.FirstOrDefault(cp => cp.Name == "test");
customProperty.Should().NotBeNull();
customProperty.Value.Should().Be("value1");

// Set it to something else
customProperty.Value = "value2";

// Update the instance
await LogicMonitorClient
.UpdateResourceDataSourceInstanceAsync(
resource.Id,
resourceDataSources[0].Id,
instance.Id,
refetchedInstance,
default)
.ConfigureAwait(true);

// Re-fetch the instance
refetchedInstance = await LogicMonitorClient
.GetResourceDataSourceInstanceAsync(resource.Id, resourceDataSources[0].Id, instance.Id, default)
.ConfigureAwait(true);

// Check that the custom property was set
customProperty = refetchedInstance.CustomProperties.FirstOrDefault(cp => cp.Name == "test");
customProperty.Should().NotBeNull();
customProperty.Value.Should().Be("value2");
}
}
15 changes: 15 additions & 0 deletions LogicMonitor.Api.Test/Version/PortalVersionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Reflection;

namespace LogicMonitor.Api.Test.Version;

public class PortalVersionTests(ITestOutputHelper iTestOutputHelper, Fixture fixture) : TestWithOutput(iTestOutputHelper, fixture)
Expand All @@ -15,6 +17,19 @@ public async Task GetPortalVersion()
portalVersion.BuildAt.Should().NotBeNull();
portalVersion.Branch.Should().NotBeNull();
portalVersion.ResultKey.Should().NotBeNull();

// These should match the current version of this library
// Get the file version of the LogicMonitor.Api project (not this executing assembly)
var fileVersion = Assembly.GetAssembly(typeof(LogicMonitorClient))?.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version;

// Split this into the module and the version
var split = fileVersion!.Split('.').Select(int.Parse).ToList();

// The module should be the first part
portalVersion.Version.Build.Should().Be(split[0]);

// The version should be the second part
portalVersion.Version.Major.Should().Be(split[1]);
}

[Fact]
Expand Down
6 changes: 6 additions & 0 deletions LogicMonitor.Api/Alerts/Alert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public class Alert : IHasEndpoint
[DataMember(Name = "alertGroupEntityValue")]
public string AlertGroupEntityValue { get; set; } = string.Empty;

/// <summary>
/// The alert query
/// </summary>
[DataMember(Name = "alertQuery")]
public object? AlertQuery { get; set; }

/// <summary>
/// The Alert type
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions LogicMonitor.Api/LogicMonitor.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Panoramic Data Limited</Authors>
<PackageProjectUrl>https://github.com/panoramicdata/LogicMonitor.Api/</PackageProjectUrl>
<Copyright>Copyright © Panoramic Data Limited 2011-2024</Copyright>
<Copyright>Copyright © Panoramic Data Limited 2011-2025</Copyright>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageIcon>Icon.png</PackageIcon>
Expand All @@ -28,7 +28,7 @@

<!-- Update the following before releasing to nuget -->
<PackageReleaseNotes>
Version 213 support, including improved/new Widget DisplaySettings POCOs.
Version 216 support. Added LogicMonitorClient.UpdateResourceDataSourceInstanceAsync().
</PackageReleaseNotes>
<UserSecretsId>57aaa0e7-815d-4065-9339-f3f070bed01e</UserSecretsId>

Expand Down
26 changes: 24 additions & 2 deletions LogicMonitor.Api/LogicMonitorClient_DataSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public async Task<List<ResourceDataSourceInstance>> GetAllResourceDataSourceInst
if (itemsThisTime.Items.Count < filter.Take)
{
return StrictPagingTotalChecking && itemsThisTime.TotalCount != items.Count
? throw new PagingException($"Mismatch between API declared total: {itemsThisTime.TotalCount} and received count: {items.Count}")
? throw new PagingException($"Mismatch between API declared total: {itemsThisTime.TotalCount} and received count: {items.Count}")
: items;
}

Expand Down Expand Up @@ -310,7 +310,7 @@ public async Task<List<ResourceDataSourceInstance>> GetAllResourceDataSourceInst
if (itemsThisTime.Items.Count < filter.Take)
{
return StrictPagingTotalChecking && itemsThisTime.TotalCount != items.Count
? throw new PagingException($"Mismatch between API declared total: {itemsThisTime.TotalCount} and received count: {items.Count}")
? throw new PagingException($"Mismatch between API declared total: {itemsThisTime.TotalCount} and received count: {items.Count}")
: items;
}

Expand Down Expand Up @@ -735,4 +735,26 @@ public Task<Page<DataSourceUpdateReason>> GetDataSourceUpdateReasonAsync(
Filter<DataSourceUpdateReason> filter,
CancellationToken cancellationToken)
=> GetPageAsync(filter, $"setting/datasources/{id}/updatereasons", cancellationToken);

/// <summary>
/// Updates a ResourceDataSourceInstance
/// </summary>
/// <param name="resourceId"></param>
/// <param name="resourceDataSourceId"></param>
/// <param name="resourceDataSourceInstanceId"></param>
/// <param name="resourceDataSourceInstance"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task UpdateResourceDataSourceInstanceAsync(
int resourceId,
int resourceDataSourceId,
int resourceDataSourceInstanceId,
ResourceDataSourceInstance resourceDataSourceInstance,
CancellationToken cancellationToken)
{
await PutAsync(
$"device/devices/{resourceId}/devicedatasources/{resourceDataSourceId}/instances/{resourceDataSourceInstanceId}",
resourceDataSourceInstance,
cancellationToken);
}
}

0 comments on commit 61c6c08

Please sign in to comment.