Skip to content

Commit

Permalink
Merge pull request #44 from smutel/AddIpamService
Browse files Browse the repository at this point in the history
feat: Add resource and data for IPAM service
  • Loading branch information
smutel authored Feb 11, 2021
2 parents bce527e + 135c573 commit e38cc5e
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: git fetch --prune --unshallow

- name: Lint
uses: reviewdog/action-golangci-lint@v1.15.0
uses: reviewdog/action-golangci-lint@v1.17.0
with:
github_token: ${{ secrets.github_token }}
golangci_lint_flags: "--config=.golangci.yml"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
go: [ '1.15.x' ]
steps:
- name: Check conventional commits in PR
uses: Namchee/conventional-pr@v0.3.0
uses: Namchee/conventional-pr@v0.4.1
with:
access_token: ${{ secrets.github_token }}
label: "no-conventional-commits"
Expand All @@ -28,7 +28,7 @@ jobs:
run: git fetch --prune --unshallow

- name: Lint
uses: reviewdog/action-golangci-lint@v1.15.0
uses: reviewdog/action-golangci-lint@v1.17.0
with:
github_token: ${{ secrets.github_token }}
golangci_lint_flags: "--config=.golangci.yml"
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v3.0.2
uses: crazy-max/ghaction-import-gpg@v3.1.0
with:
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
Expand All @@ -47,4 +47,3 @@ jobs:
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

28 changes: 28 additions & 0 deletions docs/data-sources/ipam_service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# netbox\_ipam\_service Data Source

Get info about ipam service from netbox.

## Example Usage

```hcl
data "netbox_ipam_service" "service_test" {
device_id = 5
name = "Mail"
port = 25
protocol = "tcp"
}
```

## Argument Reference

The following arguments are supported:
* ``device_id`` - (Optional) The ID of the device linked to this object.
* ``name`` - (Required) The name of this object.
* ``port`` - (Required) The port of this object.
* ``protocol`` - (Required) The protocol of this service (tcp or udp).
* ``virtualmachine_id`` - (Optional) The ID of the vm linked to this object.

## Attributes Reference

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

Manage a service within Netbox.

## Example Usage

```hcl
resource "netbox_ipam_service" "service_test" {
name = "SMTP"
virtualmachine_id = netbox_virtualization_vm.vm_test.id
ip_addresses_id = [netbox_ipam_ip_addresses.ip_test.id]
port = "22"
protocol = "tcp"
description = "Service created by terraform"
tag {
name = "tag1"
slug = "tag1"
}
custom_fields = {
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_selection = "1"
cf_text = "Some text"
cf_url = "https://github.com"
}
}
```

## Argument Reference

The following arguments are supported:
* ``custom_fields`` - (Optional) Custom Field Keys and Values for this object
* For boolean, use the string value "true" or "false"
* For data, use the string format "YYYY-MM-DD"
* For integer, use the value between double quote "10"
* For selection, use the level id
* For text, use the string value
* For URL, use the URL as string
* ``description`` - (Optional) The description of this object.
* ``device_id`` - (Optional) The ID of the device linked to this object.
* ``ip_addresses_id`` - (Optional) Array of ID of the IP addresses attached to this object.
* ``name`` - (Required) The name for this object.
* ``port`` - (Optional) The port of this object.
* ``protocol`` - (Required) The protocol of this object (tcp or udp).
* ``virtualmachine_id`` - (Optional) The ID of the vm linked to this object.

The ``tag`` block 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.
10 changes: 10 additions & 0 deletions examples/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ resource "netbox_virtualization_vm" "vm_test" {
}
}

resource "netbox_ipam_service" "service_test" {
name = "SMTP"
virtualmachine_id = netbox_virtualization_vm.vm_test.id
ip_addresses_id = [netbox_ipam_ip_addresses.ip_test.id]
port = "22"
protocol = "tcp"
description = "Service created by terraform"

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

custom_fields = {
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_selection = "1"
cf_text = "Some text"
cf_url = "https://github.com"
}
}

