Skip to content

Commit

Permalink
[bugfix] Fixed relation_infra_rs_acc_bndl_subgrp attribute in aci_acc…
Browse files Browse the repository at this point in the history
…ess_port_block to gather target dn instead of name
  • Loading branch information
shrsr authored Jun 23, 2023
1 parent 7003993 commit c8f3ea2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 57 deletions.
16 changes: 15 additions & 1 deletion aci/data_source_aci_infraportblk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aci
import (
"context"
"fmt"
"log"

"github.com/ciscoecosystem/aci-go-client/v2/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -56,6 +57,11 @@ func dataSourceAciAccessPortBlock() *schema.Resource {
Optional: true,
Computed: true,
},

"relation_infra_rs_acc_bndl_subgrp": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
}),
}
}
Expand All @@ -71,14 +77,22 @@ func dataSourceAciAccessPortBlockRead(ctx context.Context, d *schema.ResourceDat
dn := fmt.Sprintf("%s/%s", AccessPortSelectorDn, rn)

infraPortBlk, err := getRemoteAccessPortBlock(aciClient, dn)

if err != nil {
return diag.FromErr(err)
}

d.SetId(dn)

_, err = setAccessPortBlockAttributes(infraPortBlk, d)
if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] %s: infraRsAccBndlSubgrp - Beginning Read with parent DN", dn)
_, err = getAndSetReadRelationinfraRsAccBndlSubgrp(aciClient, dn, d)
if err == nil {
log.Printf("[DEBUG] %s: infraRsAccBndlSubgrp - Read finished successfully", d.Get("relation_infra_rs_acc_bndl_subgrp"))
}

return nil
}
46 changes: 25 additions & 21 deletions aci/resource_aci_infraportblk.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func resourceAciAccessPortBlock() *schema.Resource {
},

"relation_infra_rs_acc_bndl_subgrp": &schema.Schema{
Type: schema.TypeString,

Type: schema.TypeString,
Optional: true,
},
}),
Expand Down Expand Up @@ -117,30 +116,40 @@ func setAccessPortBlockAttributes(infraPortBlk *models.AccessPortBlock, d *schem
return d, nil
}

func getAndSetReadRelationinfraRsAccBndlSubgrp(client *client.Client, dn string, d *schema.ResourceData) (*schema.ResourceData, error) {
infraRsAccBndlSubgrp, err := client.ReadRelationinfraRsAccBndlSubgrpFromAccessPortBlock(dn)
if err != nil {
log.Printf("[DEBUG] Error while reading relation infraRsAccBndlSubgrp %v", err)
d.Set("relation_infra_rs_acc_bndl_subgrp", nil)
return d, err
} else {
d.Set("relation_infra_rs_acc_bndl_subgrp", infraRsAccBndlSubgrp.(string))
}
return d, nil
}

func resourceAciAccessPortBlockImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
aciClient := m.(*client.Client)

dn := d.Id()

infraPortBlk, err := getRemoteAccessPortBlock(aciClient, dn)

if err != nil {
return nil, err
}
infraPortBlkMap, err := infraPortBlk.ToMap()
if err != nil {
return nil, err
}

name := infraPortBlkMap["name"]
pDN := GetParentDn(dn, fmt.Sprintf("/portblk-%s", name))
d.Set("access_port_selector_dn", pDN)
schemaFilled, err := setAccessPortBlockAttributes(infraPortBlk, d)
if err != nil {
return nil, err
}

log.Printf("[DEBUG] %s: infraRsAccBndlSubgrp - Beginning Import with parent DN", dn)
_, err = getAndSetReadRelationinfraRsAccBndlSubgrp(aciClient, dn, d)
if err == nil {
log.Printf("[DEBUG] %s: infraRsAccBndlSubgrp - Import finished successfully", d.Get("relation_infra_rs_acc_bndl_subgrp"))
}

log.Printf("[DEBUG] %s: Import finished successfully", d.Id())

