Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-netbox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IT Services
go-netbox
Commits
8bc2717e
Commit
8bc2717e
authored
8 years ago
by
Matt Layher
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #19 from davcamer/authentication-header
Allow user to specify their API token
parents
2b01b034
d6c53a94
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
netbox/client.go
+20
-10
20 additions, 10 deletions
netbox/client.go
netbox/client_test.go
+20
-3
20 additions, 3 deletions
netbox/client_test.go
netbox/netbox_test.go
+1
-1
1 addition, 1 deletion
netbox/netbox_test.go
with
41 additions
and
14 deletions
netbox/client.go
+
20
−
10
View file @
8bc2717e
...
@@ -38,16 +38,20 @@ type Client struct {
...
@@ -38,16 +38,20 @@ type Client struct {
// Tenancy provides access to methods in NetBox's Tenancy API.
// Tenancy provides access to methods in NetBox's Tenancy API.
Tenancy
*
TenancyService
Tenancy
*
TenancyService
token
string
u
*
url
.
URL
u
*
url
.
URL
client
*
http
.
Client
client
*
http
.
Client
}
}
// NewClient returns a new instance of a NetBox client. addr specifies the address
// NewClient returns a new instance of a NetBox client. addr specifies the address
// of the NetBox server, and client specifies an optional HTTP client to use
// of the NetBox server, token specifies the api key to use,
// for requests.
// and client specifies an optional HTTP client to use for requests.
//
// If token is the empty string, no Authentication will be included in requests,
// providing anonymous read-only access depending on your NetBox config.
//
//
// If client is nil, a default HTTP client will be used.
// If client is nil, a default HTTP client will be used.
func
NewClient
(
addr
string
,
client
*
http
.
Client
)
(
*
Client
,
error
)
{
func
NewClient
(
addr
string
,
token
string
,
client
*
http
.
Client
)
(
*
Client
,
error
)
{
if
client
==
nil
{
if
client
==
nil
{
client
=
&
http
.
Client
{}
client
=
&
http
.
Client
{}
}
}
...
@@ -65,6 +69,7 @@ func NewClient(addr string, client *http.Client) (*Client, error) {
...
@@ -65,6 +69,7 @@ func NewClient(addr string, client *http.Client) (*Client, error) {
}
}
c
:=
&
Client
{
c
:=
&
Client
{
token
:
token
,
u
:
u
,
u
:
u
,
client
:
client
,
client
:
client
,
}
}
...
@@ -116,18 +121,23 @@ func (c *Client) NewDataRequest(method string, endpoint string, options Valuer,
...
@@ -116,18 +121,23 @@ func (c *Client) NewDataRequest(method string, endpoint string, options Valuer,
u
:=
c
.
u
.
ResolveReference
(
rel
)
u
:=
c
.
u
.
ResolveReference
(
rel
)
// If no valuer specified, create a request with no query parameters
// If a valuer is specified, add the values as the query string
if
options
==
nil
{
if
options
!=
nil
{
return
http
.
NewRequest
(
method
,
u
.
String
(),
body
)
values
,
err
:=
options
.
Values
()
if
err
!=
nil
{
return
nil
,
err
}
u
.
RawQuery
=
values
.
Encode
()
}
}
values
,
err
:=
options
.
Values
(
)
req
,
err
:=
http
.
NewRequest
(
method
,
u
.
String
(),
body
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
u
.
RawQuery
=
values
.
Encode
()
if
c
.
token
!=
""
{
req
.
Header
.
Set
(
"Authorization"
,
fmt
.
Sprintf
(
"Token %s"
,
c
.
token
))
return
http
.
NewRequest
(
method
,
u
.
String
(),
body
)
}
return
req
,
nil
}
}
// NewJSONRequest creates a HTTP request using the input HTTP method, URL
// NewJSONRequest creates a HTTP request using the input HTTP method, URL
...
...
This diff is collapsed.
Click to expand it.
netbox/client_test.go
+
20
−
3
View file @
8bc2717e
...
@@ -152,7 +152,7 @@ func TestClientQueryParameters(t *testing.T) {
...
@@ -152,7 +152,7 @@ func TestClientQueryParameters(t *testing.T) {
Bar
:
wantBar
,
Bar
:
wantBar
,
})
})
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
"expected
an
error
, but no error returned"
)
t
.
Fatal
f
(
"
un
expected error
: %v"
,
err
)
}
}
q
:=
req
.
URL
.
Query
()
q
:=
req
.
URL
.
Query
()
...
@@ -183,7 +183,7 @@ func TestClientPrependBaseURLPath(t *testing.T) {
...
@@ -183,7 +183,7 @@ func TestClientPrependBaseURLPath(t *testing.T) {
req
,
err
:=
c
.
NewRequest
(
http
.
MethodGet
,
"/api/ipam/vlans"
,
nil
)
req
,
err
:=
c
.
NewRequest
(
http
.
MethodGet
,
"/api/ipam/vlans"
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
"expected
an
error
, but no error returned"
)
t
.
Fatal
f
(
"
un
expected error
: %v"
,
err
)
}
}
if
want
,
got
:=
"/netbox/api/ipam/vlans"
,
req
.
URL
.
Path
;
want
!=
got
{
if
want
,
got
:=
"/netbox/api/ipam/vlans"
,
req
.
URL
.
Path
;
want
!=
got
{
...
@@ -192,6 +192,23 @@ func TestClientPrependBaseURLPath(t *testing.T) {
...
@@ -192,6 +192,23 @@ func TestClientPrependBaseURLPath(t *testing.T) {
}
}
}
}
func
TestAuthenticatingClientAddsHeader
(
t
*
testing
.
T
)
{
c
,
err
:=
NewClient
(
"localhost"
,
"auth-token"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
req
,
err
:=
c
.
NewRequest
(
http
.
MethodGet
,
"/api/ipam/vlans"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
want
,
got
:=
"Token auth-token"
,
req
.
Header
.
Get
(
"Authorization"
);
want
!=
got
{
t
.
Fatalf
(
"unexpected Authorization header:
\n
- want: %q
\n
- got: %q"
,
want
,
got
)
}
}
type
testValuer
struct
{
type
testValuer
struct
{
Foo
string
Foo
string
Bar
int
Bar
int
...
@@ -214,7 +231,7 @@ func (q testValuer) Values() (url.Values, error) {
...
@@ -214,7 +231,7 @@ func (q testValuer) Values() (url.Values, error) {
func
testClient
(
t
*
testing
.
T
,
fn
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
))
(
*
Client
,
func
())
{
func
testClient
(
t
*
testing
.
T
,
fn
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
))
(
*
Client
,
func
())
{
s
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
fn
))
s
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
fn
))
c
,
err
:=
NewClient
(
s
.
URL
,
nil
)
c
,
err
:=
NewClient
(
s
.
URL
,
""
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"error creating Client: %v"
,
err
)
t
.
Fatalf
(
"error creating Client: %v"
,
err
)
}
}
...
...
This diff is collapsed.
Click to expand it.
netbox/netbox_test.go
+
1
−
1
View file @
8bc2717e
...
@@ -72,7 +72,7 @@ func ExampleNewClient() {
...
@@ -72,7 +72,7 @@ func ExampleNewClient() {
defer
done
()
defer
done
()
// Creates a client configured to use the test server
// Creates a client configured to use the test server
c
,
err
:=
NewClient
(
addr
,
nil
)
c
,
err
:=
NewClient
(
addr
,
""
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"failed to create netbox.Client: %v"
,
err
))
panic
(
fmt
.
Sprintf
(
"failed to create netbox.Client: %v"
,
err
))
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment