// Code generated by internal/service/ec2/generate/createtags/main.go; DO NOT EDIT.

package ec2

import (
	"fmt"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
	tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
	"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

const EventualConsistencyTimeout = 5 * time.Minute

// CreateTags creates ec2 service tags for new resources.
// The identifier is typically the Amazon Resource Name (ARN), although
// it may also be a different identifier depending on the service.
func CreateTags(conn *ec2.EC2, identifier string, tagsMap interface{}) error {
	tags := tftags.New(tagsMap)
	input := &ec2.CreateTagsInput{
		Resources: aws.StringSlice([]string{identifier}),
		Tags:      Tags(tags.IgnoreAWS()),
	}

	_, err := tfresource.RetryWhenNotFound(EventualConsistencyTimeout, func() (interface{}, error) {
		output, err := conn.CreateTags(input)

		if tfawserr.ErrCodeContains(err, ".NotFound") {
			err = &resource.NotFoundError{
				LastError:   err,
				LastRequest: input,
			}
		}

		return output, err
	})

	if err != nil {
		return fmt.Errorf("error tagging resource (%s): %w", identifier, err)
	}

	return nil
}
