Report this

What is the reason for this report?

Example to create a tenant with https://github.com/digitalocean/go-netbox

Posted on November 6, 2019

Hello,

This library was created by digitalocean but does seems to be maintain today …

For the moment I would like an example of Go code to create a new tenant in Netbox with this library: https://github.com/digitalocean/go-netbox.

Thanks.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

To create a new tenant in Netbox using the go-netbox library, you can follow the example below:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/digitalocean/go-netbox/netbox"
    "github.com/digitalocean/go-netbox/netbox/client/tenancy"
    "github.com/go-openapi/strfmt"
    "github.com/go-openapi/swag"
)

func main() {
    client := netbox.NewNetboxWithAPIKey("http://netbox.example.com/api/", "YOUR_API_KEY")
    createTenant(client)
}

func createTenant(client *netbox.Netbox) {
    tenantParams := &tenancy.TenancyTenantsCreateParams{
        Data: &tenancy.TenancyTenantsCreateBody{
            Name:        swag.String("New tenant"),
            Description: swag.String("Tenant description"),
            Slug:        swag.String("new-tenant"),
            Created:     strfmt.DateTime{},
            LastUpdated: strfmt.DateTime{},
        },
        Context: context.TODO(),
    }

    result, err := client.Tenancy.TenancyTenantsCreate(tenantParams, nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("New tenant created: %s\n", *result.Payload.Name)
}

Have in mind that this is an example and the code might need a better implementation in order to serve the purpose of your application.

Make sure to replace http://netbox.example.com/api/ with your Netbox API URL and YOUR_API_KEY with your actual API key.

You can find more information on using the go-netbox library in the official GitHub repository. For DigitalOcean API, refer to the official documentation.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.