All files / src configuration.ts

80% Statements 128/160
84.61% Branches 11/13
87.5% Functions 7/8
80% Lines 128/160

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 1601x 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 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 4x 4x 3x 3x 4x     4x 4x 1x 1x 4x 4x 4x 3x 3x 4x           4x 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
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
import { Arn, ArnFormat, Fn, IResource, Lazy, Resource, Stack } from 'aws-cdk-lib';
import { CfnConfiguration } from 'aws-cdk-lib/aws-amazonmq';
import { AwsCustomResource, AwsCustomResourcePolicy, AwsSdkCall, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import { ActiveMqAuthenticationStrategy } from './activemq/activemq-authentication-strategy';
import { BrokerEngine, IBrokerDeployment } from './broker-deployment';
import { ConfigurationAssociation } from './configuration-association';
 
export interface ConfigurationProps {
  readonly name: string;
  readonly description?: string;
  readonly data: string;
  readonly engine: BrokerEngine;
  readonly engineVersion?: string;
  readonly authenticationStrategy?: ActiveMqAuthenticationStrategy;
}
 
export interface IBrokerConfiguration extends IResource {
  readonly arn: string;
  readonly id: string;
  readonly revision: number;
}
 
export interface BrokerConfigurationAttributes {
  readonly id?: string;
  readonly arn?: string;
  readonly revision: number;
}
 
export abstract class BrokerConfiguration extends Resource implements IBrokerConfiguration {
 
  /***
   * @internal
   */
  protected static _fromAttributes(scope: Construct, logicalId: string, attrs: BrokerConfigurationAttributes): IBrokerConfiguration {
    if (attrs.id === undefined && attrs.arn === undefined) {
      throw new Error("Either 'id' or 'arn' needs to be defined");
    }

    const { id, arn } = attrs;

    class Import extends Resource implements IBrokerConfiguration {
      public readonly arn: string;
      public readonly id: string;
      public readonly revision: number;
      constructor() {
        super(scope, logicalId);
        this.revision = attrs.revision;
        this.arn = arn ? arn : Stack.of(this).formatArn({
          service: 'mq',
          resource: 'configuration',
          resourceName: id,
          arnFormat: ArnFormat.COLON_RESOURCE_NAME,
        });
        this.id = id ? id : Arn.split(arn!, ArnFormat.COLON_RESOURCE_NAME).resourceName!;
      }
    }

    return new Import();
  }
 
  public readonly arn: string;
  public readonly id: string;
  public readonly revision: number;
 
  /** @internal */
  protected _authenticationStrategy: string | undefined;
  private isAuthenticationStrategySet: boolean = false;
  /***
   * @internal
   */
  protected _engineVersion: string | undefined;
  private isEngineVersionSet: boolean = false;
 
  constructor(scope: Construct, id: string, props: ConfigurationProps) {
    super(scope, id);
 
    const resource = new CfnConfiguration(this, 'Resource', {
      name: props.name,
      description: props.description,
      data: Fn.base64(props.data),
      engineType: props.engine,
      authenticationStrategy: Lazy.string({ produce: () => this._authenticationStrategy }),
      engineVersion: Lazy.string({ produce: () => this._engineVersion }),
    });
 
    this.arn = resource.attrArn;
    this.id = resource.ref;
    this.revision = resource.attrRevision;
    this.configureEngineVersion(props.engineVersion);
    this.configureAuthenticationStrategy(props.authenticationStrategy);
  }
 
  private configureEngineVersion(engineVersion: string | undefined) {
    if (engineVersion) {
      if (!this.isEngineVersionSet) {
        this._engineVersion = engineVersion;
        this.isEngineVersionSet = true;
      } else if (this._engineVersion !== engineVersion) {
        throw new Error(`A configuraiton can be associated with only one engine version. (current: ${this._engineVersion}, new: ${engineVersion})`);
      }
    }
  }
 
  private configureAuthenticationStrategy(authenticationStrategy: string | undefined) {
    // TODO: think if this is the best approach
    //       maybe we should compeare and fail if things are/aren't set
    if (!this.isAuthenticationStrategySet) {
      this._authenticationStrategy = authenticationStrategy;
      this.isAuthenticationStrategySet = true;
    } else if (this._authenticationStrategy !== authenticationStrategy) {
      // INFO: this way, as the default is simple, so an undefined will equal SIMPLE
      if (this._authenticationStrategy === ActiveMqAuthenticationStrategy.LDAP || authenticationStrategy === ActiveMqAuthenticationStrategy.LDAP) {
        throw new Error(`A configuration can be assigned only a single authentication strategy. (current: ${this._authenticationStrategy}, new: ${authenticationStrategy})`);
      }
    }
  }
 
  /***
   * @internal
   */
  protected _associateWith(broker: IBrokerDeployment) {
    this.configureEngineVersion(broker._engineVersion);
    this.configureAuthenticationStrategy(broker._authenticationStrategy);
 
    return new ConfigurationAssociation(this, 'Configuration', {
      broker,
      configuration: this,
    });
  }
 
  /***
   * @internal
   */
  protected _createRevisor(data: string, description?: string) {
    const call: AwsSdkCall = {
      service: 'mq',
      action: 'UpdateConfiguration',
      parameters: {
        ConfigurationId: this.id,
        Data: Fn.base64(data),
        Description: description,
      },
      physicalResourceId: PhysicalResourceId.fromResponse('Id'),
    };
 
    const revisor = new AwsCustomResource(this, 'Revisor', {
      onCreate: call,
      policy: AwsCustomResourcePolicy.fromSdkCalls({
        resources: [this.arn],
      }),
    });
 
    return revisor;
  }
}