1
1
package cloudamqp
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"log"
6
7
"net/http"
8
+ "os"
7
9
8
10
"github.com/84codes/go-api/api"
9
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
+ "github.com/hashicorp/terraform-plugin-framework/datasource"
12
+ "github.com/hashicorp/terraform-plugin-framework/provider"
13
+ "github.com/hashicorp/terraform-plugin-framework/provider/schema"
14
+ "github.com/hashicorp/terraform-plugin-framework/resource"
15
+ "github.com/hashicorp/terraform-plugin-framework/types"
16
+ schemaSdk "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
17
)
11
18
12
19
var version string
13
20
var enableFasterInstanceDestroy bool
14
21
15
- func Provider (v string , client * http.Client ) * schema.Provider {
22
+ type cloudamqpProvider struct {
23
+ version string
24
+ client * http.Client
25
+ }
26
+
27
+ type cloudamqpProviderModel struct {
28
+ ApiKey types.String `tfsdk:"apikey"`
29
+ BaseUrl types.String `tfsdk:"baseurl"`
30
+ EnableFasterInstanceDestroy types.Bool `tfsdk:"enable_faster_instance_destroy"`
31
+ }
32
+
33
+ func (p * cloudamqpProvider ) Metadata (_ context.Context , _ provider.MetadataRequest , response * provider.MetadataResponse ) {
34
+ response .Version = p .version
35
+ response .TypeName = "cloudamqp"
36
+ }
37
+
38
+ func (p * cloudamqpProvider ) Schema (_ context.Context , _ provider.SchemaRequest , response * provider.SchemaResponse ) {
39
+ response .Schema = schema.Schema {
40
+ Attributes : map [string ]schema.Attribute {
41
+ "apikey" : schema.StringAttribute {
42
+ Optional : true ,
43
+ Description : "Key used to authentication to the CloudAMQP Customer API" ,
44
+ },
45
+ "baseurl" : schema.StringAttribute {
46
+ Optional : true ,
47
+ Description : "Base URL to CloudAMQP Customer website" ,
48
+ },
49
+ "enable_faster_instance_destroy" : schema.BoolAttribute {
50
+ Optional : true ,
51
+ Description : "Skips destroying backend resources on 'terraform destroy'" ,
52
+ },
53
+ },
54
+ }
55
+ }
56
+
57
+ func (p * cloudamqpProvider ) Configure (ctx context.Context , request provider.ConfigureRequest , response * provider.ConfigureResponse ) {
58
+ var data cloudamqpProviderModel
59
+
60
+ // Read configuration data into model
61
+ response .Diagnostics .Append (request .Config .Get (ctx , & data )... )
62
+
63
+ apiKey := data .ApiKey .ValueString ()
64
+ baseUrl := data .BaseUrl .ValueString ()
65
+
66
+ // Check configuration data, which should take precedence over
67
+ // environment variable data, if found.
68
+ if apiKey == "" {
69
+ apiKey = os .Getenv ("CLOUDAMQP_APIKEY" )
70
+ }
71
+
72
+ if apiKey == "" {
73
+ response .Diagnostics .AddError (
74
+ "Missing API Key Configuration" ,
75
+ "While configuring the provider, the API key was not found in " +
76
+ "the CLOUDAMQP_APIKEY environment variable or provider " +
77
+ "configuration block apikey attribute." ,
78
+ )
79
+ }
80
+
81
+ if baseUrl == "" {
82
+ baseUrl = os .Getenv ("CLOUDAMQP_BASEURL" )
83
+ }
84
+
85
+ if baseUrl == "" {
86
+ baseUrl = "https://customer.cloudamqp.com"
87
+ }
88
+
89
+ useragent := fmt .Sprintf ("terraform-provider-cloudamqp_v%s" , p .version )
90
+ log .Printf ("[DEBUG] cloudamqp::provider::configure useragent: %v" , useragent )
91
+ apiClient := api .New (baseUrl , apiKey , useragent , p .client )
92
+
93
+ response .ResourceData = apiClient
94
+ response .DataSourceData = apiClient
95
+ }
96
+
97
+ func (p * cloudamqpProvider ) DataSources (_ context.Context ) []func () datasource.DataSource {
98
+ return []func () datasource.DataSource {}
99
+ }
100
+
101
+ func (p * cloudamqpProvider ) Resources (_ context.Context ) []func () resource.Resource {
102
+ return []func () resource.Resource {}
103
+ }
104
+
105
+ func New (version string , client * http.Client ) provider.Provider {
106
+ return & cloudamqpProvider {version , client }
107
+ }
108
+
109
+ func Provider (v string , client * http.Client ) * schemaSdk.Provider {
16
110
version = v
17
111
log .Printf ("Terraform-Provider-CloudAMQP Version: %s" , version )
18
- return & schema .Provider {
19
- Schema : map [string ]* schema .Schema {
112
+ return & schemaSdk .Provider {
113
+ Schema : map [string ]* schemaSdk .Schema {
20
114
"apikey" : {
21
- Type : schema .TypeString ,
115
+ Type : schemaSdk .TypeString ,
22
116
Required : true ,
23
- DefaultFunc : schema .EnvDefaultFunc ("CLOUDAMQP_APIKEY" , nil ),
117
+ DefaultFunc : schemaSdk .EnvDefaultFunc ("CLOUDAMQP_APIKEY" , nil ),
24
118
Description : "Key used to authentication to the CloudAMQP Customer API" ,
25
119
},
26
120
"baseurl" : {
27
- Type : schema .TypeString ,
28
- DefaultFunc : schema .EnvDefaultFunc ("CLOUDAMQP_BASEURL" , "https://customer.cloudamqp.com" ),
121
+ Type : schemaSdk .TypeString ,
122
+ DefaultFunc : schemaSdk .EnvDefaultFunc ("CLOUDAMQP_BASEURL" , "https://customer.cloudamqp.com" ),
29
123
Optional : true ,
30
124
Description : "Base URL to CloudAMQP Customer website" ,
31
125
},
32
126
"enable_faster_instance_destroy" : {
33
- Type : schema .TypeBool ,
34
- DefaultFunc : schema .EnvDefaultFunc ("CLOUDAMQP_ENABLE_FASTER_INSTANCE_DESTROY" , false ),
127
+ Type : schemaSdk .TypeBool ,
128
+ DefaultFunc : schemaSdk .EnvDefaultFunc ("CLOUDAMQP_ENABLE_FASTER_INSTANCE_DESTROY" , false ),
35
129
Optional : true ,
36
130
Description : "Skips destroying backend resources on 'terraform destroy'" ,
37
131
},
38
132
},
39
- DataSourcesMap : map [string ]* schema .Resource {
133
+ DataSourcesMap : map [string ]* schemaSdk .Resource {
40
134
"cloudamqp_account_vpcs" : dataSourceAccountVpcs (),
41
135
"cloudamqp_account" : dataSourceAccount (),
42
136
"cloudamqp_alarm" : dataSourceAlarm (),
@@ -50,7 +144,7 @@ func Provider(v string, client *http.Client) *schema.Provider {
50
144
"cloudamqp_vpc_gcp_info" : dataSourceVpcGcpInfo (),
51
145
"cloudamqp_vpc_info" : dataSourceVpcInfo (),
52
146
},
53
- ResourcesMap : map [string ]* schema .Resource {
147
+ ResourcesMap : map [string ]* schemaSdk .Resource {
54
148
"cloudamqp_account_action" : resourceAccountAction (),
55
149
"cloudamqp_alarm" : resourceAlarm (),
56
150
"cloudamqp_custom_domain" : resourceCustomDomain (),
@@ -78,8 +172,8 @@ func Provider(v string, client *http.Client) *schema.Provider {
78
172
}
79
173
}
80
174
81
- func configureClient (client * http.Client ) schema .ConfigureFunc {
82
- return func (d * schema .ResourceData ) (interface {}, error ) {
175
+ func configureClient (client * http.Client ) schemaSdk .ConfigureFunc {
176
+ return func (d * schemaSdk .ResourceData ) (interface {}, error ) {
83
177
enableFasterInstanceDestroy = d .Get ("enable_faster_instance_destroy" ).(bool )
84
178
useragent := fmt .Sprintf ("terraform-provider-cloudamqp_v%s" , version )
85
179
log .Printf ("[DEBUG] cloudamqp::provider::configure useragent: %v" , useragent )
0 commit comments