Skip to content

Commit

Permalink
Merge pull request #101 from kadadhic/main
Browse files Browse the repository at this point in the history
solved bug in policy-assignment
  • Loading branch information
jeroenwittock committed May 30, 2023
2 parents eac57a2 + 8745a2d commit 09a09b8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
2 changes: 0 additions & 2 deletions docs/resources/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ description: |-
]
access_policy {
id = data.fmc_access_policies.access_policy.id
type = data.fmc_access_policies.access_policy.type
}
}
Expand All @@ -42,7 +41,6 @@ resource "fmc_devices" "device1" {
]
access_policy {
id = data.fmc_access_policies.access_policy.id
type = data.fmc_access_policies.access_policy.type
}
}
```
Expand Down
1 change: 0 additions & 1 deletion docs/resources/policy_devices_assignments.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ resource "fmc_policy_devices_assignments" "policy_assignment" {

### Read-Only

- `description` (String) The description of this resource
- `id` (String) The ID of this resource.
- `name` (String) The name of this resource
- `type` (String) The type of this resource
Expand Down
1 change: 0 additions & 1 deletion fmc/fmc_policy_devices_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type PolicyDevicesAssignmentSubConfig struct {

type PolicyDevicesAssignment struct {
Name string `json:"name,omitempty"`
Description string `json:"description"`
ID string `json:"id,omitempty"`
Type string `json:"type"`
Targets []PolicyDevicesAssignmentSubConfig `json:"targets"`
Expand Down
58 changes: 35 additions & 23 deletions fmc/resource_fmc_policy_devices_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ func resourceFmcPolicyDevicesAssignments() *schema.Resource {
Computed: true,
Description: "The name of this resource",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of this resource",
},
"policy": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -123,7 +118,6 @@ func resourceFmcPolicyDevicesAssignmentsCreate(ctx context.Context, d *schema.Re

res, err := c.CreateFmcPolicyDevicesAssignment(ctx, &PolicyDevicesAssignment{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Policy: policy,
Targets: devices,
Type: policy_devices_assignments_type,
Expand Down Expand Up @@ -170,15 +164,6 @@ func resourceFmcPolicyDevicesAssignmentsRead(ctx context.Context, d *schema.Reso
return diags
}

if err := d.Set("description", item.Description); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "unable to read policy devices assignment",
Detail: err.Error(),
})
return diags
}

policy := make([]interface{}, 0, 1)
policyObj := make(map[string]interface{})
policyObj["id"] = item.Policy.ID
Expand Down Expand Up @@ -217,7 +202,7 @@ func resourceFmcPolicyDevicesAssignmentsUpdate(ctx context.Context, d *schema.Re
c := m.(*Client)
var diags diag.Diagnostics
id := d.Id()
if d.HasChanges("name", "description", "policy", "target_devices") {
if d.HasChanges("name", "policy", "target_devices") {
var policy PolicyDevicesAssignmentSubConfig

if inputObjs, ok := d.GetOk("policy"); ok {
Expand All @@ -242,9 +227,7 @@ func resourceFmcPolicyDevicesAssignmentsUpdate(ctx context.Context, d *schema.Re

_, err := c.UpdateFmcPolicyDevicesAssignment(ctx, id, &PolicyDevicesAssignment{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Policy: policy,
Targets: devices,
Type: policy_devices_assignments_type,
})
if err != nil {
Expand All @@ -263,13 +246,42 @@ func resourceFmcPolicyDevicesAssignmentsDelete(ctx context.Context, d *schema.Re

// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
c := m.(*Client)
var policy PolicyDevicesAssignmentSubConfig
id := d.Id()
if inputObjs, ok := d.GetOk("policy"); ok {
obj := inputObjs.([]interface{})[0].(map[string]interface{})
policy = PolicyDevicesAssignmentSubConfig{
ID: obj["id"].(string),
Type: obj["type"].(string),
}
}

diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Devices assignment cannot be deleted, it will be deleted on reassignment",
Detail: "This resource cannot be deleted",
})
var devices []PolicyDevicesAssignmentSubConfig

if inputObjs, ok := d.GetOk("target_devices"); ok {
for _, obj := range inputObjs.([]interface{}) {
obji := obj.(map[string]interface{})
devices = append(devices, PolicyDevicesAssignmentSubConfig{
ID: obji["id"].(string),
Type: obji["type"].(string),
})
}
}

_, err := c.UpdateFmcPolicyDevicesAssignment(ctx, id, &PolicyDevicesAssignment{
Name: d.Get("name").(string),
Policy: policy,
Type: policy_devices_assignments_type,
})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "unable to delete policy devices assignment",
Detail: err.Error(),
})
return diags
}
// d.SetId("") is automatically called assuming delete returns no errors, but
// it is added here for explicitness.
d.SetId("")
Expand Down

0 comments on commit 09a09b8

Please sign in to comment.