Skip to content

Commit

Permalink
feat: Add contact resource
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed May 20, 2022
1 parent 5976f5a commit 43d3089
Show file tree
Hide file tree
Showing 6 changed files with 614 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/data-sources/tenancy_contact.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# netbox\_tenancy\_contact Data Source

Get info about tenancy contact from netbox.

## Example Usage

```hcl
data "netbox_tenancy_contact" "contact_test" {
name = "John Doe"
}
```

## Argument Reference

The following arguments are supported:
* ``name`` - (Required) The name of the contact.

## Attributes Reference

In addition to the above arguments, the following attributes are exported:
* ``id`` - The id (ref in Netbox) of this object.
95 changes: 95 additions & 0 deletions docs/resources/tenancy_contact.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# netbox\_tenancy\_contact Resource

Manage a contact within Netbox.

## Example Usage

```hcl
resource "netbox_tenancy_contact" "contact_test" {
name = "John Doe"
title = "Someone in the world"
phone = "+330123456789"
email = "[email protected]"
address = "Somewhere in the world"
comments = "Good contact"
tag {
name = "tag1"
slug = "tag1"
}
custom_field {
name = "cf_boolean"
type = "boolean"
value = "true"
}
custom_field {
name = "cf_date"
type = "date"
value = "2020-12-25"
}
custom_field {
name = "cf_text"
type = "text"
value = "some text"
}
custom_field {
name = "cf_integer"
type = "integer"
value = "10"
}
custom_field {
name = "cf_selection"
type = "selection"
value = "1"
}
custom_field {
name = "cf_url"
type = "url"
value = "https://github.com"
}
custom_field {
name = "cf_multiple_selection"
type = "multiple"
value = "0,1"
}
}
```

## Argument Reference

The following arguments are supported:
* ``name`` - (Required) The name for this object.
* ``title`` - (Optional) The title for this object.
* ``phone`` - (Optional) The phone for this object.
* ``email`` - (Optional) The e-mail for this object.
* ``address`` - (Optional) The address for this object.
* ``comments`` - (Optional) Comments for this object.

The ``custom_field`` block (optional) supports:
* ``name`` - (Required) Name of the existing custom resource to associate with this resource.
* ``type`` - (Required) Type of the existing custom resource to associate with this resource (text, integer, boolean, url, selection, multiple).
* ``value`` - (Required) Value of the existing custom resource to associate with this resource.

The ``tag`` block (optional) supports:
* ``name`` - (Required) Name of the existing tag to associate with this resource.
* ``slug`` - (Required) Slug of the existing tag to associate with this resource.

## Attributes Reference

In addition to the above arguments, the following attributes are exported:
* ``id`` - The id (ref in Netbox) of this object.

## Import

Tenants can be imported by `id` e.g.

```
$ terraform import netbox_tenancy_contact.contact_test id
```
61 changes: 61 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,64 @@ resource "netbox_ipam_aggregate" "aggregate_test" {
value = "0,1"
}
}

resource "netbox_tenancy_contact" "contact" {
name = "John Doe"
title = "Someone in the world"
phone = "+330123456789"
email = "[email protected]"
address = "Somewhere in the world"
comments = "Good contact"

tag {
name = "tag1"
slug = "tag1"
}

tag {
name = "tag2"
slug = "tag2"
}

custom_field {
name = "cf_boolean"
type = "boolean"
value = "true"
}

custom_field {
name = "cf_date"
type = "date"
value = "2020-12-25"
}

custom_field {
name = "cf_text"
type = "text"
value = "some text"
}

custom_field {
name = "cf_integer"
type = "integer"
value = "10"
}

custom_field {
name = "cf_selection"
type = "selection"
value = "1"
}

custom_field {
name = "cf_url"
type = "url"
value = "https://github.com"
}

custom_field {
name = "cf_multiple_selection"
type = "multiple"
value = "0,1"
}
}
50 changes: 50 additions & 0 deletions netbox/data_netbox_tenancy_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package netbox

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
netboxclient "github.com/smutel/go-netbox/netbox/client"
"github.com/smutel/go-netbox/netbox/client/tenancy"
)

func dataNetboxTenancyContact() *schema.Resource {
return &schema.Resource{
Read: dataNetboxTenancyContactRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
},
}
}

func dataNetboxTenancyContactRead(d *schema.ResourceData, m interface{}) error {
client := m.(*netboxclient.NetBoxAPI)

name := d.Get("name").(string)

p := tenancy.NewTenancyContactsListParams().WithName(&name)

list, err := client.Tenancy.TenancyContactsList(p, nil)
if err != nil {
return err
}

if *list.Payload.Count < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
} else if *list.Payload.Count > 1 {
return fmt.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}

d.SetId(strconv.FormatInt(list.Payload.Results[0].ID, 10))

return nil
}
2 changes: 2 additions & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func Provider() *schema.Provider {
"netbox_ipam_service": dataNetboxIpamService(),
"netbox_ipam_vlan": dataNetboxIpamVlan(),
"netbox_ipam_vlan_group": dataNetboxIpamVlanGroup(),
"netbox_tenancy_contact": dataNetboxTenancyContact(),
"netbox_tenancy_tenant": dataNetboxTenancyTenant(),
"netbox_tenancy_tenant_group": dataNetboxTenancyTenantGroup(),
"netbox_virtualization_cluster": dataNetboxVirtualizationCluster(),
Expand All @@ -140,6 +141,7 @@ func Provider() *schema.Provider {
"netbox_ipam_service": resourceNetboxIpamService(),
"netbox_ipam_vlan": resourceNetboxIpamVlan(),
"netbox_ipam_vlan_group": resourceNetboxIpamVlanGroup(),
"netbox_tenancy_contact": resourceNetboxTenancyContact(),
"netbox_tenancy_tenant": resourceNetboxTenancyTenant(),
"netbox_tenancy_tenant_group": resourceNetboxTenancyTenantGroup(),
"netbox_virtualization_interface": resourceNetboxVirtualizationInterface(),
Expand Down
Loading

0 comments on commit 43d3089

Please sign in to comment.