// attachInlinePolicy  will attach inline policy to the main IAM role
attachInlinePolicy  will attach inline policy to the main IAM role
// attachInlinePolicy will attach inline policy to the main IAM role
func (s *IAMService) attachInlinePolicy(roleName string, roleType string, params interface{}) error {
	l := s.log.WithValues("role_name", roleName)
	i := &awsiam.ListRolePoliciesInput{
		RoleName: aws.String(roleName),
	}
	alreadyExists := false
	o, err := s.iamClient.ListRolePolicies(i)
	if err == nil {
		for _, p := range o.PolicyNames {
			if *p == policyName(s.roleType, s.clusterName) {
				alreadyExists = true
				break
			}
		}
	}
	if !alreadyExists {
		tmpl := getInlinePolicyTemplate(roleType)
		policyDocument, err := generatePolicyDocument(tmpl, params)
		if err != nil {
			l.Error(err, "failed to generate inline policy document from template for IAM role")
			return err
		}
		i := &awsiam.PutRolePolicyInput{
			PolicyName:     aws.String(policyName(s.roleType, s.clusterName)),
			PolicyDocument: aws.String(policyDocument),
			RoleName:       aws.String(roleName),
		}
		_, err = s.iamClient.PutRolePolicy(i)
		if err != nil {
			l.Error(err, "failed to add inline policy to IAM Role")
			return err
		}
		l.Info("successfully added inline policy to IAM role")
	} else {
		l.Info("inline policy for IAM role already added, skipping")
	}
	return nil
}
func ( s * IAMService ) attachInlinePolicy ( roleName string , roleType string , params interface { } ) error { l := s . log . WithValues ( " " , roleName ) i := & awsiam . ListRolePoliciesInput { RoleName : aws . String ( roleName ) , } alreadyExists := false o , err := s . iamClient . ListRolePolicies ( i ) if err == nil { for _ , p := range o . PolicyNames { if * p == policyName ( s . roleType , s . clusterName ) { alreadyExists = true break } } } if ! alreadyExists { tmpl := getInlinePolicyTemplate ( roleType ) policyDocument , err := generatePolicyDocument ( tmpl , params ) if err != nil { l . Error ( err , " " ) return err } i := & awsiam . PutRolePolicyInput { PolicyName : aws . String ( policyName ( s . roleType , s . clusterName ) ) , PolicyDocument : aws . String ( policyDocument ) , RoleName : aws . String ( roleName ) , } _ , err = s . iamClient . PutRolePolicy ( i ) if err != nil { l . Error ( err , " " ) return err } l . Info ( " " ) } else { l . Info ( " " ) } return nil }
attachInlinePolicy  will attach inline policy to the main IAM role
attachInlinePolicy will attach inline policy to the main IAM role
// check if the inline policy already exists // add inline policy to the main IAM role if it do not exist yet