Add "snap add-cap" command and API #208

Merged
merged 19 commits into from Dec 3, 2015

Conversation

Projects
None yet
3 participants
Contributor

zyga commented Dec 2, 2015

This branch depends on #204

Similar to "get all capabilities" branch earlier, this branch adds the REST API, client API and command line interface for adding capabilities to snappy daemon.

zyga added some commits Dec 2, 2015

Add client.response.processErrorResponse()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Add POST /1.0/capabilities REST API
This method allows trusted clients to add a capability to snappy.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Refactor error handling of client.Capabilities()
There are two changes here. First of all the flow is different, main
body of the code uses a switch statement to handle different response
types.  The error type is now tweaked to use errorResult to handle
errors in a way that preserves the error message produced by the server.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Add client.AddCapability()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Add "snap add-cap" command
This patch adds a new sub-command for snap for the "snap" tool to add
a new capability to the system.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Tweak comment on cmd.snap.AttributePair
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Refresh translation template
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
client/caps.go
+ }
+ return resultOk["capabilities"], nil
+ default:
+ return nil, fmt.Errorf("%s: expected sync response, got %s", errPrefix, rsp.Type)
@chipaca

chipaca Dec 2, 2015

Member

%q in the second one, there

@zyga

zyga Dec 3, 2015

Contributor

Fixed

client/caps.go
+ case "sync":
+ return nil
+ default:
+ return fmt.Errorf("cannot obtain capabilities: expected sync response, got %s", rsp.Type)
@chipaca

chipaca Dec 2, 2015

Member

and %q on the second one here (I know i've said this for one of these two in the previous review... ;-)

@zyga

zyga Dec 3, 2015

Contributor

Fixed, also fixed copy-paste error

client/caps.go
- return nil, fmt.Errorf("cannot obtain capabilities: failed to unmarshal response: %v", err)
+ switch rsp.Type {
+ case "error":
+ return rsp.processErrorResponse()
@chipaca

chipaca Dec 2, 2015

Member

something something prefix something nicer error output

@zyga

zyga Dec 3, 2015

Contributor

Fixed with merges with trunk and some simplification borrowed from other client methods

Member

chipaca commented Dec 2, 2015

👍 overall; nitpicks that can be addressed later abound.

@zyga zyga referenced this pull request Dec 2, 2015

Merged

Caps api remove capability #211

+ Type: "t",
+ Attrs: map[string]string{"k": "v"},
+ }
+ err := cs.cli.AddCapability(cap)
@niemeyer

niemeyer Dec 2, 2015

Contributor

Please replace all of the code in AddCapability with return nil. Does the test still pass? If so, what is being tested?

@zyga

zyga Dec 3, 2015

Contributor

I see your point. Since the method returns nil on success I find it hard to write a compelling test. I'm open to suggestions.

Edit: what I mean by this is that there are no useful side effects we can observe in the test. One thing we can try to do is to just see the POST request being made.

@zyga

zyga Dec 3, 2015

Contributor

And fixed, thanks for @chipaca for the tip on how to decipher the body :-)

cmd/snap/cmd_add_cap.go
+
+func init() {
+ _, err := parser.AddCommand("add-cap", shortAddCapHelp, longAddCapHelp, &cmdAddCap{
+ addCapOptions{
@niemeyer

niemeyer Dec 2, 2015

Contributor

There seems to be too much spacing here when this would all fit in a single line comfortably, and in fact none of that is needed.. &cmdAppCap{} is exactly the same as &cmdAppCap{addCapOptions{Attrs: nil}}.

@zyga

zyga Dec 3, 2015

Contributor

Fixed

+ // Verify (external)
+ c.Check(rsp.Type, check.Equals, ResponseTypeError)
+ c.Check(rsp.Status, check.Equals, 400)
+ c.Check(rsp.Result.(*errorResult).Msg, check.Matches, `can't add capability`)
@niemeyer

niemeyer Dec 2, 2015

Contributor

Can this please be replaced by an ErrorMatches on rsp.err(), and do a Equals check on the whole message, testing that it contains the relevant information we expect (including the fact that the name clashed, and what it was).

@chipaca

chipaca Dec 3, 2015

Member

this was gustavo getting confused with similar problems in client and daemon. Ignore this particular comment :-)

Contributor

niemeyer commented Dec 2, 2015

Nice branch. A couple of comments, and LGTM with them addressed.

zyga added some commits Dec 3, 2015

Tweak error message in client.Capabilities()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Tweak error message in client.AddCapability()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Fix error prefix in client.AddCapability()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Use correct error prefix
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Simplify error handling in client.AddCapability()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Simplify command initialization
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Improve test for client.AddCapability()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Simplify test for client.AddCapability()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Add missing import (after merge)
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Use new BoolFileType instead of FileType (after merge)
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

chipaca added a commit that referenced this pull request Dec 3, 2015

@chipaca chipaca merged commit ea531c5 into snapcore:master Dec 3, 2015

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage decreased (-0.03%) to 66.513%
Details

@zyga zyga deleted the zyga:caps-api-add-capability branch Dec 3, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment