generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
azure_key_vault.go
163 lines (136 loc) · 4.87 KB
/
azure_key_vault.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
161
162
163
package connector
import (
"context"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceConnectorAzureKeyVault() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating an Azure key vault in Harness.",
ReadContext: resourceConnectorAzureKeyVaultRead,
CreateContext: resourceConnectorAzureKeyVaultCreateOrUpdate,
UpdateContext: resourceConnectorAzureKeyVaultCreateOrUpdate,
DeleteContext: resourceConnectorDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"client_id": {
Description: "Application ID of the Azure App.",
Type: schema.TypeString,
Required: true,
},
"secret_key": {
Description: "The Harness text secret with the Azure authentication key as its value.",
Type: schema.TypeString,
Required: true,
},
"tenant_id": {
Description: "The Azure Active Directory (Azure AD) directory ID where you created your application.",
Type: schema.TypeString,
Required: true,
},
"vault_name": {
Description: "Name of the vault.",
Type: schema.TypeString,
Required: true,
},
"subscription": {
Description: "Azure subscription ID.",
Type: schema.TypeString,
Required: true,
},
"is_default": {
Description: "Specifies whether or not is the default value.",
Type: schema.TypeBool,
Optional: true,
},
"azure_environment_type": {
Description: "Azure environment type. Possible values: AZURE or AZURE_US_GOVERNMENT. Default value: AZURE",
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"AZURE", "AZURE_US_GOVERNMENT"}, false),
},
"delegate_selectors": {
Description: "Tags to filter delegates for connection.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceConnectorAzureKeyVaultRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := resourceConnectorReadBase(ctx, d, meta, nextgen.ConnectorTypes.AzureKeyVault)
if err != nil {
return err
}
if conn == nil {
return nil
}
if err := readConnectorAzureKeyVault(d, conn); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceConnectorAzureKeyVaultCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := buildConnectorAzureKeyVault(d)
newConn, err := resourceConnectorCreateOrUpdateBase(ctx, d, meta, conn)
if err != nil {
return err
}
if err := readConnectorAzureKeyVault(d, newConn); err != nil {
return diag.FromErr(err)
}
return nil
}
func buildConnectorAzureKeyVault(d *schema.ResourceData) *nextgen.ConnectorInfo {
connector := &nextgen.ConnectorInfo{
Type_: nextgen.ConnectorTypes.AzureKeyVault,
AzureKeyVault: &nextgen.AzureKeyVaultConnector{},
}
if attr, ok := d.GetOk("tenant_id"); ok {
connector.AzureKeyVault.TenantId = attr.(string)
}
if attr, ok := d.GetOk("client_id"); ok {
connector.AzureKeyVault.ClientId = attr.(string)
}
if attr, ok := d.GetOk("secret_key"); ok {
connector.AzureKeyVault.SecretKey = attr.(string)
}
if attr, ok := d.GetOk("vault_name"); ok {
connector.AzureKeyVault.VaultName = attr.(string)
}
if attr, ok := d.GetOk("subscription"); ok {
connector.AzureKeyVault.Subscription = attr.(string)
}
if attr, ok := d.GetOk("is_default"); ok {
connector.AzureKeyVault.IsDefault = attr.(bool)
}
if attr, ok := d.GetOk("azure_environment_type"); ok {
connector.AzureKeyVault.AzureEnvironmentType = attr.(string)
}
if attr, ok := d.GetOk("delegate_selectors"); ok {
delegate_selectors := attr.(*schema.Set).List()
if len(delegate_selectors) > 0 {
connector.Azure.DelegateSelectors = utils.InterfaceSliceToStringSlice(delegate_selectors)
}
}
return connector
}
func readConnectorAzureKeyVault(d *schema.ResourceData, connector *nextgen.ConnectorInfo) error {
d.Set("client_id", connector.AzureKeyVault.ClientId)
d.Set("secret_key", connector.AzureKeyVault.SecretKey)
d.Set("tenant_id", connector.AzureKeyVault.TenantId)
d.Set("vault_name", connector.AzureKeyVault.VaultName)
d.Set("subscription", connector.AzureKeyVault.Subscription)
d.Set("is_default", connector.AzureKeyVault.IsDefault)
d.Set("delegate_selectors", connector.AzureKeyVault.DelegateSelectors)
d.Set("azure_environment_type", connector.AzureKeyVault.AzureEnvironmentType)
return nil
}