Skip to content

Commit b1a01d1

Browse files
shrsrlhercot
authored andcommitted
Added stpifpol resource, data source, examples, docs and name alias attr schema file
1 parent ffd3101 commit b1a01d1

File tree

10 files changed

+485
-4
lines changed

10 files changed

+485
-4
lines changed

aci/base_attr_schema.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ func GetBaseAttrSchema() map[string]*schema.Schema {
99
Optional: true,
1010
Computed: true,
1111
},
12-
1312
"annotation": &schema.Schema{
1413
Type: schema.TypeString,
1514
Optional: true,
@@ -23,6 +22,5 @@ func AppendBaseAttrSchema(attrs map[string]*schema.Schema) map[string]*schema.Sc
2322
for key, value := range GetBaseAttrSchema() {
2423
attrs[key] = value
2524
}
26-
2725
return attrs
2826
}

aci/data_source_aci_stpifpol.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package aci
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ciscoecosystem/aci-go-client/client"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
)
9+
10+
func dataSourceAciSpanningTreeInterfacePolicy() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceAciSpanningTreeInterfacePolicyRead,
13+
SchemaVersion: 1,
14+
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
15+
"name": &schema.Schema{
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
20+
"ctrl": &schema.Schema{
21+
Type: schema.TypeList,
22+
Optional: true,
23+
Computed: true,
24+
Elem: &schema.Schema{
25+
Type: schema.TypeString,
26+
},
27+
},
28+
})),
29+
}
30+
}
31+
32+
func dataSourceAciSpanningTreeInterfacePolicyRead(d *schema.ResourceData, m interface{}) error {
33+
aciClient := m.(*client.Client)
34+
name := d.Get("name").(string)
35+
rn := fmt.Sprintf("ifPol-%s", name)
36+
dn := fmt.Sprintf("uni/%s", rn)
37+
stpIfPol, err := getRemoteSpanningTreeInterfacePolicy(aciClient, dn)
38+
if err != nil {
39+
return err
40+
}
41+
setSpanningTreeInterfacePolicyAttributes(stpIfPol, d)
42+
return nil
43+
}

aci/name_alias_attr_schema.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package aci
2+
3+
import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
4+
5+
func GetNameAliasAttrSchema() map[string]*schema.Schema {
6+
return map[string]*schema.Schema{
7+
"name_alias": &schema.Schema{
8+
Type: schema.TypeString,
9+
Optional: true,
10+
Computed: true,
11+
},
12+
}
13+
}
14+
15+
// AppendNameAliasAttrSchema adds the NameAliasAttr to required schema
16+
func AppendNameAliasAttrSchema(attrs map[string]*schema.Schema) map[string]*schema.Schema {
17+
for key, value := range GetNameAliasAttrSchema() {
18+
attrs[key] = value
19+
}
20+
return attrs
21+
}

