Ejemplo n.º 1
0
// runGetZones invokes the REST API with GET action and key prefix as path.
func runGetZones(cmd *commander.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		return
	}
	req, err := http.NewRequest("GET", kv.HTTPAddr()+zoneKeyPrefix+"/"+args[0], nil)
	if err != nil {
		log.Errorf("unable to create request to admin REST endpoint: %v", err)
		return
	}
	// TODO(spencer): need to move to SSL.
	b, err := sendAdminRequest(req)
	if err != nil {
		log.Errorf("admin REST request failed: %v", err)
		return
	}
	fmt.Fprintf(os.Stdout, "zone config for key prefix %q:\n%s\n", args[0], string(b))
}
Ejemplo n.º 2
0
// runRmZone invokes the REST API with DELETE action and key prefix as
// path.
func runRmZone(cmd *commander.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		return
	}
	req, err := http.NewRequest("DELETE", kv.HTTPAddr()+zoneKeyPrefix+"/"+args[0], nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unable to create request to admin REST endpoint: %v\n", err)
		return
	}
	// TODO(spencer): need to move to SSL.
	_, err = sendAdminRequest(req)
	if err != nil {
		fmt.Fprintf(os.Stderr, "admin REST request failed: %v\n", err)
		return
	}
	fmt.Fprintf(os.Stdout, "removed zone config for key prefix %q\n", args[0])
}
Ejemplo n.º 3
0
// runLsZones invokes the REST API with GET action and no path, which
// fetches a list of all zone configuration prefixes. The optional
// regexp is applied to the complete list and matching prefixes
// displayed.
func runLsZones(cmd *commander.Command, args []string) {
	if len(args) > 1 {
		cmd.Usage()
		return
	}
	req, err := http.NewRequest("GET", kv.HTTPAddr()+zoneKeyPrefix, nil)
	if err != nil {
		log.Errorf("unable to create request to admin REST endpoint: %v", err)
		return
	}
	b, err := sendAdminRequest(req)
	if err != nil {
		log.Errorf("admin REST request failed: %v", err)
		return
	}
	var prefixes []string
	if err = json.Unmarshal(b, &prefixes); err != nil {
		log.Errorf("unable to parse admin REST response: %v", err)
		return
	}
	var re *regexp.Regexp
	if len(args) == 1 {
		if re, err = regexp.Compile(args[0]); err != nil {
			log.Warningf("invalid regular expression %q; skipping regexp match and listing all zone prefixes", args[0])
			re = nil
		}
	}
	for _, prefix := range prefixes {
		if re != nil {
			unescaped, err := url.QueryUnescape(prefix)
			if err != nil || !re.MatchString(unescaped) {
				continue
			}
		}
		if prefix == "" {
			prefix = "[default]"
		}
		fmt.Fprintf(os.Stdout, "%s\n", prefix)
	}
}
Ejemplo n.º 4
0
// runSetZone invokes the REST API with POST action and key prefix as
// path. The specified configuration file is read from disk and sent
// as the POST body.
func runSetZone(cmd *commander.Command, args []string) {
	if len(args) != 2 {
		cmd.Usage()
		return
	}
	// Read in the config file.
	body, err := ioutil.ReadFile(args[1])
	if err != nil {
		log.Errorf("unable to read zone config file %q: %v", args[1], err)
		return
	}
	req, err := http.NewRequest("POST", kv.HTTPAddr()+zoneKeyPrefix+"/"+args[0], bytes.NewReader(body))
	if err != nil {
		log.Errorf("unable to create request to admin REST endpoint: %v", err)
		return
	}
	// TODO(spencer): need to move to SSL.
	_, err = sendAdminRequest(req)
	if err != nil {
		log.Errorf("admin REST request failed: %v", err)
		return
	}
	fmt.Fprintf(os.Stdout, "set zone config for key prefix %q\n", args[0])
}