Skip to content

Commit

Permalink
[minor_change] Add new aci_snmp_user resource (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrahammughal authored Jul 27, 2023
1 parent c5a1e88 commit a5871b7
Show file tree
Hide file tree
Showing 7 changed files with 601 additions and 0 deletions.
57 changes: 57 additions & 0 deletions aci/data_source_aci_snmpuserp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package aci

import (
"context"
"fmt"

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

func dataSourceAciSnmpUserProfile() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciSnmpUserProfileRead,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"snmp_policy_dn": {
Type: schema.TypeString,
Required: true,
},
"authorization_type": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"privacy_type": {
Type: schema.TypeString,
Computed: true,
},
})),
}
}

func dataSourceAciSnmpUserProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
name := d.Get("name").(string)
snmpPolicyDn := d.Get("snmp_policy_dn").(string)
dn := fmt.Sprintf("%s/%s", snmpPolicyDn, fmt.Sprintf(models.RnSnmpUserP, name))

snmpUserP, err := getRemoteSnmpUserProfile(aciClient, dn)
if err != nil {
return nil
}

d.SetId(dn)

_, err = setSnmpUserProfileAttributes(snmpUserP, d)
if err != nil {
return nil
}

return nil
}
2 changes: 2 additions & 0 deletions aci/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func Provider() *schema.Provider {
"aci_spine_switch_policy_group": resourceAciSpineSwitchPolicyGroup(),
"aci_recurring_window": resourceAciRecurringWindow(),
"aci_file_remote_path": resourceAciRemotePathofaFile(),
"aci_snmp_user": resourceAciSnmpUserProfile(),
"aci_vrf_snmp_context_community": resourceAciSNMPCommunityDeprecated(),
"aci_snmp_community": resourceAciSNMPCommunity(),
"aci_mgmt_zone": resourceAciManagedNodesZone(),
Expand Down Expand Up @@ -515,6 +516,7 @@ func Provider() *schema.Provider {
"aci_spine_switch_policy_group": dataSourceAciSpineSwitchPolicyGroup(),
"aci_recurring_window": dataSourceAciRecurringWindow(),
"aci_file_remote_path": dataSourceAciRemotePathofaFile(),
"aci_snmp_user": dataSourceAciSnmpUserProfile(),
"aci_vrf_snmp_context_community": dataSourceAciSNMPCommunityDeprecated(),
"aci_snmp_community": dataSourceAciSNMPCommunity(),
"aci_mgmt_zone": dataSourceAciManagedNodesZone(),
Expand Down
271 changes: 271 additions & 0 deletions aci/resource_aci_snmpuserp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
package aci

import (
"context"
"fmt"
"log"

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

func resourceAciSnmpUserProfile() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAciSnmpUserProfileCreate,
UpdateContext: resourceAciSnmpUserProfileUpdate,
ReadContext: resourceAciSnmpUserProfileRead,
DeleteContext: resourceAciSnmpUserProfileDelete,

Importer: &schema.ResourceImporter{
State: resourceAciSnmpUserProfileImport,
},

SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"snmp_policy_dn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"authorization_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ForceNew: true,
},
"authorization_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"hmac-md5-96",
"hmac-sha1-96",
"hmac-sha2-224",
"hmac-sha2-256",
"hmac-sha2-384",
"hmac-sha2-512",
}, false),
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"privacy_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
ForceNew: true,
},
"privacy_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"aes-128",
"des",
"none",
}, false),
},
})),
}
}

func getRemoteSnmpUserProfile(client *client.Client, dn string) (*models.SnmpUserProfile, error) {
snmpUserPCont, err := client.Get(dn)
if err != nil {
return nil, err
}
snmpUserP := models.SnmpUserProfileFromContainer(snmpUserPCont)
if snmpUserP.DistinguishedName == "" {
return nil, fmt.Errorf("SNMP User Profile %s not found", dn)
}
return snmpUserP, nil
}

func setSnmpUserProfileAttributes(snmpUserP *models.SnmpUserProfile, d *schema.ResourceData) (*schema.ResourceData, error) {
d.SetId(snmpUserP.DistinguishedName)
d.Set("description", snmpUserP.Description)
snmpUserPMap, err := snmpUserP.ToMap()
if err != nil {
return d, err
}
dn := d.Id()
if dn != snmpUserP.DistinguishedName {
d.Set("snmp_policy_dn", "")
} else {
d.Set("snmp_policy_dn", GetParentDn(snmpUserP.DistinguishedName, fmt.Sprintf("/"+models.RnSnmpUserP, snmpUserPMap["name"])))
}
d.Set("annotation", snmpUserPMap["annotation"])
authKey := snmpUserPMap["authKey"]
if authKey != "" {
d.Set("authorization_key", authKey)
}
d.Set("authorization_type", snmpUserPMap["authType"])
d.Set("name", snmpUserPMap["name"])
d.Set("name_alias", snmpUserPMap["nameAlias"])
privKey := snmpUserPMap["privKey"]
if privKey != "" {
d.Set("privacy_key", privKey)
}
d.Set("privacy_type", snmpUserPMap["privType"])
return d, nil
}

func resourceAciSnmpUserProfileImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()
snmpUserP, err := getRemoteSnmpUserProfile(aciClient, dn)
if err != nil {
return nil, err
}
schemaFilled, err := setSnmpUserProfileAttributes(snmpUserP, d)
if err != nil {
return nil, err
}

log.Printf("[DEBUG] %s: Import finished successfully", d.Id())
return []*schema.ResourceData{schemaFilled}, nil
}

func resourceAciSnmpUserProfileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] SNMP User Profile: Beginning Creation")
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
name := d.Get("name").(string)
snmpPolicyDn := d.Get("snmp_policy_dn").(string)

snmpUserPAttr := models.SnmpUserProfileAttributes{}

if Annotation, ok := d.GetOk("annotation"); ok {
snmpUserPAttr.Annotation = Annotation.(string)
} else {
snmpUserPAttr.Annotation = "{}"
}

if AuthKey, ok := d.GetOk("authorization_key"); ok {
snmpUserPAttr.AuthKey = AuthKey.(string)
}

if AuthType, ok := d.GetOk("authorization_type"); ok {
snmpUserPAttr.AuthType = AuthType.(string)
}

if Name, ok := d.GetOk("name"); ok {
snmpUserPAttr.Name = Name.(string)
}

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

if PrivKey, ok := d.GetOk("privacy_key"); ok {
snmpUserPAttr.PrivKey = PrivKey.(string)
}

if PrivType, ok := d.GetOk("privacy_type"); ok {
snmpUserPAttr.PrivType = PrivType.(string)
}
snmpUserP := models.NewSnmpUserProfile(fmt.Sprintf(models.RnSnmpUserP, name), snmpPolicyDn, desc, snmpUserPAttr)

err := aciClient.Save(snmpUserP)
if err != nil {
return diag.FromErr(err)
}

d.SetId(snmpUserP.DistinguishedName)
log.Printf("[DEBUG] %s: Creation finished successfully", d.Id())
return resourceAciSnmpUserProfileRead(ctx, d, m)
}
func resourceAciSnmpUserProfileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] SNMP User Profile: Beginning Update")
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
name := d.Get("name").(string)
snmpPolicyDn := d.Get("snmp_policy_dn").(string)

snmpUserPAttr := models.SnmpUserProfileAttributes{}

if Annotation, ok := d.GetOk("annotation"); ok {
snmpUserPAttr.Annotation = Annotation.(string)
} else {
snmpUserPAttr.Annotation = "{}"
}

if AuthKey, ok := d.GetOk("authorization_key"); ok {
snmpUserPAttr.AuthKey = AuthKey.(string)
}

if AuthType, ok := d.GetOk("authorization_type"); ok {
snmpUserPAttr.AuthType = AuthType.(string)
}

if Name, ok := d.GetOk("name"); ok {
snmpUserPAttr.Name = Name.(string)
}

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

if PrivKey, ok := d.GetOk("privacy_key"); ok {
snmpUserPAttr.PrivKey = PrivKey.(string)
}

if PrivType, ok := d.GetOk("privacy_type"); ok {
snmpUserPAttr.PrivType = PrivType.(string)
}
snmpUserP := models.NewSnmpUserProfile(fmt.Sprintf(models.RnSnmpUserP, name), snmpPolicyDn, desc, snmpUserPAttr)

snmpUserP.Status = "modified"

err := aciClient.Save(snmpUserP)
if err != nil {
return diag.FromErr(err)
}

d.SetId(snmpUserP.DistinguishedName)
log.Printf("[DEBUG] %s: Update finished successfully", d.Id())
return resourceAciSnmpUserProfileRead(ctx, d, m)
}

func resourceAciSnmpUserProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] %s: Beginning Read", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()

snmpUserP, err := getRemoteSnmpUserProfile(aciClient, dn)
if err != nil {
return errorForObjectNotFound(err, dn, d)
}

_, err = setSnmpUserProfileAttributes(snmpUserP, d)
if err != nil {
d.SetId("")
return nil
}

log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
return nil
}

func resourceAciSnmpUserProfileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] %s: Beginning Destroy", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()

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

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

0 comments on commit a5871b7

Please sign in to comment.