diff --git a/docs/data-sources/ipam_service.md b/docs/data-sources/ipam_service.md new file mode 100644 index 000000000..c6bc36c22 --- /dev/null +++ b/docs/data-sources/ipam_service.md @@ -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. diff --git a/examples/.terraform.lock.hcl b/examples/.terraform.lock.hcl new file mode 100644 index 000000000..0afd6ae06 --- /dev/null +++ b/examples/.terraform.lock.hcl @@ -0,0 +1,10 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/smutel/netbox" { + version = "0.0.1" + constraints = "0.0.1" + hashes = [ + "h1:HPT2mt+mXdoJG7KB3Ph+NiAM5SclXToVU910vXVVd/E=", + ] +} diff --git a/netbox/data_netbox_ipam_service.go b/netbox/data_netbox_ipam_service.go new file mode 100644 index 000000000..af0729557 --- /dev/null +++ b/netbox/data_netbox_ipam_service.go @@ -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 +} diff --git a/netbox/provider.go b/netbox/provider.go index 71337eb4e..950dd9b17 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -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(), diff --git a/netbox/resource_netbox_ipam_service.go b/netbox/resource_netbox_ipam_service.go index d7448adb8..bf57993ed 100644 --- a/netbox/resource_netbox_ipam_service.go +++ b/netbox/resource_netbox_ipam_service.go @@ -45,9 +45,9 @@ func resourceNetboxIpamService() *schema.Resource { ValidateFunc: validation.StringLenBetween(1, 200), }, "device_id": { - Type: schema.TypeInt, - Optional: true, - ConflictsWith: []string{"virtualmachine_id"}, + Type: schema.TypeInt, + Optional: true, + ExactlyOneOf: []string{"device_id", "virtualmachine_id"}, }, "ip_addresses_id": { Type: schema.TypeList, @@ -88,9 +88,8 @@ func resourceNetboxIpamService() *schema.Resource { }, }, "virtualmachine_id": { - Type: schema.TypeInt, - Optional: true, - ConflictsWith: []string{"device_id"}, + Type: schema.TypeInt, + Optional: true, }, }, }