@@ -13,13 +13,11 @@ import (
13
13
"strings"
14
14
15
15
"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"
19
19
"github.com/hashicorp/terraform-plugin-framework/resource"
20
20
"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"
23
21
24
22
"github.com/juju/terraform-provider-juju/internal/juju"
25
23
)
@@ -36,133 +34,6 @@ const (
36
34
JujuCACert = "ca_certificate"
37
35
)
38
36
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
-
166
37
// populateJujuProviderModelLive gets the controller config,
167
38
// first from environment variables, then from a live juju
168
39
// controller as a fallback.
@@ -192,10 +63,10 @@ func getField(field string, config map[string]string) string {
192
63
}
193
64
194
65
// Ensure jujuProvider satisfies various provider interfaces.
195
- var _ frameworkprovider .Provider = & jujuProvider {}
66
+ var _ provider .Provider = & jujuProvider {}
196
67
197
68
// NewJujuProvider returns a framework style terraform provider.
198
- func NewJujuProvider (version string ) frameworkprovider .Provider {
69
+ func NewJujuProvider (version string ) provider .Provider {
199
70
return & jujuProvider {version : version }
200
71
}
201
72
@@ -219,31 +90,31 @@ func (j jujuProviderModel) valid() bool {
219
90
220
91
// Metadata returns the metadata for the provider, such as
221
92
// 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 ) {
223
94
resp .TypeName = "juju"
224
95
resp .Version = p .version
225
96
}
226
97
227
98
// Schema returns the schema for this provider, specifically
228
99
// it defines the juju controller config necessary to create
229
100
// 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 {
234
105
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 ),
235
106
Optional : true ,
236
107
},
237
- JujuUsername : frameworkschema .StringAttribute {
108
+ JujuUsername : schema .StringAttribute {
238
109
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 ),
239
110
Optional : true ,
240
111
},
241
- JujuPassword : frameworkschema .StringAttribute {
112
+ JujuPassword : schema .StringAttribute {
242
113
Description : fmt .Sprintf ("This is the password of the username to be used. This can also be set by the `%s` environment variable" , JujuPasswordEnvKey ),
243
114
Optional : true ,
244
115
Sensitive : true ,
245
116
},
246
- JujuCACert : frameworkschema .StringAttribute {
117
+ JujuCACert : schema .StringAttribute {
247
118
Description : fmt .Sprintf ("This is the certificate to use for identification. This can also be set by the `%s` environment variable" , JujuCACertEnvKey ),
248
119
Optional : true ,
249
120
},
@@ -258,7 +129,7 @@ func (p *jujuProvider) Schema(_ context.Context, _ frameworkprovider.SchemaReque
258
129
// Values from provider configuration are often used to initialise an
259
130
// API client, which should be stored on the struct implementing the
260
131
// 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 ) {
262
133
// Get data required for configuring the juju client.
263
134
data , diags := getJujuProviderModel (ctx , req )
264
135
if diags .HasError () {
@@ -294,9 +165,9 @@ func (p *jujuProvider) Configure(ctx context.Context, req frameworkprovider.Conf
294
165
// getJujuProviderModel a filled in jujuProviderModel if able. First check
295
166
// the plan being used, then fall back to the JUJU_ environment variables,
296
167
// 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 ) {
298
169
var data jujuProviderModel
299
- var diags frameworkdiag .Diagnostics
170
+ var diags diag .Diagnostics
300
171
301
172
// Read Terraform configuration data into the data model
302
173
diags .Append (req .Config .Get (ctx , & data )... )
@@ -377,9 +248,9 @@ func (p *jujuProvider) DataSources(_ context.Context) []func() datasource.DataSo
377
248
}
378
249
}
379
250
380
- func checkClientErr (err error , config juju.ControllerConfiguration ) frameworkdiag .Diagnostics {
251
+ func checkClientErr (err error , config juju.ControllerConfiguration ) diag .Diagnostics {
381
252
var errDetail string
382
- var diags frameworkdiag .Diagnostics
253
+ var diags diag .Diagnostics
383
254
384
255
x509error := & x509.UnknownAuthorityError {}
385
256
netOpError := & net.OpError {}
0 commit comments