-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_zpa_pra_credential_controller.go
199 lines (176 loc) · 6.58 KB
/
resource_zpa_pra_credential_controller.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
package zpa
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
client "github.com/zscaler/zscaler-sdk-go/v2/zpa"
"github.com/zscaler/zscaler-sdk-go/v2/zpa/services/privilegedremoteaccess/pracredential"
)
func resourcePRACredentialController() *schema.Resource {
return &schema.Resource{
Create: resourcePRACredentialControllerCreate,
Read: resourcePRACredentialControllerRead,
Update: resourcePRACredentialControllerUpdate,
Delete: resourcePRACredentialControllerDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
service := m.(*Client).pracredential.WithMicroTenant(GetString(d.Get("microtenant_id")))
id := d.Id()
_, parseIDErr := strconv.ParseInt(id, 10, 64)
if parseIDErr == nil {
// assume if the passed value is an int
_ = d.Set("id", id)
} else {
resp, _, err := service.GetByName(id)
if err == nil {
d.SetId(resp.ID)
_ = d.Set("id", resp.ID)
} else {
return []*schema.ResourceData{d}, err
}
}
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier of the privileged credential",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the privileged credential",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The description of the privileged credential",
},
"credential_type": {
Type: schema.TypeString,
Optional: true,
Description: "The protocol type that was designated for that particular privileged credential. The protocol type options are SSH, RDP, and VNC. Each protocol type has its own credential requirements.",
ValidateFunc: validation.StringInSlice([]string{
"USERNAME_PASSWORD",
"SSH_KEY",
"PASSWORD",
}, false),
},
"passphrase": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "The password that is used to protect the SSH private key. This field is optional",
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "The password associated with the username for the login you want to use for the privileged credential",
},
"private_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "The SSH private key associated with the username for the login you want to use for the privileged credential",
},
"user_domain": {
Type: schema.TypeString,
Optional: true,
Description: "The domain name associated with the username. The domain name only needs to be specified with logging in to an RDP console that is connected to an Active Directory Domain",
},
"username": {
Type: schema.TypeString,
Optional: true,
Description: " The username for the login you want to use for the privileged credential",
},
"microtenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The unique identifier of the Microtenant for the ZPA tenant. If you are within the Default Microtenant, pass microtenantId as 0 when making requests to retrieve data from the Default Microtenant.",
},
},
}
}
func resourcePRACredentialControllerCreate(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).pracredential.WithMicroTenant(GetString(d.Get("microtenant_id")))
req := expandPRACredentialController(d)
log.Printf("[INFO] Creating credential controller with request\n%+v\n", req)
credController, _, err := service.Create(&req)
if err != nil {
return err
}
log.Printf("[INFO] Created credential controller request. ID: %v\n", credController)
d.SetId(credController.ID)
return resourcePRACredentialControllerRead(d, m)
}
func resourcePRACredentialControllerRead(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).pracredential.WithMicroTenant(GetString(d.Get("microtenant_id")))
resp, _, err := service.Get(d.Id())
if err != nil {
if errResp, ok := err.(*client.ErrorResponse); ok && errResp.IsObjectNotFound() {
log.Printf("[WARN] Removing credential controller %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[INFO] Getting credential controller:\n%+v\n", resp)
d.SetId(resp.ID)
_ = d.Set("name", resp.Name)
_ = d.Set("description", resp.Description)
_ = d.Set("credential_type", resp.CredentialType)
_ = d.Set("username", resp.UserName)
_ = d.Set("microtenant_id", resp.MicroTenantID)
return nil
}
func resourcePRACredentialControllerUpdate(d *schema.ResourceData, m interface{}) error {
if d.HasChange("credential_type") {
oldType, newType := d.GetChange("credential_type")
return fmt.Errorf("changing 'credential_type' from '%s' to '%s' is not allowed", oldType, newType)
}
service := m.(*Client).pracredential.WithMicroTenant(GetString(d.Get("microtenant_id")))
id := d.Id()
log.Printf("[INFO] Updating credential controller ID: %v\n", id)
req := expandPRACredentialController(d)
if _, _, err := service.Get(id); err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
d.SetId("")
return nil
}
}
if _, err := service.Update(id, &req); err != nil {
return err
}
return resourcePRACredentialControllerRead(d, m)
}
func resourcePRACredentialControllerDelete(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).pracredential.WithMicroTenant(GetString(d.Get("microtenant_id")))
log.Printf("[INFO] Deleting credential controller ID: %v\n", d.Id())
if _, err := service.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
log.Printf("[INFO] credential controller deleted")
return nil
}
func expandPRACredentialController(d *schema.ResourceData) pracredential.Credential {
credController := pracredential.Credential{
ID: d.Id(),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
CredentialType: d.Get("credential_type").(string),
Passphrase: d.Get("passphrase").(string),
Password: d.Get("password").(string),
PrivateKey: d.Get("private_key").(string),
UserDomain: d.Get("user_domain").(string),
UserName: d.Get("username").(string),
MicroTenantID: d.Get("microtenant_id").(string),
}
return credController
}