This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
forked from CiscoDevNet/terraform-provider-aci
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_aci_infrarsspaccportp.go
192 lines (138 loc) · 4.82 KB
/
resource_aci_infrarsspaccportp.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package aci
import (
"fmt"
"log"
"github.com/ciscoecosystem/aci-go-client/client"
"github.com/ciscoecosystem/aci-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceAciInterfaceProfile() *schema.Resource {
return &schema.Resource{
Create: resourceAciInterfaceProfileCreate,
Update: resourceAciInterfaceProfileUpdate,
Read: resourceAciInterfaceProfileRead,
Delete: resourceAciInterfaceProfileDelete,
Importer: &schema.ResourceImporter{
State: resourceAciInterfaceProfileImport,
},
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(map[string]*schema.Schema{
"spine_profile_dn": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tdn": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
}),
}
}
func getRemoteInterfaceProfile(client *client.Client, dn string) (*models.InterfaceProfile, error) {
infraRsSpAccPortPCont, err := client.Get(dn)
if err != nil {
return nil, err
}
infraRsSpAccPortP := models.InterfaceProfileFromContainer(infraRsSpAccPortPCont)
if infraRsSpAccPortP.DistinguishedName == "" {
return nil, fmt.Errorf("InterfaceProfile %s not found", infraRsSpAccPortP.DistinguishedName)
}
return infraRsSpAccPortP, nil
}
func setInterfaceProfileAttributes(infraRsSpAccPortP *models.InterfaceProfile, d *schema.ResourceData) *schema.ResourceData {
dn := d.Id()
d.SetId(infraRsSpAccPortP.DistinguishedName)
if dn != infraRsSpAccPortP.DistinguishedName {
d.Set("spine_profile_dn", "")
}
infraRsSpAccPortPMap, _ := infraRsSpAccPortP.ToMap()
d.Set("tdn", infraRsSpAccPortPMap["tDn"])
d.Set("annotation", infraRsSpAccPortPMap["annotation"])
return d
}
func resourceAciInterfaceProfileImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()
infraRsSpAccPortP, err := getRemoteInterfaceProfile(aciClient, dn)
if err != nil {
return nil, err
}
schemaFilled := setInterfaceProfileAttributes(infraRsSpAccPortP, d)
log.Printf("[DEBUG] %s: Import finished successfully", d.Id())
return []*schema.ResourceData{schemaFilled}, nil
}
func resourceAciInterfaceProfileCreate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] InterfaceProfile: Beginning Creation")
aciClient := m.(*client.Client)
tDn := d.Get("tdn").(string)
SpineProfileDn := d.Get("spine_profile_dn").(string)
infraRsSpAccPortPAttr := models.InterfaceProfileAttributes{}
if Annotation, ok := d.GetOk("annotation"); ok {
infraRsSpAccPortPAttr.Annotation = Annotation.(string)
} else {
infraRsSpAccPortPAttr.Annotation = "{}"
}
infraRsSpAccPortP := models.NewInterfaceProfile(fmt.Sprintf("rsspAccPortP-[%s]", tDn), SpineProfileDn, "", infraRsSpAccPortPAttr)
err := aciClient.Save(infraRsSpAccPortP)
if err != nil {
return err
}
d.Partial(true)
d.SetPartial("tdn")
d.Partial(false)
d.SetId(infraRsSpAccPortP.DistinguishedName)
log.Printf("[DEBUG] %s: Creation finished successfully", d.Id())
return resourceAciInterfaceProfileRead(d, m)
}
func resourceAciInterfaceProfileUpdate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] InterfaceProfile: Beginning Update")
aciClient := m.(*client.Client)
tDn := d.Get("tdn").(string)
SpineProfileDn := d.Get("spine_profile_dn").(string)
infraRsSpAccPortPAttr := models.InterfaceProfileAttributes{}
if Annotation, ok := d.GetOk("annotation"); ok {
infraRsSpAccPortPAttr.Annotation = Annotation.(string)
} else {
infraRsSpAccPortPAttr.Annotation = "{}"
}
infraRsSpAccPortP := models.NewInterfaceProfile(fmt.Sprintf("rsspAccPortP-[%s]", tDn), SpineProfileDn, "", infraRsSpAccPortPAttr)
infraRsSpAccPortP.Status = "modified"
err := aciClient.Save(infraRsSpAccPortP)
if err != nil {
return err
}
d.Partial(true)
d.SetPartial("tdn")
d.Partial(false)
d.SetId(infraRsSpAccPortP.DistinguishedName)
log.Printf("[DEBUG] %s: Update finished successfully", d.Id())
return resourceAciInterfaceProfileRead(d, m)
}
func resourceAciInterfaceProfileRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] %s: Beginning Read", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()
infraRsSpAccPortP, err := getRemoteInterfaceProfile(aciClient, dn)
if err != nil {
d.SetId("")
return nil
}
setInterfaceProfileAttributes(infraRsSpAccPortP, d)
log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
return nil
}
func resourceAciInterfaceProfileDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] %s: Beginning Destroy", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()
err := aciClient.DeleteByDn(dn, "infraRsSpAccPortP")
if err != nil {
return err
}
log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id())
d.SetId("")
return err
}