This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
provider.go
250 lines (224 loc) · 11.2 KB
/
provider.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package yandex
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/mutexkv"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
const (
defaultMaxRetries = 5
defaultEndpoint = "api.cloud.yandex.net:443"
defaultStorageEndpoint = "storage.yandexcloud.net"
)
// Global MutexKV
var mutexKV = mutexkv.NewMutexKV()
func Provider() terraform.ResourceProvider {
return provider(false)
}
func emptyFolderProvider() terraform.ResourceProvider {
return provider(true)
}
func provider(emptyFolder bool) terraform.ResourceProvider {
provider := &schema.Provider{
Schema: map[string]*schema.Schema{
"endpoint": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_ENDPOINT", defaultEndpoint),
Description: descriptions["endpoint"],
},
"folder_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_FOLDER_ID", nil),
Description: descriptions["folder_id"],
},
"cloud_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_CLOUD_ID", nil),
Description: descriptions["cloud_id"],
},
"zone": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_ZONE", nil),
Description: descriptions["zone"],
},
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_TOKEN", nil),
Description: descriptions["token"],
},
"service_account_key_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_SERVICE_ACCOUNT_KEY_FILE", nil),
Description: descriptions["service_account_key_file"],
ConflictsWith: []string{"token"},
},
"storage_endpoint": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_STORAGE_ENDPOINT_URL", defaultStorageEndpoint),
Description: descriptions["storage_endpoint"],
},
"storage_access_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_STORAGE_ACCESS_KEY", nil),
Description: descriptions["storage_access_key"],
},
"storage_secret_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_STORAGE_SECRET_KEY", nil),
Description: descriptions["storage_secret_key"],
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_INSECURE", false),
Description: descriptions["insecure"],
},
"plaintext": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("YC_PLAINTEXT", false),
Description: descriptions["plaintext"],
},
"max_retries": {
Type: schema.TypeInt,
Optional: true,
Default: defaultMaxRetries,
Description: descriptions["max_retries"],
},
},
DataSourcesMap: map[string]*schema.Resource{
"yandex_container_registry": dataSourceYandexContainerRegistry(),
"yandex_compute_disk": dataSourceYandexComputeDisk(),
"yandex_compute_image": dataSourceYandexComputeImage(),
"yandex_compute_instance": dataSourceYandexComputeInstance(),
"yandex_compute_instance_group": dataSourceYandexComputeInstanceGroup(),
"yandex_compute_snapshot": dataSourceYandexComputeSnapshot(),
"yandex_dataproc_cluster": dataSourceYandexDataprocCluster(),
"yandex_function": dataSourceYandexFunction(),
"yandex_function_trigger": dataSourceYandexFunctionTrigger(),
"yandex_iam_policy": dataSourceYandexIAMPolicy(),
"yandex_iam_role": dataSourceYandexIAMRole(),
"yandex_iam_service_account": dataSourceYandexIAMServiceAccount(),
"yandex_iam_user": dataSourceYandexIAMUser(),
"yandex_iot_core_device": dataSourceYandexIoTCoreDevice(),
"yandex_iot_core_registry": dataSourceYandexIoTCoreRegistry(),
"yandex_kubernetes_cluster": dataSourceYandexKubernetesCluster(),
"yandex_kubernetes_node_group": dataSourceYandexKubernetesNodeGroup(),
"yandex_lb_network_load_balancer": dataSourceYandexLBNetworkLoadBalancer(),
"yandex_lb_target_group": dataSourceYandexLBTargetGroup(),
"yandex_mdb_clickhouse_cluster": dataSourceYandexMDBClickHouseCluster(),
"yandex_mdb_mongodb_cluster": dataSourceYandexMDBMongodbCluster(),
"yandex_mdb_mysql_cluster": dataSourceYandexMDBMySQLCluster(),
"yandex_mdb_postgresql_cluster": dataSourceYandexMDBPostgreSQLCluster(),
"yandex_mdb_redis_cluster": dataSourceYandexMDBRedisCluster(),
"yandex_resourcemanager_cloud": dataSourceYandexResourceManagerCloud(),
"yandex_resourcemanager_folder": dataSourceYandexResourceManagerFolder(),
"yandex_vpc_network": dataSourceYandexVPCNetwork(),
"yandex_vpc_route_table": dataSourceYandexVPCRouteTable(),
"yandex_vpc_security_group": dataSourceYandexVPCSecurityGroup(),
"yandex_vpc_subnet": dataSourceYandexVPCSubnet(),
},
ResourcesMap: map[string]*schema.Resource{
"yandex_container_registry": resourceYandexContainerRegistry(),
"yandex_compute_disk": resourceYandexComputeDisk(),
"yandex_compute_image": resourceYandexComputeImage(),
"yandex_compute_instance": resourceYandexComputeInstance(),
"yandex_compute_instance_group": resourceYandexComputeInstanceGroup(),
"yandex_compute_snapshot": resourceYandexComputeSnapshot(),
"yandex_dataproc_cluster": resourceYandexDataprocCluster(),
"yandex_function": resourceYandexFunction(),
"yandex_function_trigger": resourceYandexFunctionTrigger(),
"yandex_iam_service_account": resourceYandexIAMServiceAccount(),
"yandex_iam_service_account_api_key": resourceYandexIAMServiceAccountAPIKey(),
"yandex_iam_service_account_iam_binding": resourceYandexIAMServiceAccountIAMBinding(),
"yandex_iam_service_account_iam_member": resourceYandexIAMServiceAccountIAMMember(),
"yandex_iam_service_account_iam_policy": resourceYandexIAMServiceAccountIAMPolicy(),
"yandex_iam_service_account_key": resourceYandexIAMServiceAccountKey(),
"yandex_iam_service_account_static_access_key": resourceYandexIAMServiceAccountStaticAccessKey(),
"yandex_iot_core_device": resourceYandexIoTCoreDevice(),
"yandex_iot_core_registry": resourceYandexIoTCoreRegistry(),
"yandex_kms_symmetric_key": resourceYandexKMSSymmetricKeyKey(),
"yandex_kms_secret_ciphertext": resourceYandexKMSSecretCiphertext(),
"yandex_kubernetes_cluster": resourceYandexKubernetesCluster(),
"yandex_kubernetes_node_group": resourceYandexKubernetesNodeGroup(),
"yandex_lb_network_load_balancer": resourceYandexLBNetworkLoadBalancer(),
"yandex_lb_target_group": resourceYandexLBTargetGroup(),
"yandex_mdb_clickhouse_cluster": resourceYandexMDBClickHouseCluster(),
"yandex_mdb_mongodb_cluster": resourceYandexMDBMongodbCluster(),
"yandex_mdb_mysql_cluster": resourceYandexMDBMySQLCluster(),
"yandex_mdb_postgresql_cluster": resourceYandexMDBPostgreSQLCluster(),
"yandex_mdb_redis_cluster": resourceYandexMDBRedisCluster(),
"yandex_resourcemanager_cloud_iam_binding": resourceYandexResourceManagerCloudIAMBinding(),
"yandex_resourcemanager_cloud_iam_member": resourceYandexResourceManagerCloudIAMMember(),
"yandex_resourcemanager_folder_iam_binding": resourceYandexResourceManagerFolderIAMBinding(),
"yandex_resourcemanager_folder_iam_member": resourceYandexResourceManagerFolderIAMMember(),
"yandex_resourcemanager_folder_iam_policy": resourceYandexResourceManagerFolderIAMPolicy(),
"yandex_storage_bucket": resourceYandexStorageBucket(),
"yandex_storage_object": resourceYandexStorageObject(),
"yandex_vpc_network": resourceYandexVPCNetwork(),
"yandex_vpc_route_table": resourceYandexVPCRouteTable(),
"yandex_vpc_security_group": resourceYandexVPCSecurityGroup(),
"yandex_vpc_subnet": resourceYandexVPCSubnet(),
},
}
provider.ConfigureFunc = providerConfigure(provider, emptyFolder)
return provider
}
var descriptions = map[string]string{
"endpoint": "The API endpoint for Yandex.Cloud SDK client.",
"folder_id": "The default folder ID where resources will be placed.",
"cloud_id": "ID of Yandex.Cloud tenant.",
"zone": "The zone where operations will take place. Examples\n" +
"are ru-central1-a, ru-central2-c, etc.",
"token": "The access token for API operations.",
"service_account_key_file": "Path to file with Yandex.Cloud Service Account key.",
"insecure": "Explicitly allow the provider to perform \"insecure\" SSL requests. If omitted," +
"default value is `false`.",
"plaintext": "Disable use of TLS. Default value is `false`.",
"max_retries": "The maximum number of times an API request is being executed. \n" +
"If the API request still fails, an error is thrown.",
"storage_endpoint": "Yandex.Cloud storage service endpoint. Default is \n" + defaultStorageEndpoint,
"storage_access_key": "Yandex.Cloud storage service access key. \n" +
"Used when a storage data/resource doesn't have an access key explicitly specified.",
"storage_secret_key": "Yandex.Cloud storage service secret key. \n" +
"Used when a storage data/resource doesn't have a secret key explicitly specified.",
}
func providerConfigure(provider *schema.Provider, emptyFolder bool) schema.ConfigureFunc {
return func(d *schema.ResourceData) (interface{}, error) {
config := Config{
Token: d.Get("token").(string),
ServiceAccountKeyFile: d.Get("service_account_key_file").(string),
Zone: d.Get("zone").(string),
FolderID: d.Get("folder_id").(string),
CloudID: d.Get("cloud_id").(string),
Endpoint: d.Get("endpoint").(string),
Plaintext: d.Get("plaintext").(bool),
Insecure: d.Get("insecure").(bool),
MaxRetries: d.Get("max_retries").(int),
StorageEndpoint: d.Get("storage_endpoint").(string),
StorageAccessKey: d.Get("storage_access_key").(string),
StorageSecretKey: d.Get("storage_secret_key").(string),
}
if emptyFolder {
config.FolderID = ""
}
terraformVersion := provider.TerraformVersion
if terraformVersion == "" {
// Terraform 0.12 introduced this field to the protocol
// We can therefore assume that if it's missing it's 0.10 or 0.11
terraformVersion = "0.11+compatible"
}
if err := config.initAndValidate(provider.StopContext(), terraformVersion, false); err != nil {
return nil, err
}
return &config, nil
}
}