當前位置: 首頁>>代碼示例>>Golang>>正文


Golang IAM.ListRoles方法代碼示例

本文整理匯總了Golang中github.com/aws/aws-sdk-go/service/iam.IAM.ListRoles方法的典型用法代碼示例。如果您正苦於以下問題:Golang IAM.ListRoles方法的具體用法?Golang IAM.ListRoles怎麽用?Golang IAM.ListRoles使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/aws/aws-sdk-go/service/iam.IAM的用法示例。


在下文中一共展示了IAM.ListRoles方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: GetAccountId

func GetAccountId(iamconn *iam.IAM, stsconn *sts.STS, authProviderName string) (string, error) {
	// If we have creds from instance profile, we can use metadata API
	if authProviderName == ec2rolecreds.ProviderName {
		log.Println("[DEBUG] Trying to get account ID via AWS Metadata API")

		cfg := &aws.Config{}
		setOptionalEndpoint(cfg)
		metadataClient := ec2metadata.New(session.New(cfg))
		info, err := metadataClient.IAMInfo()
		if err != nil {
			// This can be triggered when no IAM Role is assigned
			// or AWS just happens to return invalid response
			return "", fmt.Errorf("Failed getting EC2 IAM info: %s", err)
		}

		return parseAccountIdFromArn(info.InstanceProfileArn)
	}

	// Then try IAM GetUser
	log.Println("[DEBUG] Trying to get account ID via iam:GetUser")
	outUser, err := iamconn.GetUser(nil)
	if err == nil {
		return parseAccountIdFromArn(*outUser.User.Arn)
	}

	awsErr, ok := err.(awserr.Error)
	// AccessDenied and ValidationError can be raised
	// if credentials belong to federated profile, so we ignore these
	if !ok || (awsErr.Code() != "AccessDenied" && awsErr.Code() != "ValidationError") {
		return "", fmt.Errorf("Failed getting account ID via 'iam:GetUser': %s", err)
	}
	log.Printf("[DEBUG] Getting account ID via iam:GetUser failed: %s", err)

	// Then try STS GetCallerIdentity
	log.Println("[DEBUG] Trying to get account ID via sts:GetCallerIdentity")
	outCallerIdentity, err := stsconn.GetCallerIdentity(&sts.GetCallerIdentityInput{})
	if err == nil {
		return *outCallerIdentity.Account, nil
	}
	log.Printf("[DEBUG] Getting account ID via sts:GetCallerIdentity failed: %s", err)

	// Then try IAM ListRoles
	log.Println("[DEBUG] Trying to get account ID via iam:ListRoles")
	outRoles, err := iamconn.ListRoles(&iam.ListRolesInput{
		MaxItems: aws.Int64(int64(1)),
	})
	if err != nil {
		return "", fmt.Errorf("Failed getting account ID via 'iam:ListRoles': %s", err)
	}

	if len(outRoles.Roles) < 1 {
		return "", fmt.Errorf("Failed getting account ID via 'iam:ListRoles': No roles available")
	}

	return parseAccountIdFromArn(*outRoles.Roles[0].Arn)
}
開發者ID:RezaDKhan,項目名稱:terraform,代碼行數:56,代碼來源:auth_helpers.go

示例2: ListRole

func (r *Role) ListRole(cli *iam.IAM) {
	req := &iam.ListRolesInput{}
	resp, err := cli.ListRoles(req)
	raiseError(err)

	if len(resp.Roles) < 1 {
		fmt.Println("not found")
		return
	}

	for i := 0; i < len(resp.Roles); i++ {
		fmt.Printf("%s %s\n",
			*resp.Roles[i].RoleId,
			*resp.Roles[i].RoleName,
		)

		r.ListRolePolicies(cli, *resp.Roles[i].RoleName)
	}
}
開發者ID:honeybe,項目名稱:code-sample,代碼行數:19,代碼來源:role.go


注:本文中的github.com/aws/aws-sdk-go/service/iam.IAM.ListRoles方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。