Skip to content

Commit 10bcd60

Browse files
authored
Merge pull request #300 from cderici/remove-sdk2-mux-take-2
Remove sdk2 from provider & provider_tests
2 parents ff93610 + d51de4f commit 10bcd60

File tree

4 files changed

+103
-204
lines changed

4 files changed

+103
-204
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.20
55
require (
66
github.com/bflad/tfproviderlint v0.29.0
77
github.com/hashicorp/terraform-plugin-docs v0.16.0
8-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.28.0
98
// 2.9.43
109
github.com/juju/juju v0.0.0-20230601044333-3cb3f8beac4a
1110

@@ -93,6 +92,7 @@ require (
9392
github.com/hashicorp/raft v1.3.2-0.20210825230038-1a621031eb2b // indirect
9493
github.com/hashicorp/terraform-exec v0.18.1 // indirect
9594
github.com/hashicorp/terraform-json v0.17.1 // indirect
95+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.28.0 // indirect
9696
github.com/hashicorp/terraform-registry-address v0.2.1 // indirect
9797
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
9898
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect

internal/provider/provider.go

Lines changed: 18 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import (
1313
"strings"
1414

1515
"github.com/hashicorp/terraform-plugin-framework/datasource"
16-
frameworkdiag "github.com/hashicorp/terraform-plugin-framework/diag"
17-
frameworkprovider "github.com/hashicorp/terraform-plugin-framework/provider"
18-
frameworkschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
16+
"github.com/hashicorp/terraform-plugin-framework/diag"
17+
"github.com/hashicorp/terraform-plugin-framework/provider"
18+
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
1919
"github.com/hashicorp/terraform-plugin-framework/resource"
2020
"github.com/hashicorp/terraform-plugin-framework/types"
21-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
22-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2321

2422
"github.com/juju/terraform-provider-juju/internal/juju"
2523
)
@@ -36,133 +34,6 @@ const (
3634
JujuCACert = "ca_certificate"
3735
)
3836

39-
// New returns an sdk2 style terraform provider.
40-
func New(version string) func() *schema.Provider {
41-
return func() *schema.Provider {
42-
p := &schema.Provider{
43-
Schema: map[string]*schema.Schema{
44-
JujuController: {
45-
Type: schema.TypeString,
46-
Description: fmt.Sprintf("This is the Controller addresses to connect to, defaults to localhost:17070, multiple addresses can be provided in this format: <host>:<port>,<host>:<port>,.... This can also be set by the `%s` environment variable.", JujuControllerEnvKey),
47-
Optional: true,
48-
},
49-
JujuUsername: {
50-
Type: schema.TypeString,
51-
Description: fmt.Sprintf("This is the username registered with the controller to be used. This can also be set by the `%s` environment variable", JujuUsernameEnvKey),
52-
Optional: true,
53-
},
54-
JujuPassword: {
55-
Type: schema.TypeString,
56-
Description: fmt.Sprintf("This is the password of the username to be used. This can also be set by the `%s` environment variable", JujuPasswordEnvKey),
57-
Optional: true,
58-
Sensitive: true,
59-
},
60-
JujuCACert: {
61-
Type: schema.TypeString,
62-
Description: fmt.Sprintf("This is the certificate to use for identification. This can also be set by the `%s` environment variable", JujuCACertEnvKey),
63-
Optional: true,
64-
},
65-
},
66-
ResourcesMap: map[string]*schema.Resource{},
67-
}
68-
p.ConfigureContextFunc = configure()
69-
70-
return p
71-
}
72-
}
73-
74-
func configure() func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
75-
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
76-
var diags diag.Diagnostics
77-
78-
controllerAddresses := strings.Split(d.Get(JujuController).(string), ",")
79-
username := d.Get(JujuUsername).(string)
80-
password := d.Get(JujuPassword).(string)
81-
caCert := d.Get(JujuCACert).(string)
82-
83-
if (len(controllerAddresses) == 1 && controllerAddresses[0] == "") ||
84-
username == "" || password == "" || caCert == "" {
85-
// Look for any config data not directly supplied in
86-
// the plan.
87-
liveData, err := populateJujuProviderModelLive()
88-
if err != nil {
89-
diags = append(diags, diag.Diagnostic{
90-
Severity: diag.Error,
91-
Summary: "Gather live client data",
92-
Detail: err.Error(),
93-
})
94-
return nil, diags
95-
}
96-
if len(controllerAddresses) == 1 && controllerAddresses[0] == "" {
97-
controllerAddresses = strings.Split(liveData.ControllerAddrs.ValueString(), ",")
98-
}
99-
if username == "" {
100-
username = liveData.UserName.ValueString()
101-
}
102-
if password == "" {
103-
password = liveData.Password.ValueString()
104-
}
105-
if caCert == "" {
106-
caCert = liveData.CACert.ValueString()
107-
}
108-
}
109-
110-
// Validate the controller config.
111-
if username == "" || password == "" {
112-
diags = append(diags, diag.Diagnostic{
113-
Severity: diag.Error,
114-
Summary: "Username and password must be set",
115-
Detail: "Currently the provider can only authenticate using username and password based authentication, if both are empty the provider will panic",
116-
})
117-
}
118-
if len(controllerAddresses) > 1 && controllerAddresses[0] == "" {
119-
diags = append(diags, diag.Diagnostic{
120-
Severity: diag.Error,
121-
Summary: "Controller address required",
122-
Detail: "The provider must know which juju controller to use.",
123-
})
124-
}
125-
if caCert == "" {
126-
diags = append(diags, diag.Diagnostic{
127-
Severity: diag.Error,
128-
Summary: "Controller CACert",
129-
Detail: "Required for the Juju certificate authority to be trusted by your system",
130-
})
131-
}
132-
133-
if diags.HasError() {
134-
return nil, diags
135-
}
136-
137-
config := juju.ControllerConfiguration{
138-
ControllerAddresses: controllerAddresses,
139-
Username: username,
140-
Password: password,
141-
CACert: caCert,
142-
}
143-
client, err := juju.NewClient(ctx, config)
144-
if err != nil {
145-
return nil, diag.FromErr(err)
146-
}
147-
148-
// Here we are testing that we can connect successfully to the Juju server
149-
// this prevents having logic to check the connection is OK in every function
150-
testConn, err := client.Models.GetConnection(nil)
151-
if err != nil {
152-
for _, v := range checkClientErr(err, config) {
153-
diags = append(diags, diag.Diagnostic{
154-
Summary: v.Summary(),
155-
Detail: v.Detail(),
156-
})
157-
}
158-
return nil, diags
159-
}
160-
_ = testConn.Close()
161-
162-
return client, diags
163-
}
164-
}
165-
16637
// populateJujuProviderModelLive gets the controller config,
16738
// first from environment variables, then from a live juju
16839
// controller as a fallback.
@@ -192,10 +63,10 @@ func getField(field string, config map[string]string) string {
19263
}
19364

