Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
phspies committed May 16, 2022
1 parent c6f9b89 commit ddd32a3
Show file tree
Hide file tree
Showing 2,203 changed files with 234,542 additions and 0 deletions.
997 changes: 997 additions & 0 deletions SDKGenerator/Program.cs

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions SDKGenerator/SDKGenerator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="nsxt_policyClient.orig" />
<None Remove="vcenter.json" />
</ItemGroup>

<ItemGroup>
<Content Include="vcenter.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CaseExtensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="6.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NJsonSchema" Version="10.6.10" />
<PackageReference Include="NSwag.ApiDescription.Client" Version="13.15.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSwag.Core" Version="13.15.10" />
<PackageReference Include="Nustache" Version="1.16.0.10" />
<PackageReference Include="System.Management.Automation" Version="7.2.3" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions SDKGenerator/Templates/EnumModel.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.Serialization;

namespace vspheresdk.{{rootmodule}}.Models.Enums
{
/// <summary>
/// {{ToXmlDoc currentEnum.description}}
/// </summary>
public enum {{currentEnum.EnumName}}
{
{{#currentEnum.Values}}
[EnumMember(Value = "{{JsonEnumValue}}")]
{{EnumValue}} = {{EnumIndex}},
{{/currentEnum.Values}}
}
}
30 changes: 30 additions & 0 deletions SDKGenerator/Templates/Model.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.ComponentModel;
using NJsonSchema.Converters;
using Newtonsoft.Json.Converters;
using vspheresdk.{{rootmodule}}.Models.Enums;

namespace vspheresdk.{{rootmodule}}.Models
{
public class {{clz}} {{referenced}}
{
{{#properties.Values}}
/// <summary>
/// {{ToXmlDoc description "wrap"}}
/// </summary>
/// </summary>
{{#if isRequired}}
[JsonProperty(PropertyName = "{{name}}", Required = Required.AllowNull)]
{{#else}}
[JsonProperty(PropertyName = "{{name}}")]
{{/if}}
{{#IsEnumeration}}
[JsonConverter(typeof(StringEnumConverter))]
{{/IsEnumeration}}
public {{GetDotNetType . name clz}} {{GetDotNetName name "field"}} { get; set; }
{{/properties.Values}}
}
}
57 changes: 57 additions & 0 deletions SDKGenerator/Templates/Module.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Newtonsoft.Json;
using System.Net;
using vspheresdk;
using vspheresdk.{{rootmodule}}.Models;

namespace vspheresdk.{{rootmodule}}.Modules
{
public class {{module}}Module
{
private RestClient restClient;
private int retry;
private int timeout;
private CancellationToken cancellationToken;

public {{module}}Module(RestClient Client, CancellationToken _cancellationToken, int _timeout, int _retry)
{
restClient = Client;
retry = _retry;
timeout = _timeout;
cancellationToken = _cancellationToken;
}
{{#operations}}
public async {{GetOperationReturnType . "full"}} {{GetMethodName .}}Async({{GetOperationPathParams .}})
{
{{#Item4.Parameters}}
{{#isRequired}}
ArgumentNullException.ThrowIfNull({{GetParameterPascalCase name}}, "{{GetParameterPascalCase name}} cannot be null");
{{/isRequired}}
{{/Item4.Parameters}}
StringBuilder {{GetMethodName .}}ServiceURL = new StringBuilder("{{ GetServiceUrl operations.Current }}");
var request = new RestRequest
{
RequestFormat = DataFormat.Json,
Method = Method.{{GetOperationHttpMethod .}}
};
{{#Item4.Parameters}}
{{GetOperationPathInjectionCode . }}
{{/Item4.Parameters}}
request.Resource = {{GetMethodName .}}ServiceURL.ToString();
RestResponse{{GetOperationReturnType . "bare"}} response = await restClient.ExecuteTaskAsyncWithPolicy{{GetOperationReturnType . "bare"}}(request, cancellationToken, timeout, retry);
if (response.StatusCode != HttpStatusCode.OK)
{
var message = "HTTP {{GetOperationHttpMethod .}} operation to " + {{GetMethodName .}}ServiceURL.ToString() + " did not complete successfull";
throw new vSphereException(message, (int)response.StatusCode, response.Content, response.Headers, response.ErrorException);
}
{{GetOperationReturnType . "return"}}
}
{{/operations}}
}
}
49 changes: 49 additions & 0 deletions SDKGenerator/Templates/PSCmdLet.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <auto-generated>
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>

using nsxtapi;
using vspheresdk.Models;
using vspheresdk.Powershell;
using System.Management.Automation;
using Newtonsoft.Json;


namespace nsxtcli.CmdLet
{
{{#operations}}
{{#each value}}
[Cmdlet({{GetPSVerb . module}})]
{{GetOperationReturnType . "cmdlet"}}
public class {{module}}{{GetMethodName .}}CmdLet : Cmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
[ValidateNotNullOrEmpty]
public CredentialParmeters Credentials { get; set; }
{{#each value.parameters}}
[Parameter(Mandatory = {{GetLowerCase isRequired}})]
public {{GetPSValueDefinition . }} { get; set; }
{{/each}}
[Parameter(Mandatory = false)]
public bool PrettyJson { get; set; }

protected override void ProcessRecord()
{
NSXTClient _client = new NSXTClient(Credentials._host, Credentials._username, Credentials._password, Credentials._remoteCertificateValidation);
{{GetOperationReturnType . "cmdletreturn" module}}
if (returnobject != null)
{
if (PrettyJson)
{
WriteObject(JsonConvert.SerializeObject(returnobject, Formatting.Indented));
}
else
{
WriteObject(returnobject);
}
}
}
}
{{/each}}
{{/operations}}
}
17 changes: 17 additions & 0 deletions SDKGenerator/Templates/Response.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// <auto-generated>
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
using vspheresdk.ManagerModels;

namespace vspheresdk.Models
{
{{#each responses}}
/// <summary>
/// {{ToXmlDoc value.description}}
/// </summary>
[NSXTProperty(Description: @"{{ToXmlDoc value.description}}")]
public class NSXT{{GetResponseName .}}Type : {{GetResponseTypeName value}}
{
}
{{/each}}
}
85 changes: 85 additions & 0 deletions SDKGenerator/Templates/RootModule.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Serializers.NewtonsoftJson;
using System;
using System.Linq;
using vspheresdk.Authentication;

namespace vspheresdk
{
public class vSphereClient
{
private RestClient tokenRestClient { get; set; }
private RestClient sessionRestClient { get; set; }
private CancellationToken cancellationToken;
private int timeout;
private int retry;
public LoginResponseType LoginResponseDetails;

public vSphereClient(string Host, string Username, string Password, bool? RemoteCertificateValidation = true, JsonSerializerSettings? DefaultSerializationSettings = null, CancellationToken _cancellationToken = default(CancellationToken), int Port = 443, int _timeout = 100, int _retry = 2, string _defaultXAviVerion = null)
{
cancellationToken = _cancellationToken;
timeout = _timeout;
retry = _retry;


var tokenUri = new UriBuilder(Host)
{
Scheme = Uri.UriSchemeHttps,
Port = Port,
}.Uri;
var sessionUri = new UriBuilder(Host)
{
Scheme = Uri.UriSchemeHttps,
Port = Port,
Path = ""
}.Uri;
var tokenRestOptions = new RestClientOptions() { BaseUrl = tokenUri };
var sessionRestOptions = new RestClientOptions() { BaseUrl = sessionUri };
if (!RemoteCertificateValidation ?? false)
{
tokenRestOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
sessionRestOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
};
tokenRestClient = new RestClient(tokenRestOptions);
sessionRestClient = new RestClient(sessionRestOptions);
sessionRestClient.AcceptedContentTypes = new string[] { "application/json" };
sessionRestClient.AcceptedContentTypes = new string[] { "application/json" };
sessionRestClient.Authenticator = new HttpBasicAuthenticator(Username, Password);

if (DefaultSerializationSettings == null)
{
DefaultSerializationSettings = new JsonSerializerSettings
{
Error = (se, ev) => { ev.ErrorContext.Handled = true; },
DefaultValueHandling = DefaultValueHandling.Include,
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.None,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
Converters = new List<JsonConverter>() { new Newtonsoft.Json.Converters.StringEnumConverter() }
};
};
tokenRestClient.UseNewtonsoftJson(DefaultSerializationSettings);
sessionRestClient.UseNewtonsoftJson(DefaultSerializationSettings);
}
public async Task<LoginResponseType> LoginAsync()
{
RestResponse<LoginResponseType> response = await AuthenticationHelper.LoginAsync(sessionRestClient, cancellationToken, timeout, retry);
ArgumentNullException.ThrowIfNull(response.Data.Value);

tokenRestClient.AddDefaultHeader("vmware-api-session-id", response.Data.Value);
sessionRestClient.AddDefaultHeader("vmware-api-session-id", response.Data.Value);
return response.Data;
}
public async Task LogoutAsync()
{
await AuthenticationHelper.LogoutAsync(sessionRestClient, cancellationToken, timeout, retry);
}
{{#submodules}}
public {{.}}SubModule {{.}}SubModule => new {{.}}SubModule(tokenRestClient, cancellationToken, timeout, retry);
{{/submodules}}

}
}
31 changes: 31 additions & 0 deletions SDKGenerator/Templates/SubModule.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Serializers.NewtonsoftJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using vspheresdk.{{rootmodule}}.Modules;
using vspheresdk;
using System.Threading;

namespace vspheresdk
{
public class {{rootmodule}}SubModule
{
JsonSerializerSettings defaultSerializationSettings;
RestClient restClient;
private CancellationToken cancellationToken;
private int timeout;
private int retry;
public {{rootmodule}}SubModule(RestClient RestClient, CancellationToken _cancellationToken = default(CancellationToken), int Port = 443, int _timeout = 5, int _retry = 2)
{
cancellationToken = _cancellationToken;
timeout = _timeout;
retry = _retry;
restClient = RestClient;
}
{{#modules}}
public {{.}}Module {{.}}Module => new {{.}}Module(restClient, cancellationToken, timeout, retry);
{{/modules}}
}
}
Loading

0 comments on commit ddd32a3

Please sign in to comment.