mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
add aws tags
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user