refactor: ♻️ Use public nautobot/go-nautobot client and reference @nleiva work

main
Christian Adell 2023-04-24 10:13:20 +02:00
parent cd1c9e00cd
commit 95e7e3446d
No known key found for this signature in database
GPG Key ID: 888D52E62E88D026
16 changed files with 48 additions and 720268 deletions

View File

@ -1,9 +1,9 @@
TEST?=$$(go list ./... | grep -v 'vendor')
HOSTNAME=github.com
NAMESPACE=nleiva
NAMESPACE=nautobot
NAME=nautobot
BINARY=terraform-provider-${NAME}
VERSION=0.3.3
VERSION=0.0.1-beta
OS_ARCH=$(shell go env GOOS)_$(shell go env GOARCH)
@ -30,32 +30,34 @@ install: build
mkdir -p $(HOME)/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mv ${BINARY} $(HOME)/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
get-api:
cd client; wget https://demo.nautobot.com/api/swagger.yaml\?api_version\=1.3 -O swagger.yaml
# TODO: no need to generate client, use the one from nautobot/go-nautobot
# get-api:
# cd client; wget https://demo.nautobot.com/api/swagger.yaml\?api_version\=1.3 -O swagger.yaml
generate: get-api
cd client; oapi-codegen -generate client -o nautobot.go -package nautobot swagger.yaml && \
oapi-codegen -generate types -o types.go -package nautobot swagger.yaml && \
go mod tidy
# generate: get-api
# cd client; oapi-codegen -generate client -o nautobot.go -package nautobot swagger.yaml && \
# oapi-codegen -generate types -o types.go -package nautobot swagger.yaml && \
# go mod tidy
test:
go test -i $(TEST) || exit 1
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
test:
go test -i $(TEST) || exit 1
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
gpg-key:
gpg --armor --export-secret-key $(EMAIL) -w0 | xclip -selection clipboard -i
testacc:
testacc:
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
# TODO: adjust install
local: install
sed -i "s-/home/nleiva-${HOME}-" test/.terraform/plugin_path
# sed -i "s-/home/nleiva-${HOME}-" test/.terraform/plugin_path
sed -i 's-version =.*-version = "${VERSION}"-' test/main.tf
cd test; terraform init -upgrade && \
terraform apply -auto-approve; cd ..
tag: local
git add .
git commit -m "Bump to version ${VERSION}"
git tag -a -m "Bump to version ${VERSION}" v${VERSION}
git push --follow-tag
git push --follow-tag

View File

