Skip to content

Commit

Permalink
Add support for aci_snmp_community a more generic version of aci_vrf_…
Browse files Browse the repository at this point in the history
…snmp_context_community and deprecate aci_vrf_snmp_context_community
  • Loading branch information
lhercot committed Jan 27, 2022
1 parent c74d114 commit 200ccd3
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 55 deletions.
16 changes: 10 additions & 6 deletions aci/data_source_aci_snmpcommunityp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"fmt"

"github.com/ciscoecosystem/aci-go-client/client"
"github.com/ciscoecosystem/aci-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAciSNMPCommunity() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciSNMPCommunityReadContext,
ReadContext: dataSourceAciSNMPCommunityRead,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"vrf_snmp_context_dn": &schema.Schema{
"parent_dn": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
Expand All @@ -26,20 +27,23 @@ func dataSourceAciSNMPCommunity() *schema.Resource {
}
}

func dataSourceAciSNMPCommunityReadContext(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func dataSourceAciSNMPCommunityRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
name := d.Get("name").(string)
VRFSNMPCtxDn := d.Get("vrf_snmp_context_dn").(string)
rn := fmt.Sprintf("community-%s", name)
dn := fmt.Sprintf("%s/%s", VRFSNMPCtxDn, rn)
parentDn := d.Get("parent_dn").(string)
dn := fmt.Sprintf(models.DnsnmpCommunityP, parentDn, name)

snmpCommunityP, err := getRemoteSNMPCommunity(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}

d.SetId(dn)

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

return nil
}
50 changes: 50 additions & 0 deletions aci/data_source_aci_snmpcommunityp_deprecated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package aci

import (
"context"
"fmt"

"github.com/ciscoecosystem/aci-go-client/client"
"github.com/ciscoecosystem/aci-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAciSNMPCommunityDeprecated() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "Use aci_snmp_community data source instead",
ReadContext: dataSourceAciSNMPCommunityReadDeprecated,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"vrf_snmp_context_dn": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
})),
}
}

