golang 存储映射到变量并做断言

golang storing map to variable and do asserts

我正在尝试使用 Avi Go SDK 获取 gslb 配置。

首先如下所示初始化与控制器的连接,

aviClient, err := clients.NewAviClient("mycontroller", "USER",
    session.SetPassword("PASSS"),
    session.SetTenant("TENANT"),
    session.SetInsecure)
if err != nil {
    t.Error(err)
}

然后尝试获取 gslb 详细信息,如下所示,

//Fetch gslb
var obj interface{}
err = aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj)
fmt.Printf("%v\n", obj)

并打印如下详细信息,

map[_last_modified:1650419741251661 controller_health_status_enabled:true created_by:null description:null domain_names:[xxxxxxxxxxxxx] down_response:map[type:GSLB_SERVICE_DOWN_RESPONSE_ALL_RECORDS] enabled:true groups:[map[algorithm:GSLB_ALGORITHM_ROUND_ROBIN members:[map[cluster_uuid:xxxxxxxxxxxxxxxxxxxx enabled:true fqdn:xxxxxxxxxxxxxxxxxxxxxxxxx ip:map[addr:xxxxxxxxxxxxxxx type:V4] location:map[source:GSLB_LOCATION_SRC_INHERIT_FROM_SITE] ratio:1]] name:xxxxxxxxxxxxxxxxxxxxxxx priority:10]] health_monitor_refs:[https://xxxxxxxxxxxxxxxx] health_monitor_scope:GSLB_SERVICE_HEALTH_MONITOR_ALL_MEMBERS name:xxxxxxxxxxxxxxxx num_dns_ip:1 tenant_ref:https://xxxxxxxxxxxxxxxxxxx ttl:30 url:https://xxxxxxxxxxxxxxxxxxxx use_edns_client_subnet:true uuid:xxxxxxxxxxxxxxxxxxx wildcard_match:false]

现在我想将整个映射存储到变量中,以便我可以如下进行断言和测试,

var obj1 interface{}    
appDeployment := aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj1)
if err != nil {
    t.Error(err)
}
assert.Equal(t, appDeployment.domain_names, "mygslbname")

我遇到以下错误,

appDeployment.domain_names undefined (type error has no field or method domain_names)

有帮助吗?
或者如何从地图打印任何值,例如如何从地图上方打印 ttl 值?

是否因为aviClient.AviSession.GetObjectByName返回错误?您是要写信给 obj1 吗?

你有两个问题:

  1. GetObjectByNamereturnserror。 所以,你的 appDeployment 实际上是一个错误
  2. 您必须通过字符串键访问地图。因此,即使 appDeployment 是有效地图,您也必须通过字符串键访问 domain_names

这应该可以解决两个问题:

    var obj1 map[string]interface{}

    err = aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj1)
    if err != nil {
        t.Error(err)
    }
    assert.Equal(t, obj1["domain_names"], "mygslbname")