-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathschema_kubernetes_cluster_deployment_target.go
253 lines (206 loc) · 9.74 KB
/
schema_kubernetes_cluster_deployment_target.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
251
252
253
package octopusdeploy
import (
"context"
"fmt"
"net/url"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func expandKubernetesClusterDeploymentTarget(d *schema.ResourceData) *octopusdeploy.DeploymentTarget {
clusterURL, _ := url.Parse(d.Get("cluster_url").(string))
endpoint := octopusdeploy.NewKubernetesEndpoint(clusterURL)
if v, ok := d.GetOk("authentication"); ok {
endpoint.Authentication = expandKubernetesStandardAuthentication(v)
}
if v, ok := d.GetOk("aws_account_authentication"); ok {
endpoint.Authentication = expandKubernetesAwsAuthentication(v)
}
if v, ok := d.GetOk("azure_service_principal_authentication"); ok {
endpoint.Authentication = expandKubernetesAzureAuthentication(v)
}
if v, ok := d.GetOk("cluster_certificate"); ok {
endpoint.ClusterCertificate = v.(string)
}
if v, ok := d.GetOk("container"); ok {
endpoint.Container = expandContainer(v)
}
if v, ok := d.GetOk("default_worker_pool_id"); ok {
endpoint.DefaultWorkerPoolID = v.(string)
}
if v, ok := d.GetOk("namespace"); ok {
endpoint.Namespace = v.(string)
}
if v, ok := d.GetOk("proxy_id"); ok {
endpoint.ProxyID = v.(string)
}
if v, ok := d.GetOk("running_in_container"); ok {
endpoint.RunningInContainer = v.(bool)
}
if v, ok := d.GetOk("skip_tls_verification"); ok {
endpoint.SkipTLSVerification = v.(bool)
}
deploymentTarget := expandDeploymentTarget(d)
deploymentTarget.Endpoint = endpoint
return deploymentTarget
}
func flattenKubernetesClusterDeploymentTarget(deploymentTarget *octopusdeploy.DeploymentTarget) map[string]interface{} {
if deploymentTarget == nil {
return nil
}
flattenedDeploymentTarget := flattenDeploymentTarget(deploymentTarget)
endpointResource, _ := octopusdeploy.ToEndpointResource(deploymentTarget.Endpoint)
flattenedDeploymentTarget["cluster_certificate"] = endpointResource.ClusterCertificate
flattenedDeploymentTarget["container"] = flattenContainer(endpointResource.Container)
flattenedDeploymentTarget["default_worker_pool_id"] = endpointResource.DefaultWorkerPoolID
flattenedDeploymentTarget["namespace"] = endpointResource.Namespace
flattenedDeploymentTarget["proxy_id"] = endpointResource.ProxyID
flattenedDeploymentTarget["running_in_container"] = endpointResource.RunningInContainer
flattenedDeploymentTarget["skip_tls_verification"] = endpointResource.SkipTLSVerification
if endpointResource.ClusterURL != nil {
flattenedDeploymentTarget["cluster_url"] = endpointResource.ClusterURL.String()
}
switch endpointResource.Authentication.GetAuthenticationType() {
case "KubernetesAws":
flattenedDeploymentTarget["aws_account_authentication"] = flattenKubernetesAwsAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesAwsAuthentication))
case "KubernetesAzure":
flattenedDeploymentTarget["azure_service_principal_authentication"] = flattenKubernetesAzureAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesAzureAuthentication))
case "KubernetesCertificate":
flattenedDeploymentTarget["certificate_authentication"] = flattenKubernetesCertificateAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesCertificateAuthentication))
case "KubernetesStandard":
flattenedDeploymentTarget["authentication"] = flattenKubernetesStandardAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesStandardAuthentication))
case "None":
flattenedDeploymentTarget["authentication"] = flattenKubernetesStandardAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesStandardAuthentication))
}
return flattenedDeploymentTarget
}
func getKubernetesClusterDeploymentTargetDataSchema() map[string]*schema.Schema {
dataSchema := getKubernetesClusterDeploymentTargetSchema()
setDataSchema(&dataSchema)
deploymentTargetDataSchema := getDeploymentTargetDataSchema()
deploymentTargetDataSchema["kubernetes_cluster_deployment_targets"] = &schema.Schema{
Computed: true,
Description: "A list of Kubernetes cluster deployment targets that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
}
delete(deploymentTargetDataSchema, "communication_styles")
delete(deploymentTargetDataSchema, "deployment_targets")
deploymentTargetDataSchema["id"] = getDataSchemaID()
return deploymentTargetDataSchema
}
func getKubernetesClusterDeploymentTargetSchema() map[string]*schema.Schema {
kubernetesClusterDeploymentTargetSchema := getDeploymentTargetSchema()
kubernetesClusterDeploymentTargetSchema["authentication"] = &schema.Schema{
Computed: true,
Elem: &schema.Resource{Schema: getKubernetesStandardAuthenticationSchema()},
ExactlyOneOf: []string{"authentication", "aws_account_authentication", "azure_service_principal_authentication", "certificate_authentication"},
MaxItems: 1,
MinItems: 0,
Optional: true,
Type: schema.TypeList,
}
kubernetesClusterDeploymentTargetSchema["aws_account_authentication"] = &schema.Schema{
Computed: true,
Elem: &schema.Resource{Schema: getKubernetesAwsAuthenticationSchema()},
ExactlyOneOf: []string{"authentication", "aws_account_authentication", "azure_service_principal_authentication", "certificate_authentication"},
MaxItems: 1,
MinItems: 0,
Optional: true,
Type: schema.TypeList,
}
kubernetesClusterDeploymentTargetSchema["azure_service_principal_authentication"] = &schema.Schema{
Computed: true,
Elem: &schema.Resource{Schema: getKubernetesAzureAuthenticationSchema()},
ExactlyOneOf: []string{"authentication", "aws_account_authentication", "azure_service_principal_authentication", "certificate_authentication"},
MaxItems: 1,
MinItems: 0,
Optional: true,
Type: schema.TypeList,
}
kubernetesClusterDeploymentTargetSchema["certificate_authentication"] = &schema.Schema{
Computed: true,
Elem: &schema.Resource{Schema: getKubernetesCertificateAuthenticationSchema()},
ExactlyOneOf: []string{"authentication", "aws_account_authentication", "azure_service_principal_authentication", "certificate_authentication"},
MaxItems: 1,
MinItems: 0,
Optional: true,
Type: schema.TypeSet,
}
kubernetesClusterDeploymentTargetSchema["cluster_certificate"] = &schema.Schema{
Optional: true,
Type: schema.TypeString,
}
kubernetesClusterDeploymentTargetSchema["cluster_url"] = &schema.Schema{
Required: true,
Type: schema.TypeString,
}
kubernetesClusterDeploymentTargetSchema["container"] = &schema.Schema{
Computed: true,
Elem: &schema.Resource{Schema: getDeploymentActionContainerSchema()},
Optional: true,
Type: schema.TypeList,
}
kubernetesClusterDeploymentTargetSchema["default_worker_pool_id"] = &schema.Schema{
Optional: true,
Type: schema.TypeString,
}
kubernetesClusterDeploymentTargetSchema["namespace"] = &schema.Schema{
Optional: true,
Type: schema.TypeString,
}
kubernetesClusterDeploymentTargetSchema["proxy_id"] = &schema.Schema{
Optional: true,
Type: schema.TypeString,
}
kubernetesClusterDeploymentTargetSchema["running_in_container"] = &schema.Schema{
Optional: true,
Type: schema.TypeBool,
}
kubernetesClusterDeploymentTargetSchema["skip_tls_verification"] = &schema.Schema{
Optional: true,
Type: schema.TypeBool,
}
return kubernetesClusterDeploymentTargetSchema
}
func setKubernetesClusterDeploymentTarget(ctx context.Context, d *schema.ResourceData, deploymentTarget *octopusdeploy.DeploymentTarget) error {
endpointResource, err := octopusdeploy.ToEndpointResource(deploymentTarget.Endpoint)
if err != nil {
return err
}
d.Set("cluster_certificate", endpointResource.ClusterCertificate)
if err := d.Set("container", flattenContainer(endpointResource.Container)); err != nil {
return fmt.Errorf("error setting container: %s", err)
}
d.Set("default_worker_pool_id", endpointResource.DefaultWorkerPoolID)
d.Set("namespace", endpointResource.Namespace)
d.Set("proxy_id", endpointResource.ProxyID)
d.Set("running_in_container", endpointResource.RunningInContainer)
d.Set("skip_tls_verification", endpointResource.SkipTLSVerification)
if endpointResource.ClusterURL != nil {
d.Set("cluster_url", endpointResource.ClusterURL.String())
}
switch endpointResource.Authentication.GetAuthenticationType() {
case "KubernetesAws":
if err := d.Set("aws_account_authentication", flattenKubernetesAwsAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesAwsAuthentication))); err != nil {
return fmt.Errorf("error setting aws_account_authentication: %s", err)
}
case "KubernetesAzure":
if err := d.Set("azure_service_principal_authentication", flattenKubernetesAzureAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesAzureAuthentication))); err != nil {
return fmt.Errorf("error setting azure_service_principal_authentication: %s", err)
}
case "KubernetesCertificate":
if err := d.Set("certificate_authentication", flattenKubernetesCertificateAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesCertificateAuthentication))); err != nil {
return fmt.Errorf("error setting certificate_authentication: %s", err)
}
case "KubernetesStandard":
if err := d.Set("authentication", flattenKubernetesStandardAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesStandardAuthentication))); err != nil {
return fmt.Errorf("error setting authentication: %s", err)
}
case "None":
if err := d.Set("authentication", flattenKubernetesStandardAuthentication(endpointResource.Authentication.(*octopusdeploy.KubernetesStandardAuthentication))); err != nil {
return fmt.Errorf("error setting authentication: %s", err)
}
}
return setDeploymentTarget(ctx, d, deploymentTarget)
}