forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_service_source_control_token_resource.go
158 lines (127 loc) · 5.02 KB
/
app_service_source_control_token_resource.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
package web
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2021-02-01/web"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/locks"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/web/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
var appServiceSourceControlTokenResourceName = "azurerm_app_service_source_control_token"
func resourceAppServiceSourceControlToken() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceAppServiceSourceControlTokenCreateUpdate,
Read: resourceAppServiceSourceControlTokenRead,
Update: resourceAppServiceSourceControlTokenCreateUpdate,
Delete: resourceAppServiceSourceControlTokenDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := validate.SourceControlTokenName()(id, "id")
if len(err) > 0 {
return fmt.Errorf("%s", err)
}
return nil
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.SourceControlTokenName(),
},
"token": {
Type: pluginsdk.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"token_secret": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}
func resourceAppServiceSourceControlTokenCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Web.BaseClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for App Service Source Control Token creation.")
scmType := d.Get("type").(string)
token := d.Get("token").(string)
tokenSecret := d.Get("token_secret").(string)
locks.ByName(scmType, appServiceSourceControlTokenResourceName)
defer locks.UnlockByName(scmType, appServiceSourceControlTokenResourceName)
properties := web.SourceControl{
SourceControlProperties: &web.SourceControlProperties{
Token: utils.String(token),
TokenSecret: utils.String(tokenSecret),
},
}
if _, err := client.UpdateSourceControl(ctx, scmType, properties); err != nil {
return fmt.Errorf("updating App Service Source Control Token (Type %q): %s", scmType, err)
}
read, err := client.GetSourceControl(ctx, scmType)
if err != nil {
return fmt.Errorf("retrieving App Service Source Control Token (Type %q): %s", scmType, err)
}
if read.Name == nil {
return fmt.Errorf("Cannot read App Service Source Control Token (Type %q)", scmType)
}
d.SetId(*read.Name)
return resourceAppServiceSourceControlTokenRead(d, meta)
}
func resourceAppServiceSourceControlTokenRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Web.BaseClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
scmType := d.Id()
resp, err := client.GetSourceControl(ctx, scmType)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] App Service Source Control Token (Type %q) was not found - removing from state", scmType)
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on App Service Source Control Token (Type %q): %+v", scmType, err)
}
d.Set("type", resp.Name)
if props := resp.SourceControlProperties; props != nil {
d.Set("token", props.Token)
d.Set("token_secret", props.TokenSecret)
}
return nil
}
func resourceAppServiceSourceControlTokenDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Web.BaseClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
scmType := d.Id()
// Delete cleans up existing tokens by setting their values to ""
token := ""
tokenSecret := ""
locks.ByName(scmType, appServiceSourceControlTokenResourceName)
defer locks.UnlockByName(scmType, appServiceSourceControlTokenResourceName)
log.Printf("[DEBUG] Deleting App Service Source Control Token (Type %q)", scmType)
properties := web.SourceControl{
SourceControlProperties: &web.SourceControlProperties{
Token: utils.String(token),
TokenSecret: utils.String(tokenSecret),
},
}
if _, err := client.UpdateSourceControl(ctx, scmType, properties); err != nil {
return fmt.Errorf("deleting App Service Source Control Token (Type %q): %s", scmType, err)
}
return nil
}