import { Resource } from "../resource.js";
import { ignore } from "../util/ignore.js";
import { importPeer } from "../util/peer.js";
import { retry } from "./retry.js";
/**
 * AWS IAM Policy Attachment Resource
 *
 * Attaches an IAM policy to a role, enabling the role to use the permissions defined in the policy.
 *
 * @example
 * // Attach an AWS managed policy to a role
 * const adminAccess = await PolicyAttachment("admin-policy", {
 *   policyArn: "arn:aws:iam::aws:policy/AdministratorAccess",
 *   roleName: role.name
 * });
 *
 * @example
 * // Attach a custom policy to a role
 * const customPolicy = await PolicyAttachment("custom-policy", {
 *   policyArn: policy.arn,
 *   roleName: role.name
 * });
 *
 * @example
 * // Attach multiple policies to a role
 * const s3Access = await PolicyAttachment("s3-access", {
 *   policyArn: "arn:aws:iam::aws:policy/AmazonS3FullAccess",
 *   roleName: role.name
 * });
 *
 * const sqsAccess = await PolicyAttachment("sqs-access", {
 *   policyArn: "arn:aws:iam::aws:policy/AmazonSQSFullAccess",
 *   roleName: role.name
 * });
 */
export const PolicyAttachment = Resource("iam::PolicyAttachment", async function (_id, props) {
    const { AttachRolePolicyCommand, DetachRolePolicyCommand, IAMClient, NoSuchEntityException, } = await importPeer(import("@aws-sdk/client-iam"), "iam::PolicyAttachment");
    const client = new IAMClient({});
    if (this.phase === "delete") {
        await ignore(NoSuchEntityException.name, () => retry(() => client.send(new DetachRolePolicyCommand({
            PolicyArn: props.policyArn,
            RoleName: props.roleName,
        }))));
        return this.destroy();
    }
    await retry(() => client.send(new AttachRolePolicyCommand({
        PolicyArn: props.policyArn,
        RoleName: props.roleName,
    })));
    return props;
});
//# sourceMappingURL=policy-attachment.js.map