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

Add tags to identify read-only resources #805

Merged
merged 12 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
33 changes: 30 additions & 3 deletions src/Bicep.Types.UnitTests/TypeSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Azure.Bicep.Types.Concrete;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -69,12 +70,11 @@ public void Different_types_can_be_serialized_and_deserialized()
var intType = factory.Create(() => new BuiltInType(BuiltInTypeKind.Int));
var objectType = factory.Create(() => new ObjectType("steven", new Dictionary<string, ObjectProperty>(), null));
var arrayType = factory.Create(() => new ArrayType(factory.GetReference(objectType)));
var resourceType = factory.Create(() => new ResourceType("gerrard", ScopeType.ResourceGroup, factory.GetReference(objectType)));
var resourceType = factory.Create(() => new ResourceType("gerrard", ScopeType.ResourceGroup|ScopeType.Tenant, ScopeType.Tenant, factory.GetReference(objectType), ResourceFlags.None));
var unionType = factory.Create(() => new UnionType(new [] { factory.GetReference(intType), factory.GetReference(objectType) }));
var stringLiteralType = factory.Create(() => new StringLiteralType("abcdef"));
var discriminatedObjectType = factory.Create(() => new DiscriminatedObjectType("disctest", "disctest", new Dictionary<string, ObjectProperty>(), new Dictionary<string, ITypeReference>()));
var resourceFunctionType = factory.Create(() => new ResourceFunctionType("listTest", "zona", "2020-01-01", factory.GetReference(objectType), factory.GetReference(objectType)));

var serialized = TypeSerializer.Serialize(factory.GetTypes());
var deserialized = TypeSerializer.Deserialize(serialized);

Expand All @@ -91,11 +91,38 @@ public void Different_types_can_be_serialized_and_deserialized()
((ObjectType)deserialized[1]).Name.Should().Be(objectType.Name);
((ArrayType)deserialized[2]).ItemType!.Type.Should().Be(deserialized[1]);
((ResourceType)deserialized[3]).Name.Should().Be(resourceType.Name);
((ResourceType)deserialized[3]).Flags.Should().Be(resourceType.Flags);
((ResourceType)deserialized[3]).ReadOnlyScopes.HasValue.Should().Be(true);
((ResourceType)deserialized[3]).ReadOnlyScopes.Should().Be(resourceType.ReadOnlyScopes);
((UnionType)deserialized[4]).Elements![0].Type.Should().Be(deserialized[0]);
((UnionType)deserialized[4]).Elements![1].Type.Should().Be(deserialized[1]);
((StringLiteralType)deserialized[5]).Value.Should().Be(stringLiteralType.Value);
((DiscriminatedObjectType)deserialized[6]).Name.Should().Be(discriminatedObjectType.Name);
((ResourceFunctionType)deserialized[7]).Name.Should().Be(resourceFunctionType.Name);
}

[TestMethod]
public void Resources_without_flags_or_readonly_scopes_can_be_deserialized()
{
var factory = new TypeFactory(Enumerable.Empty<TypeBase>());
var objectType = factory.Create(() => new ObjectType("steven", new Dictionary<string, ObjectProperty>(), null));
var resourceType = factory.Create(() => new ResourceType("gerrard", ScopeType.ResourceGroup|ScopeType.Tenant, ScopeType.Tenant, factory.GetReference(objectType), ResourceFlags.ReadOnly));
var serialized = TypeSerializer.Serialize(factory.GetTypes());

var deserializedNode = JsonNode.Parse(serialized)!;
deserializedNode.AsArray()[1]?.AsObject()["4"]?.AsObject().Remove("Flags").Should().BeTrue();
deserializedNode.AsArray()[1]?.AsObject()["4"]?.AsObject().Remove("ReadOnlyScopes").Should().BeTrue();
serialized = deserializedNode.ToJsonString();

var deserialized = TypeSerializer.Deserialize(serialized);

deserialized[0].Should().BeOfType<ObjectType>();
deserialized[1].Should().BeOfType<ResourceType>();

((ObjectType)deserialized[0]).Name.Should().Be(objectType.Name);
((ResourceType)deserialized[1]).Name.Should().Be(resourceType.Name);
((ResourceType)deserialized[1]).Flags.Should().Be(ResourceFlags.None);
((ResourceType)deserialized[1]).ReadOnlyScopes.HasValue.Should().Be(false);
}
}
}
}
18 changes: 16 additions & 2 deletions src/Bicep.Types/Concrete/ResourceType.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;

namespace Azure.Bicep.Types.Concrete
{
[Flags]
public enum ResourceFlags
{
None = 0,
ReadOnly = 1 << 0,
}

public class ResourceType : TypeBase
{
public ResourceType(string name, ScopeType scopeType, ITypeReference body)
public ResourceType(string name, ScopeType scopeType, ScopeType? readOnlyScopes, ITypeReference body, ResourceFlags flags)
{
Name = name;
ScopeType = scopeType;
ReadOnlyScopes = readOnlyScopes;
Body = body;
Flags = flags;
}

public string Name { get; set; }

public ScopeType ScopeType { get; set; }

public ScopeType? ReadOnlyScopes { get; set; }

public ITypeReference Body { get; set; }

public ResourceFlags Flags { get; set; }
}
}
}
Loading