-
Notifications
You must be signed in to change notification settings - Fork 98
/
data_source_aci_cloudbgpasp.go
86 lines (64 loc) · 2.06 KB
/
data_source_aci_cloudbgpasp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 dataSourceAciAutonomousSystemProfile() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciAutonomousSystemProfileRead,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(map[string]*schema.Schema{
"asn": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name_alias": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
}),
}
}
func dataSourceAciAutonomousSystemProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
rn := fmt.Sprintf("clouddomp/as")
dn := fmt.Sprintf("uni/%s", rn)
cloudBgpAsP, err := getRemoteAutonomousSystemProfile(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}
_, err = setAutonomousSystemProfileAttributes(cloudBgpAsP, d)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func getRemoteAutonomousSystemProfile(client *client.Client, dn string) (*models.AutonomousSystemProfile, error) {
cloudBgpAsPCont, err := client.Get(dn)
if err != nil {
return nil, err
}
cloudBgpAsP := models.AutonomousSystemProfileFromContainer(cloudBgpAsPCont)
if cloudBgpAsP.DistinguishedName == "" {
return nil, fmt.Errorf("AutonomousSystemProfile %s not found", cloudBgpAsP.DistinguishedName)
}
return cloudBgpAsP, nil
}
func setAutonomousSystemProfileAttributes(cloudBgpAsP *models.AutonomousSystemProfile, d *schema.ResourceData) (*schema.ResourceData, error) {
d.SetId(cloudBgpAsP.DistinguishedName)
d.Set("description", cloudBgpAsP.Description)
cloudBgpAsPMap, err := cloudBgpAsP.ToMap()
if err != nil {
return d, err
}
d.Set("annotation", cloudBgpAsPMap["annotation"])
d.Set("asn", cloudBgpAsPMap["asn"])
d.Set("name_alias", cloudBgpAsPMap["nameAlias"])
return d, nil
}