/
org_user.ex
82 lines (66 loc) · 2.1 KB
/
org_user.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
defmodule NervesHubCLI.API.OrgUser do
@moduledoc """
Manage OrgUsers on NervesHub
Path: /orgs/:org_name/users
"""
alias NervesHubCLI.API
alias NervesHubCLI.API.Auth
alias NervesHubCLI.API.Org
@type role :: :admin | :delete | :write | :read
@path "users"
@roles [:admin, :delete, :write, :read]
@doc """
List all users for an org.
Verb: GET
Path: /orgs/:org_name/users
"""
@spec list(String.t(), NervesHubCLI.API.Auth.t()) :: {:error, any()} | {:ok, any()}
def list(org_name, %Auth{} = auth) do
API.request(:get, path(org_name), "", auth)
end
@doc """
Add a user to the org with a role.
Verb: POST
Path: /orgs/:org_name/users
"""
@spec add(String.t(), String.t(), NervesHubCLI.API.role(), NervesHubCLI.API.Auth.t()) ::
{:error, any()} | {:ok, any()}
def add(org_name, username, role, %Auth{} = auth) when role in @roles do
params = %{username: username, role: role}
API.request(:post, path(org_name), params, auth)
end
def add(_org_name, _username, _role, _auth) do
{:error, :invalid_role}
end
@doc """
Update an existing org user's role.
Verb: PUT
Path: /orgs/:org_name/users/:username
"""
@spec update(String.t(), String.t(), NervesHubCLI.API.role(), NervesHubCLI.API.Auth.t()) ::
{:error, any()} | {:ok, any()}
def update(org_name, username, role, %Auth{} = auth) do
params = %{role: role}
API.request(:put, path(org_name, username), params, auth)
end
@doc """
Remove a user from the org.
Verb: DELETE
Path: /orgs/:org_name/users/:username
"""
@spec remove(String.t(), String.t(), NervesHubCLI.API.Auth.t()) ::
{:error, any()} | {:ok, any()}
def remove(org_name, username, %Auth{} = auth) do
API.request(:delete, path(org_name, username), "", auth)
end
@spec path(String.t()) :: String.t()
def path(org) when is_atom(org), do: to_string(org) |> path()
def path(org) when is_binary(org) do
Path.join(Org.path(org), @path)
end
@doc false
@spec path(String.t(), String.t()) :: String.t()
def path(org_name, username) do
Path.join(path(org_name), username)
end
end