generated from crossplane/upjet-provider-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
linode.go
122 lines (104 loc) · 3.94 KB
/
linode.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
/*
Copyright 2021 Upbound Inc.
*/
package clients
import (
"context"
"encoding/json"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/crossplane/upjet/pkg/terraform"
terraformsdk "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/linode/provider-linode/apis/v1beta1"
"github.com/linode/terraform-provider-linode/v2/linode"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
"github.com/linode/terraform-provider-linode/v2/version"
)
const (
// error messages
errNoProviderConfig = "no providerConfigRef provided"
errGetProviderConfig = "cannot get referenced ProviderConfig"
errTrackUsage = "cannot track ProviderConfig usage"
errExtractCredentials = "cannot extract credentials"
errUnmarshalCredentials = "cannot unmarshal linode credentials as JSON"
)
const (
keyToken = "token"
keyURL = "api_url"
keyVersion = "api_version"
)
type SetupConfig struct {
NativeProviderPath *string
NativeProviderSource *string
NativeProviderVersion *string
TerraformVersion *string
DefaultScheduler terraform.ProviderScheduler
TerraformProvider *schema.Provider
}
// TerraformSetupBuilder builds Terraform a terraform.SetupFn function which
// returns Terraform provider setup configuration
func TerraformSetupBuilder(version, providerSource, providerVersion string, tfProvider *schema.Provider, scheduler terraform.ProviderScheduler) 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,
},
Scheduler: scheduler,
}
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 provider configuration
ps.Configuration = map[string]any{}
if v, ok := creds[keyToken]; ok {
ps.Configuration[keyToken] = v
}
if v, ok := creds[keyURL]; ok {
ps.Configuration[keyURL] = v
}
if v, ok := creds[keyVersion]; ok {
ps.Configuration[keyVersion] = v
}
return ps, errors.Wrap(configureNoForkLinodeclient(ctx, &ps, *tfProvider), "failed to configure the no-fork linode client")
}
}
func configureNoForkLinodeclient(ctx context.Context, ps *terraform.Setup, p schema.Provider) error {
// Please be aware that this implementation relies on the schema.Provider
// parameter `p` being a non-pointer. This is because normally
// the Terraform plugin SDK normally configures the provider
// only once and using a pointer argument here will cause
// race conditions between resources referring to different
// ProviderConfigs.
diag := p.Configure(context.WithoutCancel(ctx), &terraformsdk.ResourceConfig{
Config: ps.Configuration,
})
if diag != nil && diag.HasError() {
return errors.Errorf("failed to configure the provider: %v", diag)
}
ps.Meta = p.Meta()
fwProvider := linode.CreateFrameworkProviderWithMeta(version.ProviderVersion, p.Meta().(*helper.ProviderMeta))
ps.FrameworkProvider = fwProvider
return nil
}