-
Notifications
You must be signed in to change notification settings - Fork 67
/
resource_library_variable_set.go
106 lines (82 loc) · 3.15 KB
/
resource_library_variable_set.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
package octopusdeploy
import (
"context"
"log"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/libraryvariablesets"
"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 resourceLibraryVariableSet() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLibraryVariableSetCreate,
DeleteContext: resourceLibraryVariableSetDelete,
Description: "This resource manages library variable sets in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceLibraryVariableSetRead,
Schema: getLibraryVariableSetSchema(),
UpdateContext: resourceLibraryVariableSetUpdate,
}
}
func resourceLibraryVariableSetCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
libraryVariableSet := expandLibraryVariableSet(d)
log.Printf("[INFO] creating library variable set: %#v", libraryVariableSet)
client := m.(*client.Client)
createdLibraryVariableSet, err := libraryvariablesets.Add(client, libraryVariableSet)
if err != nil {
return diag.FromErr(err)
}
if err := setLibraryVariableSet(ctx, d, createdLibraryVariableSet); err != nil {
return diag.FromErr(err)
}
d.SetId(createdLibraryVariableSet.GetID())
log.Printf("[INFO] library variable set created (%s)", d.Id())
return nil
}
func resourceLibraryVariableSetDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting library variable set (%s)", d.Id())
var spaceID string
if v, ok := d.GetOk("space_id"); ok {
spaceID = v.(string)
}
client := m.(*client.Client)
err := libraryvariablesets.DeleteByID(client, spaceID, d.Id())
if err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] library variable set deleted (%s)", d.Id())
d.SetId("")
return nil
}
func resourceLibraryVariableSetRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading library variable set (%s)", d.Id())
var spaceID string
if v, ok := d.GetOk("space_id"); ok {
spaceID = v.(string)
}
client := m.(*client.Client)
libraryVariableSet, err := libraryvariablesets.GetByID(client, spaceID, d.Id())
if err != nil {
return errors.ProcessApiError(ctx, d, err, "library variable set")
}
if err := setLibraryVariableSet(ctx, d, libraryVariableSet); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] library variable set read (%s)", d.Id())
return nil
}
func resourceLibraryVariableSetUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] updating library variable set (%s)", d.Id())
libraryVariableSet := expandLibraryVariableSet(d)
client := m.(*client.Client)
updatedLibraryVariableSet, err := libraryvariablesets.Update(client, libraryVariableSet)
if err != nil {
return diag.FromErr(err)
}
if err := setLibraryVariableSet(ctx, d, updatedLibraryVariableSet); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] library variable set updated (%s)", d.Id())
return nil
}