forked from DrFaust92/terraform-provider-bitbucket
/
resource_repository_variable.go
160 lines (132 loc) · 4.06 KB
/
resource_repository_variable.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
package bitbucket
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"github.com/DrFaust92/bitbucket-go-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceRepositoryVariable() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceRepositoryVariableCreate,
UpdateWithoutTimeout: resourceRepositoryVariableUpdate,
ReadWithoutTimeout: resourceRepositoryVariableRead,
DeleteWithoutTimeout: resourceRepositoryVariableDelete,
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Computed: true,
},
"key": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"secured": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"repository": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func newRepositoryVariableFromResource(d *schema.ResourceData) bitbucket.PipelineVariable {
dk := bitbucket.PipelineVariable{
Key: d.Get("key").(string),
Value: d.Get("value").(string),
Secured: d.Get("secured").(bool),
}
return dk
}
func resourceRepositoryVariableCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
pipeApi := c.ApiClient.PipelinesApi
rvcr := newRepositoryVariableFromResource(d)
repo := d.Get("repository").(string)
workspace, repoSlug, err := repoVarId(repo)
if err != nil {
return diag.FromErr(err)
}
rvRes, _, err := pipeApi.CreateRepositoryPipelineVariable(c.AuthContext, rvcr, workspace, repoSlug)
if err := handleClientError(err); err != nil {
return diag.FromErr(err)
}
d.Set("uuid", rvRes.Uuid)
d.SetId(rvRes.Key)
return resourceRepositoryVariableRead(ctx, d, m)
}
func resourceRepositoryVariableRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
pipeApi := c.ApiClient.PipelinesApi
repo := d.Get("repository").(string)
workspace, repoSlug, err := repoVarId(repo)
if err != nil {
return diag.FromErr(err)
}
rvRes, res, err := pipeApi.GetRepositoryPipelineVariable(c.AuthContext, workspace, repoSlug, d.Get("uuid").(string))
if res.StatusCode == http.StatusNotFound {
log.Printf("[WARN] Repository Variable (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err := handleClientError(err); err != nil {
return diag.FromErr(err)
}
d.Set("uuid", rvRes.Uuid)
d.Set("key", rvRes.Key)
d.Set("secured", rvRes.Secured)
if !rvRes.Secured {
d.Set("value", rvRes.Value)
} else {
d.Set("value", d.Get("value").(string))
}
return nil
}
func resourceRepositoryVariableUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
pipeApi := c.ApiClient.PipelinesApi
repo := d.Get("repository").(string)
workspace, repoSlug, err := repoVarId(repo)
if err != nil {
return diag.FromErr(err)
}
rvcr := newRepositoryVariableFromResource(d)
_, _, err = pipeApi.UpdateRepositoryPipelineVariable(c.AuthContext, rvcr, workspace, repoSlug, d.Get("uuid").(string))
if err := handleClientError(err); err != nil {
return diag.FromErr(err)
}
return resourceRepositoryVariableRead(ctx, d, m)
}
func resourceRepositoryVariableDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
pipeApi := c.ApiClient.PipelinesApi
repo := d.Get("repository").(string)
workspace, repoSlug, err := repoVarId(repo)
if err != nil {
return diag.FromErr(err)
}
_, err = pipeApi.DeleteRepositoryPipelineVariable(c.AuthContext, workspace, repoSlug, d.Get("uuid").(string))
if err := handleClientError(err); err != nil {
return diag.FromErr(err)
}
return nil
}
func repoVarId(repo string) (string, string, error) {
idparts := strings.Split(repo, "/")
if len(idparts) == 2 {
return idparts[0], idparts[1], nil
} else {
return "", "", fmt.Errorf("incorrect ID format, should match `owner/key`")
}
}