return []*schema.ResourceData{schemaFilled}, nil
Expand Down Expand Up @@ -237,8 +246,7 @@ func resourceAciAccessPortBlockCreate(ctx context.Context, d *schema.ResourceDat

if relationToinfraRsAccBndlSubgrp, ok := d.GetOk("relation_infra_rs_acc_bndl_subgrp"); ok {
relationParam := relationToinfraRsAccBndlSubgrp.(string)
relationParamName := GetMOName(relationParam)
err = aciClient.CreateRelationinfraRsAccBndlSubgrpFromAccessPortBlock(infraPortBlk.DistinguishedName, relationParamName)
err = aciClient.CreateRelationinfraRsAccBndlSubgrpFromAccessPortBlock(infraPortBlk.DistinguishedName, relationParam)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -308,12 +316,11 @@ func resourceAciAccessPortBlockUpdate(ctx context.Context, d *schema.ResourceDat

if d.HasChange("relation_infra_rs_acc_bndl_subgrp") {
_, newRelParam := d.GetChange("relation_infra_rs_acc_bndl_subgrp")
newRelParamName := GetMOName(newRelParam.(string))
err = aciClient.DeleteRelationinfraRsAccBndlSubgrpFromAccessPortBlock(infraPortBlk.DistinguishedName)
if err != nil {
return diag.FromErr(err)
}
err = aciClient.CreateRelationinfraRsAccBndlSubgrpFromAccessPortBlock(infraPortBlk.DistinguishedName, newRelParamName)
err = aciClient.CreateRelationinfraRsAccBndlSubgrpFromAccessPortBlock(infraPortBlk.DistinguishedName, newRelParam.(string))
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -345,13 +352,10 @@ func resourceAciAccessPortBlockRead(ctx context.Context, d *schema.ResourceData,
return nil
}

infraRsAccBndlSubgrpData, err := aciClient.ReadRelationinfraRsAccBndlSubgrpFromAccessPortBlock(dn)
if err != nil {
log.Printf("[DEBUG] Error while reading relation infraRsAccBndlSubgrp %v", err)
setRelationAttribute(d, "relation_infra_rs_acc_bndl_subgrp", "")

} else {
setRelationAttribute(d, "relation_infra_rs_acc_bndl_subgrp", infraRsAccBndlSubgrpData.(string))
log.Printf("[DEBUG] %s: infraRsAccBndlSubgrp - Beginning Read with parent DN", dn)
_, err = getAndSetReadRelationinfraRsAccBndlSubgrp(aciClient, dn, d)
if err == nil {
log.Printf("[DEBUG] %s: infraRsAccBndlSubgrp - Read finished successfully", d.Get("relation_infra_rs_acc_bndl_subgrp"))
}

log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
Expand Down
47 changes: 32 additions & 15 deletions examples/leaf_profile/port_block.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,43 @@ provider "aci" {
url = ""
insecure = true
}

resource "aci_leaf_interface_profile" "example" {
name = "demo_leaf_profile"
}

resource "aci_leaf_access_bundle_policy_group" "test_access_bundle_policy_group" {
description = "From Terraform"
name = "tf_bundle_group"
annotation = "tag_if_pol"
lag_t = "link"
name_alias = "alias_if_pol"
}

resource "aci_leaf_access_bundle_policy_sub_group" "test_access_bundle_policy_sub_group" {
leaf_access_bundle_policy_group_dn = aci_leaf_access_bundle_policy_group.test_access_bundle_policy_group.id
name = "tf_sub_group"
}

resource "aci_access_port_selector" "fooaccess_port_selector" {
leaf_interface_profile_dn = aci_leaf_interface_profile.example.id
description = "From Terraform"
name = "tf_test"
access_port_selector_type = "default"
annotation = "tag_port_selector"
name_alias = "alias_port_selector"
leaf_interface_profile_dn = aci_leaf_interface_profile.example.id
description = "From Terraform"
name = "tf_test"
access_port_selector_type = "range"
annotation = "tag_port_selector"
name_alias = "alias_port_selector"
relation_infra_rs_acc_base_grp = aci_leaf_access_bundle_policy_group.test_access_bundle_policy_group.id
}

resource "aci_access_port_block" "test_port_block" {
access_port_selector_dn = aci_access_port_selector.fooaccess_port_selector.id
name = "tf_test_block"
description = "From Terraform"
annotation = "tag_port_block"
from_card = "1"
from_port = "1"
name_alias = "alias_port_block"
to_card = "3"
to_port = "3"
access_port_selector_dn = aci_access_port_selector.fooaccess_port_selector.id
name = "tf_test_block"
description = "From Terraform"
annotation = "tag_port_block"
from_card = "1"
from_port = "1"
name_alias = "alias_port_block"
to_card = "3"
to_port = "3"
relation_infra_rs_acc_bndl_subgrp = aci_leaf_access_bundle_policy_sub_group.test_access_bundle_policy_sub_group.id
}
21 changes: 16 additions & 5 deletions website/docs/d/access_port_block.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ description: |-

Data source for ACI Access Port Block

## API Information ##

* `Class` - infraPortBlk
* `Distinguished Name` - uni/infra/accportprof-{leaf_interface_profile_name}/hports-{leaf_interface_profile_dn}-typ-{type}/portblk-{name}

## GUI Information ##

* `Location` - Fabric -> Access Policies -> Interfaces -> Profiles -> Create Leaf Interface Profile -> Interface Selectors -> Port Blocks

## Example Usage

```hcl
Expand All @@ -24,16 +33,18 @@ data "aci_access_port_block" "dev_port_blk" {

## Argument Reference

- `access_port_selector_dn` - (Required) Distinguished name of parent Access Port Selector or Spine Access Port Selector object.
- `name` - (Required) Name of Object Access Port Block.
- `access_port_selector_dn` - (Required) Distinguished name of the parent Access Port Selector or Spine Access Port Selector object.
- `name` - (Required) Name of the Access Port Block object.

## Attribute Reference

- `id` - Attribute id set to the Dn of the Access Port Block.
- `description` - (Optional) Description for object Access Port Block.
- `annotation` - (Optional) Annotation for object Access Port Block.
- `description` - (Optional) Description of the Access Port Block object.
- `annotation` - (Optional) Annotation of the Access Port Block object.
- `from_card` - (Optional) The beginning (from-range) of the card range block for the leaf Access Port Block.
- `from_port` - (Optional) The beginning (from-range) of the port range block for the leaf Access Port Block.
- `name_alias` - (Optional) Name alias for object Access Port Block.
- `name_alias` - (Optional) Name alias of the Access Port Block object.
- `to_card` - (Optional) The end (to-range) of the card range block for the leaf Access Port Block.
- `to_port` - (Optional) The end (to-range) of the port range block for the leaf Access Port Block.

- `relation_infra_rs_acc_bndl_subgrp` - (Optional) Relation to class infraAccBndlSubgrp. Cardinality - N_TO_ONE. Type - String.
40 changes: 25 additions & 15 deletions website/docs/r/access_port_block.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,41 @@ description: |-

Manages ACI Access Port Block

## API Information ##

* `Class` - infraPortBlk
* `Distinguished Name` - uni/infra/accportprof-{leaf_interface_profile_name}/hports-{leaf_interface_profile_dn}-typ-{type}/portblk-{name}

## GUI Information ##

* `Location` - Fabric -> Access Policies -> Interfaces -> Profiles -> Create Leaf Interface Profile -> Interface Selectors -> Port Blocks

## Example Usage

```hcl
resource "aci_access_port_block" "fooaccess_port_block" {
access_port_selector_dn = aci_access_port_selector.example.id
description = "from terraform"
name = "demo_port_block"
annotation = "tag_port_block"
from_card = "1"
from_port = "1"
name_alias = "alias_port_block"
to_card = "3"
to_port = "3"
resource "aci_access_port_block" "test_port_block" {
access_port_selector_dn = aci_access_port_selector.fooaccess_port_selector.id
name = "tf_test_block"
description = "From Terraform"
annotation = "tag_port_block"
from_card = "1"
from_port = "1"
name_alias = "alias_port_block"
to_card = "3"
to_port = "3"
relation_infra_rs_acc_bndl_subgrp = aci_leaf_access_bundle_policy_sub_group.test_access_bundle_policy_sub_group.id
}
```

## Argument Reference

- `access_port_selector_dn` - (Required) Distinguished name of parent Access Port Selector or Spine Access Port Selector object.
- `name` - (Optional) name of Object Access Port Block.
- `annotation` - (Optional) Annotation for object Access Port Block.
- `description` - (Optional) Description for object Access Port Block.
- `access_port_selector_dn` - (Required) Distinguished name of the parent Access Port Selector or Spine Access Port Selector object.
- `name` - (Optional) Name of the Access Port Block object.
- `annotation` - (Optional) Annotation of the Access Port Block object.
- `description` - (Optional) Description of the Access Port Block object.
- `from_card` - (Optional) The beginning (from-range) of the card range block for the leaf access port block. Allowed value range is 1-100. Default value is "1".
- `from_port` - (Optional) The beginning (from-range) of the port range block for the leaf access port block. Allowed value range is 1-127. Default value is "1".
- `name_alias` - (Optional) Name alias for object Access Port Block.
- `name_alias` - (Optional) Name alias of the Access Port Block object.
- `to_card` - (Optional) The end (to-range) of the card range block for the leaf access port block. Allowed value range is 1-100. Default value is "1".
- `to_port` - (Optional) The end (to-range) of the port range block for the leaf access port block. Allowed value range is 1-127. Default value is "1".

Expand Down

0 comments on commit c8f3ea2

Please sign in to comment.