@@ -22,8 +22,11 @@ public void RegisterHelper(ScriptObject scriptObject)
2222 {
2323 scriptObject . Import ( nameof ( GetInterfaceName ) , new Func < JsonSchema , string > ( GetInterfaceName ) ) ;
2424 scriptObject . Import ( nameof ( GetMethodName ) , new Func < OpenApiOperation , string , string > ( GetMethodName ) ) ;
25+ scriptObject . Import ( nameof ( GetActionName ) ,
26+ new Func < OpenApiOperationDescription , string , string , string > ( GetActionName ) ) ;
2527 scriptObject . Import ( nameof ( GetDotNetName ) , new Func < string , string , string > ( GetDotNetName ) ) ;
26- scriptObject . Import ( nameof ( GetDotNetNameOpenApiParameter ) , new Func < OpenApiParameter , string , string > ( GetDotNetNameOpenApiParameter ) ) ;
28+ scriptObject . Import ( nameof ( GetDotNetNameOpenApiParameter ) ,
29+ new Func < OpenApiParameter , string , string > ( GetDotNetNameOpenApiParameter ) ) ;
2730 }
2831
2932 private string GetInterfaceName ( JsonSchema definition )
@@ -162,5 +165,60 @@ public static string GetMethodName(OpenApiOperation watchOperation, string suffi
162165
163166 return methodName ;
164167 }
168+
169+ public static string GetActionName ( OpenApiOperationDescription apiOperation , string resource , string suffix )
170+ {
171+ var actionType = apiOperation . Operation ? . ExtensionData ? [ "x-kubernetes-action" ] as string ;
172+
173+ if ( string . IsNullOrEmpty ( actionType ) )
174+ {
175+ return $ "{ apiOperation . Method . ToPascalCase ( ) } { suffix } ";
176+ }
177+
178+ var resourceNamespace = ParsePathSegmentAfterParameter ( apiOperation . Path , "namespace" ) . ToPascalCase ( ) ;
179+ var resourceName = ParsePathSegmentAfterParameter ( apiOperation . Path , "name" ) . ToPascalCase ( ) ;
180+ var actionMappings = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase )
181+ {
182+ { "get" , "Get" } ,
183+ { "list" , "List" } ,
184+ { "put" , "Put" } ,
185+ { "patch" , "Patch" } ,
186+ { "post" , "Post" } ,
187+ { "delete" , "Delete" } ,
188+ { "deletecollection" , "DeleteCollection" } ,
189+ { "watch" , "Watch" } ,
190+ { "watchlist" , "WatchList" } ,
191+ { "proxy" , "Proxy" } ,
192+ } ;
193+
194+ if ( actionMappings . TryGetValue ( actionType , out var actionPrefix ) )
195+ {
196+ return Regex . Replace ( $ "{ actionPrefix } { resourceNamespace } { resourceName } { suffix } ", resource , string . Empty ,
197+ RegexOptions . IgnoreCase ) ;
198+ }
199+
200+ if ( string . Equals ( "connect" , actionType , StringComparison . OrdinalIgnoreCase ) )
201+ {
202+ return Regex . Replace ( $ "Connect{ apiOperation . Method } { resourceNamespace } { resourceName } { suffix } ", resource ,
203+ string . Empty ,
204+ RegexOptions . IgnoreCase ) ;
205+ }
206+
207+ return $ "{ actionType . ToPascalCase ( ) } { suffix } ";
208+ }
209+
210+ private static string ParsePathSegmentAfterParameter ( string path , string variableName = "namespace" )
211+ {
212+ var pattern = $@ "/\{{{variableName}\}}/([^/]+)/?";
213+
214+ var match = Regex . Match ( path , pattern ) ;
215+
216+ if ( match . Success && match . Groups . Count > 1 )
217+ {
218+ return match . Groups [ 1 ] . Value ;
219+ }
220+
221+ return string . Empty ;
222+ }
165223 }
166224}
0 commit comments