-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathresource_ssh_connection_deployment_target.go
98 lines (77 loc) · 3.19 KB
/
resource_ssh_connection_deployment_target.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 resourceSSHConnectionDeploymentTarget() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSSHConnectionDeploymentTargetCreate,
DeleteContext: resourceSSHConnectionDeploymentTargetDelete,
Description: "This resource manages SSH connection deployment targets in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceSSHConnectionDeploymentTargetRead,
Schema: getSSHConnectionDeploymentTargetSchema(),
UpdateContext: resourceSSHConnectionDeploymentTargetUpdate,
}
}
func resourceSSHConnectionDeploymentTargetCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
deploymentTarget := expandSSHConnectionDeploymentTarget(d)
log.Printf("[INFO] creating SSH connection deployment target: %#v", deploymentTarget)
client := m.(*octopusdeploy.Client)
createdDeploymentTarget, err := client.Machines.Add(deploymentTarget)
if err != nil {
return diag.FromErr(err)
}
if err := setSSHConnectionDeploymentTarget(ctx, d, createdDeploymentTarget); err != nil {
return diag.FromErr(err)
}
d.SetId(createdDeploymentTarget.GetID())
log.Printf("[INFO] SSH connection deployment target created (%s)", d.Id())
return nil
}
func resourceSSHConnectionDeploymentTargetDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting SSH connection deployment target (%s)", d.Id())
client := m.(*octopusdeploy.Client)
if err := client.Machines.DeleteByID(d.Id()); err != nil {
return diag.FromErr(err)
}
d.SetId("")
log.Printf("[INFO] SSH connection deployment target deleted")
return nil
}
func resourceSSHConnectionDeploymentTargetRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading SSH connection deployment target (%s)", d.Id())
client := m.(*octopusdeploy.Client)
deploymentTarget, err := client.Machines.GetByID(d.Id())
if err != nil {
apiError := err.(*octopusdeploy.APIError)
if apiError.StatusCode == 404 {
log.Printf("[INFO] SSH connection deployment target (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
return diag.FromErr(err)
}
if err := setSSHConnectionDeploymentTarget(ctx, d, deploymentTarget); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] SSH connection deployment target read (%s)", d.Id())
return nil
}
func resourceSSHConnectionDeploymentTargetUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] updating SSH connection deployment target (%s)", d.Id())
deploymentTarget := expandSSHConnectionDeploymentTarget(d)
client := m.(*octopusdeploy.Client)
updatedDeploymentTarget, err := client.Machines.Update(deploymentTarget)
if err != nil {
return diag.FromErr(err)
}
if err := setSSHConnectionDeploymentTarget(ctx, d, updatedDeploymentTarget); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] SSH connection deployment target updated (%s)", d.Id())
return nil
}