-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_appgate_criteria_script.go
164 lines (137 loc) · 4.76 KB
/
resource_appgate_criteria_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
package appgate
import (
"context"
"fmt"
"log"
"time"
"github.com/appgate/sdp-api-client-go/api/v15/openapi"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAppgateCriteriaScript() *schema.Resource {
return &schema.Resource{
Create: resourceAppgateCriteriaScriptCreate,
Read: resourceAppgateCriteriaScriptRead,
Update: resourceAppgateCriteriaScriptUpdate,
Delete: resourceAppgateCriteriaScriptDelete,
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{
"criteria_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(),
"expression": {
Type: schema.TypeString,
Description: "A JavaScript expression that returns boolean.",
Required: true,
},
},
}
}
func resourceAppgateCriteriaScriptCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Creating Criteria script: %s", d.Get("name").(string))
token := meta.(*Client).Token
api := meta.(*Client).API.CriteriaScriptsApi
args := openapi.NewCriteriaScriptWithDefaults()
args.Id = uuid.New().String()
args.SetName(d.Get("name").(string))
args.SetNotes(d.Get("notes").(string))
args.SetTags(schemaExtractTags(d))
if v, ok := d.GetOk("expression"); ok {
args.SetExpression(v.(string))
}
request := api.CriteriaScriptsPost(context.TODO())
request = request.CriteriaScript(*args)
criteraScript, _, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not create Criteria script %+v", prettyPrintAPIError(err))
}
d.SetId(criteraScript.Id)
d.Set("criteria_script_id", criteraScript.Id)
return resourceAppgateCriteriaScriptRead(d, meta)
}
func resourceAppgateCriteriaScriptRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading Criteria script id: %+v", d.Id())
token := meta.(*Client).Token
api := meta.(*Client).API.CriteriaScriptsApi
ctx := context.TODO()
request := api.CriteriaScriptsIdGet(ctx, d.Id())
criteraScript, _, err := request.Authorization(token).Execute()
if err != nil {
// TODO check if 404
d.SetId("")
return fmt.Errorf("Failed to read Criteria script, %+v", err)
}
d.SetId(criteraScript.Id)
d.Set("criteria_script_id", criteraScript.Id)
d.Set("name", criteraScript.Name)
d.Set("notes", criteraScript.Notes)
d.Set("tags", criteraScript.Tags)
d.Set("expression", criteraScript.Expression)
return nil
}
func resourceAppgateCriteriaScriptUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Updating Criteria script: %s", d.Get("name").(string))
log.Printf("[DEBUG] Updating Criteria script id: %+v", d.Id())
token := meta.(*Client).Token
api := meta.(*Client).API.CriteriaScriptsApi
ctx := context.TODO()
request := api.CriteriaScriptsIdGet(ctx, d.Id())
originalCriteriaScript, _, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Failed to read Criteria script while updating, %+v", err)
}
if d.HasChange("name") {
originalCriteriaScript.SetName(d.Get("name").(string))
}
if d.HasChange("notes") {
originalCriteriaScript.SetNotes(d.Get("notes").(string))
}
if d.HasChange("tags") {
originalCriteriaScript.SetTags(schemaExtractTags(d))
}
if d.HasChange("expression") {
originalCriteriaScript.SetExpression(d.Get("expression").(string))
}
req := api.CriteriaScriptsIdPut(ctx, d.Id())
req = req.CriteriaScript(originalCriteriaScript)
_, _, err = req.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not update Criteria script %+v", prettyPrintAPIError(err))
}
return resourceAppgateCriteriaScriptRead(d, meta)
}
func resourceAppgateCriteriaScriptDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Delete Criteria script: %s", d.Get("name").(string))
log.Printf("[DEBUG] Reading Criteria script id: %+v", d.Id())
token := meta.(*Client).Token
api := meta.(*Client).API.CriteriaScriptsApi
request := api.CriteriaScriptsIdDelete(context.TODO(), d.Id())
_, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not delete Criteria script %+v", prettyPrintAPIError(err))
}
d.SetId("")
return nil
}