Skip to content

Instantly share code, notes, and snippets.

@scottdware
Last active March 14, 2016 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottdware/d8d14d73e06009a24dde to your computer and use it in GitHub Desktop.
Save scottdware/d8d14d73e06009a24dde to your computer and use it in GitHub Desktop.
package main
import (
"encoding/xml"
"fmt"
"github.com/scottdware/go-junos"
"github.com/scottdware/go-rested"
)
// dynamicHosts parses the overall XML returned from the Infoblox query.
type dynamicHosts struct {
XMLName xml.Name `xml:"list"`
Hosts []hostEntry `xml:"value>ipv4addrs>list>value"`
}
// hostEntry parses the XML for each individual host.
type hostEntry struct {
Name string `xml:"host"`
Address string `xml:"ipv4addr"`
}
var (
searchString = "ware"
ipamGM = "infoblox.company.com"
ipamUser = "ibadmin"
ipamPass = "infoblox"
srxHost = "srx240.company.com"
srxUser = "juniper"
srxPass = "Juniper123!"
groupName = "Dynamic-IPAM-Hosts"
)
// queryIPAM searches throughout Infoblox for the given search string...which matches the "comment" field
// within Infoblox and returns addresses that we will build our address-set for.
func queryIPAM() (*dynamicHosts, error) {
var data dynamicHosts
reqURL := fmt.Sprintf("https://%s/wapi/v1.0/record:host?comment~:=%s", ipamGM, searchString)
r := rested.NewRequest()
r.BasicAuth(ipamUser, ipamPass)
headers := map[string]string{
"Accept": "application/xml",
}
// Send our HTTP request to Infoblox.
resp := r.Send("get", reqURL, nil, headers, nil)
if resp.Error != nil {
fmt.Println(resp.Error)
}
err := xml.Unmarshal(resp.Body, &data)
if err != nil {
return nil, err
}
return &data, nil
}
func main() {
var srxConfig []string
// Run our query against IPAM/Infoblox to get our addresses.
d, err := queryIPAM()
if err != nil {
fmt.Println(err)
}
// Create our address entries first, and append them to our config.
for _, ae := range d.Hosts {
srxConfig = append(srxConfig, fmt.Sprintf("set security address-book global address %s %s/32\n", ae.Name, ae.Address))
}
// Create the address-set/group and assign the addresses we just created to it.
for _, as := range d.Hosts {
srxConfig = append(srxConfig, fmt.Sprintf("set security address-book global address-set %s address %s\n", groupName, as.Name))
}
// Connect to our SRX.
jnpr, err := junos.NewSession(srxHost, srxUser, srxPass)
if err != nil {
fmt.Println(err)
}
// Load our configuration into the SRX from the "srxConfig" variable we set earlier.
err = jnpr.Config(srxConfig, "set", false)
if err != nil {
fmt.Println(err)
}
// Commit the configuration to our SRX.
jnpr.Commit()
// Print the changes out to the console.
changes, _ := jnpr.ConfigDiff(1)
fmt.Println(changes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment