Beispiel #1
0
/*
	Get_interfaces sends a request to the switch and generate a map of single interface structs that is
	indexed by the interface name.  The single interface struct contains very limited information compared
	to what is returned by the switch.
*/
func (aif *Arista_api) Get_interfaces(ifstate string) (ifmap map[string]*Swif, err error) {

	var (
		tag string = "root"
	)

	ifmap = make(map[string]*Swif)

	query := `"show interfaces"`
	raw_json, err := aif.Submit_req(&query, false)
	if err != nil {
		return
	}

	jif, err := jsontools.Json2blob(raw_json, &tag, jsontools.NOPRINT) // parse the json into an interface hierarchy
	if err != nil {
		return
	}

	ifaces, err := getresult(jif, 0, "interfaces") // pull the list of switch interfaces into an array of interface type, one for each interface
	if err != nil {
		return
	}

	for ik, _ := range ifaces { // ifaces should be an array of struct
		sif, ok := ifaces[ik].(map[string]interface{}) // single interface data
		if !ok {
			fmt.Errorf("single interface %s was not expected type", ik)
			return
		}

		if ifstate == "all" || sif["interfaceStatus"].(string) == ifstate {
			ifmap[ik] = &Swif{
				Name:      sif["name"].(string),
				Mtu:       sif["mtu"].(float64),
				State:     sif["interfaceStatus"].(string),
				Bandwidth: sif["bandwidth"].(float64),
				Addr:      sif["physicalAddress"].(string),
			}
		}
	}

	return
}
Beispiel #2
0
func TestJsonblob(t *testing.T) {
	var (
		jstring string
		jbytes  []byte
	)

	jstring = `{ "actions": [ { "action": "setqueues1", "qdata": [ "queue1", "queue2", "queue3" ] }, { "action": "setqueues2", "qdata": [ "2queue1", "2queue2", "2queue3" ] } ] }`
	jbytes = []byte(jstring)
	jif, err := jsontools.Json2blob(jbytes[:], nil, false)

	if err != nil {
		fmt.Fprintf(os.Stderr, "errors unpacking json: %s    [FAIL]\n", err)
		t.Fail()
		return
	}

	root_map := jif.(map[string]interface{})
	alist := root_map["actions"].([]interface{})

	if alist == nil {
		fmt.Fprintf(os.Stderr, "alist in json was nil  [FAIL]\n")
		t.Fail()
		return
	}
	fmt.Fprintf(os.Stderr, "found alist, has %d actions   [OK]\n", len(alist))

	for i := range alist {
		action := alist[i].(map[string]interface{})
		atype := action["action"].(string)
		data := action["qdata"].([]interface{})

		fmt.Fprintf(os.Stderr, "action %d has type: %s\n", i, atype)
		for j := range data {
			fmt.Fprintf(os.Stderr, " data[%d] = %s\n", j, data[j].(string))
		}
	}

	fmt.Fprintf(os.Stderr, "===== end blob testing ======\n\n")
}
Beispiel #3
0
func main() {
	var (
		version      string = "rjprt v1.4/16195"
		auth         *string
		err          error
		resp         *http.Response
		verbose      *bool
		root         *string
		raw_json     *bool
		needs_help   *bool
		target_url   *string
		dot_fmt      *bool
		look4        *string
		method       *string
		request_data *string
		req          *http.Request
	)

	needs_help = flag.Bool("?", false, "show usage")

	auth = flag.String("a", "", "authorisation token")
	dot_fmt = flag.Bool("d", false, "dotted named output")
	request_data = flag.String("D", "", "post data")
	raw_json = flag.Bool("j", false, "raw-json")
	show_headers := flag.Bool("h", false, "show http response headers")
	appl_json := flag.Bool("J", false, "data type is json")
	look4 = flag.String("l", "", "look4 string in hashtab")
	method = flag.String("m", "GET", "request method")
	root = flag.String("r", "", "top level root name")
	target_url = flag.String("t", "", "target url")
	tight_sec := flag.Bool("T", false, "tight security ON")
	verbose = flag.Bool("v", false, "verbose")
	flag.Parse() // actually parse the commandline

	input_type := "text" // type of data being sent in body
	if *appl_json {
		input_type = "application/json"
	}

	if *needs_help {
		usage(version)
		os.Exit(0)
	}

	if *look4 != "" { // -l imples -d
		*dot_fmt = true
	}

	if *target_url == "" {
		fmt.Fprintf(os.Stderr, "target url is missing from command line (use -t url)\n")
		os.Exit(1)
	}

	if *verbose {
		fmt.Fprintf(os.Stderr, "target=%s\n", *target_url)
	}

	trparms := &http.Transport{ // override default transport parms to skip the verify
		TLSClientConfig: &tls.Config{InsecureSkipVerify: !*tight_sec},
	}

	client := &http.Client{Transport: trparms} // default client except with our parms

	switch *method {
	case "GET", "get":
		req, err = http.NewRequest("GET", *target_url, nil)
		if err == nil {
			if auth != nil && *auth != "" {
				req.Header.Set("X-Auth-Tegu", *auth)
			}

			resp, err = client.Do(req)
		}

	case "POST", "post":
		if *request_data == "" {
			req, err = http.NewRequest("POST", *target_url, os.Stdin)
		} else {
			req, err = http.NewRequest("POST", *target_url, bytes.NewBufferString(*request_data))
		}

		if err == nil {
			req.Header.Set("Content-Type", input_type)
			if auth != nil && *auth != "" {
				req.Header.Set("X-Auth-Tegu", *auth)
			}

			resp, err = client.Do(req)
		}

	case "DELETE", "del", "delete":
		if *request_data == "" {
			req, err = http.NewRequest("DELETE", *target_url, os.Stdin)
		} else {
			req, err = http.NewRequest("DELETE", *target_url, bytes.NewBufferString(*request_data))
		}

		if err == nil {
			if auth != nil && *auth != "" {
				req.Header.Add("X-Auth-Tegu", *auth)
			}
			resp, err = client.Do(req)
		}

	default:
		fmt.Fprintf(os.Stderr, "%s method is not supported\n", *method)
		os.Exit(1)
	}

	if err != nil {
		fmt.Printf("%s request failed: %s\n", *method, err)
		os.Exit(1)
	} else {
		data, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			fmt.Printf("read of data from url failed\n")
			os.Exit(1)
		}
		resp.Body.Close()

		if *show_headers {
			for k, v := range resp.Header {
				fmt.Printf("header: %s = %s\n", k, v)
			}
		}

		if data == nil {
			os.Exit(0) // maybe not what they were expecting, but nothing isn't an error
		}

		if *raw_json {
			fmt.Printf("%s\n", data)
			os.Exit(0)
		}

		if *dot_fmt {
			m, err := jsontools.Json2map(data, root, false) // build the map
			if err == nil {
				if *look4 != "" {
					result := m[*look4]
					if result != nil {
						switch result.(type) {
						case string:
							fmt.Printf("%s = %s\n", *look4, result.(string))

						case int:
							fmt.Printf("%s = %d\n", *look4, result.(int))

						case float64:
							fmt.Printf("%s = %.2f\n", *look4, result.(float64))

						default:
							fmt.Printf("found %s, but its in an unprintable format\n", *look4)
						}

					} else {
						fmt.Fprintf(os.Stderr, "didn't find: %s\n", *look4)
					}
				} else {
					jsontools.Print(m, *root, true)
				}
			} else {
				fmt.Fprintf(os.Stderr, "ERR: %s \n", err)
			}
		} else {
			_, err = jsontools.Json2blob(data, root, true) // normal hiarchy can be printed as it blobs, so ignore jif coming back
			if err != nil {                                // assume mirroring which doesn't put out json in all cases (boo)
				//fmt.Fprintf( os.Stderr, "ERR: %s \n", err );
				fmt.Fprintf(os.Stdout, "%s\n", data)
			}
		}
	}

	os.Exit(0)
}