add aws tags

This commit is contained in:
TangBin
2018-11-01 20:07:30 +08:00
parent bbcaf0084c
commit 914a8889bd
2 changed files with 33 additions and 1 deletions
+6 -1
View File
@@ -144,7 +144,12 @@ func (self *SInstance) IsEmulated() bool {
func (self *SInstance) GetMetadata() *jsonutils.JSONDict {
data := jsonutils.NewDict()
// todo: add price_key here
tags, err := FetchTags(self.host.zone.region.ec2Client, self.InstanceId)
if err != nil {
log.Errorf(err.Error())
}
data.Update(tags)
if len(self.ImageId) > 0 {
if image, err := self.host.zone.region.GetImage(self.ImageId); err != nil {
log.Errorf("Failed to find image %s for instance %s zone %s", self.ImageId, self.GetId(), self.ZoneId)
+27
View File
@@ -5,6 +5,7 @@ import (
"net"
"reflect"
"strings"
"yunion.io/x/jsonutils"
"github.com/aws/aws-sdk-go/service/ec2"
"yunion.io/x/log"
@@ -388,4 +389,30 @@ func NextDeviceName(curDeviceNames []string) (string, error) {
}
return "", fmt.Errorf("disk devicename out of index, current deivces: %s", currents)
}
// fetch tags
func FetchTags(client *ec2.EC2, resourceId string) (*jsonutils.JSONDict, error) {
result := jsonutils.NewDict()
params := &ec2.DescribeTagsInput{}
filters := []*ec2.Filter{}
if len(resourceId) == 0 {
return result,fmt.Errorf("resource id should not be empty")
}
// todo: add resource type filter
filters = AppendSingleValueFilter(filters, "resource-id", resourceId)
params.SetFilters(filters)
ret, err := client.DescribeTags(params)
if err != nil {
return result, err
}
for _, tag := range ret.Tags {
if tag.Key != nil && tag.Value != nil {
result.Set(*tag.Key, jsonutils.NewString(*tag.Value))
}
}
return result, nil
}