dougou7008 2018-03-02 18:58
浏览 73

如何使用循环内返回的值更新for循环条件?

I am trying to loop over some results using the aws cloudwatch alarms sdk. The request set is too big for one API request, and it gives me a next token. This is the first time I have had to make more than one call for a results set. I am trying to figure out how to update the input parameters with the next token and go back through the loop till I have no token and extract the alarm name based on the Dimension name InstanceId. I want to return a []string with the alarms names associated with that instance. I know the code below is not correct but it is as close as I have been able to get. It gets Alarms, but I have not been able to get it to update the input parameters. I know there are excessive logging statements in there.

Data Sample:

{
            "EvaluationPeriods": 1,
            "AlarmArn": "arn:aws:cloudwatch:us-east-1:713901513416:alarm:[arm] Memory Utilization on urjanet-qc i-07e66faf",
            "StateUpdatedTimestamp": "2015-12-08T14:41:12.885Z",
            "AlarmConfigurationUpdatedTimestamp": "2015-07-16T18:59:18.262Z",
            "ComparisonOperator": "GreaterThanOrEqualToThreshold",
            "AlarmActions": [
                "arn:aws:sns:us-east-1:2134:uAlerts-arm"
            ],
            "Namespace": "System/Linux",
            "AlarmDescription": "Raise an alarm when i-07e66faf memory utilization exceeds 90%",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2015-12-08T14:41:12.873+0000\",\"unit\":\"Percent\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[],\"threshold\":90.0}",
            "Period": 300,
            "StateValue": "INSUFFICIENT_DATA",
            "Threshold": 90.0,
            "AlarmName": "[arm] Memory Utilization on urjanet-qc i-07e66faf",
            "Dimensions": [
                {
                    "Name": "InstanceId",
                    "Value": "i-07e66faf"
                }
            ],
            "Unit": "Percent",
            "Statistic": "Average",
            "StateReason": "Insufficient Data: 1 datapoint was unknown.",
            "InsufficientDataActions": [],
            "OKActions": [
                "arn:aws:sns:us-east-1:12341:uAlerts-arm"
            ],
            "ActionsEnabled": true,
            "MetricName": "MemoryUtilization"
        }

Partially working code:

func GetInstanceAlarms(instance *Instance) []string {
    log.Println("In GetInstanceAlarms")
    log.Println(instance.InstanceId)
    var DeleteAlarmNames []string

    NoToken := int(0)
    c := cloudwatch.New(session.New())
    for NoToken == 0 {
        log.Println(NoToken)
        params := &cloudwatch.DescribeAlarmsInput{
            NextToken: aws.String(""),
        }
        log.Println(params.NextToken)
        resp, err := c.DescribeAlarms(params)
        if err != nil {
            panic(err.Error())
        }
        log.Println(reflect.TypeOf(resp))

        for idx := range resp.MetricAlarms {
            for _, alarm := range resp.MetricAlarms[idx].Dimensions {
                log.Println(*alarm.Name)
                log.Println(*alarm.Value)
                if *alarm.Name == "InstanceId" && *alarm.Value == instance.InstanceId {
                    log.Println(*alarm.Name)
                    log.Println(*alarm.Value)
                    DeleteAlarmNames = append(DeleteAlarmNames, *resp.MetricAlarms[idx].AlarmName)
                    log.Println(&DeleteAlarmNames)

                }
            }
            if *resp.NextToken == "" {
                log.Println("No Token Token")
                NoToken = 1
            }
            if *resp.NextToken != "" {
                log.Println("NextToken")
                log.Println(*resp.NextToken)
                log.Println("Found Token")
                NoToken = 0
                params.NextToken = resp.NextToken
            }
        }
    }
    log.Println(DeleteAlarmNames)
    return DeleteAlarmNames
}
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 西门子S7-Graph,S7-300
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改