-
Notifications
You must be signed in to change notification settings - Fork 11
/
openstack.go
106 lines (95 loc) · 4.26 KB
/
openstack.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
/*
Copyright 2021 Upbound Inc.
*/
package clients
import (
"context"
"encoding/json"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/crossplane/upjet/pkg/terraform"
"github.com/crossplane-contrib/provider-openstack/apis/v1beta1"
)
const (
// error messages
errNoProviderConfig = "no providerConfigRef provided"
errGetProviderConfig = "cannot get referenced ProviderConfig"
errTrackUsage = "cannot track ProviderConfig usage"
errExtractCredentials = "cannot extract credentials"
errUnmarshalCredentials = "cannot unmarshal openstack credentials as JSON"
)
// TerraformSetupBuilder builds Terraform a terraform.SetupFn function which
// returns Terraform provider setup configuration
func TerraformSetupBuilder(version, providerSource, providerVersion string) terraform.SetupFn {
return func(ctx context.Context, client client.Client, mg resource.Managed) (terraform.Setup, error) {
ps := terraform.Setup{
Version: version,
Requirement: terraform.ProviderRequirement{
Source: providerSource,
Version: providerVersion,
},
}
configRef := mg.GetProviderConfigReference()
if configRef == nil {
return ps, errors.New(errNoProviderConfig)
}
pc := &v1beta1.ProviderConfig{}
if err := client.Get(ctx, types.NamespacedName{Name: configRef.Name}, pc); err != nil {
return ps, errors.Wrap(err, errGetProviderConfig)
}
t := resource.NewProviderConfigUsageTracker(client, &v1beta1.ProviderConfigUsage{})
if err := t.Track(ctx, mg); err != nil {
return ps, errors.Wrap(err, errTrackUsage)
}
data, err := resource.CommonCredentialExtractor(ctx, pc.Spec.Credentials.Source, client, pc.Spec.Credentials.CommonCredentialSelectors)
if err != nil {
return ps, errors.Wrap(err, errExtractCredentials)
}
creds := map[string]string{}
if err := json.Unmarshal(data, &creds); err != nil {
return ps, errors.Wrap(err, errUnmarshalCredentials)
}
// Set credentials in Terraform provider configuration.
// Keep in sync with configuration options of Terraform OpenStack provider:
// https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs
// Exceptions:
// "cloud" is not used, because we dont support giving a clouds.yaml directly to the provider.
ps.Configuration = map[string]any{
"auth_url": creds["auth_url"],
"region": creds["region"],
"user_name": creds["user_name"],
"user_id": creds["user_id"],
"application_credential_id": creds["application_credential_id"],
"application_credential_name": creds["application_credential_name"],
"application_credential_secret": creds["application_credential_secret"],
"tenant_id": creds["tenant_id"],
"tenant_name": creds["tenant_name"],
"password": creds["password"],
"token": creds["token"],
"user_domain_name": creds["user_domain_name"],
"user_domain_id": creds["user_domain_id"],
"project_domain_name": creds["project_domain_name"],
"project_domain_id": creds["project_domain_id"],
"domain_id": creds["domain_id"],
"domain_name": creds["domain_name"],
"default_domain": creds["default_domain"],
"system_scope": creds["system_scope"],
"insecure": creds["insecure"],
"cacert_file": creds["cacert_file"],
"cert": creds["cert"],
"key": creds["key"],
"endpoint_type": creds["endpoint_type"],
"endpoint_overrides": creds["endpoint_overrides"],
"swauth": creds["swauth"],
"use_octavia": creds["use_octavia"],
"disable_no_cache_header": creds["disable_no_cache_header"],
"delayed_auth": creds["delayed_auth"],
"allow_reauth": creds["allow_reauth"],
"max_retries": creds["max_retries"],
"enable_logging": creds["enable_logging"],
}
return ps, nil
}
}