Skip to content

Commit

Permalink
dcim{,_sites}: add DCIMService, Client.DCIM.Sites methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlayher committed Jul 8, 2016
1 parent 2a646e1 commit 24810d8
Show file tree
Hide file tree
Showing 7 changed files with 550 additions and 14 deletions.
97 changes: 97 additions & 0 deletions asn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2016 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netbox

// An ASN is an Autonomous System Number, used to designate that a collection of
// Internet Protocol routing prefixes are under the control of one or more
// network operators.
type ASN int

// Reserved and private ASN ranges, used in filters below.
// Reference: http://www.iana.org/assignments/as-numbers/as-numbers.xhtml.
const (
reservedStart16Bit = 0
reservedMin16Bit = 64297
reservedMax16Bit = 64495
reservedDocMin16Bit = 64496
reservedDocMax16Bit = 64511
reservedEnd16Bit = 65535

reservedDocMin32Bit = 65536
reservedDocMax32Bit = 65551
reservedMin32Bit = 65552
reservedMax32Bit = 131071
reservedEnd32Bit = 4294967295

privateMin16Bit = 64512
privateMax16Bit = 65534

privateMin32Bit = 4200000000
privateMax32Bit = 4294967294

// RFC 6793, Section 9, AS_TRANS, reserved
asTrans = 23456
)

// Private determines if an ASN resides within a private range.
func (a ASN) Private() bool {
switch {
case a >= privateMin16Bit && a <= privateMax16Bit:
return true
case a >= privateMin32Bit && a <= privateMax32Bit:
return true
default:
return false
}
}

// Public determines if an ASN does not reside within a private or reserved
// range.
func (a ASN) Public() bool {
if !a.Valid() {
return false
}

return !a.Private() && !a.Reserved()
}

// Reserved determines if an ASN resides within a reserved range.
func (a ASN) Reserved() bool {
switch {
case a == reservedStart16Bit:
return true
case a == asTrans:
return true
case a >= reservedMin16Bit && a <= reservedMax16Bit:
return true
case a >= reservedDocMin16Bit && a <= reservedDocMax16Bit:
return true
case a == reservedEnd16Bit:
return true
case a >= reservedDocMin32Bit && a <= reservedDocMax32Bit:
return true
case a >= reservedMin32Bit && a <= reservedMax32Bit:
return true
case a == reservedEnd32Bit:
return true
default:
return false
}
}

// Valid determines if an ASN is valid.
func (a ASN) Valid() bool {
return a >= reservedStart16Bit && a <= reservedEnd32Bit
}
284 changes: 284 additions & 0 deletions asn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
// Copyright 2016 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netbox

import (
"math"
"testing"
)

func TestASNPrivate(t *testing.T) {
var tests = []struct {
a ASN
ok bool
}{
{
a: 0,
},
{
a: math.MinInt64,
},
{
a: math.MaxInt64,
},
{
a: privateMin16Bit,
ok: true,
},
{
a: privateMin16Bit + 1,
ok: true,
},
{
a: privateMax16Bit - 1,
ok: true,
},
{
a: privateMax16Bit,
ok: true,
},
{
a: privateMin32Bit,
ok: true,
},
{
a: privateMax32Bit,
ok: true,
},
}

for _, tt := range tests {
if want, got := tt.ok, tt.a.Private(); want != got {
t.Fatalf("unexpected ASN(%d).Private():\n- want: %v\n- got: %v",
tt.a, want, got)
}
}
}

func TestASNPublic(t *testing.T) {
var tests = []struct {
a ASN
ok bool
}{
{
a: math.MinInt64,
},
{
a: math.MaxInt64,
},
{
a: privateMin16Bit,
},
{
a: privateMin16Bit + 1,
},
{
a: privateMax16Bit - 1,
},
{
a: privateMax16Bit,
},
{
a: privateMin32Bit,
},
{
a: privateMax32Bit,
},
{
a: reservedStart16Bit,
},
{
a: reservedMin16Bit,
},
{
a: reservedMin16Bit + 1,
},
{
a: reservedMax16Bit - 1,
},
{
a: reservedMax16Bit,
},
{
a: reservedDocMin16Bit,
},
{
a: reservedDocMin16Bit + 1,
},
{
a: reservedDocMax16Bit - 1,
},
{
a: reservedDocMax16Bit,
},
{
a: reservedEnd16Bit,
},
{
a: reservedDocMin32Bit,
},
{
a: reservedDocMin32Bit + 1,
},
{
a: reservedDocMax32Bit - 1,
},
{
a: reservedDocMax32Bit,
},
{
a: reservedMin32Bit,
},
{
a: reservedMin32Bit + 1,
},
{
a: reservedMax32Bit - 1,
},
{
a: reservedMax32Bit,
},
{
a: reservedEnd32Bit,
},
{
a: asTrans,
},
{
a: reservedStart16Bit + 1,
ok: true,
},
{
a: reservedMin16Bit - 1,
ok: true,
},
{
a: reservedMax32Bit + 1,
ok: true,
},
{
a: privateMin32Bit - 1,
ok: true,
},
}

for _, tt := range tests {
if want, got := tt.ok, tt.a.Public(); want != got {
t.Fatalf("unexpected ASN(%d).Public():\n- want: %v\n- got: %v",
tt.a, want, got)
}
}
}

func TestASNReserved(t *testing.T) {
var tests = []struct {
a ASN
ok bool
}{
{
a: math.MinInt64,
},
{
a: math.MaxInt64,
},
{
a: reservedStart16Bit,
ok: true,
},
{
a: reservedMin16Bit,
ok: true,
},
{
a: reservedMin16Bit + 1,
ok: true,
},
{
a: reservedMax16Bit - 1,
ok: true,
},
{
a: reservedMax16Bit,
ok: true,
},
{
a: reservedDocMin16Bit,
ok: true,
},
{
a: reservedDocMin16Bit + 1,
ok: true,
},
{
a: reservedDocMax16Bit - 1,
ok: true,
},
{
a: reservedDocMax16Bit,
ok: true,
},
{
a: reservedEnd16Bit,
ok: true,
},
{
a: reservedDocMin32Bit,
ok: true,
},
{
a: reservedDocMin32Bit + 1,
ok: true,
},
{
a: reservedDocMax32Bit - 1,
ok: true,
},
{
a: reservedDocMax32Bit,
ok: true,
},
{
a: reservedMin32Bit,
ok: true,
},
{
a: reservedMin32Bit + 1,
ok: true,
},
{
a: reservedMax32Bit - 1,
ok: true,
},
{
a: reservedMax32Bit,
ok: true,
},
{
a: reservedEnd32Bit,
ok: true,
},
{
a: asTrans,
ok: true,
},
}

for _, tt := range tests {
if want, got := tt.ok, tt.a.Reserved(); want != got {
t.Fatalf("unexpected ASN(%d).Reserved():\n- want: %v\n- got: %v",
tt.a, want, got)
}
}
}
Loading

0 comments on commit 24810d8

Please sign in to comment.