// It applies the configuration for resources of kind aws-copyami.
It applies the configuration for resources of kind aws-copyami.
// It applies the configuration for resources of kind aws - copyami .
func (r *Resource) apply(srcAmiInfo SrcAmiInfo, targetInfo TargetInfo) error {
	sess, err := awscommon.GetAwsSession(srcAmiInfo.CredsInfo)
	if err != nil {
		return err
	}
	svc := sts.New(sess)
	callerInfo, err := awscommon.GetCallerInfo(svc)
	if err != nil {
		return err
	}
	iamSVC := iam.New(sess)
	accountAlias, err := awscommon.GetAccountAlias(iamSVC)
	if err != nil {
		return err
	}
	r.Record.AccountMeta = awscommon.AccountMeta{
		ID:    callerInfo.Account,
		Alias: accountAlias,
	}
	srcAmiInfo.AccountID = callerInfo.Account
	sess.Config.Region = srcAmiInfo.Region
	ci := awscommon.AwsClientInfo{
		SVC:    ec2.New(sess),
		Region: sess.Config.Region,
	}
	images, err := awscommon.GetAmiInfo(ci, srcAmiInfo.Filters)
	if err != nil {
		return err
	}
	srcAmiInfo.Image = images[0]
	r.Record.SrcImage.Region = srcAmiInfo.Region
	r.Record.SrcImage.Name = images[0].Name
	r.Record.SrcImage.ID = images[0].ImageId
	r.Record.TargetImages = make(map[*string]awscommon.AmiMeta)
	errMap := map[string]error{}
	if targetInfo.CopyTags {
		targetInfo.Tags = append(targetInfo.Tags, srcAmiInfo.Image.Tags...)
	}
	for _, targetRegion := range targetInfo.Regions {
		wg.Add(1)
		go r.copyAmi(sess.Copy(&aws.Config{Region: targetRegion}), srcAmiInfo, targetInfo.Tags, errMap)
	}
	wg.Wait()
	if len(errMap) != 0 {
		clogger.Error("AMI Copy Operation Failed For following Regions:")
		for region, err := range errMap {
			clogger.Errorf("%s:\n", region)
			clogger.Errorf("\tReason: [%s] ", err)
		}
		return fmt.Errorf("Failed")
	}
	return nil
}
func ( r * Resource ) apply ( srcAmiInfo SrcAmiInfo , targetInfo TargetInfo ) error { sess , err := awscommon . GetAwsSession ( srcAmiInfo . CredsInfo ) if err != nil { return err } svc := sts . New ( sess ) callerInfo , err := awscommon . GetCallerInfo ( svc ) if err != nil { return err } iamSVC := iam . New ( sess ) accountAlias , err := awscommon . GetAccountAlias ( iamSVC ) if err != nil { return err } r . Record . AccountMeta = awscommon . AccountMeta { ID : callerInfo . Account , Alias : accountAlias , } srcAmiInfo . AccountID = callerInfo . Account sess . Config . Region = srcAmiInfo . Region ci := awscommon . AwsClientInfo { SVC : ec2 . New ( sess ) , Region : sess . Config . Region , } images , err := awscommon . GetAmiInfo ( ci , srcAmiInfo . Filters ) if err != nil { return err } srcAmiInfo . Image = images [ 0 ] r . Record . SrcImage . Region = srcAmiInfo . Region r . Record . SrcImage . Name = images [ 0 ] . Name r . Record . SrcImage . ID = images [ 0 ] . ImageId r . Record . TargetImages = make ( map [ * string ] awscommon . AmiMeta ) errMap := map [ string ] error { } if targetInfo . CopyTags { targetInfo . Tags = append ( targetInfo . Tags , srcAmiInfo . Image . Tags ... ) } for _ , targetRegion := range targetInfo . Regions { wg . Add ( 1 ) go r . copyAmi ( sess . Copy ( & aws . Config { Region : targetRegion } ) , srcAmiInfo , targetInfo . Tags , errMap ) } wg . Wait ( ) if len ( errMap ) != 0 { clogger . Error ( " " ) for region , err := range errMap { clogger . Errorf ( " \n " , region ) clogger . Errorf ( " \t " , err ) } return fmt . Errorf ( " " ) } return nil }
It applies the configuration for resources of kind aws-copyami.
It applies the configuration for resources of kind aws - copyami .