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

79.31% Statements 92/116
93.75% Branches 15/16
85.71% Functions 6/7
79.31% Lines 92/116

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 1161x 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 12x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 9x 9x 9x 9x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 11x 11x                                                 11x 11x 11x 11x 1x 1x 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 { RabbitMqBrokerConfigurationDefinition } from './rabbitmq-broker-configuration-definition';
import { IRabbitMqBrokerDeployment } from './rabbitmq-broker-deployment';
import { RabbitMqBrokerEngineVersion } from './rabbitmq-broker-engine-version';
import { ActiveMqAuthenticationStrategy } from '../activemq/activemq-authentication-strategy';
import { BrokerEngine } from '../broker-deployment';
import { BrokerConfiguration, BrokerConfigurationAttributes, IBrokerConfiguration } from '../configuration';
import { ConfigurationAssociation } from '../configuration-association';
 
export interface RabbitMqBrokerConfigurationOptions {
  readonly description?: string;
  readonly definition: RabbitMqBrokerConfigurationDefinition;
}
 
export interface RabbitMqBrokerConfigurationProps extends RabbitMqBrokerConfigurationOptions {
  readonly name?: string;
  readonly engineVersion?: RabbitMqBrokerEngineVersion;
}
 
export interface IRabbitMqBrokerConfiguration extends IBrokerConfiguration {
  associateWith(broker: IRabbitMqBrokerDeployment): ConfigurationAssociation;
  createRevision(options: RabbitMqBrokerConfigurationOptions): IRabbitMqBrokerConfiguration;
}
 
export class RabbitMqBrokerConfiguration extends BrokerConfiguration {
  public static fromAttributes(scope: Construct, logicalId: string, attrs: BrokerConfigurationAttributes): IRabbitMqBrokerConfiguration {
    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 IRabbitMqBrokerConfiguration {
      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: IRabbitMqBrokerDeployment): ConfigurationAssociation {
        return new ConfigurationAssociation(this, 'Configuration', {
          broker,
          configuration: this,
        });
      }
 
      createRevision(options: RabbitMqBrokerConfigurationOptions): IRabbitMqBrokerConfiguration {
        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 RabbitMqBrokerConfiguration.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: RabbitMqBrokerConfigurationProps) {
    super(scope, id, {
      ...props,
      name: props.name ?? id,
      authenticationStrategy: ActiveMqAuthenticationStrategy.SIMPLE,
      engineVersion: props.engineVersion?.toString(),
      engine: BrokerEngine.RABBITMQ,
      data: props.definition.toString(),
    });
  }
 
  public associateWith(broker: IRabbitMqBrokerDeployment) {
    return this._associateWith(broker);
  }
 
  public createRevision(options: RabbitMqBrokerConfigurationOptions) {
    const revisor = this._createRevisor(options.definition.toString(), options.description);
 
    return RabbitMqBrokerConfiguration.fromAttributes(this, 'Revision', {
      id: revisor.getResponseField('Id'),
      arn: revisor.getResponseField('Arn'),
      revision: Token.asNumber(revisor.getResponseField('LatestRevision.Revision')),
    });
  }
}