All files / src policy-template.ts

100% Statements 131/131
100% Branches 6/6
100% Functions 4/4
100% Lines 131/131

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x  
import { CfnPolicyTemplate } from 'aws-cdk-lib/aws-verifiedpermissions';
import { IResource, Resource } from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
import { IPolicyStore } from './policy-store';
 
export interface IPolicyTemplate extends IResource {
  /**
   * The ID of the policy template.
   *
   * @attribute
   */
  readonly policyTemplateId: string;
}
 
export interface PolicyTemplateProps {
  /**
   * Specifies the content that you want to use for the new policy template, written in the Cedar policy language.
   *
   * @default - The statement to attach to the new or updated policy template.
   */
  readonly statement: string;
 
  /**
   * The description to attach to the new or updated policy template.
   *
   * @default - No description.
   */
  readonly description?: string;
 
  /**
   * The policy store that contains the template.
   *
   * @default - No policy store.
   */
  readonly policyStore?: IPolicyStore;
}
 
export interface PolicyTemplateAttributes {
  /**
   * The id of the Amazon Verified Permissions PolicyTemplate.
   */
  readonly policyTemplateId: string;
}
 
abstract class PolicyTemplateBase extends Resource implements IPolicyTemplate {
  abstract readonly policyTemplateId: string;
}
 
export class PolicyTemplate extends PolicyTemplateBase {
  /**
   * Create a PolicyTemplate construct that represents an external policy template via policy template id.
   *
   * @param scope The parent creating construct (usually `this`).
   * @param id The construct's name.
   * @param policyTemplateId The PolicyTemplate's id.
   */
  public static fromPolicyTemplateId(
    scope: Construct,
    id: string,
    policyTemplateId: string,
  ): IPolicyTemplate {
    return PolicyTemplate.fromPolicyTemplateAttributes(scope, id, {
      policyTemplateId,
    });
  }
 
  /**
   * Creates a PolicyStore construct that represents an external Policy Store.
   *
   * @param scope The parent creating construct (usually `this`).
   * @param id The construct's name.
   * @param attrs A `PolicyStoreAttributes` object.
   */
  public static fromPolicyTemplateAttributes(
    scope: Construct,
    id: string,
    attrs: PolicyTemplateAttributes,
  ): IPolicyTemplate {
    class Import extends PolicyTemplateBase {
      readonly policyTemplateId: string;
 
      constructor(policyTemplateId: string) {
        super(scope, id);
 
        this.policyTemplateId = policyTemplateId;
      }
    }
 
    let policyTemplateId: string;
    policyTemplateId = attrs.policyTemplateId;
 
    return new Import(policyTemplateId);
  }
 
  private readonly policyTemplate: CfnPolicyTemplate;
  /**
   * The ID of the policy template.
   *
   * @attribute
   */
  readonly policyTemplateId: string;
 
  /**
   * The statement of the policy template.
   */
  readonly statement: string;
 
  /**
   * Description of the policy template.
   */
  readonly description?: string;
 
  /**
   * The Policy store that contains the template.
   */
  readonly policyStore?: IPolicyStore;
 
  constructor(scope: Construct, id: string, props: PolicyTemplateProps) {
    super(scope, id);
 
    this.policyTemplate = new CfnPolicyTemplate(this, id, {
      statement: props.statement,
      description: props.description,
      policyStoreId: props.policyStore?.policyStoreId,
    });
    this.policyTemplateId = this.policyTemplate.attrPolicyTemplateId;
    this.statement = this.policyTemplate.statement;
    this.description = props.description;
    this.policyStore = props.policyStore;
  }
}