All files / src/activemq activemq-broker-configuration.ts

80.32% Statements 98/122
86.66% Branches 13/15
85.71% Functions 6/7
80.32% Lines 98/122

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 1221x 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 16x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 13x 13x 13x 13x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 15x 15x                                                 15x 15x 15x 15x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 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, Resource, Stack, Token } from 'aws-cdk-lib';
import { AwsCustomResource, AwsCustomResourcePolicy, AwsSdkCall, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import { ActiveMqAuthenticationStrategy } from './activemq-authentication-strategy';
import { ActiveMqBrokerConfigurationDefinition } from './activemq-broker-configuration-definition';
import { IActiveMqBrokerDeployment } from './activemq-broker-deployment';
import { ActiveMqBrokerEngineVersion } from './activemq-broker-engine-version';
import { BrokerEngine } from '../broker-deployment';
import { BrokerConfiguration, BrokerConfigurationAttributes, IBrokerConfiguration } from '../configuration';
import { ConfigurationAssociation } from '../configuration-association';
 
export interface ActiveMqBrokerConfigurationOptions {
  readonly description?: string;
  readonly definition: ActiveMqBrokerConfigurationDefinition;
}
 
export interface ActiveMqBrokerConfigurationProps extends ActiveMqBrokerConfigurationOptions {
  readonly name?: string;
  readonly engineVersion?: ActiveMqBrokerEngineVersion;
  readonly authenticationStrategy?: ActiveMqAuthenticationStrategy;
}
 
export interface IActiveMqBrokerConfiguration extends IBrokerConfiguration {
  associateWith(broker: IActiveMqBrokerDeployment): ConfigurationAssociation;
  createRevision(options: ActiveMqBrokerConfigurationOptions): IActiveMqBrokerConfiguration;
}
 
export class ActiveMqBrokerConfiguration extends BrokerConfiguration {
  public static fromAttributes(scope: Construct, logicalId: string, attrs: BrokerConfigurationAttributes): IActiveMqBrokerConfiguration {
    if (attrs.id === undefined && attrs.arn === undefined) {
      throw new Error('Either id or arn must be provided');
    }
 
    const { id, arn } = attrs;
 
    class Import extends Resource implements IActiveMqBrokerConfiguration {
      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!;
      }
 
      associateWith(broker: IActiveMqBrokerDeployment): ConfigurationAssociation {
        return new ConfigurationAssociation(this, 'Configuration', {
          broker,
          configuration: this,
        });
      }
 
      createRevision(options: ActiveMqBrokerConfigurationOptions): IActiveMqBrokerConfiguration {
        const call: AwsSdkCall = {
          service: 'mq',
          action: 'UpdateConfiguration',
          parameters: {
            ConfigurationId: this.id,
            Data: Fn.base64(options.definition.toString()),
            Description: options.description,
          },
          physicalResourceId: PhysicalResourceId.fromResponse('Id'),
        };

        const revisor = new AwsCustomResource(this, 'Revisor', {
          onCreate: call,
          policy: AwsCustomResourcePolicy.fromSdkCalls({
            resources: [this.arn],
          }),
        });

        return ActiveMqBrokerConfiguration.fromAttributes(this, 'Revision', {
          id: revisor.getResponseField('Id'),
          arn: revisor.getResponseField('Arn'),
          revision: Token.asNumber(revisor.getResponseField('LatestRevision.Revision')),
        });
      }
    }
 
    return new Import();
  }
 
  constructor(scope: Construct, id: string, props: ActiveMqBrokerConfigurationProps) {
    super(scope, id, {
      ...props,
      name: props.name ?? id,
      authenticationStrategy: props.authenticationStrategy,
      engineVersion: props.engineVersion?.toString(),
      engine: BrokerEngine.ACTIVEMQ,
      data: props.definition.toString(),
    });
 
    // TODO: Add some validation regarding the configuration contents
    //       with SIMPLE auth strategy we need to have AuthorizationMap in data
    //       with LDAP auth strategy we need to have CachedLDAPAuthorizationMap in data
  }
 
  public associateWith(broker: IActiveMqBrokerDeployment) {
    return this._associateWith(broker);
  }
 
  public createRevision(options: ActiveMqBrokerConfigurationOptions) {
    const revisor = this._createRevisor(options.definition.toString(), options.description);
 
    return ActiveMqBrokerConfiguration.fromAttributes(this, 'Revision', {
      id: revisor.getResponseField('Id'),
      arn: revisor.getResponseField('Arn'),
      revision: Token.asNumber(revisor.getResponseField('LatestRevision.Revision')),
    });
  }
}