//snippet-sourcedescription:[DetachRolePolicy.java demonstrates how to detache a policy from an IAM role.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[iam]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[]
//snippet-sourceauthor:[soo-aws]
/*
 * Copyright 2010-2019 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.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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.
 */
package com.example.iam;
// snippet-start:[iam.java2.detach_role_policy.complete]
// snippet-start:[iam.java2.detach_role_policy.import]
import software.amazon.awssdk.services.iam.model.DetachRolePolicyRequest;
import software.amazon.awssdk.services.iam.model.DetachRolePolicyResponse;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.iam.IamClient;
 
// snippet-end:[iam.java2.detach_role_policy.import]
/**
 * Detaches a policy from a role
 */
public class DetachRolePolicy {
    public static void main(String[] args) {

        final String USAGE =
            "To run this example, supply a role name and policy arn\n" +
            "Ex: DetachRolePolicy <role-name> <policy-arn>>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String role_name = args[0];
        String policy_arn = args[1];

        // snippet-start:[iam.java2.detach_role_policy.main]
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder().region(region).build();

        DetachRolePolicyRequest request = DetachRolePolicyRequest.builder()
            .roleName(role_name)
            .policyArn(policy_arn).build();

        DetachRolePolicyResponse response = iam.detachRolePolicy(request);
        // snippet-end:[iam.java2.detach_role_policy.main]
        
        System.out.println("Successfully detached policy " + policy_arn +
                " from role " + role_name);
    }
}
// snippet-end:[iam.java2.detach_role_policy.complete]

@@ -70,17 +70,16 @@
 o detach
-e
  a polic
@@ -255,16 +255,26 @@
 cedate:%5B
+03/02/2020
 %5D%0D%0A//sni
@@ -297,10 +297,15 @@
 r:%5Bs
-oo
+cmacdon
 -aws
@@ -328,18 +328,8 @@
 ght 
-2010-2019 
 Amaz
@@ -1187,18 +1187,16 @@
 ponse;%0D%0A
-%0D%0A
 import s
@@ -1289,17 +1289,80 @@
 lient;%0D%0A
- 
+import software.amazon.awssdk.services.iam.model.IamException;%0D%0A
 %0D%0A// sni
@@ -1576,16 +1576,20 @@
         
+    
 %22To run 
@@ -1638,24 +1638,36 @@
 y arn%5Cn%22 +%0D%0A
+            
             
@@ -1717,25 +1717,24 @@
 rn%3E%3E%5Cn%22;%0D%0A%0D%0A
- 
        if (a
@@ -1747,17 +1747,17 @@
 ngth != 
-1
+2
 ) %7B%0D%0A   
@@ -1829,17 +1829,16 @@
 %0A       
- 
 %7D%0D%0A%0D%0A   
@@ -1853,18 +1853,17 @@
 ing role
-_n
+N
 ame = ar
@@ -1891,18 +1891,17 @@
 g policy
-_a
+A
 rn = arg
@@ -2078,32 +2078,53 @@
 on).build();%0D%0A%0D%0A
+        try %7B%0D%0A%0D%0A    
         DetachRo
@@ -2184,16 +2184,20 @@
 lder()%0D%0A
+    
         
@@ -2218,16 +2218,19 @@
 role
-_n
+N
 ame)%0D%0A
+    
     
@@ -2254,18 +2254,17 @@
 n(policy
-_a
+A
 rn).buil
@@ -2263,32 +2263,37 @@
 rn).build();%0D%0A%0D%0A
+     
         DetachRo
@@ -2352,32 +2352,37 @@
 uest);%0D%0A        
+ 
+    
 // snippet-end:%5B
@@ -2421,26 +2421,22 @@
 n%5D%0D%0A
- 
+%0D%0A
        
-%0D%0A
      
-   
 Syst
@@ -2494,10 +2494,9 @@
 licy
-_a
+A
 rn +
@@ -2537,13 +2537,196 @@
 role
-_name
+Name);%0D%0A%0D%0A        %7D catch (IamException e) %7B%0D%0A            System.err.println(e.awsErrorDetails().errorMessage());%0D%0A            System.exit(1);%0D%0A        %7D%0D%0A        System.out.println(%22Done%22
 );%0D%0A

Update DetachRolePolicy.java