Skip to content

Commit

Permalink
fix(Sdk): Fixed the ExternalResourceDefinition to implement changes i…
Browse files Browse the repository at this point in the history
…ntroduced by serverlessworkflow/specification#975

Signed-off-by: Charles d'Avernas <charles.davernas@neuroglia.io>
  • Loading branch information
cdavernas committed Aug 21, 2024
1 parent 73a219f commit 6eab8a1
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 50 deletions.
85 changes: 85 additions & 0 deletions src/ServerlessWorkflow.Sdk.Builders/EndpointDefinitionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright © 2024-Present The Serverless Workflow Specification Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"),
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace ServerlessWorkflow.Sdk.Builders;

/// <summary>
/// Represents the default implementation of the <see cref="IEndpointDefinitionBuilder"/> interface
/// </summary>
public class EndpointDefinitionBuilder
: IEndpointDefinitionBuilder
{

/// <summary>
/// Gets/sets the uri that references the external resource
/// </summary>
protected virtual Uri? Uri { get; set; }

/// <summary>
/// Gets/sets a reference to the authentication policy to use
/// </summary>
protected virtual Uri? AuthenticationReference { get; set; }

/// <summary>
/// Gets/sets the authentication policy to use
/// </summary>
protected virtual AuthenticationPolicyDefinition? Authentication { get; set; }

/// <inheritdoc/>
public virtual IEndpointDefinitionBuilder WithUri(Uri uri)
{
ArgumentNullException.ThrowIfNull(uri);
this.Uri = uri;
return this;
}

/// <inheritdoc/>
public virtual IEndpointDefinitionBuilder UseAuthentication(Uri reference)
{
ArgumentNullException.ThrowIfNull(reference);
this.AuthenticationReference = reference;
return this;
}

/// <inheritdoc/>
public virtual IEndpointDefinitionBuilder UseAuthentication(AuthenticationPolicyDefinition authentication)
{
ArgumentNullException.ThrowIfNull(authentication);
this.Authentication = authentication;
return this;
}

/// <inheritdoc/>
public virtual IEndpointDefinitionBuilder UseAuthentication(Action<IAuthenticationPolicyDefinitionBuilder> setup)
{
ArgumentNullException.ThrowIfNull(setup);
var builder = new AuthenticationPolicyDefinitionBuilder();
setup(builder);
this.Authentication = builder.Build();
return this;
}

/// <inheritdoc/>
public virtual EndpointDefinition Build()
{
if (this.Uri == null) throw new NullReferenceException("The uri that references the external resource must be set");
var endpoint = new EndpointDefinition()
{
Uri = this.Uri
};
if (this.AuthenticationReference == null) endpoint.Authentication = new() { Ref = this.AuthenticationReference };
else if (this.Authentication != null) endpoint.Authentication = this.Authentication;
return endpoint;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@ public class ExternalResourceDefinitionBuilder
protected virtual string? Name { get; set; }

/// <summary>
/// Gets/sets the uri that references the external resource
/// Gets/sets the endpoint at which to get the defined resource
/// </summary>
protected virtual Uri? Uri { get; set; }

/// <summary>
/// Gets/sets a reference to the authentication policy to use
/// </summary>
protected virtual Uri? AuthenticationReference { get; set; }

/// <summary>
/// Gets/sets the authentication policy to use
/// </summary>
protected virtual AuthenticationPolicyDefinition? Authentication { get; set; }
protected virtual OneOf<EndpointDefinition, Uri>? Endpoint { get; set; }

/// <inheritdoc/>
public virtual IExternalResourceDefinitionBuilder WithName(string name)
Expand All @@ -48,53 +38,33 @@ public virtual IExternalResourceDefinitionBuilder WithName(string name)
}

/// <inheritdoc/>
public virtual IExternalResourceDefinitionBuilder WithUri(Uri uri)
public virtual IExternalResourceDefinitionBuilder WithEndpoint(OneOf<EndpointDefinition, Uri> endpoint)
{
ArgumentNullException.ThrowIfNull(uri);
this.Uri = uri;
ArgumentNullException.ThrowIfNull(endpoint);
this.Endpoint = endpoint;
return this;
}

/// <inheritdoc/>
public virtual IExternalResourceDefinitionBuilder UseAuthentication(Uri reference)
{
ArgumentNullException.ThrowIfNull(reference);
this.AuthenticationReference = reference;
return this;
}

/// <inheritdoc/>
public virtual IExternalResourceDefinitionBuilder UseAuthentication(AuthenticationPolicyDefinition authentication)
{
ArgumentNullException.ThrowIfNull(authentication);
this.Authentication = authentication;
return this;
}

/// <inheritdoc/>
public virtual IExternalResourceDefinitionBuilder UseAuthentication(Action<IAuthenticationPolicyDefinitionBuilder> setup)
public virtual IExternalResourceDefinitionBuilder WithEndpoint(Action<IEndpointDefinitionBuilder> setup)
{
ArgumentNullException.ThrowIfNull(setup);
var builder = new AuthenticationPolicyDefinitionBuilder();
var builder = new EndpointDefinitionBuilder();
setup(builder);
this.Authentication = builder.Build();
this.Endpoint = builder.Build();
return this;
}

/// <inheritdoc/>
public virtual ExternalResourceDefinition Build()
{
if (this.Uri == null) throw new NullReferenceException("The uri that references the external resource must be set");
var reference = new ExternalResourceDefinition()
if (this.Endpoint == null) throw new NullReferenceException("The endpoint at which to get the defined resource must be set");
var externalResource = new ExternalResourceDefinition()
{
Name = this.Name,
Uri = this.Uri
Endpoint = this.Endpoint
};
if (this.AuthenticationReference == null) reference.Authentication = new() { Ref = this.AuthenticationReference };
else if (this.Authentication != null) reference.Authentication = this.Authentication;
return reference;
return externalResource;
}

EndpointDefinition IEndpointDefinitionBuilder<IExternalResourceDefinitionBuilder>.Build() => this.Build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="ExternalResourceDefinition"/>s
/// </summary>
public interface IExternalResourceDefinitionBuilder
: IEndpointDefinitionBuilder<IExternalResourceDefinitionBuilder>
{

/// <summary>
Expand All @@ -27,10 +26,24 @@ public interface IExternalResourceDefinitionBuilder
/// <returns>The configured <see cref="IExternalResourceDefinitionBuilder"/></returns>
IExternalResourceDefinitionBuilder WithName(string name);

/// <summary>
/// Configures the endpoint at which to get the defined resource
/// </summary>
/// <param name="endpoint">The endpoint at which to get the defined resource</param>
/// <returns>The configured <see cref="IExternalResourceDefinitionBuilder"/></returns>
IExternalResourceDefinitionBuilder WithEndpoint(OneOf<EndpointDefinition, Uri> endpoint);

/// <summary>
/// Configures the endpoint at which to get the defined resource.
/// </summary>
/// <param name="setup">An <see cref="Action{T}"/> used to setup the endpoint at which to get the defined resource.</param>
/// <returns>The configured <see cref="IExternalResourceDefinitionBuilder"/></returns>
IExternalResourceDefinitionBuilder WithEndpoint(Action<IEndpointDefinitionBuilder> setup);

/// <summary>
/// Builds the configured <see cref="ExternalResourceDefinition"/>
/// </summary>
/// <returns>A new <see cref="ExternalResourceDefinition"/></returns>
new ExternalResourceDefinition Build();
ExternalResourceDefinition Build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public override ScriptProcessDefinition Build()
Environment = this.Environment
};
if(this.Source != null) process.Source = this.Source;
else if(this.SourceUri != null) process.Source = new() { Uri = this.SourceUri };
else if(this.SourceUri != null) process.Source = new() { Endpoint = this.SourceUri };
return process;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha2.9</VersionSuffix>
<VersionSuffix>alpha2.10</VersionSuffix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<FileVersion>$(VersionPrefix)</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha2.9</VersionSuffix>
<VersionSuffix>alpha2.10</VersionSuffix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<FileVersion>$(VersionPrefix)</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ namespace ServerlessWorkflow.Sdk.Models;
/// </summary>
[DataContract]
public record ExternalResourceDefinition
: EndpointDefinition
{

/// <summary>
/// Gets/sets the external resource's name, if any
/// </summary>
[Required]
[DataMember(Name = "name", Order = 1), JsonPropertyName("name"), JsonPropertyOrder(1), YamlMember(Alias = "name", Order = 1)]
public virtual string? Name { get; set; }

/// <summary>
/// Gets/sets the endpoint at which to get the defined resource
/// </summary>
[Required]
[DataMember(Name = "endpoint", Order = 2), JsonPropertyName("endpoint"), JsonPropertyOrder(2), YamlMember(Alias = "endpoint", Order = 2)]
public virtual OneOf<EndpointDefinition, Uri> Endpoint { get; set; } = null!;

}
2 changes: 1 addition & 1 deletion src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha2.9</VersionSuffix>
<VersionSuffix>alpha2.10</VersionSuffix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<FileVersion>$(VersionPrefix)</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
Expand Down

0 comments on commit 6eab8a1

Please sign in to comment.