Exemple #1
0
// setTags is a helper to set the tags for a resource. It expects the
// tags field to be named "tags"
func setTagsS3(conn *s3.S3, d *schema.ResourceData) error {
	if d.HasChange("tags") {
		oraw, nraw := d.GetChange("tags")
		o := oraw.(map[string]interface{})
		n := nraw.(map[string]interface{})
		create, remove := diffTagsS3(tagsFromMapS3(o), tagsFromMapS3(n))

		// Set tags
		if len(remove) > 0 {
			log.Printf("[DEBUG] Removing tags: %#v", remove)
			err := conn.DeleteBucketTagging(&s3.DeleteBucketTaggingRequest{
				Bucket: aws.String(d.Get("bucket").(string)),
			})
			if err != nil {
				return err
			}
		}
		if len(create) > 0 {
			log.Printf("[DEBUG] Creating tags: %#v", create)
			tagging := s3.Tagging{
				TagSet: create,
				XMLName: xml.Name{
					Space: "http://s3.amazonaws.com/doc/2006-03-01/",
					Local: "Tagging",
				},
			}
			// AWS S3 API requires us to send a base64 encoded md5 hash of the
			// content, which we need to build ourselves since aws-sdk-go does not.
			b, err := xml.Marshal(tagging)
			if err != nil {
				return err
			}
			h := md5.New()
			h.Write(b)
			base := base64.StdEncoding.EncodeToString(h.Sum(nil))

			req := &s3.PutBucketTaggingRequest{
				Bucket:     aws.String(d.Get("bucket").(string)),
				ContentMD5: aws.String(base),
				Tagging:    &tagging,
			}

			err = conn.PutBucketTagging(req)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
Exemple #2
0
// return a slice of s3 tags associated with the given s3 bucket. Essentially
// s3.GetBucketTagging, except returns an empty slice instead of an error when
// there are no tags.
func getTagSetS3(s3conn *s3.S3, bucket string) ([]s3.Tag, error) {
	request := &s3.GetBucketTaggingRequest{
		Bucket: aws.String(bucket),
	}

	response, err := s3conn.GetBucketTagging(request)
	if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "NoSuchTagSet" {
		// There is no tag set associated with the bucket.
		return []s3.Tag{}, nil
	} else if err != nil {
		return nil, err
	}

	return response.TagSet, nil
}