19465
// Ensure jujuProvider satisfies various provider interfaces.
195-
var _ frameworkprovider.Provider = &jujuProvider{}
66+
var _ provider.Provider = &jujuProvider{}
19667

19768
// NewJujuProvider returns a framework style terraform provider.
198-
func NewJujuProvider(version string) frameworkprovider.Provider {
69+
func NewJujuProvider(version string) provider.Provider {
19970
return &jujuProvider{version: version}
20071
}
20172

@@ -219,31 +90,31 @@ func (j jujuProviderModel) valid() bool {
21990

22091
// Metadata returns the metadata for the provider, such as
22192
// a type name and version data.
222-
func (p *jujuProvider) Metadata(_ context.Context, _ frameworkprovider.MetadataRequest, resp *frameworkprovider.MetadataResponse) {
93+
func (p *jujuProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
22394
resp.TypeName = "juju"
22495
resp.Version = p.version
22596
}
22697

22798
// Schema returns the schema for this provider, specifically
22899
// it defines the juju controller config necessary to create
229100
// a juju client.
230-
func (p *jujuProvider) Schema(_ context.Context, _ frameworkprovider.SchemaRequest, resp *frameworkprovider.SchemaResponse) {
231-
resp.Schema = frameworkschema.Schema{
232-
Attributes: map[string]frameworkschema.Attribute{
233-
JujuController: frameworkschema.StringAttribute{
101+
func (p *jujuProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
102+
resp.Schema = schema.Schema{
103+
Attributes: map[string]schema.Attribute{
104+
JujuController: schema.StringAttribute{
234105
Description: fmt.Sprintf("This is the Controller addresses to connect to, defaults to localhost:17070, multiple addresses can be provided in this format: <host>:<port>,<host>:<port>,.... This can also be set by the `%s` environment variable.", JujuControllerEnvKey),
235106
Optional: true,
236107
},
237-
JujuUsername: frameworkschema.StringAttribute{
108+
JujuUsername: schema.StringAttribute{
238109
Description: fmt.Sprintf("This is the username registered with the controller to be used. This can also be set by the `%s` environment variable", JujuUsernameEnvKey),
239110
Optional: true,
240111
},
241-
JujuPassword: frameworkschema.StringAttribute{
112+
JujuPassword: schema.StringAttribute{
242113
Description: fmt.Sprintf("This is the password of the username to be used. This can also be set by the `%s` environment variable", JujuPasswordEnvKey),
243114
Optional: true,
244115
Sensitive: true,
245116
},
246-
JujuCACert: frameworkschema.StringAttribute{
117+
JujuCACert: schema.StringAttribute{
247118
Description: fmt.Sprintf("This is the certificate to use for identification. This can also be set by the `%s` environment variable", JujuCACertEnvKey),
248119
Optional: true,
249120
},
@@ -258,7 +129,7 @@ func (p *jujuProvider) Schema(_ context.Context, _ frameworkprovider.SchemaReque
258129
// Values from provider configuration are often used to initialise an
259130
// API client, which should be stored on the struct implementing the
260131
// Provider interface.
261-
func (p *jujuProvider) Configure(ctx context.Context, req frameworkprovider.ConfigureRequest, resp *frameworkprovider.ConfigureResponse) {
132+
func (p *jujuProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
262133
// Get data required for configuring the juju client.
263134
data, diags := getJujuProviderModel(ctx, req)
264135
if diags.HasError() {
@@ -294,9 +165,9 @@ func (p *jujuProvider) Configure(ctx context.Context, req frameworkprovider.Conf
294165
// getJujuProviderModel a filled in jujuProviderModel if able. First check
295166
// the plan being used, then fall back to the JUJU_ environment variables,
296167
// lastly check to see if an active juju can supply the data.
297-
func getJujuProviderModel(ctx context.Context, req frameworkprovider.ConfigureRequest) (jujuProviderModel, frameworkdiag.Diagnostics) {
168+
func getJujuProviderModel(ctx context.Context, req provider.ConfigureRequest) (jujuProviderModel, diag.Diagnostics) {
298169
var data jujuProviderModel
299-
var diags frameworkdiag.Diagnostics
170+
var diags diag.Diagnostics
300171

301172
// Read Terraform configuration data into the data model
302173
diags.Append(req.Config.Get(ctx, &data)...)
@@ -377,9 +248,9 @@ func (p *jujuProvider) DataSources(_ context.Context) []func() datasource.DataSo
377248
}
378249
}
379250

380-
func checkClientErr(err error, config juju.ControllerConfiguration) frameworkdiag.Diagnostics {
251+
func checkClientErr(err error, config juju.ControllerConfiguration) diag.Diagnostics {
381252
var errDetail string
382-
var diags frameworkdiag.Diagnostics
253+
var diags diag.Diagnostics
383254

384255
x509error := &x509.UnknownAuthorityError{}
385256
netOpError := &net.OpError{}

0 commit comments

Comments
 (0)