-
Notifications
You must be signed in to change notification settings - Fork 98
/
data_source_aci_dhcpoption.go
115 lines (91 loc) · 2.59 KB
/
data_source_aci_dhcpoption.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package aci
import (
"context"
"fmt"
"github.com/ciscoecosystem/aci-go-client/v2/client"
"github.com/ciscoecosystem/aci-go-client/v2/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceAciDHCPOption() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciDHCPOptionRead,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"dhcp_option_policy_dn": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"data": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dhcp_option_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name_alias": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"annotation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceAciDHCPOptionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
name := d.Get("name").(string)
rn := fmt.Sprintf("opt-%s", name)
DHCPOptionPolicyDn := d.Get("dhcp_option_policy_dn").(string)
dn := fmt.Sprintf("%s/%s", DHCPOptionPolicyDn, rn)
dhcpOption, err := getRemoteDHCPOption(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}
d.SetId(dn)
_, err = setDHCPOptionAttributes(dhcpOption, d)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func getRemoteDHCPOption(client *client.Client, dn string) (*models.DHCPOption, error) {
dhcpOptionCont, err := client.Get(dn)
if err != nil {
return nil, err
}
dhcpOption := models.DHCPOptionFromContainer(dhcpOptionCont)
if dhcpOption.DistinguishedName == "" {
return nil, fmt.Errorf("DHCPOption %s not found", dhcpOption.DistinguishedName)
}
return dhcpOption, nil
}
func setDHCPOptionAttributes(dhcpOption *models.DHCPOption, d *schema.ResourceData) (*schema.ResourceData, error) {
dn := d.Id()
d.SetId(dhcpOption.DistinguishedName)
//d.Set("description", dhcpOption.Description)
if dn != dhcpOption.DistinguishedName {
d.Set("dhcp_option_policy_dn", "")
}
dhcpOptionMap, err := dhcpOption.ToMap()
if err != nil {
return d, err
}
d.Set("name", dhcpOptionMap["name"])
d.Set("annotation", dhcpOptionMap["annotation"])
d.Set("data", dhcpOptionMap["data"])
d.Set("dhcp_option_id", dhcpOptionMap["id"])
d.Set("name_alias", dhcpOptionMap["nameAlias"])
return d, nil
}