-
Notifications
You must be signed in to change notification settings - Fork 98
/
resource_aci_aaaconsoleauth.go
206 lines (185 loc) · 6.71 KB
/
resource_aci_aaaconsoleauth.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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 resourceAciConsoleAuthenticationMethod() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAciConsoleAuthenticationMethodCreate,
UpdateContext: resourceAciConsoleAuthenticationMethodUpdate,
ReadContext: resourceAciConsoleAuthenticationMethodRead,
DeleteContext: resourceAciConsoleAuthenticationMethodDelete,
Importer: &schema.ResourceImporter{
State: resourceAciConsoleAuthenticationMethodImport,
},
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"provider_group": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"realm": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"ldap",
"local",
"radius",
"rsa",
"saml",
"tacacs",
}, false),
},
"realm_sub_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"default",
"duo",
}, false),
},
})),
}
}
func getRemoteConsoleAuthenticationMethod(client *client.Client, dn string) (*models.ConsoleAuthenticationMethod, error) {
aaaConsoleAuthCont, err := client.Get(dn)
if err != nil {
return nil, err
}
aaaConsoleAuth := models.ConsoleAuthenticationMethodFromContainer(aaaConsoleAuthCont)
if aaaConsoleAuth.DistinguishedName == "" {
return nil, fmt.Errorf("Console Authentication Method %s not found", dn)
}
return aaaConsoleAuth, nil
}
func setConsoleAuthenticationMethodAttributes(aaaConsoleAuth *models.ConsoleAuthenticationMethod, d *schema.ResourceData) (*schema.ResourceData, error) {
d.SetId(aaaConsoleAuth.DistinguishedName)
d.Set("description", aaaConsoleAuth.Description)
aaaConsoleAuthMap, err := aaaConsoleAuth.ToMap()
if err != nil {
return nil, err
}
d.Set("annotation", aaaConsoleAuthMap["annotation"])
d.Set("provider_group", aaaConsoleAuthMap["providerGroup"])
d.Set("realm", aaaConsoleAuthMap["realm"])
d.Set("realm_sub_type", aaaConsoleAuthMap["realmSubType"])
d.Set("name_alias", aaaConsoleAuthMap["nameAlias"])
return d, nil
}
func resourceAciConsoleAuthenticationMethodImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
aciClient := m.(*client.Client)
dn := d.Id()
aaaConsoleAuth, err := getRemoteConsoleAuthenticationMethod(aciClient, dn)
if err != nil {
return nil, err
}
schemaFilled, err := setConsoleAuthenticationMethodAttributes(aaaConsoleAuth, d)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] %s: Import finished successfully", d.Id())
return []*schema.ResourceData{schemaFilled}, nil
}
func resourceAciConsoleAuthenticationMethodCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] ConsoleAuthenticationMethod: Beginning Creation")
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
aaaConsoleAuthAttr := models.ConsoleAuthenticationMethodAttributes{}
nameAlias := ""
if NameAlias, ok := d.GetOk("name_alias"); ok {
nameAlias = NameAlias.(string)
}
if Annotation, ok := d.GetOk("annotation"); ok {
aaaConsoleAuthAttr.Annotation = Annotation.(string)
} else {
aaaConsoleAuthAttr.Annotation = "{}"
}
if ProviderGroup, ok := d.GetOk("provider_group"); ok {
aaaConsoleAuthAttr.ProviderGroup = ProviderGroup.(string)
}
if Realm, ok := d.GetOk("realm"); ok {
aaaConsoleAuthAttr.Realm = Realm.(string)
}
if RealmSubType, ok := d.GetOk("realm_sub_type"); ok {
aaaConsoleAuthAttr.RealmSubType = RealmSubType.(string)
}
aaaConsoleAuth := models.NewConsoleAuthenticationMethod(fmt.Sprintf("userext/authrealm/consoleauth"), "uni", desc, nameAlias, aaaConsoleAuthAttr)
aaaConsoleAuth.Status = "modified"
err := aciClient.Save(aaaConsoleAuth)
if err != nil {
return diag.FromErr(err)
}
d.SetId(aaaConsoleAuth.DistinguishedName)
log.Printf("[DEBUG] %s: Creation finished successfully", d.Id())
return resourceAciConsoleAuthenticationMethodRead(ctx, d, m)
}
func resourceAciConsoleAuthenticationMethodUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] ConsoleAuthenticationMethod: Beginning Update")
aciClient := m.(*client.Client)
desc := d.Get("description").(string)
aaaConsoleAuthAttr := models.ConsoleAuthenticationMethodAttributes{}
nameAlias := ""
if NameAlias, ok := d.GetOk("name_alias"); ok {
nameAlias = NameAlias.(string)
}
if Annotation, ok := d.GetOk("annotation"); ok {
aaaConsoleAuthAttr.Annotation = Annotation.(string)
} else {
aaaConsoleAuthAttr.Annotation = "{}"
}
if ProviderGroup, ok := d.GetOk("provider_group"); ok {
aaaConsoleAuthAttr.ProviderGroup = ProviderGroup.(string)
}
if Realm, ok := d.GetOk("realm"); ok {
aaaConsoleAuthAttr.Realm = Realm.(string)
}
if RealmSubType, ok := d.GetOk("realm_sub_type"); ok {
aaaConsoleAuthAttr.RealmSubType = RealmSubType.(string)
}
aaaConsoleAuth := models.NewConsoleAuthenticationMethod(fmt.Sprintf("userext/authrealm/consoleauth"), "uni", desc, nameAlias, aaaConsoleAuthAttr)
aaaConsoleAuth.Status = "modified"
err := aciClient.Save(aaaConsoleAuth)
if err != nil {
return diag.FromErr(err)
}
d.SetId(aaaConsoleAuth.DistinguishedName)
log.Printf("[DEBUG] %s: Update finished successfully", d.Id())
return resourceAciConsoleAuthenticationMethodRead(ctx, d, m)
}
func resourceAciConsoleAuthenticationMethodRead(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()
aaaConsoleAuth, err := getRemoteConsoleAuthenticationMethod(aciClient, dn)
if err != nil {
return errorForObjectNotFound(err, dn, d)
}
_, err = setConsoleAuthenticationMethodAttributes(aaaConsoleAuth, d)
if err != nil {
d.SetId("")
return nil
}
log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
return nil
}
func resourceAciConsoleAuthenticationMethodDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[DEBUG] %s: Beginning Destroy", d.Id())
d.SetId("")
var diags diag.Diagnostics
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Resource with class name aaaConsoleAuth cannot be deleted",
})
log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id())
return diags
}