@@ -14,7 +14,6 @@ import (
1414)
1515
1616func resourceAciSpanningTreeInterfacePolicy () * schema.Resource {
17-
1817 return & schema.Resource {
1918 Create : resourceAciSpanningTreeInterfacePolicyCreate ,
2019 Update : resourceAciSpanningTreeInterfacePolicyUpdate ,
@@ -27,12 +26,8 @@ func resourceAciSpanningTreeInterfacePolicy() *schema.Resource {
2726
2827 SchemaVersion : 1 ,
2928 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 {
29+
30+ "ctrl" : {
3631 Type : schema .TypeList ,
3732 Optional : true ,
3833 Computed : true ,
@@ -41,9 +36,15 @@ func resourceAciSpanningTreeInterfacePolicy() *schema.Resource {
4136 ValidateFunc : validation .StringInSlice ([]string {
4237 "bpdu-filter" ,
4338 "bpdu-guard" ,
39+ "unspecified" ,
4440 }, false ),
4541 },
4642 },
43+ "name" : {
44+ Type : schema .TypeString ,
45+ Required : true ,
46+ ForceNew : true ,
47+ },
4748 })),
4849 }
4950}
@@ -64,7 +65,6 @@ func setSpanningTreeInterfacePolicyAttributes(stpIfPol *models.SpanningTreeInter
6465 d .SetId (stpIfPol .DistinguishedName )
6566 d .Set ("description" , stpIfPol .Description )
6667 stpIfPolMap , _ := stpIfPol .ToMap ()
67- d .Set ("name" , stpIfPolMap ["name" ])
6868 d .Set ("annotation" , stpIfPolMap ["annotation" ])
6969 ctrlGet := make ([]string , 0 , 1 )
7070 for _ , val := range strings .Split (stpIfPolMap ["ctrl" ], "," ) {
@@ -85,6 +85,7 @@ func setSpanningTreeInterfacePolicyAttributes(stpIfPol *models.SpanningTreeInter
8585 } else {
8686 d .Set ("ctrl" , ctrlGet )
8787 }
88+ d .Set ("name" , stpIfPolMap ["name" ])
8889 d .Set ("name_alias" , stpIfPolMap ["nameAlias" ])
8990 return d
9091}
@@ -103,54 +104,64 @@ func resourceAciSpanningTreeInterfacePolicyImport(d *schema.ResourceData, m inte
103104}
104105
105106func 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 ())
107+ log .Printf ("[DEBUG] SpanningTreeInterfacePolicy: Beginning Creation" )
115108 aciClient := m .(* client.Client )
116- dn := d .Id ()
117- stpIfPol , err := getRemoteSpanningTreeInterfacePolicy (aciClient , dn )
118- if err != nil {
119- d .SetId ("" )
120- return nil
109+ desc := d .Get ("description" ).(string )
110+ name := d .Get ("name" ).(string )
111+ stpIfPolAttr := models.SpanningTreeInterfacePolicyAttributes {}
112+ nameAlias := ""
113+ if NameAlias , ok := d .GetOk ("name_alias" ); ok {
114+ nameAlias = NameAlias .(string )
115+ }
116+ if Annotation , ok := d .GetOk ("annotation" ); ok {
117+ stpIfPolAttr .Annotation = Annotation .(string )
118+ } else {
119+ stpIfPolAttr .Annotation = "{}"
121120 }
122- setSpanningTreeInterfacePolicyAttributes (stpIfPol , d )
123- log .Printf ("[DEBUG] %s: Read finished successfully" , d .Id ())
124- return nil
125- }
126121
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" )
122+ if Ctrl , ok := d .GetOk ("ctrl" ); ok {
123+ ctrlList := make ([]string , 0 , 1 )
124+ for _ , val := range Ctrl .([]interface {}) {
125+ ctrlList = append (ctrlList , val .(string ))
126+ }
127+ Ctrl := strings .Join (ctrlList , "," )
128+ stpIfPolAttr .Ctrl = Ctrl
129+ }
130+
131+ if Name , ok := d .GetOk ("name" ); ok {
132+ stpIfPolAttr .Name = Name .(string )
133+ }
134+ stpIfPol := models .NewSpanningTreeInterfacePolicy (fmt .Sprintf ("infra/ifPol-%s" , name ), "uni" , desc , nameAlias , stpIfPolAttr )
135+ err := aciClient .Save (stpIfPol )
132136 if err != nil {
133137 return err
134138 }
135- log .Printf ("[DEBUG] %s: Destroy finished successfully" , d .Id ())
136- d .SetId ("" )
137- return err
139+ d .Partial (true )
140+ d .SetPartial ("name" )
141+ d .Partial (false )
142+
143+ d .SetId (stpIfPol .DistinguishedName )
144+ log .Printf ("[DEBUG] %s: Creation finished successfully" , d .Id ())
145+ return resourceAciSpanningTreeInterfacePolicyRead (d , m )
138146}
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 )
147+
148+ func resourceAciSpanningTreeInterfacePolicyUpdate (d * schema.ResourceData , m interface {}) error {
149+ log .Printf ("[DEBUG] SpanningTreeInterfacePolicy: Beginning Update" )
145150 aciClient := m .(* client.Client )
146151 desc := d .Get ("description" ).(string )
147152 name := d .Get ("name" ).(string )
148153 stpIfPolAttr := models.SpanningTreeInterfacePolicyAttributes {}
154+ nameAlias := ""
155+ if NameAlias , ok := d .GetOk ("name_alias" ); ok {
156+ nameAlias = NameAlias .(string )
157+ }
158+
149159 if Annotation , ok := d .GetOk ("annotation" ); ok {
150160 stpIfPolAttr .Annotation = Annotation .(string )
151161 } else {
152162 stpIfPolAttr .Annotation = "{}"
153163 }
164+
154165 if Ctrl , ok := d .GetOk ("ctrl" ); ok {
155166 ctrlList := make ([]string , 0 , 1 )
156167 for _ , val := range Ctrl .([]interface {}) {
@@ -159,23 +170,49 @@ func resourceAciSpanningTreeInterfacePolicyCreateOrUpdate(d *schema.ResourceData
159170 Ctrl := strings .Join (ctrlList , "," )
160171 stpIfPolAttr .Ctrl = Ctrl
161172 }
162- nameAlias := ""
163- if NameAlias , ok := d .GetOk ("name_alias" ); ok {
164- nameAlias = NameAlias .(string )
165- }
166173
167- stpIfPol := models .NewSpanningTreeInterfacePolicy (fmt .Sprintf ("infra/ifPol-%s" , name ), "uni" , desc , nameAlias , stpIfPolAttr )
168- if update == true {
169- stpIfPol .Status = "modified"
174+ if Name , ok := d .GetOk ("name" ); ok {
175+ stpIfPolAttr .Name = Name .(string )
170176 }
177+ stpIfPol := models .NewSpanningTreeInterfacePolicy (fmt .Sprintf ("infra/ifPol-%s" , name ), "uni" , desc , nameAlias , stpIfPolAttr )
178+ stpIfPol .Status = "modified"
171179 err := aciClient .Save (stpIfPol )
172180 if err != nil {
173181 return err
174182 }
175183 d .Partial (true )
176184 d .SetPartial ("name" )
177185 d .Partial (false )
186+
178187 d .SetId (stpIfPol .DistinguishedName )
179- log .Printf ("[DEBUG] %s: %s finished successfully" , d .Id (), action )
188+ log .Printf ("[DEBUG] %s: Update finished successfully" , d .Id ())
180189 return resourceAciSpanningTreeInterfacePolicyRead (d , m )
181190}
191+
192+ func resourceAciSpanningTreeInterfacePolicyRead (d * schema.ResourceData , m interface {}) error {
193+ log .Printf ("[DEBUG] %s: Beginning Read" , d .Id ())
194+ aciClient := m .(* client.Client )
195+ dn := d .Id ()
196+ stpIfPol , err := getRemoteSpanningTreeInterfacePolicy (aciClient , dn )
197+ if err != nil {
198+ d .SetId ("" )
199+ return err
200+ }
201+ setSpanningTreeInterfacePolicyAttributes (stpIfPol , d )
202+
203+ log .Printf ("[DEBUG] %s: Read finished successfully" , d .Id ())
204+ return nil
205+ }
206+
207+ func resourceAciSpanningTreeInterfacePolicyDelete (d * schema.ResourceData , m interface {}) error {
208+ log .Printf ("[DEBUG] %s: Beginning Destroy" , d .Id ())
209+ aciClient := m .(* client.Client )
210+ dn := d .Id ()
211+ err := aciClient .DeleteByDn (dn , "stpIfPol" )
212+ if err != nil {
213+ return err
214+ }
215+ log .Printf ("[DEBUG] %s: Destroy finished successfully" , d .Id ())
216+ d .SetId ("" )
217+ return err
218+ }
0 commit comments