Skip to content

Add cmdlets for managing Backend Entity in ApiManagement #3586

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

Merged
merged 5 commits into from
Mar 7, 2017
Merged
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
8 changes: 7 additions & 1 deletion src/ResourceManager/ApiManagement/AzureRM.ApiManagement.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ CmdletsToExport = 'Add-AzureRmApiManagementRegion',
'Get-AzureRmApiManagementIdentityProvider',
'New-AzureRmApiManagementIdentityProvider',
'Set-AzureRmApiManagementIdentityProvider',
'Remove-AzureRmApiManagementIdentityProvider'
'Remove-AzureRmApiManagementIdentityProvider',
'Get-AzureRmApiManagementBackend',
'New-AzureRmApiManagementBackend',
'New-AzureRmApiManagementBackendCredential',
'New-AzureRmApiManagementBackendProxy',
'Set-AzureRmApiManagementBackend',
'Remove-AzureRmApiManagementBackend'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
10 changes: 9 additions & 1 deletion src/ResourceManager/ApiManagement/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
- Additional information about change #1
-->
## Current Release

* Added new cmdlets to manage Backend entity
- New-AzureRmApiManagementBackend
- Get-AzureRmApiManagementBackend
- Set-AzureRmApiManagementBackend
- Remove-AzureRmApiManagementBackend
* Created supporting cmdlets to create in-memory objects required while Creating or Updating Backend entity
- New-AzureRmApiManagementBackendCredential
- New-AzureRmApiManagementBackendProxy

## Version 3.4.0

## Version 3.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Globalization;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common;

namespace Microsoft.Azure.Commands.ApiManagement.ServiceManagement
{
Expand Down Expand Up @@ -67,6 +68,7 @@ private static void ConfigurePowershellToSmapiMappings()
Mapper.CreateMap<PsApiManagementRequest, RequestContract>();
Mapper.CreateMap<PsApiManagementResponse, ResponseContract>();
Mapper.CreateMap<PsApiManagementRepresentation, RepresentationContract>();
Mapper.CreateMap<PsApiManagementAuthorizationHeaderCredential, AuthorizationHeaderCredentialsContract>();
}

private static void ConfigureSmapiToPowershellMappings()
Expand Down Expand Up @@ -183,6 +185,43 @@ private static void ConfigureSmapiToPowershellMappings()
.ForMember(dest => dest.ClientSecret, opt => opt.MapFrom(src => src.ClientSecret))
.ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type))
.ForMember(dest => dest.AllowedTenants, opt => opt.MapFrom(src => src.AllowedTenants == null ? new string[0] : src.AllowedTenants.ToArray()));

Mapper
.CreateMap<BackendProxyContract, PsApiManagementBackendProxy>()
.ForMember(dest => dest.Url, opt => opt.MapFrom(src => src.Url))
.ForMember(dest => dest.Password, opt => opt.MapFrom(src => src.Password))
.ForMember(dest => dest.UserName, opt => opt.MapFrom(src => src.Username));

Mapper
.CreateMap<BackendCredentialsContract, PsApiManagementBackendCredential>()
.ForMember(dest => dest.Certificate, opt => opt.MapFrom(src => src.Certificate))
.ForMember(dest => dest.Query, opt => opt.Ignore())
.ForMember(dest => dest.Header, opt => opt.Ignore())
.AfterMap((src, dest) =>
dest.Query = src.Query == null
? (Hashtable)null
: DictionaryToHashTable(src.Query))
.AfterMap((src, dest) =>
dest.Header = src.Header == null
? (Hashtable)null
: DictionaryToHashTable(src.Header));
Mapper
.CreateMap<AuthorizationHeaderCredentialsContract, PsApiManagementAuthorizationHeaderCredential>()
.ForMember(dest => dest.Scheme, opt => opt.MapFrom(src => src.Scheme))
.ForMember(dest => dest.Parameter, opt => opt.MapFrom(src => src.Parameter));

Mapper
.CreateMap<BackendGetContract, PsApiManagementBackend>()
.ForMember(dest => dest.BackendId, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Url, opt => opt.MapFrom(src => src.Url))
.ForMember(dest => dest.Protocol, opt => opt.MapFrom(src => src.Protocol))
.ForMember(dest => dest.ResourceId, opt => opt.MapFrom(src => src.ResourceId))
.ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
.ForMember(dest => dest.Properties, opt => opt.MapFrom(src => src.Properties))
.ForMember(dest => dest.Proxy, opt => opt.MapFrom(src => src.Proxy));

Mapper.CreateMap<Hashtable, Hashtable>();
}

public ApiManagementClient(AzureContext context)
Expand Down Expand Up @@ -1879,5 +1918,243 @@ public void IdentityProviderSet(PsApiManagementContext context, string identityP
"*");
}
#endregion

