diff --git a/aci/data_source_aci_infraportblk.go b/aci/data_source_aci_infraportblk.go index 84bece7b8..454422d51 100644 --- a/aci/data_source_aci_infraportblk.go +++ b/aci/data_source_aci_infraportblk.go @@ -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" @@ -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, + }, }), } } @@ -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 } diff --git a/aci/resource_aci_infraportblk.go b/aci/resource_aci_infraportblk.go index 5e1230e49..0b845c0af 100644 --- a/aci/resource_aci_infraportblk.go +++ b/aci/resource_aci_infraportblk.go @@ -70,8 +70,7 @@ func resourceAciAccessPortBlock() *schema.Resource { }, "relation_infra_rs_acc_bndl_subgrp": &schema.Schema{ - Type: schema.TypeString, - + Type: schema.TypeString, Optional: true, }, }), @@ -117,6 +116,18 @@ 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) @@ -124,23 +135,21 @@ func resourceAciAccessPortBlockImport(d *schema.ResourceData, m interface{}) ([] 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 @@ -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) } @@ -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) } @@ -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()) diff --git a/examples/leaf_profile/port_block.tf b/examples/leaf_profile/port_block.tf index 61a2748c0..65583c869 100644 --- a/examples/leaf_profile/port_block.tf +++ b/examples/leaf_profile/port_block.tf @@ -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 } \ No newline at end of file diff --git a/website/docs/d/access_port_block.html.markdown b/website/docs/d/access_port_block.html.markdown index abe267534..89c10f4af 100644 --- a/website/docs/d/access_port_block.html.markdown +++ b/website/docs/d/access_port_block.html.markdown @@ -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 @@ -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. diff --git a/website/docs/r/access_port_block.html.markdown b/website/docs/r/access_port_block.html.markdown index c551a440c..2cc066a9e 100644 --- a/website/docs/r/access_port_block.html.markdown +++ b/website/docs/r/access_port_block.html.markdown @@ -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".