Example #1
0
// Commit and print the outcome to stdout, including any error that happened
// during population of the commit buffer prior to this call
func CommitAndPrintOutcome(m2m *xrm2m.M2MClient, label string) {
	commit_id := m2m.Commit(label, "")
	if m2m.Error == nil {
		if commit_id == "" {
			fmt.Println("No commit required")
		} else {
			fmt.Println("New commit ", commit_id)
		}
	} else {
		fmt.Printf("Error during %s: %s\n", m2m.LastOp, m2m.Error.Error())
	}
}
Example #2
0
// Print any changes that would be effected with a commit to stdout
func PrintChanges(m2m *xrm2m.M2MClient) {
	changes := m2m.GetChanges()
	if len(changes) > 0 {
		fmt.Println("Changes to be committed:\n")
		for _, change := range changes {
			for key, value := range change {
				fmt.Printf("%10s: %v\n", key, value)
			}
			fmt.Println("")
		}
	}
}
Example #3
0
// Upddate a telemetry policy. Assume we use the policy name as filename
func UpdateTelemetryPolicy(m2m *xrm2m.M2MClient, policy *TelemetryPolicy) {
	if m2m.Error != nil {
		return
	}
	path := "/telemetry/policies/" + policy.Name + ".policy"
	data, err := json.MarshalIndent(policy, "", "  ")
	if err != nil {
		m2m.Error = err
		m2m.LastOp = "UpdateTelemetryPolicy"
		return
	}
	m2m.WriteFile(path, data)
}
Example #4
0
// Select one of the sample policies, fill in the live metadata, and update
func set_policy(m2m *xrm2m.M2MClient, policy_name string) error {
	policy, ok := policies[policy_name]
	if !ok {
		return errors.New("bad policy name")
	}

	// Add some metadata including the time we're changing policy
	now, _ := time.Now().MarshalText()
	policy.Metadata = &Metadata{
		Script: "Telemetry management bot 0.1",
		Date:   string(now),
	}

	// Set it using the M2M API
	m2m.UpdateTelemetryPolicy(policy)
	return m2m.Error
}
Example #5
0
// Populate the commit buffer with our desired state for collctors
func update_collectors(m2m *xrm2m.M2MClient, collectors *[]Collector) {
	const path_root = "RootCfg.Telemetry.JSON.PolicyGroup(['main']).IPv4Address"

	// Indicate we're going to replace the whole subtree for this Policy
	m2m.Replace(path_root)

	// Set each collector's IP address and port
	for _, collector := range *collectors {
		key, _ := json.Marshal(collector)
		m2m.Set(fmt.Sprintf("%s(%s)", path_root, key), true)
	}

	// For educational purposes, show what changes are about to be committed
	xrm2m_util.PrintChanges(m2m)

	// Commit any changes if there are any
	xrm2m_util.CommitAndPrintOutcome(m2m, "telemetry bot v0.1")
}
Example #6
0
func enter_shell(m2m *xrm2m.M2MClient) {
	const done = "<done>"
	shell := ishell.NewShell()
	shell.Println("Welcome to the XR M2M CLI")

	// CLI transition functions

	shell.Register("cli_exec", func(args ...string) (string, error) {
		return m2m.CliExec(strings.Join(args, " ")), m2m.Error
	})
	shell.Register("cli_get", func(args ...string) (string, error) {
		return pretty_print(m2m.CliGet(strings.Join(args, " ")), m2m.Error)
	})
	shell.Register("cli_set", func(args ...string) (string, error) {
		m2m.CliSet(strings.Join(args, " "))
		return done, m2m.Error
	})
	shell.Register("write_file", func(args ...string) (string, error) {
		shell.Println("Input multiple lines and end with semicolon ';'.")
		data := shell.ReadMultiLines(";")
		data = strings.TrimSuffix(data, ";") // @@@ bug in ishell
		m2m.WriteFile(args[0], []byte(data))
		return done, m2m.Error
	})

	// Basic schema ops

	shell.Register("get", func(args ...string) (string, error) {
		return pretty_print(m2m.Get(strings.Join(args, " ")), m2m.Error)
	})

	shell.Register("get_children", func(args ...string) (string, error) {
		return pretty_print(m2m.GetChildren(strings.Join(args, " ")), m2m.Error)
	})

	shell.Register("set", func(args ...string) (string, error) {
		m2m.Set(args[0], args[1])
		return done, m2m.Error
	})

	shell.Register("delete", func(args ...string) (string, error) {
		m2m.Delete(strings.Join(args, " "))
		return done, m2m.Error
	})

	shell.Register("replace", func(args ...string) (string, error) {
		m2m.Replace(strings.Join(args, " "))
		return done, m2m.Error
	})

	// Commit operations. Both "comment" and "label" are optional

	shell.Register("commit", func(args ...string) (string, error) {
		comment, label := optional(args)
		return m2m.Commit(comment, label), m2m.Error
	})

	shell.Register("commit_replace", func(args ...string) (string, error) {
		comment, label := optional(args)
		return m2m.CommitReplace(comment, label), m2m.Error
	})

	shell.Register("discard_changes", func(args ...string) (string, error) {
		m2m.DiscardChanges()
		return done, m2m.Error
	})

	shell.Register("get_changes", func(args ...string) (string, error) {
		return pretty_print(m2m.GetChanges(), m2m.Error)
	})

	// Schema

	shell.Register("get_schema", func(args ...string) (string, error) {
		path := args[0]
		var fields string
		if len(args) == 2 {
			fields = args[1]
		}
		return pretty_print(m2m.GetSchema(path, fields), m2m.Error)
	})

	shell.Register("get_version", func(args ...string) (string, error) {
		return pretty_print(m2m.GetVersion(), m2m.Error)
	})

	shell.Start()
}