-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
resource_arm_app_service_source_control_token.go
157 lines (127 loc) · 4.87 KB
/
resource_arm_app_service_source_control_token.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
package azurerm
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2018-02-01/web"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
var appServiceSourceControlTokenResourceName = "azurerm_app_service_source_control_token"
func resourceArmAppServiceSourceControlToken() *schema.Resource {
return &schema.Resource{
Create: resourceArmAppServiceSourceControlTokenCreateUpdate,
Read: resourceArmAppServiceSourceControlTokenRead,
Update: resourceArmAppServiceSourceControlTokenCreateUpdate,
Delete: resourceArmAppServiceSourceControlTokenDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"BitBucket",
"Dropbox",
"GitHub",
"OneDrive",
}, false),
},
"token": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},
"token_secret": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
}
}
func resourceArmAppServiceSourceControlTokenCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Web.BaseClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).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("Error updating App Service Source Control Token (Type %q): %s", scmType, err)
}
read, err := client.GetSourceControl(ctx, scmType)
if err != nil {
return fmt.Errorf("Error 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 resourceArmAppServiceSourceControlTokenRead(d, meta)
}
func resourceArmAppServiceSourceControlTokenRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Web.BaseClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).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("Error 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 resourceArmAppServiceSourceControlTokenDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Web.BaseClient
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).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("Error deleting App Service Source Control Token (Type %q): %s", scmType, err)
}
return nil
}