-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add NewClient function to netbox package
Signed-off-by: Jacob Salmela <jacob.salmela@hpe.com>
- Loading branch information
1 parent
04a433b
commit 75ea6f6
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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 | ||
} |