#region Backends
public PsApiManagementBackend BackendCreate(
PsApiManagementContext context,
string backendId,
string url,
string protocol,
string title,
string description,
string resourceId,
bool? skipCertificateChainValidation,
bool? skipCertificateNameValidation,
PsApiManagementBackendCredential credential,
PsApiManagementBackendProxy proxy)
{
var backendCreateParams = new BackendCreateParameters(url, protocol);
if (!string.IsNullOrEmpty(resourceId))
{
backendCreateParams.ResourceId = resourceId;
}

if (!string.IsNullOrEmpty(title))
{
backendCreateParams.Title = title;
}

if (!string.IsNullOrEmpty(description))
{
backendCreateParams.Description = description;
}

if (skipCertificateChainValidation.HasValue || skipCertificateNameValidation.HasValue)
{
backendCreateParams.Properties = new Dictionary<string, object>();
if (skipCertificateNameValidation.HasValue)
{
backendCreateParams.Properties.Add("skipCertificateNameValidation", skipCertificateNameValidation.Value);
}

if (skipCertificateChainValidation.HasValue)
{
backendCreateParams.Properties.Add("skipCertificateChainValidation", skipCertificateChainValidation.Value);
}
}

if (credential != null)
{
backendCreateParams.Credentials = new BackendCredentialsContract();
if (credential.Query != null)
{
backendCreateParams.Credentials.Query = HashTableToDictionary(credential.Query);
}

if (credential.Header != null)
{
backendCreateParams.Credentials.Header = HashTableToDictionary(credential.Header);
}

if (credential.Certificate != null && credential.Certificate.Any())
{
backendCreateParams.Credentials.Certificate = credential.Certificate.ToList();
}

if (credential.Authorization != null)
{
backendCreateParams.Credentials.Authorization =
Mapper.Map<AuthorizationHeaderCredentialsContract>(credential.Authorization);
}
}

if (proxy != null)
{
backendCreateParams.Proxy = Mapper.Map<PsApiManagementBackendProxy, BackendProxyContract>(proxy);
}

Client.Backends.Create(context.ResourceGroupName, context.ServiceName, backendId, backendCreateParams);

var response = Client.Backends.Get(context.ResourceGroupName, context.ServiceName, backendId);
var backend = Mapper.Map<BackendGetContract, PsApiManagementBackend>(response.Value);

return backend;
}

static Dictionary<string, List<string>> HashTableToDictionary(Hashtable table)
{
if (table == null)
{
return null;
}

var result = new Dictionary<string, List<string>>();
foreach (var entry in table.Cast<DictionaryEntry>())
{
var entryValue = entry.Value as object[];
if (entryValue == null)
{
throw new ArgumentException(
string.Format(CultureInfo.InvariantCulture,
"Invalid input type specified for Key '{0}', expected string[]",
entry.Key));
}
result.Add(entry.Key.ToString(), entryValue.Select(i => i.ToString()).ToList());
}

return result;
}

static Hashtable DictionaryToHashTable(IDictionary<string, List<string>> dictionary)
{
if (dictionary == null)
{
return null;
}

var result = new Hashtable();
foreach (var keyEntry in dictionary.Keys)
{
var keyValue = dictionary[keyEntry];

result.Add(keyEntry, keyValue.Cast<object>().ToArray());
}

return result;
}

public IList<PsApiManagementBackend> BackendsList(PsApiManagementContext context)
{
var results = ListPagedAndMap<PsApiManagementBackend, BackendGetContract>(
() => Client.Backends.List(context.ResourceGroupName, context.ServiceName, null),
nextLink => Client.Backends.ListNext(nextLink));

return results;
}

public PsApiManagementBackend BackendById(PsApiManagementContext context, string loggerId)
{
var response = Client.Backends.Get(context.ResourceGroupName, context.ServiceName, loggerId);
var backend = Mapper.Map<PsApiManagementBackend>(response.Value);

return backend;
}

public void BackendRemove(PsApiManagementContext context, string backendId)
{
Client.Backends.Delete(context.ResourceGroupName, context.ServiceName, backendId, "*");
}

public void BackendSet(
PsApiManagementContext context,
string backendId,
string url,
string protocol,
string title,
string description,
string resourceId,
bool? skipCertificateChainValidation,
bool? skipCertificateNameValidation,
PsApiManagementBackendCredential credential,
PsApiManagementBackendProxy proxy)
{
var backendUpdateParams = new BackendUpdateParameters();
if (!string.IsNullOrEmpty(url))
{
backendUpdateParams.Url = url;
}

if (!string.IsNullOrEmpty(protocol))
{
backendUpdateParams.Protocol = protocol;
}

if (!string.IsNullOrEmpty(resourceId))
{
backendUpdateParams.ResourceId = resourceId;
}

if (!string.IsNullOrEmpty(title))
{
backendUpdateParams.Title = title;
}

if (!string.IsNullOrEmpty(description))
{
backendUpdateParams.Description = description;
}

if (skipCertificateChainValidation.HasValue || skipCertificateNameValidation.HasValue)
{
backendUpdateParams.Properties = new Dictionary<string, object>();
if (skipCertificateNameValidation.HasValue)
{
backendUpdateParams.Properties.Add("skipCertificateNameValidation", skipCertificateNameValidation.Value);
}

if (skipCertificateChainValidation.HasValue)
{
backendUpdateParams.Properties.Add("skipCertificateChainValidation", skipCertificateChainValidation.Value);
}
}

if (credential != null)
{
backendUpdateParams.Credentials = new BackendCredentialsContract();
if (credential.Query != null)
{
backendUpdateParams.Credentials.Query = HashTableToDictionary(credential.Query);
}

if (credential.Header != null)
{
backendUpdateParams.Credentials.Header = HashTableToDictionary(credential.Header);
}

if (credential.Certificate != null && credential.Certificate.Any())
{
backendUpdateParams.Credentials.Certificate = credential.Certificate.ToList();
}

if (credential.Authorization != null)
{
backendUpdateParams.Credentials.Authorization =
Mapper.Map<AuthorizationHeaderCredentialsContract>(credential.Authorization);
}
}

if (proxy != null)
{
backendUpdateParams.Proxy = Mapper.Map<PsApiManagementBackendProxy, BackendProxyContract>(proxy);
}

Client.Backends.Update(
context.ResourceGroupName,
context.ServiceName,
backendId,
backendUpdateParams,
"*");
}
#endregion
}
}
Loading