def is_complete()

in lambda/workmail-org-user-domain-lambda/workmailcreateorg.py [0:0]


def is_complete(event, context):
    org_name = os.environ['work_org_name']
    user_name = os.environ['user_name']
    password = os.environ['password']
    physical_id = event["PhysicalResourceId"]
    request_type = event["RequestType"]

    if request_type == 'Create':
    # check if resource is stable based on request_type
        is_response = client.list_organizations()
        #print(is_response)
        is_ready = False
        for i in is_response['OrganizationSummaries']:
            #checking the status of the organization created
            if i['State'] == "Active" and i['Alias'] == org_name:
                print ('user getting created.....')
                #if yes creating the user and registering the user is done. if not goes to exception block
                try:
                    user_response = client.create_user(
                                        OrganizationId=i['OrganizationId'],
                                        Name=user_name,
                                        DisplayName=user_name,
                                        Password=password)
                    print(user_response)
                    reg_user_response = client.register_to_work_mail(
                                        OrganizationId=i['OrganizationId'],
                                        EntityId=user_response['UserId'],
                                        Email=user_name+'@'+org_name+'.awsapps.com')
                    print(reg_user_response)
                    is_ready = True
                except ClientError as e:
                    print(e.response)
                    # if the error is NameAvailabilityException, it will go for user registeration for email
                    if e.response['Error']['Code'] == 'NameAvailabilityException':
                        print("User already exists..registering user to the domain")
                        try:
                            user_avail = client.list_users(
                                                OrganizationId=i['OrganizationId']
                                                )
                            for x in user_avail['Users']:
                                print('start registering users')
                                if x['Name'] == user_name:
                                    print(x['Id'])
                                    try:    
                                        reg_user_response = client.register_to_work_mail(
                                                    OrganizationId=i['OrganizationId'],
                                                    EntityId=x['Id'],
                                                    Email=user_name+'@'+org_name+'.awsapps.com')
                                        is_ready = True
                                        print (reg_user_response)
                                    except ClientError as g:
                                        print(g.response)
                                        if g.response['Error']['Code'] == 'MailDomainStateException':
                                            print('Domain is still getting created..please wait')
                                            is_ready = False
                                        else:                                       
                                            print("Unexpected error: %s" % g) 
                                            #is_ready = True 
                                            raise g
                        except ClientError as f:
                            print(f.response)
                            # if the error is NameAvailabilityException (means user already exist) and should go to user registration
                            if f.response['Error']['Code'] == 'NameAvailabilityException':
                                is_ready = False
                                print ('Domain is still getting created..please wait')
                            else:
                                print("Unexpected error on listing users: %s" % f)
                                #is_ready = True
                                raise f
                    elif e.response['Error']['Code'] == 'MailDomainStateException':
                        print('Domain is still getting created..please wait')
                        is_ready = False
                    else:
                        print("Unexpected error: %s" % e)     
                        #is_ready = True
                        raise e
        return { 'IsComplete': is_ready }
    elif request_type == 'Delete':
        is_ready = True
        return { 'IsComplete': is_ready }