import * as http from 'http' ; import request = require ( 'request' ) ; import { ApisApi , HttpError , ObjectSerializer , V1APIResource , V1APIResourceList , V1DeleteOptions , V1Status , } from './api' ; import { KubeConfig } from './config' ; import { KubernetesListObject , KubernetesObject } from './types' ; /** Union type of body types returned by KubernetesObjectApi. */ type KubernetesObjectResponseBody = KubernetesObject | KubernetesListObject < KubernetesObject > | V1Status | V1APIResourceList ; /** Kubernetes API verbs. */ type KubernetesApiAction = 'create' | 'delete' | 'patch' | 'read' | 'replace' ; /**
 * Valid Content-Type header values for patch operations.  See
 * https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/
 * for details.
 */ enum KubernetesPatchStrategies { /** Diff-like JSON format. */ JsonPatch = 'application/json-patch+json' , /** Simple merge. */ MergePatch = 'application/merge-patch+json' , /** Merge with different strategies depending on field metadata. */ StrategicMergePatch = 'application/strategic-merge-patch+json' } /**
 * Dynamically construct Kubernetes API request URIs so client does not have to know what type of object it is acting
 * on.
 */ export class KubernetesObjectApi extends ApisApi { param kc return public static makeApiClient ( kc ) { const client = kc . makeApiClient ( KubernetesObjectApi ) ; client . setDefaultNamespace ( kc ) ; return client ; } /** Initialize the default namespace.  May be overwritten by context. */ protected defaultNamespace : string = 'default' ; /** Cache resource API response. */ protected apiVersionResourceCache : Record < string , V1APIResourceList > = { } ; param spec param pretty param dryRun param fieldManager param options return public async create ( spec , pretty ? , dryRun ? , fieldManager ? , options = { headers : { } } ) { if ( spec === null || spec === undefined ) { throw new Error ( 'Required parameter spec was null or undefined when calling create.' ) ; } const localVarPath = await this . specUriPath ( spec , 'create' ) ; const localVarQueryParameters = { } ; const localVarHeaderParams = this . generateHeaders ( options . headers ) ; if ( pretty !== undefined ) { localVarQueryParameters . pretty = ObjectSerializer . serialize ( pretty , 'string' ) ; } if ( dryRun !== undefined ) { localVarQueryParameters . dryRun = ObjectSerializer . serialize ( dryRun , 'string' ) ; } if ( fieldManager !== undefined ) { localVarQueryParameters . fieldManager = ObjectSerializer . serialize ( fieldManager , 'string' ) ; } const localVarRequestOptions = { method : 'POST' , qs : localVarQueryParameters , headers : localVarHeaderParams , uri : localVarPath , useQuerystring : this . _useQuerystring , json : true , body : ObjectSerializer . serialize ( spec , 'KubernetesObject' ) , } ; return this . requestPromise ( localVarRequestOptions ) ; } param spec param pretty param dryRun param gracePeriodSeconds param orphanDependents param propagationPolicy param body param options return public async delete ( spec , pretty ? , dryRun ? , gracePeriodSeconds ? , orphanDependents ? , propagationPolicy ? , body ? , options = { headers : { } } ) { if ( spec === null || spec === undefined ) { throw new Error ( 'Required parameter spec was null or undefined when calling delete.' ) ; } const localVarPath = await this . specUriPath ( spec , 'delete' ) ; const localVarQueryParameters = { } ; const localVarHeaderParams = this . generateHeaders ( options . headers ) ; if ( pretty !== undefined ) { localVarQueryParameters . pretty = ObjectSerializer . serialize ( pretty , 'string' ) ; } if ( dryRun !== undefined ) { localVarQueryParameters . dryRun = ObjectSerializer . serialize ( dryRun , 'string' ) ; } if ( gracePeriodSeconds !== undefined ) { localVarQueryParameters . gracePeriodSeconds = ObjectSerializer . serialize ( gracePeriodSeconds , 'number' ) ; } if ( orphanDependents !== undefined ) { localVarQueryParameters . orphanDependents = ObjectSerializer . serialize ( orphanDependents , 'boolean' ) ; } if ( propagationPolicy !== undefined ) { localVarQueryParameters . propagationPolicy = ObjectSerializer . serialize ( propagationPolicy , 'string' ) ; } const localVarRequestOptions = { method : 'DELETE' , qs : localVarQueryParameters , headers : localVarHeaderParams , uri : localVarPath , useQuerystring : this . _useQuerystring , json : true , body : ObjectSerializer . serialize ( body , 'V1DeleteOptions' ) , } ; return this . requestPromise < V1Status > ( localVarRequestOptions , 'V1Status' ) ; } param spec param pretty param dryRun param fieldManager param force param options return public async patch ( spec , pretty ? , dryRun ? , fieldManager ? , force ? , options = { headers : { } } ) { if ( spec === null || spec === undefined ) { throw new Error ( 'Required parameter spec was null or undefined when calling patch.' ) ; } const localVarPath = await this . specUriPath ( spec , 'patch' ) ; const localVarQueryParameters = { } ; const localVarHeaderParams = this . generateHeaders ( options . headers , 'PATCH' ) ; if ( pretty !== undefined ) { localVarQueryParameters . pretty = ObjectSerializer . serialize ( pretty , 'string' ) ; } if ( dryRun !== undefined ) { localVarQueryParameters . dryRun = ObjectSerializer . serialize ( dryRun , 'string' ) ; } if ( fieldManager !== undefined ) { localVarQueryParameters . fieldManager = ObjectSerializer . serialize ( fieldManager , 'string' ) ; } if ( force !== undefined ) { localVarQueryParameters . force = ObjectSerializer . serialize ( force , 'boolean' ) ; } const localVarRequestOptions = { method : 'PATCH' , qs : localVarQueryParameters , headers : localVarHeaderParams , uri : localVarPath , useQuerystring : this . _useQuerystring , json : true , body : ObjectSerializer . serialize ( spec , 'object' ) , } ; return this . requestPromise ( localVarRequestOptions ) ; } param spec param pretty param exact param exportt param options return public async read ( spec , pretty ? , exact ? , exportt ? , options = { headers : { } } ) { if ( spec === null || spec === undefined ) { throw new Error ( 'Required parameter spec was null or undefined when calling read.' ) ; } const localVarPath = await this . specUriPath ( spec , 'read' ) ; const localVarQueryParameters = { } ; const localVarHeaderParams = this . generateHeaders ( options . headers ) ; if ( pretty !== undefined ) { localVarQueryParameters . pretty = ObjectSerializer . serialize ( pretty , 'string' ) ; } if ( exact !== undefined ) { localVarQueryParameters . exact = ObjectSerializer . serialize ( exact , 'boolean' ) ; } if ( exportt !== undefined ) { localVarQueryParameters . export = ObjectSerializer . serialize ( exportt , 'boolean' ) ; } const localVarRequestOptions = { method : 'GET' , qs : localVarQueryParameters , headers : localVarHeaderParams , uri : localVarPath , useQuerystring : this . _useQuerystring , json : true , } ; return this . requestPromise ( localVarRequestOptions ) ; } param spec param pretty param dryRun param fieldManager param options return public async replace ( spec , pretty ? , dryRun ? , fieldManager ? , options = { headers : { } } ) { if ( spec === null || spec === undefined ) { throw new Error ( 'Required parameter spec was null or undefined when calling replace.' ) ; } const localVarPath = await this . specUriPath ( spec , 'replace' ) ; const localVarQueryParameters = { } ; const localVarHeaderParams = this . generateHeaders ( options . headers ) ; if ( pretty !== undefined ) { localVarQueryParameters . pretty = ObjectSerializer . serialize ( pretty , 'string' ) ; } if ( dryRun !== undefined ) { localVarQueryParameters . dryRun = ObjectSerializer . serialize ( dryRun , 'string' ) ; } if ( fieldManager !== undefined ) { localVarQueryParameters . fieldManager = ObjectSerializer . serialize ( fieldManager , 'string' ) ; } const localVarRequestOptions = { method : 'PUT' , qs : localVarQueryParameters , headers : localVarHeaderParams , uri : localVarPath , useQuerystring : this . _useQuerystring , json : true , body : ObjectSerializer . serialize ( spec , 'KubernetesObject' ) , } ; return this . requestPromise ( localVarRequestOptions ) ; } /** Set default namespace from current context, if available. */ protected setDefaultNamespace ( kc ) { if ( kc . currentContext ) { const currentContext = kc . getContextObject ( kc . currentContext ) ; if ( currentContext && currentContext . namespace ) { this . defaultNamespace = currentContext . namespace ; } } return this . defaultNamespace ; } param spec param action return protected async specUriPath ( spec , action ) { if ( ! spec . kind ) { throw new Error ( 'Required spec property kind is not set' ) ; } if ( ! spec . apiVersion ) { spec . apiVersion = 'v1' ; } if ( ! spec . metadata ) { spec . metadata = { } ; } const resource = await this . resource ( spec . apiVersion , spec . kind ) ; if ( ! resource ) { throw new Error ( `Unrecognized API version and kind: ${ spec . apiVersion } ${ spec . kind }` ) ; } if ( resource . namespaced && ! spec . metadata . namespace ) { spec . metadata . namespace = this . defaultNamespace ; } const parts = [ this . apiVersionPath ( spec . apiVersion ) ] ; if ( resource . namespaced && spec . metadata . namespace ) { parts . push ( 'namespaces' , encodeURIComponent ( String ( spec . metadata . namespace ) ) ) ; } parts . push ( resource . name ) ; if ( action !== 'create' ) { if ( ! spec . metadata . name ) { throw new Error ( 'Required spec property name is not set' ) ; } parts . push ( encodeURIComponent ( String ( spec . metadata . name ) ) ) ; } return parts . join ( '/' ) . toLowerCase ( ) ; } /** Return root of API path up to API version. */ protected apiVersionPath ( apiVersion ) { const api = apiVersion . includes ( '/' ) ? 'apis' : 'api' ; return [ this . basePath , api , apiVersion ] . join ( '/' ) ; } param optionHeaders param action return protected generateHeaders ( optionsHeaders , action = 'GET' ) { const headers = Object . assign ( { } , this . _defaultHeaders ) ; headers . accept = 'application/json' ; if ( action === 'PATCH' ) { headers [ 'content-type' ] = KubernetesPatchStrategies . StrategicMergePatch ; } Object . assign ( headers , optionsHeaders ) ; return headers ; } param apiVersion param kind return protected async resource ( apiVersion , kind ) { if ( apiVersion === null || apiVersion === undefined ) { throw new Error ( 'Required parameter apiVersion was null or undefined when calling resource' ) ; } if ( kind === null || kind === undefined ) { throw new Error ( 'Required parameter kind was null or undefined when calling resource' ) ; } if ( this . apiVersionResourceCache [ apiVersion ] ) { const resource = this . apiVersionResourceCache [ apiVersion ] . resources . find ( ( r ) => r . kind === kind ) ; if ( resource ) { return resource ; } } const localVarPath = this . apiVersionPath ( apiVersion ) ; const localVarQueryParameters = { } ; const localVarHeaderParams = this . generateHeaders ( { } ) ; const localVarRequestOptions = { method : 'GET' , qs : localVarQueryParameters , headers : localVarHeaderParams , uri : localVarPath , useQuerystring : this . _useQuerystring , json : true , } ; try { const getApiResponse = await this . requestPromise < V1APIResourceList > ( localVarRequestOptions , 'V1APIResourceList' ) ; this . apiVersionResourceCache [ apiVersion ] = getApiResponse . body ; return this . apiVersionResourceCache [ apiVersion ] . resources . find ( ( r ) => r . kind === kind ) ; } catch ( e ) { e . message = `Failed to fetch resource metadata for ${ apiVersion }/${ kind }: ${ e . message }` ; throw e ; } } /**
     * Standard Kubernetes request wrapped in a Promise.
     */ protected async requestPromise < T extends KubernetesObjectResponseBody = KubernetesObject > ( requestOptions , tipe = 'KubernetesObject' ) { let authenticationPromise = Promise . resolve ( ) ; if ( this . authentications . BearerToken . apiKey ) { authenticationPromise = authenticationPromise . then ( ( ) => this . authentications . BearerToken . applyToRequest ( requestOptions ) ) ; } authenticationPromise = authenticationPromise . then ( ( ) => this . authentications . default . applyToRequest ( requestOptions ) ) ; let interceptorPromise = authenticationPromise ; for ( const interceptor of this . interceptors ) { interceptorPromise = interceptorPromise . then ( ( ) => interceptor ( requestOptions ) ) ; } await interceptorPromise ; return new Promise < { body : T ; response : http . IncomingMessage ; } > ( ( resolve , reject ) => { request ( requestOptions , ( error , response , body ) => { if ( error ) { reject ( error ) ; } else { body = ObjectSerializer . deserialize ( body , tipe ) ; if ( response . statusCode && response . statusCode >= 200 && response . statusCode <= 299 ) { resolve ( { response , body } ) ; } else { reject ( new HttpError ( response , body , response . statusCode ) ) ; } } } ) ; } ) ; } }