-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_appgate_device_script.go
207 lines (176 loc) · 5.62 KB
/
resource_appgate_device_script.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
207
package appgate
import (
"context"
"encoding/base64"
"fmt"
"log"
"net/http"
"time"
"github.com/appgate/sdp-api-client-go/api/v16/openapi"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAppgateDeviceScript() *schema.Resource {
return &schema.Resource{
Create: resourceAppgateDeviceScriptCreate,
Read: resourceAppgateDeviceScriptRead,
Update: resourceAppgateDeviceScriptUpdate,
Delete: resourceAppgateDeviceScriptDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"device_script_id": {
Type: schema.TypeString,
Description: "ID of the object.",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the object.",
Required: true,
},
"notes": {
Type: schema.TypeString,
Description: "Notes for the object. Used for documentation purposes.",
Default: DefaultDescription,
Optional: true,
},
"tags": tagsSchema(),
"filename": {
Type: schema.TypeString,
Description: "The name of the file to be downloaded as to the client devices.",
Required: true,
},
"file": {
Type: schema.TypeString,
Description: "Path to the Device Script binary.",
Optional: true,
ConflictsWith: []string{"content"},
},
"content": {
Type: schema.TypeString,
Description: "The Device Script content.",
Optional: true,
ConflictsWith: []string{"file"},
},
"checksum_sha256": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAppgateDeviceScriptCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Creating Device script: %s", d.Get("name").(string))
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.DeviceClaimScriptsApi
args := openapi.NewDeviceScriptWithDefaults()
args.Id = uuid.New().String()
args.SetName(d.Get("name").(string))
args.SetNotes(d.Get("notes").(string))
args.SetFilename(d.Get("filename").(string))
args.SetTags(schemaExtractTags(d))
content, err := getResourceFileContent(d, "file")
if err != nil {
return err
}
encoded := base64.StdEncoding.EncodeToString(content)
args.SetFile(encoded)
request := api.DeviceScriptsPost(context.TODO())
request = request.DeviceScript(*args)
deviceScript, _, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not create Device script %+v", prettyPrintAPIError(err))
}
d.SetId(deviceScript.Id)
d.Set("device_script_id", deviceScript.Id)
return resourceAppgateDeviceScriptRead(d, meta)
}
func resourceAppgateDeviceScriptRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading Device script id: %+v", d.Id())
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.DeviceClaimScriptsApi
ctx := context.TODO()
request := api.DeviceScriptsIdGet(ctx, d.Id())
deviceScript, res, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
if res.StatusCode == http.StatusNotFound {
return nil
}
return fmt.Errorf("Failed to read Device script, %+v", err)
}
d.SetId(deviceScript.Id)
d.Set("device_script_id", deviceScript.Id)
d.Set("name", deviceScript.Name)
d.Set("notes", deviceScript.Notes)
d.Set("tags", deviceScript.Tags)
d.Set("checksum_sha256", deviceScript.GetChecksumSha256())
return nil
}
func resourceAppgateDeviceScriptUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Updating Device script: %s", d.Get("name").(string))
log.Printf("[DEBUG] Updating Device script id: %+v", d.Id())
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.DeviceClaimScriptsApi
ctx := context.TODO()
request := api.DeviceScriptsIdGet(ctx, d.Id())
originalDeviceScript, _, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Failed to read Device script while updating, %+v", err)
}
if d.HasChange("name") {
originalDeviceScript.SetName(d.Get("name").(string))
}
if d.HasChange("notes") {
originalDeviceScript.SetNotes(d.Get("notes").(string))
}
if d.HasChange("tags") {
originalDeviceScript.SetTags(schemaExtractTags(d))
}
if d.HasChange("file") || d.HasChange("content") {
content, err := getResourceFileContent(d, "file")
if err != nil {
return err
}
encoded := base64.StdEncoding.EncodeToString(content)
originalDeviceScript.SetFile(encoded)
}
req := api.DeviceScriptsIdPut(ctx, d.Id())
req = req.DeviceScript(originalDeviceScript)
_, _, err = req.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not update Device script %+v", prettyPrintAPIError(err))
}
return resourceAppgateDeviceScriptRead(d, meta)
}
func resourceAppgateDeviceScriptDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Delete Device script: %s", d.Get("name").(string))
log.Printf("[DEBUG] Reading Device script id: %+v", d.Id())
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.DeviceClaimScriptsApi
if _, err := api.DeviceScriptsIdDelete(context.Background(), d.Id()).Authorization(token).Execute(); err != nil {
return fmt.Errorf("Could not delete Device script %+v", prettyPrintAPIError(err))
}
d.SetId("")
return nil
}