-
Notifications
You must be signed in to change notification settings - Fork 65
/
resource_runbook_process.go
170 lines (135 loc) · 4.94 KB
/
resource_runbook_process.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
package octopusdeploy
import (
"context"
"log"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/runbookprocess"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/runbooks"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceRunbookProcess() *schema.Resource {
return &schema.Resource{
CreateContext: resourceRunbookProcessCreate,
DeleteContext: resourceRunbookProcessDelete,
Description: "This resource manages runbook processes in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceRunbookProcessRead,
Schema: getRunbookProcessSchema(),
UpdateContext: resourceRunbookProcessUpdate,
}
}
func getRunbookProcessSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": getIDSchema(),
"last_snapshot_id": {
Description: "Read only value containing the last snapshot ID.",
Optional: true,
Type: schema.TypeString,
},
"project_id": {
Description: "The project ID associated with this runbook process.",
Optional: true,
Computed: true,
Type: schema.TypeString,
},
"runbook_id": {
Description: "The runbook ID associated with this runbook process.",
Required: true,
Type: schema.TypeString,
},
"space_id": getSpaceIDSchema(),
"step": getDeploymentStepSchema(),
"version": {
Computed: true,
Description: "The version number of this runbook process.",
Optional: true,
Type: schema.TypeInt,
},
}
}
// resourceRunbookProcessCreate "creates" a new runbook deployment process. In reality every runbook has a deployment process
// already, so this function retrieves the existing process and updates it.
func resourceRunbookProcessCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*client.Client)
runbookProcess := expandRunbookProcess(ctx, d, client)
log.Printf("[INFO] creating runbook process: %#v", runbookProcess)
runbook, err := runbooks.GetByID(client, d.Get("space_id").(string), runbookProcess.RunbookID)
if err != nil {
return diag.FromErr(err)
}
var current *runbookprocess.RunbookProcess
current, err = runbookprocess.GetByID(client, d.Get("space_id").(string), runbook.RunbookProcessID)
runbookProcess.ID = current.ID
runbookProcess.Links = current.Links
runbookProcess.Version = current.Version
createdRunbookProcess, err := runbookprocess.Update(client, runbookProcess)
if err != nil {
return diag.FromErr(err)
}
if err := setRunbookProcess(ctx, d, createdRunbookProcess); err != nil {
return diag.FromErr(err)
}
id := createdRunbookProcess.GetID()
d.SetId(id)
log.Printf("[INFO] deployment process created (%s)", d.Id())
return nil
}
func resourceRunbookProcessDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting runbook process (%s)", d.Id())
// "Deleting" a runbook process just means to clear it out
client := m.(*client.Client)
current, err := runbookprocess.GetByID(client, d.Get("space_id").(string), d.Id())
if err != nil {
return diag.FromErr(err)
}
runbookProcess := &runbookprocess.RunbookProcess{
Version: current.Version,
}
runbookProcess.Links = current.Links
runbookProcess.ID = d.Id()
if v, ok := d.GetOk("space_id"); ok {
runbookProcess.SpaceID = v.(string)
}
_, err = runbookprocess.Update(client, runbookProcess)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
log.Printf("[INFO] deployment process deleted")
return nil
}
func resourceRunbookProcessRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading runbook process (%s)", d.Id())
client := m.(*client.Client)
runbookProcess, err := runbookprocess.GetByID(client, d.Get("space_id").(string), d.Id())
if err != nil {
return errors.ProcessApiError(ctx, d, err, "runbook_process")
}
if err := setRunbookProcess(ctx, d, runbookProcess); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] runbook process read (%s)", d.Id())
return nil
}
func resourceRunbookProcessUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] updating runbook process (%s)", d.Id())
client := m.(*client.Client)
runbookProcess := expandRunbookProcess(ctx, d, client)
current, err := runbookprocess.GetByID(client, runbookProcess.SpaceID, d.Id())
if err != nil {
return diag.FromErr(err)
}
runbookProcess.Links = current.Links
runbookProcess.Version = current.Version
updatedRunbookProcess, err := runbookprocess.Update(client, runbookProcess)
if err != nil {
return diag.FromErr(err)
}
if err := setRunbookProcess(ctx, d, updatedRunbookProcess); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] deployment process updated (%s)", d.Id())
return nil
}