15
15
using System . Globalization ;
16
16
using Microsoft . Azure . Commands . Common . Authentication ;
17
17
using Microsoft . Azure . Commands . Common . Authentication . Models ;
18
+ using Microsoft . WindowsAzure . Commands . Common ;
18
19
19
20
namespace Microsoft . Azure . Commands . ApiManagement . ServiceManagement
20
21
{
@@ -67,6 +68,7 @@ private static void ConfigurePowershellToSmapiMappings()
67
68
Mapper . CreateMap < PsApiManagementRequest , RequestContract > ( ) ;
68
69
Mapper . CreateMap < PsApiManagementResponse , ResponseContract > ( ) ;
69
70
Mapper . CreateMap < PsApiManagementRepresentation , RepresentationContract > ( ) ;
71
+ Mapper . CreateMap < PsApiManagementAuthorizationHeaderCredential , AuthorizationHeaderCredentialsContract > ( ) ;
70
72
}
71
73
72
74
private static void ConfigureSmapiToPowershellMappings ( )
@@ -183,6 +185,43 @@ private static void ConfigureSmapiToPowershellMappings()
183
185
. ForMember ( dest => dest . ClientSecret , opt => opt . MapFrom ( src => src . ClientSecret ) )
184
186
. ForMember ( dest => dest . Type , opt => opt . MapFrom ( src => src . Type ) )
185
187
. ForMember ( dest => dest . AllowedTenants , opt => opt . MapFrom ( src => src . AllowedTenants == null ? new string [ 0 ] : src . AllowedTenants . ToArray ( ) ) ) ;
188
+
189
+ Mapper
190
+ . CreateMap < BackendProxyContract , PsApiManagementBackendProxy > ( )
191
+ . ForMember ( dest => dest . Url , opt => opt . MapFrom ( src => src . Url ) )
192
+ . ForMember ( dest => dest . Password , opt => opt . MapFrom ( src => src . Password ) )
193
+ . ForMember ( dest => dest . UserName , opt => opt . MapFrom ( src => src . Username ) ) ;
194
+
195
+ Mapper
196
+ . CreateMap < BackendCredentialsContract , PsApiManagementBackendCredential > ( )
197
+ . ForMember ( dest => dest . Certificate , opt => opt . MapFrom ( src => src . Certificate ) )
198
+ . ForMember ( dest => dest . Query , opt => opt . Ignore ( ) )
199
+ . ForMember ( dest => dest . Header , opt => opt . Ignore ( ) )
200
+ . AfterMap ( ( src , dest ) =>
201
+ dest . Query = src . Query == null
202
+ ? ( Hashtable ) null
203
+ : DictionaryToHashTable ( src . Query ) )
204
+ . AfterMap ( ( src , dest ) =>
205
+ dest . Header = src . Header == null
206
+ ? ( Hashtable ) null
207
+ : DictionaryToHashTable ( src . Header ) ) ;
208
+ Mapper
209
+ . CreateMap < AuthorizationHeaderCredentialsContract , PsApiManagementAuthorizationHeaderCredential > ( )
210
+ . ForMember ( dest => dest . Scheme , opt => opt . MapFrom ( src => src . Scheme ) )
211
+ . ForMember ( dest => dest . Parameter , opt => opt . MapFrom ( src => src . Parameter ) ) ;
212
+
213
+ Mapper
214
+ . CreateMap < BackendGetContract , PsApiManagementBackend > ( )
215
+ . ForMember ( dest => dest . BackendId , opt => opt . MapFrom ( src => src . Id ) )
216
+ . ForMember ( dest => dest . Url , opt => opt . MapFrom ( src => src . Url ) )
217
+ . ForMember ( dest => dest . Protocol , opt => opt . MapFrom ( src => src . Protocol ) )
218
+ . ForMember ( dest => dest . ResourceId , opt => opt . MapFrom ( src => src . ResourceId ) )
219
+ . ForMember ( dest => dest . Title , opt => opt . MapFrom ( src => src . Title ) )
220
+ . ForMember ( dest => dest . Description , opt => opt . MapFrom ( src => src . Description ) )
221
+ . ForMember ( dest => dest . Properties , opt => opt . MapFrom ( src => src . Properties ) )
222
+ . ForMember ( dest => dest . Proxy , opt => opt . MapFrom ( src => src . Proxy ) ) ;
223
+
224
+ Mapper . CreateMap < Hashtable , Hashtable > ( ) ;
186
225
}
187
226
188
227
public ApiManagementClient ( AzureContext context )
@@ -1879,5 +1918,243 @@ public void IdentityProviderSet(PsApiManagementContext context, string identityP
1879
1918
"*" ) ;
1880
1919
}
1881
1920
#endregion
1921
+
1922
+ #region Backends
1923
+ public PsApiManagementBackend BackendCreate (
1924
+ PsApiManagementContext context ,
1925
+ string backendId ,
1926
+ string url ,
1927
+ string protocol ,
1928
+ string title ,
1929
+ string description ,
1930
+ string resourceId ,
1931
+ bool ? skipCertificateChainValidation ,
1932
+ bool ? skipCertificateNameValidation ,
1933
+ PsApiManagementBackendCredential credential ,
1934
+ PsApiManagementBackendProxy proxy )
1935
+ {
1936
+ var backendCreateParams = new BackendCreateParameters ( url , protocol ) ;
1937
+ if ( ! string . IsNullOrEmpty ( resourceId ) )
1938
+ {
1939
+ backendCreateParams . ResourceId = resourceId ;
1940
+ }
1941
+
1942
+ if ( ! string . IsNullOrEmpty ( title ) )
1943
+ {
1944
+ backendCreateParams . Title = title ;
1945
+ }
1946
+
1947
+ if ( ! string . IsNullOrEmpty ( description ) )
1948
+ {
1949
+ backendCreateParams . Description = description ;
1950
+ }
1951
+
1952
+ if ( skipCertificateChainValidation . HasValue || skipCertificateNameValidation . HasValue )
1953
+ {
1954
+ backendCreateParams . Properties = new Dictionary < string , object > ( ) ;
1955
+ if ( skipCertificateNameValidation . HasValue )
1956
+ {
1957
+ backendCreateParams . Properties . Add ( "skipCertificateNameValidation" , skipCertificateNameValidation . Value ) ;
1958
+ }
1959
+
1960
+ if ( skipCertificateChainValidation . HasValue )
1961
+ {
1962
+ backendCreateParams . Properties . Add ( "skipCertificateChainValidation" , skipCertificateChainValidation . Value ) ;
1963
+ }
1964
+ }
1965
+
1966
+ if ( credential != null )
1967
+ {
1968
+ backendCreateParams . Credentials = new BackendCredentialsContract ( ) ;
1969
+ if ( credential . Query != null )
1970
+ {
1971
+ backendCreateParams . Credentials . Query = HashTableToDictionary ( credential . Query ) ;
1972
+ }
1973
+
1974
+ if ( credential . Header != null )
1975
+ {
1976
+ backendCreateParams . Credentials . Header = HashTableToDictionary ( credential . Header ) ;
1977
+ }
1978
+
1979
+ if ( credential . Certificate != null && credential . Certificate . Any ( ) )
1980
+ {
1981
+ backendCreateParams . Credentials . Certificate = credential . Certificate . ToList ( ) ;
1982
+ }
1983
+
1984
+ if ( credential . Authorization != null )
1985
+ {
1986
+ backendCreateParams . Credentials . Authorization =
1987
+ Mapper . Map < AuthorizationHeaderCredentialsContract > ( credential . Authorization ) ;
1988
+ }
1989
+ }
1990
+
1991
+ if ( proxy != null )
1992
+ {
1993
+ backendCreateParams . Proxy = Mapper . Map < PsApiManagementBackendProxy , BackendProxyContract > ( proxy ) ;
1994
+ }
1995
+
1996
+ Client . Backends . Create ( context . ResourceGroupName , context . ServiceName , backendId , backendCreateParams ) ;
1997
+
1998
+ var response = Client . Backends . Get ( context . ResourceGroupName , context . ServiceName , backendId ) ;
1999
+ var backend = Mapper . Map < BackendGetContract , PsApiManagementBackend > ( response . Value ) ;
2000
+
2001
+ return backend ;
2002
+ }
2003
+
2004
+ static Dictionary < string , List < string > > HashTableToDictionary ( Hashtable table )
2005
+ {
2006
+ if ( table == null )
2007
+ {
2008
+ return null ;
2009
+ }
2010
+
2011
+ var result = new Dictionary < string , List < string > > ( ) ;
2012
+ foreach ( var entry in table . Cast < DictionaryEntry > ( ) )
2013
+ {
2014
+ var entryValue = entry . Value as object [ ] ;
2015
+ if ( entryValue == null )
2016
+ {
2017
+ throw new ArgumentException (
2018
+ string . Format ( CultureInfo . InvariantCulture ,
2019
+ "Invalid input type specified for Key '{0}', expected string[]" ,
2020
+ entry . Key ) ) ;
2021
+ }
2022
+ result . Add ( entry . Key . ToString ( ) , entryValue . Select ( i => i . ToString ( ) ) . ToList ( ) ) ;
2023
+ }
2024
+
2025
+ return result ;
2026
+ }
2027
+
2028
+ static Hashtable DictionaryToHashTable ( IDictionary < string , List < string > > dictionary )
2029
+ {
2030
+ if ( dictionary == null )
2031
+ {
2032
+ return null ;
2033
+ }
2034
+
2035
+ var result = new Hashtable ( ) ;
2036
+ foreach ( var keyEntry in dictionary . Keys )
2037
+ {
2038
+ var keyValue = dictionary [ keyEntry ] ;
2039
+
2040
+ result . Add ( keyEntry , keyValue . Cast < object > ( ) . ToArray ( ) ) ;
2041
+ }
2042
+
2043
+ return result ;
2044
+ }
2045
+
2046
+ public IList < PsApiManagementBackend > BackendsList ( PsApiManagementContext context )
2047
+ {
2048
+ var results = ListPagedAndMap < PsApiManagementBackend , BackendGetContract > (
2049
+ ( ) => Client . Backends . List ( context . ResourceGroupName , context . ServiceName , null ) ,
2050
+ nextLink => Client . Backends . ListNext ( nextLink ) ) ;
2051
+
2052
+ return results ;
2053
+ }
2054
+
2055
+ public PsApiManagementBackend BackendById ( PsApiManagementContext context , string loggerId )
2056
+ {
2057
+ var response = Client . Backends . Get ( context . ResourceGroupName , context . ServiceName , loggerId ) ;
2058
+ var backend = Mapper . Map < PsApiManagementBackend > ( response . Value ) ;
2059
+
2060
+ return backend ;
2061
+ }
2062
+
2063
+ public void BackendRemove ( PsApiManagementContext context , string backendId )
2064
+ {
2065
+ Client . Backends . Delete ( context . ResourceGroupName , context . ServiceName , backendId , "*" ) ;
2066
+ }
2067
+
2068
+ public void BackendSet (
2069
+ PsApiManagementContext context ,
2070
+ string backendId ,
2071
+ string url ,
2072
+ string protocol ,
2073
+ string title ,
2074
+ string description ,
2075
+ string resourceId ,
2076
+ bool ? skipCertificateChainValidation ,
2077
+ bool ? skipCertificateNameValidation ,
2078
+ PsApiManagementBackendCredential credential ,
2079
+ PsApiManagementBackendProxy proxy )
2080
+ {
2081
+ var backendUpdateParams = new BackendUpdateParameters ( ) ;
2082
+ if ( ! string . IsNullOrEmpty ( url ) )
2083
+ {
2084
+ backendUpdateParams . Url = url ;
2085
+ }
2086
+
2087
+ if ( ! string . IsNullOrEmpty ( protocol ) )
2088
+ {
2089
+ backendUpdateParams . Protocol = protocol ;
2090
+ }
2091
+
2092
+ if ( ! string . IsNullOrEmpty ( resourceId ) )
2093
+ {
2094
+ backendUpdateParams . ResourceId = resourceId ;
2095
+ }
2096
+
2097
+ if ( ! string . IsNullOrEmpty ( title ) )
2098
+ {
2099
+ backendUpdateParams . Title = title ;
2100
+ }
2101
+
2102
+ if ( ! string . IsNullOrEmpty ( description ) )
2103
+ {
2104
+ backendUpdateParams . Description = description ;
2105
+ }
2106
+
2107
+ if ( skipCertificateChainValidation . HasValue || skipCertificateNameValidation . HasValue )
2108
+ {
2109
+ backendUpdateParams . Properties = new Dictionary < string , object > ( ) ;
2110
+ if ( skipCertificateNameValidation . HasValue )
2111
+ {
2112
+ backendUpdateParams . Properties . Add ( "skipCertificateNameValidation" , skipCertificateNameValidation . Value ) ;
2113
+ }
2114
+
2115
+ if ( skipCertificateChainValidation . HasValue )
2116
+ {
2117
+ backendUpdateParams . Properties . Add ( "skipCertificateChainValidation" , skipCertificateChainValidation . Value ) ;
2118
+ }
2119
+ }
2120
+
2121
+ if ( credential != null )
2122
+ {
2123
+ backendUpdateParams . Credentials = new BackendCredentialsContract ( ) ;
2124
+ if ( credential . Query != null )
2125
+ {
2126
+ backendUpdateParams . Credentials . Query = HashTableToDictionary ( credential . Query ) ;
2127
+ }
2128
+
2129
+ if ( credential . Header != null )
2130
+ {
2131
+ backendUpdateParams . Credentials . Header = HashTableToDictionary ( credential . Header ) ;
2132
+ }
2133
+
2134
+ if ( credential . Certificate != null && credential . Certificate . Any ( ) )
2135
+ {
2136
+ backendUpdateParams . Credentials . Certificate = credential . Certificate . ToList ( ) ;
2137
+ }
2138
+
2139
+ if ( credential . Authorization != null )
2140
+ {
2141
+ backendUpdateParams . Credentials . Authorization =
2142
+ Mapper . Map < AuthorizationHeaderCredentialsContract > ( credential . Authorization ) ;
2143
+ }
2144
+ }
2145
+
2146
+ if ( proxy != null )
2147
+ {
2148
+ backendUpdateParams . Proxy = Mapper . Map < PsApiManagementBackendProxy , BackendProxyContract > ( proxy ) ;
2149
+ }
2150
+
2151
+ Client . Backends . Update (
2152
+ context . ResourceGroupName ,
2153
+ context . ServiceName ,
2154
+ backendId ,
2155
+ backendUpdateParams ,
2156
+ "*" ) ;
2157
+ }
2158
+ #endregion
1882
2159
}
1883
2160
}
0 commit comments