Skip to content

Commit 93d2e3e

Browse files
akabhaskmdmohan
authored andcommitted
Fix for removing fabric name in policy and other additional fixes in vpc pair, network and fabric resources.
1 parent e5e028f commit 93d2e3e

File tree

13 files changed

+70
-84
lines changed

13 files changed

+70
-84
lines changed

docs/resources/policy.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Resource to configure policies on a switch
1414

1515
```terraform
1616
resource "ndfc_policy" "test_resource_policy_1" {
17-
fabric_name = "CML"
1817
is_policy_group = false
1918
deploy = true
2019
entity_name = "Switch"
@@ -42,7 +41,6 @@ resource "ndfc_policy" "test_resource_policy_1" {
4241
- `device_serial_number` (String) Serial number of the device
4342
- `entity_name` (String) Policy Name
4443
- `entity_type` (String) Type of the entity
45-
- `fabric_name` (String) Name of the fabric
4644
- `template_name` (String) Name of the template
4745

4846
### Optional

examples/resources/ndfc_policy/resource.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
resource "ndfc_policy" "test_resource_policy_1" {
3-
fabric_name = "CML"
43
is_policy_group = false
54
deploy = true
65
entity_name = "Switch"

generator/defs/policy.yaml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ resource:
66
description: "Resource to configure policies on a switch"
77
import_id:
88
- "POLICY-26910"
9-
import_desc:
9+
import_desc:
1010
- "Policy-ID"
1111
attributes:
1212
- model_name: id
@@ -21,14 +21,6 @@ resource:
2121
description: "Policy ID"
2222
computed: true
2323
use_state: true
24-
- model_name: fabricName
25-
tf_name: fabric_name
26-
type: String
27-
description: "Name of the fabric"
28-
mandatory: true
29-
payload_hide: true
30-
tf_requires_replace: true
31-
example: "CML"
3224
- model_name: policyGroup
3325
tf_name: is_policy_group
3426
type: Bool

internal/provider/ndfc/api/vpc_pair_api.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,22 @@ const urlVpcPair = "/lan-fabric/rest/vpcpair"
1919
const urlVpcPairGet = urlVpcPair + "?serialNumber=%s"
2020
const urlVpcPairRecmd = urlVpcPair + "/recommendation?serialNumber=%s&useVirtualPeerlink=%t"
2121

22-
2322
type VpcPairAPI struct {
2423
NDFCAPICommon
25-
mutex *sync.Mutex
26-
CheckStatus map[string]bool
27-
FabricName string
28-
VirtualPeerLink bool
29-
VpcPairID string
24+
mutex *sync.Mutex
25+
GetRecommendations bool
26+
FabricName string
27+
VirtualPeerLink bool
28+
VpcPairID string
3029
}
3130

3231
func (c *VpcPairAPI) GetLock() *sync.Mutex {
3332
return c.mutex
3433
}
3534

3635
func (c *VpcPairAPI) GetUrl() string {
37-
if c.CheckStatus["recommendations"] {
36+
if c.GetRecommendations {
3837
return fmt.Sprintf(urlVpcPairRecmd, c.VpcPairID, c.VirtualPeerLink)
39-
} else if c.CheckStatus["switchesByFabric"] {
40-
return fmt.Sprintf(UrlSwitchesByFabric, c.FabricName)
4138
} else {
4239
return fmt.Sprintf(urlVpcPairGet, c.VpcPairID)
4340
}

internal/provider/ndfc/fabric_common.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *NDFC) RscReadFabric(ctx context.Context, dg *diag.Diagnostics, tf resou
4747
payload, err := fabricApi.Get()
4848
if len(payload) == 0 {
4949
if err == nil {
50-
err = fmt.Errorf("payload empty from NDFC")
50+
err = fmt.Errorf("fabric not found in NDFC")
5151
}
5252
tflog.Error(ctx, "RscReadFabric: Failed to get fabric")
5353
dg.AddError("Failed to get fabric", fmt.Sprintf("Error: %q", err.Error()))
@@ -97,19 +97,19 @@ func (c *NDFC) RscCreateFabric(ctx context.Context, dg *diag.Diagnostics, tf res
9797
func (c *NDFC) RscUpdateFabric(ctx context.Context, dg *diag.Diagnostics, tf resource_fabric_common.FabricModel, fabricType string) {
9898
tflog.Info(ctx, "Update Fabric")
9999
ndfcFabricModel := tf.GetModelData()
100+
fabricName := ndfcFabricModel.FabricName
101+
deploy := ndfcFabricModel.Deploy
100102
fabricApi, payload := c.RscGetFabricApiDetails(ctx, dg, ndfcFabricModel, fabricType)
101103
if dg.HasError() {
102104
return
103105
}
104-
fabricApi.FabricName = ndfcFabricModel.FabricName
106+
fabricApi.FabricName = fabricName
105107
res, err := fabricApi.Put(payload)
106108
if err != nil {
107109
tflog.Error(ctx, "RscUpdateFabric: PUT failed with payload %s", map[string]interface{}{"Payload": payload})
108110
dg.AddError("Failed to update fabric", fmt.Sprintf("Error: %q %q", err.Error(), res.String()))
109111
return
110112
}
111-
fabricName := ndfcFabricModel.FabricName
112-
deploy := ndfcFabricModel.Deploy
113113
c.RscDeployFabric(ctx, dg, fabricName, deploy)
114114
if dg.HasError() {
115115
return
@@ -175,10 +175,7 @@ func (c NDFC) RscGetFabricApiDetails(ctx context.Context, dg *diag.Diagnostics,
175175
}
176176
func (c *NDFC) RscDeployFabric(ctx context.Context, dg *diag.Diagnostics, fabricName string, deploy bool) {
177177
if deploy {
178-
fabricApi := api.NewFabricAPI(c.GetLock(ResourceFabrics), &c.apiClient)
179-
fabricApi.FabricName = fabricName
180-
fabricApi.GetSwitchesInFabric = true
181-
payload, err := fabricApi.Get()
178+
payload, err := c.GetSwitchesInFabric(ctx, fabricName)
182179
tflog.Debug(ctx, fmt.Sprintf("RscDeployFabric: payload %s", string(payload)))
183180
if len(payload) == 0 || string(payload) == "[]" {
184181
if err == nil {
@@ -195,7 +192,29 @@ func (c *NDFC) RscDeployFabric(ctx context.Context, dg *diag.Diagnostics, fabric
195192
c.DeployConfiguration(ctx, dg, fabricName, nil)
196193
}
197194
}
198-
func (c NDFC) DSGetFabricBulk(ctx context.Context, dg *diag.Diagnostics) *datasource_fabric.FabricModel {
195+
func (c *NDFC) GetSwitchesInFabric(ctx context.Context, fabricName string) ([]byte, error) {
196+
fabricApi := api.NewFabricAPI(c.GetLock(ResourceFabrics), &c.apiClient)
197+
fabricApi.FabricName = fabricName
198+
fabricApi.GetSwitchesInFabric = true
199+
return fabricApi.Get()
200+
}
201+
func (c *NDFC) GetFabricName(ctx context.Context, serialNumber string) (string) {
202+
fabricApi := api.NewFabricAPI(c.GetLock(ResourceFabrics), &c.apiClient)
203+
fabricApi.Serialnumber = serialNumber
204+
payload, err := fabricApi.Get()
205+
if err != nil {
206+
tflog.Error(ctx, fmt.Sprintf("GetFabricName: Failed to get fabric name for serial number %s", serialNumber))
207+
return ""
208+
}
209+
var FabricNamePayload resource_fabric_common.NdfcFabricNamePayload
210+
err = json.Unmarshal(payload, &FabricNamePayload)
211+
if err != nil {
212+
return ""
213+
}
214+
return FabricNamePayload.FabricName
215+
216+
}
217+
func (c *NDFC) DSGetFabricBulk(ctx context.Context, dg *diag.Diagnostics) *datasource_fabric.FabricModel {
199218
tflog.Debug(ctx, "DSGetFabricBulk entry")
200219
fabricApi := api.NewFabricAPI(c.GetLock(ResourceFabrics), &c.apiClient)
201220
res, err := fabricApi.Get()
@@ -227,4 +246,4 @@ func (c NDFC) DSGetFabricBulk(ctx context.Context, dg *diag.Diagnostics) *dataso
227246
tflog.Debug(ctx, "datasource_vrf_bulk: SetModelData OK")
228247
}
229248
return data
230-
}
249+
}

internal/provider/ndfc/ndfc_global_deploy.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ func (c NDFC) SaveConfiguration(ctx context.Context, diags *diag.Diagnostics, fa
3333
_, err := saveApi.Post([]byte{})
3434
if err != nil {
3535
diags.AddError("Config Save failed", err.Error())
36-
//saveApi.Preview = false
37-
//time.Sleep(3 * time.Second)
38-
//res, _ := saveApi.Get()
39-
// TODO determine which error should be returned
40-
///diags.AddError("Deploy Errors:", string(res))
36+
return
4137
}
4238
}
4339

internal/provider/ndfc/net_attach_private.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,9 @@ func processPortListOrder(portList *types.CSVString, order []string) {
467467
}
468468
}
469469

470-
func (c NDFC) createVpcPairMap(ctx context.Context, FabricName string) map[string]string {
470+
func (c NDFC) createVpcPairMap(ctx context.Context, fabricName string) map[string]string {
471471

472-
api := api.NewVpcPairAPI(c.GetLock(ResourceVpcPair), &c.apiClient)
473-
api.CheckStatus = make(map[string]bool)
474-
api.CheckStatus["switchesByFabric"] = true
475-
api.FabricName = FabricName
476-
payload, err := api.Get()
472+
payload, err := c.GetSwitchesInFabric(ctx, fabricName)
477473
if err != nil || string(payload) == "[]" || payload == nil {
478474
tflog.Debug(ctx, "createVpcPairMap: Failed to get switchesByFabric")
479475
return nil

internal/provider/ndfc/policy.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func (c *NDFC) RscCreatePolicy(ctx context.Context, dg *diag.Diagnostics, model
7272
}
7373
model.SetModelData(newModel)
7474
model.Deploy = types.BoolValue(inData.Deploy)
75-
model.FabricName = types.StringValue(inData.FabricName)
7675
}
7776

7877
func getPolicyIdFromResponse(res *gjson.Result) string {
@@ -81,7 +80,6 @@ func getPolicyIdFromResponse(res *gjson.Result) string {
8180

8281
func (c *NDFC) RscReadPolicy(ctx context.Context, dg *diag.Diagnostics, model *resource_policy.PolicyModel) {
8382

84-
FabricName := model.FabricName.ValueString()
8583
Deploy := model.Deploy.ValueBool()
8684
if model.IsPolicyGroup.ValueBool() {
8785
tflog.Error(ctx, "Policy group is not supported")
@@ -97,7 +95,6 @@ func (c *NDFC) RscReadPolicy(ctx context.Context, dg *diag.Diagnostics, model *r
9795
return
9896
}
9997
data.Deploy = Deploy
100-
data.FabricName = FabricName
10198
model.SetModelData(data)
10299

103100
}
@@ -174,14 +171,14 @@ func (c *NDFC) RscUpdatePolicy(ctx context.Context, dg *diag.Diagnostics, model
174171

175172
model.SetModelData(newModel)
176173
model.Deploy = types.BoolValue(policyData.Deploy)
177-
model.FabricName = types.StringValue(policyData.FabricName)
178174
}
179175

180176
func (c *NDFC) RscDeletePolicy(ctx context.Context, dg *diag.Diagnostics, model *resource_policy.PolicyModel) {
181177

182178
policyID := model.PolicyId.ValueString()
183-
FabricName := model.FabricName.ValueString()
184-
tflog.Debug(ctx, fmt.Sprintf("Before Deploying configuration for Fabric: %s", FabricName))
179+
serialNumber := model.GetModelData().DeviceSerialNumber
180+
fabricName := c.GetFabricName(ctx, serialNumber)
181+
tflog.Debug(ctx, fmt.Sprintf("Before Deploying configuration for Fabric: %s", fabricName))
185182
tflog.Debug(ctx, fmt.Sprintf("RscDeletePolicy: Deleting policy ID %s", policyID))
186183

187184
if model.IsPolicyGroup.ValueBool() {
@@ -213,7 +210,7 @@ func (c *NDFC) RscDeletePolicy(ctx context.Context, dg *diag.Diagnostics, model
213210
}
214211
// Incase of delete, policyID is made as FabricName:SerialNumber for switch deploy
215212
// This avoids additional parameters to be passed to delete and also policyID is not used in delete
216-
policyID = FabricName + ":" + model.GetModelData().DeviceSerialNumber
213+
policyID = fabricName + ":" + serialNumber
217214
c.RscDeployPolicy(ctx, dg, policyID)
218215
if dg.HasError() {
219216
tflog.Error(ctx, "Failed to switch deploy")
@@ -250,7 +247,6 @@ func (c *NDFC) RscDeployPolicy(ctx context.Context, dg *diag.Diagnostics, policy
250247
FabricName := parts[0]
251248
serialNumber := parts[1]
252249
switchSerialNumber := []string{serialNumber}
253-
254250
tflog.Debug(ctx, fmt.Sprintf("Deploying configuration for Fabric: %s, Serial Numbers: %s", FabricName, serialNumber))
255251
c.DeployConfiguration(ctx, dg, FabricName, switchSerialNumber)
256252
return

internal/provider/ndfc/vpc_pair.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (c *NDFC) RscUpdateVpcPair(ctx context.Context, dg *diag.Diagnostics, tf *r
148148
// Convert NDFC model to the json data
149149
payload, err := json.Marshal(nfdcVpcPairModel)
150150
if err != nil {
151-
tflog.Error(ctx, "RscCreateOrUpdate: RscCreateOrUpdate: Failed to marshal vPC Pair data")
151+
tflog.Error(ctx, "RscUpdateVpcPair: Failed to marshal vPC Pair data")
152152
dg.AddError("Failed to marshal vPC Pair data", fmt.Sprintf("Error %v", err))
153153
return
154154
}
@@ -157,14 +157,15 @@ func (c *NDFC) RscUpdateVpcPair(ctx context.Context, dg *diag.Diagnostics, tf *r
157157
api := api.NewVpcPairAPI(c.GetLock(ResourceVpcPair), &c.apiClient)
158158
res, err := api.Put(payload)
159159
if err != nil {
160-
tflog.Error(ctx, "RscCreateOrUpdate: Failed to update vPC Pair")
160+
tflog.Error(ctx, "RscUpdateVpcPair: Failed to update vPC Pair")
161161
dg.AddError("Failed to update vPC Pair", fmt.Sprintf("Error %v: %v", err, res.String()))
162162
return
163163
}
164164
// Check if the vPC Pair is present after update
165165
nfdcVpcPairModel = c.rscGetVpcPair(ctx, tf)
166166
if nfdcVpcPairModel == nil {
167-
dg.AddError("Failed to create vPC Pair", "vPC Pair data empty in NDFC")
167+
tflog.Error(ctx, "RscUpdateVpcPair: Failed to get vPC Pair after update")
168+
dg.AddError("Failed to update vPC Pair", "vPC Pair data empty in NDFC")
168169
return
169170
}
170171
fabricName := nfdcVpcPairModel.PeerOneSwitchDetails.FabricName
@@ -241,23 +242,22 @@ func (c *NDFC) rscCheckVpcPairRecommendations(ctx context.Context,
241242
recmdList := []resource_vpc_pair.NDFCVpcPairRecommendations{}
242243

243244
api.VpcPairID = nfdcVpcPairModel.SerialNumbers[0]
244-
api.CheckStatus = make(map[string]bool)
245-
api.CheckStatus["recommendations"] = true
245+
api.GetRecommendations = true
246246
api.VirtualPeerLink = *nfdcVpcPairModel.UseVirtualPeerlink
247-
248247
payload, err := api.Get()
249-
api.CheckStatus["recommendations"] = false
250248
if err != nil {
251249
tflog.Error(ctx, "RscVpcPairRecommendations: Failed to get recommendations")
252250
err = fmt.Errorf("failed to get recommendations")
253251
return err
254252
}
255-
253+
tflog.Debug(ctx, fmt.Sprintf("Recommendations payload: %s", payload))
254+
api.GetRecommendations = false
256255
err = json.Unmarshal(payload, &recmdList)
257256
if err != nil {
258257
tflog.Error(ctx, "RscVpcPairRecommendations: Failed to unmarshal recommendations data")
259258
return err
260259
}
260+
tflog.Debug(ctx, fmt.Sprintf("Recommendations: %v", recmdList))
261261
for _, rec := range recmdList {
262262
log.Printf("SerialNumber %v, Recmd %v, RecReason %v", rec.SerialNumber, rec.Recommended, rec.RecommendationReason)
263263
if rec.SerialNumber == nfdcVpcPairModel.SerialNumbers[1] {
@@ -266,6 +266,7 @@ func (c *NDFC) rscCheckVpcPairRecommendations(ctx context.Context,
266266
return nil
267267
} else {
268268
if rec.RecommendationReason == "Switches are not connected" {
269+
tflog.Debug(ctx, "NDFC Recommendation not met, but it is a transient error")
269270
// This is a transient error, it will be really known when config apply is done.
270271
return nil
271272
} else {
@@ -290,6 +291,7 @@ func (c *NDFC) RscImportVpcPairs(ctx context.Context,
290291
return
291292
}
292293
tf.SerialNumbers, err = types.SetValue(types.StringType, []attr.Value{types.StringValue(idSplit[0]), types.StringValue(idSplit[1])})
294+
tflog.Debug(ctx, fmt.Sprintf("SerialNumbers %v", tf.SerialNumbers))
293295
if err != nil {
294296
dg.AddError("Import state failed", "Failed to set serial numbers")
295297
return
@@ -299,6 +301,7 @@ func (c *NDFC) RscImportVpcPairs(ctx context.Context,
299301
dg.AddError("Unable to get vpcPair", "vPC Pair data empty in NDFC")
300302
return
301303
}
304+
tflog.Debug(ctx, fmt.Sprintf("vPC Pair model: %v", vpcPairModel))
302305
rscCreateModelId(tf, vpcPairModel)
303306
}
304307

@@ -310,6 +313,8 @@ func (c *NDFC) RscDeployVpcPair(ctx context.Context, dg *diag.Diagnostics, tf *r
310313

311314
ndfcVpcPairModel := tf.GetModelData()
312315
serialNumbers := ndfcVpcPairModel.SerialNumbers
316+
tflog.Debug(ctx, fmt.Sprintf("RscDeployVpcPair: Deploying vPC Pair with serial numbers: %v", serialNumbers))
317+
tflog.Debug(ctx, "Performing config save and deploy for vPC Pair")
313318
c.SaveConfiguration(ctx, dg, fabricName)
314319
if dg.HasError() {
315320
return

internal/provider/resources/resource_fabric_common/resource_custom_codec.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@ type FabricModel interface {
1818
GetModelData() *NDFCFabricCommonModel
1919
SetModelData(*NDFCFabricCommonModel) diag.Diagnostics
2020
}
21-
2221
type NdfcFabricPayload struct {
2322
FabricName string `json:"fabricName,omitempty"`
2423
FabricType string `json:"templateName,omitempty"`
2524
NdfcFabricNvPairs NDFCFabricCommonModel `json:"nvPairs,omitempty"`
2625
}
26+
type NdfcFabricNamePayload struct {
27+
FabricName string `json:"fabricName,omitempty"`
28+
}
29+
type CustomNdfcFabricNamePayload NdfcFabricNamePayload
2730
type CustomNdfcFabricPayload NdfcFabricPayload
31+
32+
func (m *NdfcFabricNamePayload) UnmarshalJSON(data []byte) error {
33+
var customModel CustomNdfcFabricNamePayload
34+
err := json.Unmarshal(data, &customModel)
35+
if err != nil {
36+
return err
37+
}
38+
m.FabricName = customModel.FabricName
39+
return nil
40+
}
2841
func (m *NdfcFabricPayload) UnmarshalJSON(data []byte) error {
2942
var customModel CustomNdfcFabricPayload
3043
err := json.Unmarshal(data, &customModel)

0 commit comments

Comments
 (0)