resource "netbox_virtualization_interface" "interface_test" {
name = "default"
virtualmachine_id = netbox_virtualization_vm.vm_test.id
Expand Down
30 changes: 30 additions & 0 deletions examples/netbox_docker_variables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

TMP_JSON_FILE="/tmp/docker.json"

if ! which jq > /dev/null; then
echo "This script need jq binary to work"
fi

curl -sq --unix-socket /var/run/docker.sock http://localhost/containers/json -o ${TMP_JSON_FILE}

DOCKER_COUNT=$(cat ${TMP_JSON_FILE} | jq ".[] | length" | uniq)

for ((i = 0 ; i < ${DOCKER_COUNT} ; i++)); do
IMAGE_NAME=$(cat /tmp/docker.json | jq .[$i].Image | xargs | cut -d":" -f1)

if [ "${IMAGE_NAME}" == "nginx" ]; then
PORTS_COUNT=$(cat /tmp/docker.json | jq ".[$i].Ports | length")

for ((j = 0 ; j < ${PORTS_COUNT} ; j++)); do
PORT=$(cat /tmp/docker.json | jq .[$i].Ports[$j].PublicPort | xargs)
if [ "$PORT" != "null" ]; then
PUBLIC_PORT=$PORT
fi
done
fi
done

export NETBOX_URL="127.0.0.1:$PUBLIC_PORT"
export NETBOX_SCHEME="http"
export NETBOX_TOKEN="0123456789abcdef0123456789abcdef01234567"
85 changes: 85 additions & 0 deletions netbox/data_netbox_ipam_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package netbox

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
netboxclient "github.com/netbox-community/go-netbox/netbox/client"
"github.com/netbox-community/go-netbox/netbox/client/ipam"
)

func dataNetboxIpamService() *schema.Resource {
return &schema.Resource{
Read: dataNetboxIpamServiceRead,

Schema: map[string]*schema.Schema{
"device_id": {
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"virtualmachine_id"},
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},
"port": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 65535),
},
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"tcp", "udp"}, false),
},
"virtualmachine_id": {
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"device_id"},
},
},
}
}

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

deviceID := int64(d.Get("device_id").(int))
deviceIDStr := strconv.FormatInt(deviceID, 10)
name := d.Get("name").(string)
port := int64(d.Get("port").(int))
portStr := strconv.FormatInt(port, 10)
protocol := d.Get("protocol").(string)
vmID := int64(d.Get("virtualmachine_id").(int))
vmIDStr := strconv.FormatInt(vmID, 10)

p := ipam.NewIpamServicesListParams().WithName(&name)
p.SetPort(&portStr)
p.SetProtocol(&protocol)
if deviceID != 0 {
p.SetDeviceID(&deviceIDStr)
} else if vmID != 0 {
p.SetVirtualMachineID(&vmIDStr)
}

list, err := client.Ipam.IpamServicesList(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 @@ -41,6 +41,7 @@ func Provider() *schema.Provider {
"netbox_ipam_aggregate": dataNetboxIpamAggregate(),
"netbox_ipam_ip_addresses": dataNetboxIpamIPAddresses(),
"netbox_ipam_role": dataNetboxIpamRole(),
"netbox_ipam_service": dataNetboxIpamService(),
"netbox_ipam_vlan": dataNetboxIpamVlan(),
"netbox_ipam_vlan_group": dataNetboxIpamVlanGroup(),
"netbox_json_circuits_circuit_terminations_list": dataNetboxJSONCircuitsCircuitTerminationsList(),
Expand Down Expand Up @@ -118,6 +119,7 @@ func Provider() *schema.Provider {
"netbox_ipam_aggregate": resourceNetboxIpamAggregate(),
"netbox_ipam_ip_addresses": resourceNetboxIpamIPAddresses(),
"netbox_ipam_prefix": resourceNetboxIpamPrefix(),
"netbox_ipam_service": resourceNetboxIpamService(),
"netbox_ipam_vlan": resourceNetboxIpamVlan(),
"netbox_ipam_vlan_group": resourceNetboxIpamVlanGroup(),
"netbox_tenancy_tenant": resourceNetboxTenancyTenant(),
Expand Down
Loading

0 comments on commit e38cc5e

Please sign in to comment.