Skip to content

Commit

Permalink
Merge pull request #191 from Cray-HPE/known-aquaman
Browse files Browse the repository at this point in the history
make cani netbox package and vendor
  • Loading branch information
jacobsalmela committed Mar 20, 2024
2 parents 0efb701 + 7f00ad9 commit 3452be4
Show file tree
Hide file tree
Showing 23 changed files with 6,448 additions and 800 deletions.
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# MIT License
#
# (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP
# (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -185,6 +185,19 @@ bin/swagger-codegen-cli.jar:
mkdir -p ./bin
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.43/swagger-codegen-cli-3.0.43.jar -O bin/swagger-codegen-cli.jar

# needs human munging until go-jsonschema can read a dir for resolving refs
netbox-dt-schema:
go-jsonschema -p netbox devicetype-library/schema/devicetype.json -o pkg/netbox/types_devicetypes.go

# needs human munging until go-jsonschema can read a dir for resolving refs
netbox-mt-schema:
go-jsonschema -p netbox devicetype-library/schema/moduletype.json -o pkg/netbox/types_moduletypes.go

# needs human munging find/replace to make the generated file work
nbschema: netbox-dt-schema netbox-mt-schema
go-jsonschema -p netbox pkg/netbox/schema/devicetype-for-go-jsonschema.json -o pkg/netbox/types_devicetypes.go
go-jsonschema -p netbox pkg/netbox/schema/moduletype-for-go-jsonschema.json -o pkg/netbox/types_moduletypes.go

vet: version
go vet -v ./...

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/mapstructure v1.5.0
github.com/netbox-community/go-netbox/v3 v3.7.1-alpha.0
github.com/rs/zerolog v1.29.1
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.8.4
golang.org/x/oauth2 v0.8.0
golang.org/x/term v0.16.0
gopkg.in/ini.v1 v1.67.0
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ github.com/mutecomm/go-sqlcipher/v4 v4.4.0/go.mod h1:PyN04SaWalavxRGH9E8ZftG6Ju7
github.com/nakagami/firebirdsql v0.0.0-20190310045651-3c02a58cfed8/go.mod h1:86wM1zFnC6/uDBfZGNwB65O+pR2OFi5q/YQaEUid1qA=
github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo=
github.com/neo4j/neo4j-go-driver v1.8.1-0.20200803113522-b626aa943eba/go.mod h1:ncO5VaFWh0Nrt+4KT4mOZboaczBZcLuHrG+/sUeP8gI=
github.com/netbox-community/go-netbox/v3 v3.7.1-alpha.0 h1:lh17EC7tIUMkD7Z9BO0TlbaHMzW5EWNqFjPu42IOtA8=
github.com/netbox-community/go-netbox/v3 v3.7.1-alpha.0/go.mod h1:RNFSP40YwfddVs1EJGTDcFwlHWp+Dv0+x7tNmrsnMTE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
Expand Down Expand Up @@ -324,8 +326,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xanzy/go-gitlab v0.15.0/go.mod h1:8zdQa/ri1dfn8eS3Ir1SyfvOKlw7WBJ8DVThkpGiXrs=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
Expand Down Expand Up @@ -659,7 +661,6 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
Expand Down
76 changes: 76 additions & 0 deletions pkg/netbox/netbox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* MIT License
*
* (C) Copyright 2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package netbox

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"

"github.com/hashicorp/go-retryablehttp"
"github.com/netbox-community/go-netbox/v3"
)

// NewClient creates a new netbox client using the environment variables
// This maintains parity/compatiblity with Device-Type-Librery-Import repo
// by using the same environment variables
func NewClient() (*netbox.APIClient, context.Context, error) {
// use the certificates in the http client
tlsConfig := &tls.Config{}
if os.Getenv("IGNORE_SSL_ERRORS") == "True" {
tlsConfig.InsecureSkipVerify = true
} else {
tlsConfig.InsecureSkipVerify = false
}

// use TLS config in transport
tr := &http.Transport{
TLSClientConfig: tlsConfig,
}

// Setup our HTTP transport and client
httpClient := retryablehttp.NewClient()
httpClient.HTTPClient.Transport = tr
httpClient.Logger = nil
c := httpClient.StandardClient()

// create the netbox config
token := os.Getenv("NETBOX_TOKEN")
host := stripProtocolsAndSpecialChars(os.Getenv("NETBOX_URL"))
nbcfg := netbox.NewConfiguration()
nbcfg.Host = host
nbcfg.HTTPClient = c
nbcfg.DefaultHeader["Authorization"] = fmt.Sprintf("Token %s", token)
nbcfg.Debug = false
nbcfg.Scheme = "https"

ctx := context.Background()
client := netbox.NewAPIClient(nbcfg)

return client, ctx, nil
}

0 comments on commit 3452be4

Please sign in to comment.