/
resource_git_credential.go
101 lines (78 loc) · 3.01 KB
/
resource_git_credential.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
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGitCredential() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGitCredentialCreate,
DeleteContext: resourceGitCredentialDelete,
Description: "This resource manages Git credentials in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceGitCredentialRead,
Schema: getGitCredentialSchema(),
UpdateContext: resourceGitCredentialUpdate,
}
}
func resourceGitCredentialCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
resource := expandGitCredential(d)
tflog.Info(ctx, fmt.Sprintf("creating Git credential, %s", resource.Name))
client := m.(*client.Client)
createdResource, err := credentials.Add(client, resource)
if err != nil {
return diag.FromErr(err)
}
createdResource, err = credentials.GetByID(client, d.Get("space_id").(string), createdResource.GetID())
if err != nil {
return diag.FromErr(err)
}
if err := setGitCredential(ctx, d, createdResource); err != nil {
return diag.FromErr(err)
}
d.SetId(createdResource.GetID())
tflog.Info(ctx, fmt.Sprintf("Git credential created (%s)", d.Id()))
return nil
}
func resourceGitCredentialDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("deleting Git credential (%s)", d.Id()))
client := m.(*client.Client)
if err := credentials.DeleteByID(client, d.Get("space_id").(string), d.Id()); err != nil {
return diag.FromErr(err)
}
d.SetId("")
tflog.Info(ctx, "Git credential deleted")
return nil
}
func resourceGitCredentialRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("reading Git credential (%s)", d.Id()))
client := m.(*client.Client)
resource, err := credentials.GetByID(client, d.Get("space_id").(string), d.Id())
if err != nil {
return errors.ProcessApiError(ctx, d, err, "Git credential")
}
if err := setGitCredential(ctx, d, resource); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("Git credential read (%s)", resource.GetID()))
return nil
}
func resourceGitCredentialUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
resource := expandGitCredential(d)
tflog.Info(ctx, fmt.Sprintf("updating Git credential (%s)", resource.GetID()))
client := m.(*client.Client)
updatedResource, err := credentials.Update(client, resource)
if err != nil {
return diag.FromErr(err)
}
if err := setGitCredential(ctx, d, updatedResource); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("Git credential updated (%s)", d.Id()))
return nil
}