-
Notifications
You must be signed in to change notification settings - Fork 19
/
data_netbox_tenancy_contact.go
60 lines (49 loc) · 1.62 KB
/
data_netbox_tenancy_contact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package tenancy
import (
"context"
"strconv"
"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"
netboxclient "github.com/smutel/go-netbox/v3/netbox/client"
"github.com/smutel/go-netbox/v3/netbox/client/tenancy"
"github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util"
)
func DataNetboxTenancyContact() *schema.Resource {
return &schema.Resource{
ReadContext: dataNetboxTenancyContactRead,
Schema: map[string]*schema.Schema{
"content_type": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
},
}
}
func dataNetboxTenancyContactRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
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 diag.FromErr(err)
}
if *list.Payload.Count < 1 {
return diag.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
} else if *list.Payload.Count > 1 {
return diag.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}
r := list.Payload.Results[0]
d.SetId(strconv.FormatInt(r.ID, 10))
if err = d.Set("content_type", util.ConvertURIContentType(r.URL)); err != nil {
return diag.FromErr(err)
}
return nil
}