AWSSDK.Tests.Framework.UtilityMethods.CreateRoleIfNotExists C# (CSharp) Метод

CreateRoleIfNotExists() публичный статический Метод

public static CreateRoleIfNotExists ( IAmazonIdentityManagementService iamClient, string roleName, string assumeRolePolicyDocument, bool waitForEventualConsistency = true ) : string
iamClient IAmazonIdentityManagementService
roleName string
assumeRolePolicyDocument string
waitForEventualConsistency bool
Результат string
        public static string CreateRoleIfNotExists(IAmazonIdentityManagementService iamClient, string roleName, string assumeRolePolicyDocument, bool waitForEventualConsistency = true)
        {
            AutoResetEvent ars = new AutoResetEvent(false);
            Exception responseException = new Exception();
            Role role = GetRoleIfExists(iamClient, roleName);
            if (role != null && !string.IsNullOrEmpty(role.Arn))
            {
                return role.Arn;
            }
            iamClient.CreateRoleAsync(new CreateRoleRequest
            {
                RoleName = roleName,
                AssumeRolePolicyDocument = assumeRolePolicyDocument
            }, (response) =>
            {
                responseException = response.Exception;
                if (responseException == null)
                {
                    role = response.Response.Role;
                }
                ars.Set();
            }, new AsyncOptions { ExecuteCallbackOnMainThread = false });
            ars.WaitOne();
            Assert.IsNull(responseException);
            if (waitForEventualConsistency)
            {
                // Wait for eventual consistency of IAM role:
                Thread.Sleep(TimeSpan.FromSeconds(10));
            }
            return role.Arn;
        }

Usage Example

        public static void Initialize()
        {
            LambdaClient = new AmazonLambdaClient(TestRunner.Credentials, TestRunner.RegionEndpoint);
            IAMClient    = new AmazonIdentityManagementServiceClient(TestRunner.Credentials, TestRunner.RegionEndpoint);

            RoleArn = UtilityMethods.CreateRoleIfNotExists(IAMClient, RoleName, UtilityMethods.LambdaAssumeRolePolicyDocument);
            UtilityMethods.CreatePolicyIfNotExists(IAMClient, RolePolicyName, RolePolicyDocument, RoleName);

            foreach (string[] functionInfo in FunctionNames)
            {
                CreateFunction(functionInfo[0], functionInfo[1], functionInfo[2]);
            }
        }