-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_appgate_blacklist_user.go
126 lines (108 loc) · 3.35 KB
/
resource_appgate_blacklist_user.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
package appgate
import (
"context"
"fmt"
"log"
"github.com/appgate/sdp-api-client-go/api/v14/openapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAppgateBlacklistUser() *schema.Resource {
return &schema.Resource{
Create: resourceAppgateBlacklistUserCreate,
Read: resourceAppgateBlacklistUserRead,
Delete: resourceAppgateBlacklistUserDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"user_distinguished_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"username": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
},
"provider_name": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
},
"reason": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
},
}
}
func resourceAppgateBlacklistUserCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Creating blacklisted user")
token := meta.(*Client).Token
api := meta.(*Client).API.BlacklistedUsersApi
args := openapi.NewBlacklistEntryWithDefaults()
if v, ok := d.GetOk("user_distinguished_name"); ok {
args.SetUserDistinguishedName(v.(string))
}
if v, ok := d.GetOk("username"); ok {
args.SetUsername(v.(string))
}
if v, ok := d.GetOk("provider_name"); ok {
args.SetProviderName(v.(string))
}
if v, ok := d.GetOk("reason"); ok {
args.SetReason(v.(string))
}
request := api.BlacklistPost(context.TODO())
request = request.BlacklistEntry(*args)
entry, _, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not create blacklisted user %+v", prettyPrintAPIError(err))
}
d.SetId(entry.GetUserDistinguishedName())
return resourceAppgateBlacklistUserRead(d, meta)
}
func queryEntry(ctx context.Context, api *openapi.BlacklistedUsersApiService, token, distinguishedName string) (*openapi.BlacklistEntry, error) {
request := api.BlacklistGet(ctx)
list, _, err := request.Authorization(token).Execute()
if err != nil {
return nil, err
}
for _, s := range list.GetData() {
if s.GetUserDistinguishedName() == distinguishedName {
return &s, nil
}
}
return nil, fmt.Errorf("Failed to find blacklist user %s", distinguishedName)
}
func resourceAppgateBlacklistUserRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading blacklisted user id: %+v", d.Id())
token := meta.(*Client).Token
api := meta.(*Client).API.BlacklistedUsersApi
entry, err := queryEntry(context.TODO(), api, token, d.Id())
if err != nil {
return err
}
d.Set("user_distinguished_name", entry.GetUserDistinguishedName())
d.Set("username", entry.GetUsername())
d.Set("provider_name", entry.GetProviderName())
d.Set("reason", entry.GetReason())
return nil
}
func resourceAppgateBlacklistUserDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading blacklisted user id: %+v", d.Id())
token := meta.(*Client).Token
api := meta.(*Client).API.BlacklistedUsersApi
request := api.BlacklistDistinguishedNameDelete(context.TODO(), d.Id())
_, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not delete blacklisted user %+v", prettyPrintAPIError(err))
}
d.SetId("")
return nil
}