-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathresource_environment.go
98 lines (77 loc) · 2.68 KB
/
resource_environment.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
package octopusdeploy
import (
"context"
"log"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceEnvironment() *schema.Resource {
return &schema.Resource{
CreateContext: resourceEnvironmentCreate,
DeleteContext: resourceEnvironmentDelete,
Description: "This resource manages environments in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceEnvironmentRead,
Schema: getEnvironmentSchema(),
UpdateContext: resourceEnvironmentUpdate,
}
}
func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
environment := expandEnvironment(d)
log.Printf("[INFO] creating environment: %#v", environment)
client := m.(*octopusdeploy.Client)
createdEnvironment, err := client.Environments.Add(environment)
if err != nil {
return diag.FromErr(err)
}
if err := setEnvironment(ctx, d, createdEnvironment); err != nil {
return diag.FromErr(err)
}
d.SetId(createdEnvironment.GetID())
log.Printf("[INFO] environment created (%s)", d.Id())
return nil
}
func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting environment (%s)", d.Id())
client := m.(*octopusdeploy.Client)
if err := client.Environments.DeleteByID(d.Id()); err != nil {
return diag.FromErr(err)
}
d.SetId("")
log.Printf("[INFO] environment deleted")
return nil
}
func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading environment (%s)", d.Id())
client := m.(*octopusdeploy.Client)
environment, err := client.Environments.GetByID(d.Id())
if err != nil {
apiError := err.(*octopusdeploy.APIError)
if apiError.StatusCode == 404 {
log.Printf("[INFO] environment (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
return diag.FromErr(err)
}
if err := setEnvironment(ctx, d, environment); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] environment read (%s)", d.Id())
return nil
}
func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] updating environment (%s)", d.Id())
environment := expandEnvironment(d)
client := m.(*octopusdeploy.Client)
updatedEnvironment, err := client.Environments.Update(environment)
if err != nil {
return diag.FromErr(err)
}
if err := setEnvironment(ctx, d, updatedEnvironment); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] environment updated (%s)", d.Id())
return nil
}