Query CMDB configuration items
Read the configuration items of a CMDB class and their fields.
When to use this pattern
- You're exporting or reconciling CI data with an external system
- You're querying by CMDB class (
cmdb_ci_linux_server,cmdb_ci_appl, …) rather than a plain table - For non-CMDB tables, use List and query records
Required values
| Value | Description |
|---|---|
| CMDB class | The CI class to query (for example, cmdb_ci_linux_server) |
Example
package main
import (
"context"
"fmt"
"log"
servicenow "github.com/michaeldcanady/servicenow-sdk-go/v2"
"github.com/michaeldcanady/servicenow-sdk-go/v2/credentials"
)
func main() {
// Step 1: Authenticate and initialize the client
cred := credentials.NewBasicProvider("{username}", "{password}")
client, err := servicenow.NewServiceNowServiceClient(
servicenow.WithAuthenticationProvider(cred),
servicenow.WithInstance("{instance}"),
)
if err != nil {
log.Fatalf("failed to initialize client: %v", err)
}
ctx := context.Background()
// Step 2: Query the CIs of a class
response, err := client.Now().Cmdb().Instance().ByClass("cmdb_ci_linux_server").Get(ctx, nil)
if err != nil {
log.Fatalf("unable to query CIs: %v", err)
}
// Step 3: Read fields from each CI
cis, err := response.GetResult()
if err != nil {
log.Fatal(err)
}
for _, ci := range cis {
name, err := ci.GetName()
if err != nil {
log.Fatal(err)
}
if name != nil {
fmt.Printf("CI: %s\n", *name)
}
}
}
Tips
ByClass(...).ByID(sysID)gets a single CI;Patchupdates one.- CI models expose
GetName,GetClassName, andGetSysID; other attributes depend on the class.