-
Notifications
You must be signed in to change notification settings - Fork 98
/
resource_aci_snmpcommunityp_deprecated.go
176 lines (142 loc) · 5.1 KB
/
resource_aci_snmpcommunityp_deprecated.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package aci
import (
"context"
"fmt"
"log"
"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 resourceAciSNMPCommunityDeprecated() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "Use aci_snmp_community resource instead",
CreateContext: resourceAciSNMPCommunityCreateDeprecated,
UpdateContext: resourceAciSNMPCommunityUpdateDeprecated,
ReadContext: resourceAciSNMPCommunityReadDeprecated,
DeleteContext: resourceAciSNMPCommunityDelete,
Importer: &schema.ResourceImporter{
State: resourceAciSNMPCommunityImport,
},
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"vrf_snmp_context_dn": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateNameAttribute(),
},
})),
}
}
func setSNMPCommunityAttributesDeprecated(snmpCommunityP *models.SNMPCommunity, d *schema.ResourceData) (*schema.ResourceData, error) {
dn := d.Id()
d.SetId(snmpCommunityP.DistinguishedName)
d.Set("description", snmpCommunityP.Description)
if dn != snmpCommunityP.DistinguishedName {
d.Set("vrf_snmp_context_dn", "")
}
snmpCommunityPMap, err := snmpCommunityP.ToMap()
if err != nil {
return d, err
}
d.Set("name", snmpCommunityPMap["name"])
d.Set("name_alias", snmpCommunityPMap["nameAlias"])
d.Set("annotation", snmpCommunityPMap["annotation"])
d.Set("vrf_snmp_context_dn", GetParentDn(d.Id(), fmt.Sprintf("/community-%s", snmpCommunityPMap["name"])))
return d, nil
}
func resourceAciSNMPCommunityImportDeprecated(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()
snmpCommunityP, err := getRemoteSNMPCommunity(aciClient, dn)
if err != nil {
return nil, err
}
schemaFilled, err := setSNMPCommunityAttributesDeprecated(snmpCommunityP, d)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] %s: Import finished successfully", d.Id())
return []*schema.ResourceData{schemaFilled}, nil
}
func resourceAciSNMPCommunityCreateDeprecated(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] SNMPCommunity: Beginning Creation")
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
name := d.Get("name").(string)
SNMPPolicyDn := d.Get("vrf_snmp_context_dn").(string)
snmpCommunityPAttr := models.SNMPCommunityAttributes{}
nameAlias := ""
if NameAlias, ok := d.GetOk("name_alias"); ok {
nameAlias = NameAlias.(string)
}
if Annotation, ok := d.GetOk("annotation"); ok {
snmpCommunityPAttr.Annotation = Annotation.(string)
} else {
snmpCommunityPAttr.Annotation = "{}"
}
if Name, ok := d.GetOk("name"); ok {
snmpCommunityPAttr.Name = Name.(string)
}
snmpCommunityP := models.NewSNMPCommunity(fmt.Sprintf(models.RnsnmpCommunityP, name), SNMPPolicyDn, desc, nameAlias, snmpCommunityPAttr)
err := aciClient.Save(snmpCommunityP)
if err != nil {
return diag.FromErr(err)
}
d.SetId(snmpCommunityP.DistinguishedName)
log.Printf("[DEBUG] %s: Creation finished successfully", d.Id())
return resourceAciSNMPCommunityReadDeprecated(ctx, d, m)
}
func resourceAciSNMPCommunityUpdateDeprecated(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] SNMPCommunity: Beginning Update")
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
name := d.Get("name").(string)
SNMPPolicyDn := d.Get("vrf_snmp_context_dn").(string)
snmpCommunityPAttr := models.SNMPCommunityAttributes{}
nameAlias := ""
if NameAlias, ok := d.GetOk("name_alias"); ok {
nameAlias = NameAlias.(string)
}
if Annotation, ok := d.GetOk("annotation"); ok {
snmpCommunityPAttr.Annotation = Annotation.(string)
} else {
snmpCommunityPAttr.Annotation = "{}"
}
if Name, ok := d.GetOk("name"); ok {
snmpCommunityPAttr.Name = Name.(string)
}
snmpCommunityP := models.NewSNMPCommunity(fmt.Sprintf("community-%s", name), SNMPPolicyDn, desc, nameAlias, snmpCommunityPAttr)
snmpCommunityP.Status = "modified"
err := aciClient.Save(snmpCommunityP)
if err != nil {
return diag.FromErr(err)
}
d.SetId(snmpCommunityP.DistinguishedName)
log.Printf("[DEBUG] %s: Update finished successfully", d.Id())
return resourceAciSNMPCommunityReadDeprecated(ctx, d, m)
}
func resourceAciSNMPCommunityReadDeprecated(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] %s: Beginning Read", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()
snmpCommunityP, err := getRemoteSNMPCommunity(aciClient, dn)
if err != nil {
d.SetId("")
return nil
}
_, err = setSNMPCommunityAttributesDeprecated(snmpCommunityP, d)
if err != nil {
d.SetId("")
return nil
}
log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
return nil
}