Ejemplo n.º 1
0
func (c *Chef) CreateClient(name string) (string, error) {
	pl, err := rpc.MarshalToJsonReader(map[string]string{"name": name})
	if err != nil {
		return "", err
	}

	responce, err := c.Post("clients", nil, pl)
	if err != nil {
		return "", err
	}

	body, err := ioutil.ReadAll(responce.Body)
	responceHash := zhash.NewHash()
	json.Unmarshal(body, &responceHash)

	switch responce.StatusCode {
	case 201:
		key, err := responceHash.GetString("private_key")
		if err != nil {
			return "", errors.New(fmt.Sprintf("Error retrieving key: %s", err))
		}
		return key, nil
	default:
		errorMessage := getErrorMessage(responceHash)
		return "", errors.New(fmt.Sprintf("Response status code %d. "+
			"Error: %s", responce.StatusCode, errorMessage))
	}
}
Ejemplo n.º 2
0
func (c *Chef) CreateEnvironment(env Environment) error {
	payload, err := rpc.MarshalToJsonReader(env)
	if err != nil {
		return err
	}

	responce, err := c.Post("environments", nil, payload)
	if err != nil {
		return err
	}

	body, err := ioutil.ReadAll(responce.Body)
	responceHash := zhash.NewHash()
	json.Unmarshal(body, &responceHash)

	switch responce.StatusCode {
	case 201:
		return nil
	case 409:
		return errors.New(fmt.Sprintf("Environment %s already exists", env.Name))
	default:
		errorMessage := getErrorMessage(responceHash)
		return errors.New(fmt.Sprintf("Response status code %d. "+
			"Error: %s; raw body: %s", responce.StatusCode, errorMessage, body))
	}
}
Ejemplo n.º 3
0
func (c *Chef) CreateDatabag(databag string) error {
	data := map[string]string{"name": databag}
	payload, err := rpc.MarshalToJsonReader(data)
	if err != nil {
		return err
	}

	responce, err := c.Post("data", nil, payload)
	if err != nil {
		return err
	}

	body, err := ioutil.ReadAll(responce.Body)
	responceHash := zhash.NewHash()
	json.Unmarshal(body, &responceHash)
	switch responce.StatusCode {
	case 201:
		return err
	case 409:
		return errors.New(fmt.Sprintf("Databag %s already exists", databag))
	default:
		errorMessage := getErrorMessage(responceHash)
		return errors.New(fmt.Sprintf("Response status code %d. "+
			"Error: %s", responce.StatusCode, errorMessage))
	}
}
Ejemplo n.º 4
0
func (c *Chef) CreateDatabagItem(databag, item string, value zhash.Hash) error {
	payload, err := rpc.MarshalToJsonReader(
		DataBagItem{Id: item, Data: value})
	if err != nil {
		return err
	}

	responce, err := c.Post("data/"+databag, nil, payload)
	if err != nil {
		return err
	}

	body, err := ioutil.ReadAll(responce.Body)
	responceHash := zhash.NewHash()
	err = json.Unmarshal(body, &responceHash)
	switch responce.StatusCode {
	case 200:
		return err
	case 409:
		return errors.New(fmt.Sprintf("Databag item %s/%s already exists",
			databag, item))
	default:
		errorMessage := getErrorMessage(responceHash)
		return errors.New(fmt.Sprintf("Response status code %d. "+
			"Error: %s", responce.StatusCode, errorMessage))
	}

	return nil
}
Ejemplo n.º 5
0
func getConfig(path string) (zhash.Hash, error) {
	configData := map[string]interface{}{}
	_, err := toml.DecodeFile(path, &configData)
	if err != nil {
		if os.IsNotExist(err) {
			return zhash.NewHash(), err
		}

		return zhash.NewHash(), ser.Errorf(
			err,
			"can't decode toml file: %s",
		)
	}

	return zhash.HashFromMap(configData), nil
}
Ejemplo n.º 6
0
func Example_tomlUnmarshaller() {
	h := zhash.NewHash()
	h.SetUnmarshallerFunc(unmarshalToml)

	blob := []byte(`
key1 = "string"
key2 = 10
`)

	b := bytes.NewBuffer(blob)
	h.ReadHash(b)
	fmt.Println(h.String())

	//Output:
	//{
	//   "key1": "string",
	//   "key2": 10
	//}
}
Ejemplo n.º 7
0
// Example ReadHash shows how to initialize your hash using Unmarshal function
// You can use any function that satisfies Unmarshaller type for ReadHash. For
// example, see TomlExample for example of using BurntSushi/toml for
// unmarshalling
func Example_readHash() {
	h := zhash.NewHash()
	h.SetMarshallerFunc(json.Marshal)
	h.SetUnmarshallerFunc(json.Unmarshal)

	b := bytes.NewBuffer([]byte("{\"this\": \"is\", \"some_json_map\": 14.1}"))

	err := h.ReadHash(b)
	if err != nil {
		log.Fatal(err)
	}

	s, err := h.GetString("this")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("This %s working", s)
	// Output:
	// This is working
}
Ejemplo n.º 8
0
Archivo: node.go Proyecto: zazab/go-rpc
func (c *Chef) DeleteNode(name string) error {
	responce, err := c.Delete("nodes/"+name, nil)
	if err != nil {
		return errors.New("chef-golang: " + err.Error())
	}

	body, err := ioutil.ReadAll(responce.Body)
	responceHash := zhash.NewHash()
	json.Unmarshal(body, &responceHash)

	switch responce.StatusCode {
	case 200:
		c.log.Debug("Node %s deleted", name)
		return nil
	case 404:
		c.log.Notice("Node %s not found", name)
		return nil
	default:
		errorMessage := getErrorMessage(responceHash)
		return errors.New(fmt.Sprintf("Response status code %d. "+
			"Error: %s", responce.StatusCode, errorMessage))
	}
}
Ejemplo n.º 9
0
func (c *Chef) DeleteDatabagItem(databag, item string) error {
	responce, err := c.Delete(fmt.Sprintf("data/%s/%s", databag, item), nil)
	if err != nil {
		return err
	}

	body, err := ioutil.ReadAll(responce.Body)
	responceHash := zhash.NewHash()
	json.Unmarshal(body, &responceHash)

	switch responce.StatusCode {
	case 200:
		c.log.Debug("Databag item %s/%s deleted", databag, item)
		return nil
	case 404:
		c.log.Notice("Databag item %s/%s not found", databag, item)
		return nil
	default:
		errorMessage := getErrorMessage(responceHash)
		return errors.New(fmt.Sprintf("Response status code %d. "+
			"Error: %s", responce.StatusCode, errorMessage))
	}
}
Ejemplo n.º 10
0
Archivo: node.go Proyecto: zazab/go-rpc
func (c *Chef) CreateNode(node Node) error {
	pl, err := rpc.MarshalToJsonReader(node)
	if err != nil {
		return err
	}

	responce, err := c.Post("nodes", nil, pl)
	if err != nil {
		return err
	}

	body, err := ioutil.ReadAll(responce.Body)
	responceHash := zhash.NewHash()
	json.Unmarshal(body, &responceHash)

	switch responce.StatusCode {
	case 201:
		return nil
	default:
		errorMessage := getErrorMessage(responceHash)
		return errors.New(fmt.Sprintf("Response status code %d. "+
			"Error: %s", responce.StatusCode, errorMessage))
	}
}