All files / lib/account-handler index.ts

100% Statements 22/22
94.74% Branches 18/19
100% Functions 2/2
100% Lines 22/22

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                                                1x               1x     2x   2x 1x   1x           1x 1x   1x               1x     4x   4x 1x     3x   3x 3x     3x   3x 3x   3x       2x     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);
 
  if (event.RequestType === "Create") {
    const awsOrganizationsClient = new Organizations({region: 'us-east-1'});
 
    const data = await awsOrganizationsClient
      .createAccount({
        Email: event.ResourceProperties.Email,
        AccountName: event.ResourceProperties.AccountName,
      })
      .promise();
    console.log("creat account: %j", data);
    return { PhysicalResourceId: data.CreateAccountStatus?.Id };
  }
  throw new Error("UpdateAccount 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":
    case "Update":
      // Complete when replica is reported as ACTIVE
      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");
  }
}