@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
138 lines • 8.3 kB
TypeScript
import type { Client, Command } from '@smithy/smithy-client';
import { AwsCredentialIdentityWithMetaData } from '../aws/coreAuth.js';
import { ResourceTypeParts } from '../persistence/AwsIamStore.js';
import { AwsService } from '../services.js';
import { Tags } from '../utils/tags.js';
import { Sync } from './sync.js';
export type ClientConstructor = new (args: any) => Client<any, any, any, any>;
type CommandConstructor = new (args: any) => Command<any, any, any, any, any>;
type ShortCommandConstructor = new (args: any) => Command<any, any, any>;
export type CommandConstructors = CommandConstructor | ShortCommandConstructor;
export type ExtractOutputType<T> = T extends CommandConstructors ? InstanceType<T> extends Command<any, infer Output, any, any, any> ? Output : never : never;
type ExtractInputType<T> = T extends CommandConstructors ? InstanceType<T> extends Command<infer Input, any, any, any, any> ? Input : never : never;
type Pagination<C extends CommandConstructors> = {
inputKey: Extract<keyof ExtractInputType<C>, string>;
outputKey: Extract<keyof ExtractOutputType<C>, string>;
} | '::no-pagination::';
type ClientInstanceOrConstructor = InstanceType<ClientConstructor>;
/**
* Paginates through all available resources for a given AWS API
*
* @param clientClass The AWS Client class to use
* @param commandClass The command class to invoke for the resource
* @param key The key of the resource in the command output to pull
* @param paginationConfig The pagination configuration for the command, defines the keys to use for pagination, use `::no-pagination::` to indicate no pagination
* @param params Optional parameters to pass to the command
* @returns Returns an array of resources returned from the commandClass Response Type key
*/
export declare function paginateResource<C extends CommandConstructors, K extends keyof ExtractOutputType<C>>(clientClassOrInstance: ClientInstanceOrConstructor, commandClass: C, key: K, paginationConfig: Pagination<C>, params?: Partial<ExtractInputType<C>>): Promise<NonNullable<ExtractOutputType<C>[K]>>;
type ArrayElementType<T> = T extends (infer E)[] ? E : never;
type ResourceElementType<C extends CommandConstructors, K extends keyof ExtractOutputType<C>> = ArrayElementType<ExtractOutputType<C>[K]> extends string ? {
name: string;
} : ArrayElementType<ExtractOutputType<C>[K]>;
type PromiseType<T extends Promise<any>> = T extends Promise<infer U> ? U : never;
export type ExtraFieldsDefinition<C extends ClientConstructor, Cmd extends CommandConstructors, K extends keyof ExtractOutputType<Cmd>> = Record<string, (client: InstanceType<C>, resource: ResourceElementType<Cmd, K>, accountId: string, region: string, partition: string) => Promise<any>>;
type ExtraFieldsReturnType<C extends ClientConstructor, Cmd extends CommandConstructors, K extends keyof ExtractOutputType<Cmd>, T extends ExtraFieldsDefinition<any, Cmd, K>> = {
[K in keyof T]: PromiseType<ReturnType<T[K]>>;
};
export type ExtendedResourceElementType<C extends ClientConstructor, Cmd extends CommandConstructors, K extends keyof ExtractOutputType<Cmd>, ExtraFieldsFunc extends ExtraFieldsDefinition<any, Cmd, K>> = ResourceElementType<Cmd, K> & {
extraFields: ExtraFieldsReturnType<C, Cmd, K, ExtraFieldsFunc>;
};
export type ResourceSyncType<C extends ClientConstructor, Cmd extends CommandConstructors, K extends keyof ExtractOutputType<Cmd>, ExtraFields extends ExtraFieldsDefinition<C, Cmd, K> = Record<string, () => Promise<any>>> = {
/**
* The client to use
*/
client: C;
/**
* The command to execute
*/
command: Cmd;
/**
* The key of the resource in the command output to pull
*/
key: K;
/**
* The pagination token to get from the response and pass to the next call
*/
paginationConfig: Pagination<Cmd>;
/**
* The resource type parts used to sync with storage
*
* @param accountId the account ID of the resource
* @param region the region of the resource
* @returns the resource type parts
*/
resourceTypeParts: (accountId: string, region: string) => ResourceTypeParts;
/**
* Custom arguments to pass to the command, useful for filtering resources.
*
* @param awsId The AWS account ID being queried
* @param region The region being queried
* @returns a set of arguments to pass to the command
*/
arguments?: (awsId: string, region: string) => Partial<ExtractInputType<Cmd>>;
/**
* Indicates if the resource type is global.
*/
globalResourceType?: boolean;
/**
* The function to get the tags for a resource
* @param resource The resource to get the tags for
* @returns The tags for the resource, or undefined if there are no tags
*/
tags: (resource: ExtendedResourceElementType<C, Cmd, K, ExtraFields>) => Tags | undefined;
/**
* Create the ARN for a resource
*
* @param resource the resource to create the ARN for
* @param region the region the resource is in
* @param accountId the account the resource is in
* @returns the ARN for the resource
*/
arn: (resource: ResourceElementType<Cmd, K>, region: string, accountId: string, partition: string) => string;
/**
* The extra fields to get for a resource
* @param client The client to use to get the extra fields
* @param resource The resource to get the extra fields for
* @returns an object of extra fields to get, where the key is the name of the field and the value is a function that returns the value of the field
*/
extraFields?: ExtraFields;
/**
* The results to persist for a resource, except for the ARN and tags, those are handled automatically.
*
* @param resource The resource to get the custom fields for, includes extraFields from the `extraFields` function
*/
results: (resource: ExtendedResourceElementType<C, Cmd, K, ExtraFields>) => Record<string, any>;
};
/**
* Creates a resource sync type and returns it. This provides cleaner syntax than
* making one directly and having to define the types twice.
*
* @param config Configuration for the resource sync type
* @returns The ResourceSyncType instance passed in.
*/
export declare function createResourceSyncType<const C extends ClientConstructor, const Cmd extends CommandConstructors, const K extends keyof ExtractOutputType<Cmd>, const ExtraFieldsFunc extends ExtraFieldsDefinition<C, Cmd, K>>(config: ResourceSyncType<C, Cmd, K, ExtraFieldsFunc>): ResourceSyncType<C, Cmd, K, ExtraFieldsFunc>;
/**
* Paginates a ResourceSyncType and returns the resources.
*
* @param resourceTypeSync The configuration to use.
* @param credentials The credentials to use for AWS API calls. Pass undefined to use the environment credentials.
* @param region The region to get the resources for.
* @returns Returns all the resources for the given type in the given region with extraFields populated.
*/
export declare function paginateResourceConfig<C extends ClientConstructor, Cmd extends CommandConstructors, K extends keyof ExtractOutputType<Cmd>, ExtraFieldsFunc extends ExtraFieldsDefinition<C, Cmd, K>>(resourceTypeSync: ResourceSyncType<C, Cmd, K, ExtraFieldsFunc>, credentials: AwsCredentialIdentityWithMetaData, region: string, endpoint: string | undefined): Promise<ExtendedResourceElementType<C, Cmd, K, ExtraFieldsFunc>[]>;
/**
* Create a typed sync operation for a given AWS service and resource type.
*
* Because of obscure typescript issues, the order of the keys in `resourceTypeSync` is important.
* This order is known to work:
* client, command, key, paginationConfig, arn, extraFields, tags, resourceTypeParts, results
*
* @param awsService the AWS service to sync
* @param name the name of the sync operation
* @param resourceTypeSync the resource type sync configuration
* @returns The sync operation
*/
export declare function createTypedSyncOperation<C extends ClientConstructor, Cmd extends CommandConstructors, K extends keyof ExtractOutputType<Cmd>, ExtraFieldsFunc extends ExtraFieldsDefinition<C, Cmd, K>>(awsService: AwsService, name: string, resourceTypeSync: ResourceSyncType<C, Cmd, K, ExtraFieldsFunc>): Sync;
export {};
//# sourceMappingURL=typedSync.d.ts.map