@ -1,17 +1,20 @@
# Terraform Provider Nautobot
# Terraform Provider Nautobot
Nautobot provider created for educational purposes. You can fork it for long-term development :-)
## :warning: Disclaimer :warning:
This project is in **beta** development stage, and it's suitable to change before being released as generally available. Use it at your own discretion.
## Requirements
- [Terraform](https://www.terraform.io/downloads.html) >= 0.13.x
- [Go](https://golang.org/doc/install) >= 1.20
- [Terraform](https://www.terraform.io/downloads.html) >= 0.13.x
- [Go](https://golang.org/doc/install) >= 1.20
## Building The Provider
1. Clone the repository
2. Enter the repository directory
3. Build the provider using the `make` command:
3. Build the provider using the `make` command:
```sh
$ make install
```
@ -32,16 +35,14 @@ Then commit the changes to `go.mod` and `go.sum`.
## Using the provider
The provide takes two arguments, `url` and `token`. For the data sources and resources supported, take a look at the [internal/provider](internal/provider) folder. In the next example, we capture the data of all manufacturers and create a new manufacturer "Vendor I".
```hcl
terraform {
required_providers {
nautobot = {
version = "0.3.3"
source = "nleiva/nautobot"
version = "0.0.1-beta"
source = "nautobot/nautobot"
}
}
}
@ -63,15 +64,19 @@ resource "nautobot_manufacturer" "new" {
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Requirements](#requirements) above).
There are a few make tagets you can leverage you can leverage:
There are a few make targets you can leverage:
- `make install`: To compile the provider.
- `go generate ./...`: To generate or update documentation.
- `make local`: Test local version of the provider.
- `make testacc`: To run the full suite of Acceptance tests.
*Note:* Acceptance tests create real resources, and often cost money to run.
_Note:_ Acceptance tests create real resources, and often cost money to run.
```sh
$ make testacc
```
## Credits
This [project](https://github.com/nleiva/terraform-provider-nautobot) started as an exercise for educational purposes by @nleiva during the development of his book "Network Automation with Go". Thank you Nicolas for your effort and collaboration!

View File

@ -1,15 +0,0 @@
module github.com/nautobot/go-nautobot
go 1.20
require (
github.com/deepmap/oapi-codegen v1.12.4
github.com/tidwall/gjson v1.14.4
)
require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)

View File

@ -1,25 +0,0 @@
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/deepmap/oapi-codegen v1.12.4 h1:pPmn6qI9MuOtCz82WY2Xaw46EQjgvxednXXrP7g5Q2s=
github.com/deepmap/oapi-codegen v1.12.4/go.mod h1:3lgHGMu6myQ2vqbbTXH2H1o4eXFTGnFiDaOaKKl5yas=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

File diff suppressed because it is too large Load Diff

View File

@ -1,97 +0,0 @@
package nautobot_test
import (
"context"
"fmt"
"net/http"
"os"
"testing"
nb "github.com/nautobot/go-nautobot"
"github.com/tidwall/gjson"
)
const (
deviceExist = "manufacturer with this name already exists."
)
func NewSecurityProviderNautobotToken(t string) (*SecurityProviderNautobotToken, error) {
return &SecurityProviderNautobotToken{
token: t,
}, nil
}
type SecurityProviderNautobotToken struct {
token string
}
func (s *SecurityProviderNautobotToken) Intercept(ctx context.Context, req *http.Request) error {
req.Header.Set("Authorization", fmt.Sprintf("Token %s", s.token))
return nil
}
func getenv(name string) (string, error) {
v := os.Getenv(name)
if v == "" {
return v, fmt.Errorf("%s environment variable not set", name)
}
return v, nil
}
func TestDcimManufacturersCreateWithResponse(t *testing.T) {
server, err := getenv("NAUTOBOT_URL")
if err != nil {
t.Fatal(err)
}
token, err := getenv("NAUTOBOT_TOKEN")
if err != nil {
t.Fatal(err)
}
tk, err := NewSecurityProviderNautobotToken(token)
if err != nil {
t.Fatalf("could not create a Nautobot token for %s", server)
}
c, err := nb.NewClientWithResponses(
server,
nb.WithRequestEditorFn(tk.Intercept),
)
if err != nil {
t.Fatalf("could not setup a client connection to %s", server)
}
// Test table
tt := []struct {
name string
vendor string
err string
}{
{name: "New Vendor", vendor: "new vendor 1"},
{name: "Existing Vendor", vendor: "Cisco", err: deviceExist},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var m nb.Manufacturer
m.Name = tc.vendor
rsp, err := c.DcimManufacturersCreateWithResponse(
context.Background(),
nb.DcimManufacturersCreateJSONRequestBody(m))
data := string(rsp.Body)
dataName := gjson.Get(data, "name.0")
if dataName.String() == deviceExist && tc.err == deviceExist {
return
}
if err != nil {
t.Fatalf("failed to create manufacturer %s on %s: %s", tc.vendor, server, err.Error())
}
})
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

6
go.mod
View File

@ -1,4 +1,4 @@
module github.com/nleiva/terraform-provider-nautobot
module github.com/nautobot/terraform-provider-nautobot
go 1.20
@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
github.com/nautobot/go-nautobot v0.0.0-00010101000000-000000000000
github.com/nautobot/go-nautobot v1.5.8-beta
github.com/tidwall/gjson v1.14.4
)
@ -74,5 +74,3 @@ require (
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
replace github.com/nautobot/go-nautobot => ./client

2
go.sum
View File

@ -317,6 +317,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nautobot/go-nautobot v1.5.8-beta h1:bBLMfg1GA4ms2tRTFLcWz5Lhz+r1KTCGL+ohBPy6u6c=
github.com/nautobot/go-nautobot v1.5.8-beta/go.mod h1:XeWVogQH4iHksB8xkL6x2r3FCAZglKtqRZatt3yJPGk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=

View File

@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"
nb "github.com/nautobot/go-nautobot"
nb "github.com/nautobot/go-nautobot/pkg/nautobot"
)
func dataSourceManufacturers() *schema.Resource {

View File

@ -7,7 +7,7 @@ import (
"time"
"github.com/deepmap/oapi-codegen/pkg/types"
nb "github.com/nautobot/go-nautobot"
nb "github.com/nautobot/go-nautobot/pkg/nautobot"
)
func NewSecurityProviderNautobotToken(t string) (*SecurityProviderNautobotToken, error) {

View File

@ -7,7 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
nb "github.com/nautobot/go-nautobot"
nb "github.com/nautobot/go-nautobot/pkg/nautobot"
)
func init() {

View File

@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"
nb "github.com/nautobot/go-nautobot"
nb "github.com/nautobot/go-nautobot/pkg/nautobot"
)
func resourceManufacturer() *schema.Resource {

View File

@ -4,7 +4,7 @@ import (
"flag"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/nleiva/terraform-provider-nautobot/internal/provider"
"github.com/nautobot/terraform-provider-nautobot/internal/provider"
)
// Run "go generate" to format example terraform files and generate the docs for the registry/website
@ -36,7 +36,7 @@ func main() {
Debug: debugMode,
// TODO: update this string with the full name of your provider as used in your configs
ProviderAddr: "registry.terraform.io/nleiva/nautobot",
ProviderAddr: "registry.terraform.io/nautobot/nautobot",
ProviderFunc: provider.New(version),
}

View File

@ -1,18 +1,18 @@
terraform {
required_providers {
nautobot = {
version = "0.3.3"
source = "github.com/nleiva/nautobot"
version = "0.0.1-beta"
source = "github.com/nautobot/nautobot"
}
}
}
provider "nautobot" {
url = "https://demo.nautobot.com/api/"
url = "https://demo.nautobot.com/api/"
token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
}
resource "nautobot_manufacturer" "new" {
description = "Created with Terraform"
name = "New Vendor"
}
name = "New Vendor"
}