-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_script.go
122 lines (100 loc) · 2.89 KB
/
resource_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
package other
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
nexus "github.com/nduyphuong/go-nexus-client/nexus3"
nexusSchema "github.com/nduyphuong/go-nexus-client/nexus3/schema"
"github.com/nduyphuong/terraform-provider-nexus/internal/schema/common"
)
func ResourceScript() *schema.Resource {
return &schema.Resource{
Description: "Use this resource to create and execute a custom script.",
Create: resourceScriptCreate,
Read: resourceScriptRead,
Update: resourceScriptUpdate,
Delete: resourceScriptDelete,
Exists: resourceScriptExists,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": common.ResourceID,
"name": {
Description: "The name of the script.",
Required: true,
Type: schema.TypeString,
},
"content": {
Description: "The content of this script.",
Required: true,
Type: schema.TypeString,
},
"type": {
Description: "The type of the script, defaults to `groovy` if unset",
Optional: true,
Type: schema.TypeString,
Default: "groovy",
},
},
}
}
func getScriptFromResourceData(d *schema.ResourceData) nexusSchema.Script {
return nexusSchema.Script{
Name: d.Get("name").(string),
Content: d.Get("content").(string),
Type: d.Get("type").(string),
}
}
func resourceScriptCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
script := getScriptFromResourceData(d)
if err := client.Script.Create(&script); err != nil {
return err
}
// TODO: It should be possible to configure whether to run script or not
if err := client.Script.Run(script.Name, ""); err != nil {
return err
}
d.SetId(script.Name)
return resourceScriptRead(d, m)
}
func resourceScriptRead(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
script, err := client.Script.Get(d.Id())
if err != nil {
return err
}
if script == nil {
d.SetId("")
return nil
}
d.Set("name", script.Name)
d.Set("type", script.Type)
d.Set("content", script.Content)
return nil
}
func resourceScriptUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
if d.HasChange("content") || d.HasChange("type") {
script := getScriptFromResourceData(d)
if err := client.Script.Update(&script); err != nil {
return err
}
if err := client.Script.Run(script.Name, ""); err != nil {
return err
}
}
return resourceScriptRead(d, m)
}
func resourceScriptDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
if err := client.Script.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
return nil
}
func resourceScriptExists(d *schema.ResourceData, m interface{}) (bool, error) {
client := m.(*nexus.NexusClient)
script, err := client.Script.Get(d.Id())
return script != nil, err
}