-1

I am trying to use Avi Go SDK to fetch a gslb configuration.

First initilizing a connection to the controller as below,

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

Then trying to grab a gslb details as below,

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

and that print the details as below,

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]

Now I would like to store that whole map to variable so that I can do asserts and test as below,

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

I get below error,

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

Any assitance?
Or How do print any values from the map, for eg how to print ttl value from above map?

Simon
  • 111
  • 1
  • 9
  • `appDeployment` is an error. Maybe you meant to write `obj1`? – Burak Serdar Apr 21 '22 at 04:04
  • in my other kubernetes project it works fine as below, appDeployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), tfVars.DeploymentName, v1.GetOptions{}) if err != nil { t.Error(err) } assert.Equal(t, appDeployment.Spec.Template.Spec.ServiceAccountName, "myserviceaccount") – Simon Apr 21 '22 at 04:11
  • In your second code snippet, `GetObjectByName` is returning an error. In the last one, you are expecting an `appDeployment` from the same method. – Burak Serdar Apr 21 '22 at 04:22
  • sorry, I am not a regular programmer. Let me ask this. In second code snippet I have a var called `obj` and then storing the output of aviClient.AviSession.GetObjectByName method in `err` Then I am printing `obj` using `fmt.Printf("%v\n", obj)` so what is in `err` ? – Simon Apr 21 '22 at 04:51
  • It would have any error response returned from that method call. – Burak Serdar Apr 21 '22 at 05:01
  • So the actual map output (which is the output of aviClient.AviSession.GetObjectByName) is stored at `obj` ? When I do `fmt.Printf("%v\n", obj)` why is that `"%v\n"` needed there? – Simon Apr 21 '22 at 05:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244070/discussion-between-simon-and-burak-serdar). – Simon Apr 21 '22 at 05:06

2 Answers2

1

You have two problems:

  1. GetObjectByName returns error. So, your appDeployment is actually an error
  2. You have to access the map by string keys. So, even if appDeployment were a valid map, you would have to access domain_names by string key.

This should solve both problems:

    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")
ABDULLOKH MUKHAMMADJONOV
  • 4,249
  • 3
  • 22
  • 40
  • Now getting error, invalid operation: cannot index appDeployment (variable of type error) – Simon Apr 21 '22 at 10:07
  • tried your new code snippet and getting below error now. ./avi_learn_test.go:58:9: no new variables on left side of := ./avi_learn_test.go:62:25: invalid operation: cannot index obj1 (variable of type interface{}) – Simon Apr 21 '22 at 10:52
  • if I may ask how to assert other data from the map? such as groups, location etc. I can do some trial and error and find out too.. – Simon Apr 21 '22 at 11:20
  • @Simon you can access any value of the map by a string key. e.g. `obj1["domain_names"]`, `obj1["location"]` – ABDULLOKH MUKHAMMADJONOV Apr 21 '22 at 11:27
  • one more small issue, Error: Not equal: expected: []interface {}([]interface {}{"helloworldspringbootssl-dit1.xxx.xxx.com"}) actual : string("helloworldspringbootssl-dit1.xxx.xxx.com") how to match them exactly as I pass? – Simon Apr 21 '22 at 11:33
0

Is it because aviClient.AviSession.GetObjectByName is returning an error? Did you mean to write to obj1?

  • I think you might be right, Burak also pointed out the same and was chatting through comments. I tried assert.Equal(obj1.domain_names, "mygslb") and getting error obj1.name undefined (type interface{} has no field or method name) – Simon Apr 21 '22 at 05:29
  • or in other words, how do I print any entries from the map, for eg ttl – Simon Apr 21 '22 at 05:45
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 09:14