aws-sdk-go的describeStacks()函数尝试获取堆栈ID列表或从堆栈名称获取堆栈ID。

huangapple go评论84阅读模式
英文:

aws-sdk-go describeStacks() trying to get a list of stackids or get stackid from stack name

问题

首先,我为我的英语道歉,并且我要提醒你我对Go语言还不熟悉。
我正在尝试获取我opsworks堆栈上所有stackId的列表,或者像我在Ruby SDK中那样提供stackname并获取stackid,以便我可以在其他服务调用中使用,但现在我正在尝试获取它们以便熟悉该SDK。

func main() {

    svc := opsworks.New(session.New(&aws.Config{
        Region:      aws.String("us-east-1"),
        Credentials: credentials.NewSharedCredentials("", "development"),
    })) 

    resp, err := svc.DescribeStacks()
    if err != nil {
        fmt.Println(err.Error())
        return
    }       

    fmt.Println(resp)
}

我省略了"params"的原因是因为这个链接中提到了:http://docs.aws.amazon.com/sdk-for-go/api/service/opsworks.html#type-DescribeStacksInput
它说:"一个指定要描述的堆栈的堆栈ID数组。如果省略此参数,DescribeStacks将返回每个堆栈的描述。"

我一直在得到"not enough arguments in call to svc.DescribeStacks"或"alidationException: Please provide no arguments or, one or more stack IDs"或"!v(PANIC=reflect: call of reflect.Value.Interface on zero Value)"的错误。

所以我尝试了很多种方式,不仅仅是我在这里粘贴的这种方式。这就是为什么结果不同。我尝试将()更改为(nil)等方式来省略参数,以便我可以获取所有堆栈的列表。
我确定这很愚蠢,有什么建议吗?我在谷歌等搜索引擎中搜索过,但所有的示例都是关于EC2或S3的。

英文:

First I apologice for my English and I advice I'm new to Go.
I'm trying to get a list of all stackId's on my opsworks stacks,
or to give the stackname like I did with the ruby sdk and get the stackid so I can use with another service calls, but for now im trying to get them all to get familiar with the sdk.

func main() {

        svc := opsworks.New(session.New(&aws.Config{
                Region:      aws.String("us-east-1"),
                Credentials: credentials.NewSharedCredentials("", "development"),
        })) 
                     
        
        resp, err := svc.DescribeStacks()
        if err != nil {
                fmt.Println(err.Error())
                return
        }       
   
        fmt.Println(resp)
}

the reason I omitted "params" is because of this:
http://docs.aws.amazon.com/sdk-for-go/api/service/opsworks.html#type-DescribeStacksInput
that says: "An array of stack IDs that specify the stacks to be described. If you omit this parameter, DescribeStacks returns a description of every stack."

O keep getting or "not enough arguments in call to svc.DescribeStacks"
or "alidationException: Please provide no arguments or, one or more stack IDs" or "!v(PANIC=reflect: call of reflect.Value.Interface on zero Value)"

So I'm trying many ways.. not just the one I pasted here.. thats why diff results... changing to () to (nil) etc.. to omit the parameters so I can get a list of all the stacks.
Im sure is very silly, any idea? I have search in google etc but all examples are for ec2 or s3...

答案1

得分: 0

在主要的 GitHub 问题网站上得到了一个有效的回答。

我明白了,谢谢你的澄清。你在 180498284 中列出的示例实际上是获取所有堆栈 ID 的最佳方法。

要从响应中检索单个堆栈 ID,你可以使用 opsworks.DescribeStacksOutput 结构体的字段。这个结构体有一个名为 Stacks 的字段,它是一个 opsworks.Stack 结构体的列表。StackId 是这个结构体上的一个字段。

resp, err := svc.DescribeStacks(nil)
if err != nil {
    return err
}

for _, stack := range resp.Stacks {
   stackName := aws.StringValue(stack.Name)
   if stackName == owstackname {
       fmt.Printf("found %s stackid\n", stackName)
       fmt.Println(aws.StringValue(stack.StackId))
   }
}

aws.StringValue 函数是用来将 SDK 的输入/输出结构体的字符串指针类型转换为值的辅助函数。它还可以防止值为 nil 的情况,如果值为 nil,它将返回空字符串。

英文:

got a response that worked in the main github issues site.

I see thanks for clarifying. The example you listed in 180498284 is actually the best way to get a list of all of your stack Ids.

To retrieve the individual stack Ids from the response you use the fields of opsworks.DescribeStacksOutput struct. This struct has a field Stacks which is a list of opsworks.Stack structs. The StackId is a field on this struct.

resp, err := svc.DescribeStacks(nil)
if err != nil {
    return err
}

for _, stack := range resp.Stacks {
   stackName := aws.StringValue(stack.Name)
   if stackName == owstackname {
       fmt.Printf("found %s stackid\n", stackName)
       fmt.Println(aws.StringValue(stack.StackId))
   }
}

The aws.StringValue functions are helpers to convert the string pointer types of the SDK's input/output structs to values. It also protects against the value being nil where it will return empty string.

huangapple
  • 本文由 发表于 2016年2月4日 17:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/35196823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定