AWSSDK.Tests.Framework.UtilityMethods.CreatePolicyIfNotExists C# (CSharp) Method

CreatePolicyIfNotExists() public static method

public static CreatePolicyIfNotExists ( IAmazonIdentityManagementService iamClient, string policyName, string policyDocument, string roleName ) : void
iamClient IAmazonIdentityManagementService
policyName string
policyDocument string
roleName string
return void
        public static void CreatePolicyIfNotExists(IAmazonIdentityManagementService iamClient, string policyName, string policyDocument, string roleName)
        {
            if (DoesRoleHavePolicy(iamClient, policyName, roleName))
            {
                return;
            }
            AutoResetEvent ars = new AutoResetEvent(false);
            Exception responseException = new Exception();
            iamClient.PutRolePolicyAsync(new PutRolePolicyRequest()
            {
                PolicyName = policyName,
                PolicyDocument = policyDocument,
                RoleName = roleName
            }, (response) =>
            {
                responseException = response.Exception;
                ars.Set();
            }, new AsyncOptions { ExecuteCallbackOnMainThread = false });
            ars.WaitOne();
            Assert.IsNull(responseException);
        }

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]);
            }
        }