aci/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func Provider() terraform.ResourceProvider {
153153
"aci_spine_interface_profile": resourceAciSpineInterfaceProfile(),
154154
"aci_spine_port_policy_group": resourceAciSpineAccessPortPolicyGroup(),
155155
"aci_fabric_if_pol": resourceAciLinkLevelPolicy(),
156+
"aci_stp_if_pol": resourceAciSpanningTreeInterfacePolicy(),
156157
"aci_aaa_domain": resourceAciSecurityDomain(),
157158
"aci_l4_l7_service_graph_template": resourceAciL4L7ServiceGraphTemplate(),
158159
"aci_logical_device_context": resourceAciLogicalDeviceContext(),
@@ -289,6 +290,7 @@ func Provider() terraform.ResourceProvider {
289290
"aci_spine_port_policy_group": dataSourceAciSpineAccessPortPolicyGroup(),
290291
"aci_fabric_path_ep": dataSourceAciFabricPathEndpoint(),
291292
"aci_fabric_if_pol": dataSourceAciLinkLevelPolicy(),
293+
"aci_stp_if_pol": dataSourceAciSpanningTreeInterfacePolicy(),
292294
"aci_aaa_domain": dataSourceAciSecurityDomain(),
293295
"aci_client_end_point": dataSourceAciClientEndPoint(),
294296
"aci_l4_l7_service_graph_template": dataSourceAciL4L7ServiceGraphTemplate(),

aci/resource_aci_stpifpol.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package aci
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"reflect"
7+
"sort"
8+
"strings"
9+
10+
"github.com/ciscoecosystem/aci-go-client/client"
11+
"github.com/ciscoecosystem/aci-go-client/models"
12+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
14+
)
15+
16+
func resourceAciSpanningTreeInterfacePolicy() *schema.Resource {
17+
18+
return &schema.Resource{
19+
Create: resourceAciSpanningTreeInterfacePolicyCreate,
20+
Update: resourceAciSpanningTreeInterfacePolicyUpdate,
21+
Read: resourceAciSpanningTreeInterfacePolicyRead,
22+
Delete: resourceAciSpanningTreeInterfacePolicyDelete,
23+
24+
Importer: &schema.ResourceImporter{
25+
State: resourceAciSpanningTreeInterfacePolicyImport,
26+
},
27+
28+
SchemaVersion: 1,
29+
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
30+
"name": &schema.Schema{
31+
Type: schema.TypeString,
32+
Required: true,
33+
ForceNew: true,
34+
},
35+
"ctrl": &schema.Schema{
36+
Type: schema.TypeList,
37+
Optional: true,
38+
Computed: true,
39+
Elem: &schema.Schema{
40+
Type: schema.TypeString,
41+
ValidateFunc: validation.StringInSlice([]string{
42+
"bpdu-filter",
43+
"bpdu-guard",
44+
}, false),
45+
},
46+
},
47+
})),
48+
}
49+
}
50+
51+
func getRemoteSpanningTreeInterfacePolicy(client *client.Client, dn string) (*models.SpanningTreeInterfacePolicy, error) {
52+
stpIfPolCont, err := client.Get(dn)
53+
if err != nil {
54+
return nil, err
55+
}
56+
stpIfPol := models.SpanningTreeInterfacePolicyFromContainer(stpIfPolCont)
57+
if stpIfPol.DistinguishedName == "" {
58+
return nil, fmt.Errorf("SpanningTreeInterfacePolicy %s not found", stpIfPol.DistinguishedName)
59+
}
60+
return stpIfPol, nil
61+
}
62+
63+
func setSpanningTreeInterfacePolicyAttributes(stpIfPol *models.SpanningTreeInterfacePolicy, d *schema.ResourceData) *schema.ResourceData {
64+
d.SetId(stpIfPol.DistinguishedName)
65+
d.Set("description", stpIfPol.Description)
66+
stpIfPolMap, _ := stpIfPol.ToMap()
67+
d.Set("name", stpIfPolMap["name"])
68+
d.Set("annotation", stpIfPolMap["annotation"])
69+
ctrlGet := make([]string, 0, 1)
70+
for _, val := range strings.Split(stpIfPolMap["ctrl"], ",") {
71+
ctrlGet = append(ctrlGet, strings.Trim(val, " "))
72+
}
73+
sort.Strings(ctrlGet)
74+
if ctrlIntr, ok := d.GetOk("ctrl"); ok {
75+
ctrlAct := make([]string, 0, 1)
76+
for _, val := range ctrlIntr.([]interface{}) {
77+
ctrlAct = append(ctrlAct, val.(string))
78+
}
79+
sort.Strings(ctrlAct)
80+
if reflect.DeepEqual(ctrlAct, ctrlGet) {
81+
d.Set("ctrl", d.Get("ctrl").([]interface{}))
82+
} else {
83+
d.Set("ctrl", ctrlGet)
84+
}
85+
} else {
86+
d.Set("ctrl", ctrlGet)
87+
}
88+
d.Set("name_alias", stpIfPolMap["nameAlias"])
89+
return d
90+
}
91+
92+
func resourceAciSpanningTreeInterfacePolicyImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
93+
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
94+
aciClient := m.(*client.Client)
95+
dn := d.Id()
96+
stpIfPol, err := getRemoteSpanningTreeInterfacePolicy(aciClient, dn)
97+
if err != nil {
98+
return nil, err
99+
}
100+
schemaFilled := setSpanningTreeInterfacePolicyAttributes(stpIfPol, d)
101+
log.Printf("[DEBUG] %s: Import finished successfully", d.Id())
102+
return []*schema.ResourceData{schemaFilled}, nil
103+
}
104+
105+
func resourceAciSpanningTreeInterfacePolicyCreate(d *schema.ResourceData, m interface{}) error {
106+
return resourceAciSpanningTreeInterfacePolicyCreateOrUpdate(d, m, false)
107+
}
108+
109+
func resourceAciSpanningTreeInterfacePolicyUpdate(d *schema.ResourceData, m interface{}) error {
110+
return resourceAciSpanningTreeInterfacePolicyCreateOrUpdate(d, m, true)
111+
}
112+
113+
func resourceAciSpanningTreeInterfacePolicyRead(d *schema.ResourceData, m interface{}) error {
114+
log.Printf("[DEBUG] %s: Beginning Read", d.Id())
115+
aciClient := m.(*client.Client)
116+
dn := d.Id()
117+
stpIfPol, err := getRemoteSpanningTreeInterfacePolicy(aciClient, dn)
118+
if err != nil {
119+
d.SetId("")
120+
return nil
121+
}
122+
setSpanningTreeInterfacePolicyAttributes(stpIfPol, d)
123+
log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
124+
return nil
125+
}
126+
127+
func resourceAciSpanningTreeInterfacePolicyDelete(d *schema.ResourceData, m interface{}) error {
128+
log.Printf("[DEBUG] %s: Beginning Destroy", d.Id())
129+
aciClient := m.(*client.Client)
130+
dn := d.Id()
131+
err := aciClient.DeleteByDn(dn, "stpIfPol")
132+
if err != nil {
133+
return err
134+
}
135+
log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id())
136+
d.SetId("")
137+
return err
138+
}
139+
func resourceAciSpanningTreeInterfacePolicyCreateOrUpdate(d *schema.ResourceData, m interface{}, update bool) error {
140+
action := "Creation"
141+
if update == true {
142+
action = "Update"
143+
}
144+
log.Printf("[DEBUG] SpanningTreeInterfacePolicy: Beginning %s", action)
145+
aciClient := m.(*client.Client)
146+
desc := d.Get("description").(string)
147+
name := d.Get("name").(string)
148+
stpIfPolAttr := models.SpanningTreeInterfacePolicyAttributes{}
149+
if Annotation, ok := d.GetOk("annotation"); ok {
150+
stpIfPolAttr.Annotation = Annotation.(string)
151+
} else {
152+
stpIfPolAttr.Annotation = "{}"
153+
}
154+
if Ctrl, ok := d.GetOk("ctrl"); ok {
155+
ctrlList := make([]string, 0, 1)
156+
for _, val := range Ctrl.([]interface{}) {
157+
ctrlList = append(ctrlList, val.(string))
158+
}
159+
Ctrl := strings.Join(ctrlList, ",")
160+
stpIfPolAttr.Ctrl = Ctrl
161+
}
162+
nameAlias := ""
163+
if NameAlias, ok := d.GetOk("name_alias"); ok {
164+
nameAlias = NameAlias.(string)
165+
}
166+
167+
stpIfPol := models.NewSpanningTreeInterfacePolicy(fmt.Sprintf("infra/ifPol-%s", name), "uni", desc, nameAlias, stpIfPolAttr)
168+
if update == true {
169+
stpIfPol.Status = "modified"
170+
}
171+
err := aciClient.Save(stpIfPol)
172+
if err != nil {
173+
return err
174+
}
175+
d.Partial(true)
176+
d.SetPartial("name")
177+
d.Partial(false)
178+
d.SetId(stpIfPol.DistinguishedName)
179+
log.Printf("[DEBUG] %s: %s finished successfully", d.Id(), action)
180+
return resourceAciSpanningTreeInterfacePolicyRead(d, m)
181+
}

0 commit comments

Comments
 (0)