func dataSourceAciSNMPCommunityReadDeprecated(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
name := d.Get("name").(string)
parentDn := d.Get("vrf_snmp_context_dn").(string)
dn := fmt.Sprintf(models.DnsnmpCommunityP, parentDn, name)

snmpCommunityP, err := getRemoteSNMPCommunity(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}

d.SetId(dn)

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

return nil
}
6 changes: 4 additions & 2 deletions aci/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ func Provider() *schema.Provider {
"aci_spine_switch_policy_group": resourceAciSpineSwitchPolicyGroup(),
"aci_recurring_window": resourceAciRecurringWindow(),
"aci_file_remote_path": resourceAciRemotePathofaFile(),
"aci_vrf_snmp_context_community": resourceAciSNMPCommunity(),
"aci_vrf_snmp_context_community": resourceAciSNMPCommunityDeprecated(),
"aci_snmp_community": resourceAciSNMPCommunity(),
"aci_mgmt_zone": resourceAciManagedNodesZone(),
"aci_vrf_snmp_context": resourceAciSNMPContextProfile(),
"aci_endpoint_ip_aging_profile": resourceAciIPAgingPolicy(),
Expand Down Expand Up @@ -460,7 +461,8 @@ func Provider() *schema.Provider {
"aci_spine_switch_policy_group": dataSourceAciSpineSwitchPolicyGroup(),
"aci_recurring_window": dataSourceAciRecurringWindow(),
"aci_file_remote_path": dataSourceAciRemotePathofaFile(),
"aci_vrf_snmp_context_community": dataSourceAciSNMPCommunity(),
"aci_vrf_snmp_context_community": dataSourceAciSNMPCommunityDeprecated(),
"aci_snmp_community": dataSourceAciSNMPCommunity(),
"aci_mgmt_zone": dataSourceAciManagedNodesZone(),
"aci_vrf_snmp_context": dataSourceAciSNMPContextProfile(),
"aci_endpoint_ip_aging_profile": dataSourceAciIPAgingPolicy(),
Expand Down
3 changes: 1 addition & 2 deletions aci/resource_aci_fvctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,7 @@ func resourceAciVRFRead(ctx context.Context, d *schema.ResourceData, m interface
fvRsCtxMcastToData, err := aciClient.ReadRelationfvRsCtxMcastToFromVRF(dn)
if err != nil {
log.Printf("[DEBUG] Error while reading relation fvRsCtxMcastTo %v", err)
d.Set("relation_fv_rs_ctx_mcast_to", fvRsCtxMcastToData)

setRelationAttribute(d, "relation_fv_rs_ctx_mcast_to", make([]interface{}, 0, 1))
} else {
setRelationAttribute(d, "relation_fv_rs_ctx_mcast_to", toStringList(fvRsCtxMcastToData.(*schema.Set).List()))
}
Expand Down
1 change: 0 additions & 1 deletion aci/resource_aci_fvtenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ func resourceAciTenantRead(ctx context.Context, d *schema.ResourceData, m interf
if err != nil {
log.Printf("[DEBUG] Error while reading relation fvRsTnDenyRule %v", err)
setRelationAttribute(d, "relation_fv_rs_tn_deny_rule", make([]interface{}, 0, 1))

} else {
setRelationAttribute(d, "relation_fv_rs_tn_deny_rule", toStringList(fvRsTnDenyRuleData.(*schema.Set).List()))
}
Expand Down
38 changes: 30 additions & 8 deletions aci/resource_aci_snmpcommunityp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func resourceAciSNMPCommunity() *schema.Resource {

SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"vrf_snmp_context_dn": &schema.Schema{
"parent_dn": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Expand All @@ -44,24 +44,32 @@ func getRemoteSNMPCommunity(client *client.Client, dn string) (*models.SNMPCommu
if err != nil {
return nil, err
}

snmpCommunityP := models.SNMPCommunityFromContainer(snmpCommunityPCont)
if snmpCommunityP.DistinguishedName == "" {
return nil, fmt.Errorf("SNMPCommunity %s not found", snmpCommunityP.DistinguishedName)
return nil, fmt.Errorf("SNMP Community %s not found", snmpCommunityP.DistinguishedName)
}

return snmpCommunityP, nil
}

func setSNMPCommunityAttributes(snmpCommunityP *models.SNMPCommunity, d *schema.ResourceData) (*schema.ResourceData, error) {
dn := d.Id()
d.SetId(snmpCommunityP.DistinguishedName)
d.Set("description", snmpCommunityP.Description)
if dn != snmpCommunityP.DistinguishedName {
d.Set("parent_dn", "")
}

snmpCommunityPMap, err := snmpCommunityP.ToMap()
if err != nil {
return nil, err
return d, err
}

d.Set("name", snmpCommunityPMap["name"])
d.Set("name_alias", snmpCommunityPMap["nameAlias"])
d.Set("annotation", snmpCommunityPMap["annotation"])
d.Set("vrf_snmp_context_dn", GetParentDn(d.Id(), fmt.Sprintf("/community-%s", snmpCommunityPMap["name"])))
d.Set("parent_dn", GetParentDn(d.Id(), fmt.Sprintf("/community-%s", snmpCommunityPMap["name"])))

return d, nil
}
Expand All @@ -70,14 +78,17 @@ func resourceAciSNMPCommunityImport(d *schema.ResourceData, m interface{}) ([]*s
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()

snmpCommunityP, err := getRemoteSNMPCommunity(aciClient, dn)
if err != nil {
return nil, err
}

schemaFilled, err := setSNMPCommunityAttributes(snmpCommunityP, d)
if err != nil {
return nil, err
}

log.Printf("[DEBUG] %s: Import finished successfully", d.Id())
return []*schema.ResourceData{schemaFilled}, nil
}
Expand All @@ -87,13 +98,15 @@ func resourceAciSNMPCommunityCreate(ctx context.Context, d *schema.ResourceData,
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
name := d.Get("name").(string)
VRFSNMPCtxDn := d.Get("vrf_snmp_context_dn").(string)
SNMPPolicyDn := d.Get("parent_dn").(string)

snmpCommunityPAttr := models.SNMPCommunityAttributes{}

nameAlias := ""
if NameAlias, ok := d.GetOk("name_alias"); ok {
nameAlias = NameAlias.(string)
}

if Annotation, ok := d.GetOk("annotation"); ok {
snmpCommunityPAttr.Annotation = Annotation.(string)
} else {
Expand All @@ -103,7 +116,8 @@ func resourceAciSNMPCommunityCreate(ctx context.Context, d *schema.ResourceData,
if Name, ok := d.GetOk("name"); ok {
snmpCommunityPAttr.Name = Name.(string)
}
snmpCommunityP := models.NewSNMPCommunity(fmt.Sprintf("community-%s", name), VRFSNMPCtxDn, desc, nameAlias, snmpCommunityPAttr)

snmpCommunityP := models.NewSNMPCommunity(fmt.Sprintf(models.RnsnmpCommunityP, name), SNMPPolicyDn, desc, nameAlias, snmpCommunityPAttr)

err := aciClient.Save(snmpCommunityP)
if err != nil {
Expand All @@ -120,8 +134,10 @@ func resourceAciSNMPCommunityUpdate(ctx context.Context, d *schema.ResourceData,
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
name := d.Get("name").(string)
VRFSNMPCtxDn := d.Get("vrf_snmp_context_dn").(string)
SNMPPolicyDn := d.Get("parent_dn").(string)

snmpCommunityPAttr := models.SNMPCommunityAttributes{}

nameAlias := ""
if NameAlias, ok := d.GetOk("name_alias"); ok {
nameAlias = NameAlias.(string)
Expand All @@ -136,9 +152,11 @@ func resourceAciSNMPCommunityUpdate(ctx context.Context, d *schema.ResourceData,
if Name, ok := d.GetOk("name"); ok {
snmpCommunityPAttr.Name = Name.(string)
}
snmpCommunityP := models.NewSNMPCommunity(fmt.Sprintf("community-%s", name), VRFSNMPCtxDn, desc, nameAlias, snmpCommunityPAttr)

snmpCommunityP := models.NewSNMPCommunity(fmt.Sprintf("community-%s", name), SNMPPolicyDn, desc, nameAlias, snmpCommunityPAttr)

snmpCommunityP.Status = "modified"

err := aciClient.Save(snmpCommunityP)
if err != nil {
return diag.FromErr(err)
Expand All @@ -153,11 +171,13 @@ func resourceAciSNMPCommunityRead(ctx context.Context, d *schema.ResourceData, m
log.Printf("[DEBUG] %s: Beginning Read", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()

snmpCommunityP, err := getRemoteSNMPCommunity(aciClient, dn)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}

_, err = setSNMPCommunityAttributes(snmpCommunityP, d)
if err != nil {
d.SetId("")
Expand All @@ -172,10 +192,12 @@ func resourceAciSNMPCommunityDelete(ctx context.Context, d *schema.ResourceData,
log.Printf("[DEBUG] %s: Beginning Destroy", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()

err := aciClient.DeleteByDn(dn, "snmpCommunityP")
if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id())
d.SetId("")
return diag.FromErr(err)
Expand Down
Loading

0 comments on commit 200ccd3

Please sign in to comment.