marpaia / chef-golang

Go library to interact with the Chef server API
Other
77 stars 32 forks source link

/environments/X/cookbook-versions not implemented. #1

Closed spheromak closed 10 years ago

spheromak commented 10 years ago

was playing around with this over the holiday, and I would like to use this server API call: http://docs.opscode.com/api_chef_server.html#environments-n-cookbook-versions

But it's not implemented in this client wrapper, and when I tried to use the chef.Post method directly it looks like there is no way to specify the body. I am fairly new to Golang, and It is entirely possible i am doing it wrong.

marpaia commented 10 years ago

The second argument in the chef.Post method is params which is a map of post body parameters. That should work, but if it doesn't please write back and I'll fix it.

marpaia commented 10 years ago

Also, if you implement it using chef.Post, please submit a PR or post your code here and I'll fashion it into the repo.

spheromak commented 10 years ago

Yup I saw this in the code when i started looking. So i tried something like this:

params := make(map[string]string)
params["run_list"] = "base"

env := "_default"
endpoint := fmt.Sprintf("/environments/%s/cookbook_versions", env)

resp, err := chef.Post(endpoint, params)

Ends up emitting errors on the chef-server side:

[2014-01-03T10:24:24-08:00] DEBUG: POST environments/_default/cookbook_versions
[2014-01-03T10:24:24-08:00] ERROR: #<JSON::ParserError: A JSON text must at least contain two octets!>

Do i need to marshal the params to json ? Not clear how i submit the json body that's needed by chef-server through the implementation of adding fields to the body in the Post func.

marpaia commented 10 years ago

hmn, I wouldn't think so, but perhaps? I just looked through the API and none of the existing calls make POST requests, they're all GETs so I guess I haven't come across this yet. If you want to dig around a bit and let me know how you do and I'll put some time into it this weekend?

spheromak commented 10 years ago

Yea my issue is I am also a Go Newbie

http://docs.opscode.com/api_chef_server.html#environments-n-cookbook-versions

request body should be json

{
  "run_list": [
    "zed@0.0.1",
    "bar",
    "mysql",
    "gem",
    "nginx@0.99.2",
    "cron",
    "foo"
  ]
}

I see in chef.generateRequest you attach the fields to the body:

https://github.com/marpaia/chef-golang/blob/master/api.go#L218-L224

currently my confusion is around body := url.Values{} not sure where url is coming from in the above section of code.

marpaia commented 10 years ago

url is a package in Go's standard library:

marpaia commented 10 years ago

hey, did you ever get anywhere with this?

spheromak commented 10 years ago

Nothing worth pushing was gonna get back to it this weekend

spheromak commented 10 years ago

@marpaia getting back to this. My thought is that there should be chef.Post and chef.PostForm these mirror the http libraries 2 post methods. chef.PostForm is for posting with args, and chef.Post more generically takes a body type and submits it to the server. These 2 methods would then handle all the Chef-server use cases. As I am interested in making this library able to manage cookbooks I would be using these post methods a lot. This would be a API change to the behavior of the current chef.Post, and you may want me to do something else instead. Let me know your thoughts.

spheromak commented 10 years ago

see spheromak/chef-golang@0b9538d6d637 for example of what i am thinking

marpaia commented 10 years ago

@spheromak, cool if i close this?

spheromak commented 10 years ago

@marpaia oh yea for sure. sorry I should have done it when it got merged.