diff --git a/pkg/util/aws/instance.go b/pkg/util/aws/instance.go index cc1e29f390..df5678fea2 100644 --- a/pkg/util/aws/instance.go +++ b/pkg/util/aws/instance.go @@ -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) diff --git a/pkg/util/aws/utils.go b/pkg/util/aws/utils.go index 143bc3b0db..045ed00547 100644 --- a/pkg/util/aws/utils.go +++ b/pkg/util/aws/utils.go @@ -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 } \ No newline at end of file