All files / lib/account-handler index.ts

94.74% Statements 36/38
88.46% Branches 23/26
100% Functions 4/4
94.44% Lines 34/36

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                                                1x                 1x     2x   2x   1x 1x 1x 1x 7x   1x             1x 1x         1x                       1x     5x   5x 1x     4x   4x 4x     4x   4x 4x   4x   2x   1x 1x 1x 1x 5x   1x           1x   1x     1x      
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
/* eslint-disable no-console */
import type {
  IsCompleteRequest,
  IsCompleteResponse,
  OnEventResponse,
} from "@aws-cdk/custom-resources/lib/provider-framework/types";
 
// eslint-disable-line import/no-extraneous-dependencies
import { Organizations } from "aws-sdk"; 
 
 
/**
 * A function capable of creating an account into an AWS Organisation
 * @param event An event with the following ResourceProperties: Email (coresponding to the account email) and AccountName (corresponding to the account name)
 * @returns Returns a PhysicalResourceId corresponding to the CreateAccount request's id necessary to check the status of the creation
 */
 
export async function onEventHandler(
  event: any
): Promise<OnEventResponse> {
  console.log("Event: %j", event);
 
  switch (event.RequestType) {
    case "Create":
      const awsOrganizationsClient = new Organizations({region: 'us-east-1'});
      try {
        const tags: { Key: string; Value: any; }[] = [];
        Object.keys(event.ResourceProperties).forEach( propertyKey => {
          if( propertyKey != 'ServiceToken' ) tags.push({Key: propertyKey, Value: event.ResourceProperties[propertyKey]});
        });
        const data = await awsOrganizationsClient
        .createAccount({
          Email: event.ResourceProperties.Email,
          AccountName: event.ResourceProperties.AccountName,
          Tags: tags
        })
        .promise();
        console.log("create account: %j", data);
        return { PhysicalResourceId: data.CreateAccountStatus?.Id };
      } catch (error) {
        throw new Error(`Failed to create account: ${error}`);
      }
    case "Update":
      return { PhysicalResourceId: event.PhysicalResourceId, ResourceProperties: event.ResourceProperties };
    default:
      throw new Error(`${event.RequestType} is not a supported operation`);
  }
 
}
 
/**
 * A function capable to check if an account creation request has been completed
 * @param event An event containing a PhysicalResourceId corresponding to a CreateAccountRequestId
 * @returns A payload containing the IsComplete Flag requested by cdk Custom Resource fwk to figure out if the resource has been created or failed to be or if it needs to retry
 */
export async function isCompleteHandler(
  event: IsCompleteRequest
): Promise<IsCompleteResponse> {
  console.log("Event: %j", event);
 
  if(!event.PhysicalResourceId) {
    throw new Error("Missing PhysicalResourceId parameter.");
  }
 
  const awsOrganizationsClient = new Organizations({region: 'us-east-1'});
 
  const describeCreateAccountStatusParams : Organizations.DescribeCreateAccountStatusRequest = {CreateAccountRequestId: event.PhysicalResourceId}
  const data: Organizations.DescribeCreateAccountStatusResponse  = await awsOrganizationsClient
    .describeCreateAccountStatus(describeCreateAccountStatusParams).promise();
 
  console.log("Describe account: %j", data);
 
  const CreateAccountStatus = data.CreateAccountStatus?.State;
  const AccountId = data.CreateAccountStatus?.AccountId;
 
  switch (event.RequestType) {
    case "Create":
      return { IsComplete: CreateAccountStatus === "SUCCEEDED", Data: {AccountId: AccountId} };
    case "Update":
      Eif(AccountId) {
        console.log(`Add tags: type = ${event.ResourceProperties.AccountType}`);
        const tags: { Key: string; Value: any; }[] = [];
        Object.keys(event.ResourceProperties).forEach( propertyKey => {
          if( propertyKey != 'ServiceToken' ) tags.push({Key: propertyKey, Value: event.ResourceProperties[propertyKey]});
        });
        const tagsUpdateRequestData = await awsOrganizationsClient
        .tagResource({
          ResourceId: AccountId!,
          Tags: tags
        })
        .promise();
        console.log("Updated account tags: %j", tagsUpdateRequestData);
      }
        return { IsComplete: CreateAccountStatus === "SUCCEEDED", Data: {AccountId: AccountId} };
    case "Delete":
      // TODO: figure out what to do here
      throw new Error("DeleteAccount is not a supported operation");
  }
}