登录
首页 >  Golang >  Go问答

AWS Config 通知的正确格式是怎样的?

来源:stackoverflow

时间:2024-03-08 11:00:26 376浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《AWS Config 通知的正确格式是怎样的?》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我对 Go 还很陌生,我必须使用 aws go sdk 来读取来自 SQS 的 AWS Config 通知。 (AWS 配置服务 -> sns -> sqs) 我能够收到消息。但我想进入消息获取资源类型、资源Id、awsRegion 等信息。 这是我的示例消息字符串(字符串化的 json)。 https://gist.github.com/HarishAtGitHub/fcbb01515d11044d04bde14a3d9f6e7a

我有Python背景,在Python中这很容易做到,因为json就像一本字典。我们可以通过嵌套索引轻松获取它。 但在 go 中,我似乎应该使用正确的结构来理解此消息。

有人可以向我指出正确的结构或有关如何获取消息中不同属性的任何想法吗?


解决方案


我总是使用 this 工具从 json blob 生成结构定义。您自己需要做的唯一一件事就是为 null 值选择一个类型:它显然无法确定这些值,因此将它们设置为 interface{} (任何类型都可以实现此功能)。

如果您只想解组,则可以省略结构标记(例如 json:"changetype"),而不是相反。

type ConfigNotification struct {
    ConfigurationItemDiff struct {
        ChangedProperties struct {
        } `json:"changedProperties"`
        ChangeType string `json:"changeType"`
    } `json:"configurationItemDiff"`
    ConfigurationItem struct {
        RelatedEvents []interface{} `json:"relatedEvents"`
        Relationships []interface{} `json:"relationships"`
        Configuration struct {
            Attachments      []interface{} `json:"attachments"`
            AvailabilityZone string        `json:"availabilityZone"`
            CreateTime       time.Time     `json:"createTime"`
            Encrypted        bool          `json:"encrypted"`
            KmsKeyID         interface{}   `json:"kmsKeyId"`
            Size             int           `json:"size"`
            SnapshotID       string        `json:"snapshotId"`
            State            string        `json:"state"`
            VolumeID         string        `json:"volumeId"`
            Iops             int           `json:"iops"`
            Tags             []struct {
                Key   string `json:"key"`
                Value string `json:"value"`
            } `json:"tags"`
            VolumeType string `json:"volumeType"`
        } `json:"configuration"`
        SupplementaryConfiguration struct {
        } `json:"supplementaryConfiguration"`
        Tags struct {
            Creator string `json:"creator"`
        } `json:"tags"`
        ConfigurationItemVersion     string      `json:"configurationItemVersion"`
        ConfigurationItemCaptureTime time.Time   `json:"configurationItemCaptureTime"`
        ConfigurationStateID         int64       `json:"configurationStateId"`
        AwsAccountID                 string      `json:"awsAccountId"`
        ConfigurationItemStatus      string      `json:"configurationItemStatus"`
        ResourceType                 string      `json:"resourceType"`
        ResourceID                   string      `json:"resourceId"`
        ResourceName                 interface{} `json:"resourceName"`
        ARN                          string      `json:"ARN"`
        AwsRegion                    string      `json:"awsRegion"`
        AvailabilityZone             string      `json:"availabilityZone"`
        ConfigurationStateMd5Hash    string      `json:"configurationStateMd5Hash"`
        ResourceCreationTime         time.Time   `json:"resourceCreationTime"`
    } `json:"configurationItem"`
    NotificationCreationTime time.Time `json:"notificationCreationTime"`
    MessageType              string    `json:"messageType"`
    RecordVersion            string    `json:"recordVersion"`
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《AWS Config 通知的正确格式是怎样的?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>