Skip to content

Commit bb3249e

Browse files
committed
update error handling and provider model
1 parent 531888c commit bb3249e

File tree

3 files changed

+66
-52
lines changed

3 files changed

+66
-52
lines changed

internal/model/provider.go

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,85 @@ type Provider struct {
1515
CloudSettings ProviderCloudSettings `tfsdk:"cloud_settings"`
1616
}
1717

18+
func (p *Provider) Attributes() console.ClusterProviderAttributes {
19+
return console.ClusterProviderAttributes{
20+
Name: p.Name.ValueString(),
21+
Namespace: p.Namespace.ValueStringPointer(),
22+
Cloud: p.Cloud.ValueStringPointer(),
23+
CloudSettings: p.CloudSettings.Attributes(),
24+
}
25+
}
26+
27+
func (p *Provider) UpdateAttributes() console.ClusterProviderUpdateAttributes {
28+
return console.ClusterProviderUpdateAttributes{
29+
CloudSettings: p.CloudSettings.Attributes(),
30+
}
31+
}
32+
33+
func (p *Provider) From(cp *console.ClusterProviderFragment) {
34+
p.Id = types.StringValue(cp.ID)
35+
p.Name = types.StringValue(cp.Name)
36+
p.Namespace = types.StringValue(cp.Namespace)
37+
p.Editable = types.BoolPointerValue(cp.Editable)
38+
p.Cloud = types.StringValue(cp.Cloud)
39+
}
40+
1841
type ProviderCloudSettings struct {
1942
AWS *ProviderCloudSettingsAWS `tfsdk:"aws"`
2043
Azure *ProviderCloudSettingsAzure `tfsdk:"azure"`
2144
GCP *ProviderCloudSettingsGCP `tfsdk:"gcp"`
2245
}
2346

47+
func (p *ProviderCloudSettings) Attributes() *console.CloudProviderSettingsAttributes {
48+
if p.GCP != nil {
49+
return &console.CloudProviderSettingsAttributes{Aws: p.AWS.Attributes()}
50+
}
51+
52+
if p.Azure != nil {
53+
return &console.CloudProviderSettingsAttributes{Azure: p.Azure.Attributes()}
54+
}
55+
56+
if p.GCP != nil {
57+
return &console.CloudProviderSettingsAttributes{Gcp: p.GCP.Attributes()}
58+
}
59+
60+
return nil
61+
}
62+
2463
type ProviderCloudSettingsAWS struct {
2564
AccessKeyId types.String `tfsdk:"access_key_id"`
2665
SecretAccessKey types.String `tfsdk:"secret_access_key"`
2766
}
2867

68+
func (p *ProviderCloudSettingsAWS) Attributes() *console.AwsSettingsAttributes {
69+
return &console.AwsSettingsAttributes{
70+
AccessKeyID: p.AccessKeyId.ValueString(),
71+
SecretAccessKey: p.SecretAccessKey.ValueString(),
72+
}
73+
}
74+
2975
type ProviderCloudSettingsAzure struct {
3076
SubscriptionId types.String `tfsdk:"subscription_id"`
3177
TenantId types.String `tfsdk:"tenant_id"`
3278
ClientId types.String `tfsdk:"client_id"`
3379
ClientSecret types.String `tfsdk:"client_secret"`
3480
}
3581

36-
type ProviderCloudSettingsGCP struct {
37-
Credentials types.String `tfsdk:"credentials"`
38-
}
39-
40-
func (p *Provider) CloudProviderSettingsAttributes() *console.CloudProviderSettingsAttributes {
41-
if IsCloud(p.Cloud.ValueString(), CloudAWS) {
42-
return &console.CloudProviderSettingsAttributes{
43-
Aws: &console.AwsSettingsAttributes{
44-
AccessKeyID: p.CloudSettings.AWS.AccessKeyId.ValueString(),
45-
SecretAccessKey: p.CloudSettings.AWS.SecretAccessKey.ValueString(),
46-
},
47-
}
48-
}
49-
50-
if IsCloud(p.Cloud.ValueString(), CloudAzure) {
51-
return &console.CloudProviderSettingsAttributes{
52-
Azure: &console.AzureSettingsAttributes{
53-
SubscriptionID: p.CloudSettings.Azure.SubscriptionId.ValueString(),
54-
TenantID: p.CloudSettings.Azure.TenantId.ValueString(),
55-
ClientID: p.CloudSettings.Azure.ClientId.ValueString(),
56-
ClientSecret: p.CloudSettings.Azure.ClientSecret.ValueString(),
57-
},
58-
}
82+
func (p *ProviderCloudSettingsAzure) Attributes() *console.AzureSettingsAttributes {
83+
return &console.AzureSettingsAttributes{
84+
SubscriptionID: p.SubscriptionId.ValueString(),
85+
TenantID: p.TenantId.ValueString(),
86+
ClientID: p.ClientId.ValueString(),
87+
ClientSecret: p.ClientSecret.ValueString(),
5988
}
60-
61-
if IsCloud(p.Cloud.ValueString(), CloudGCP) {
62-
return &console.CloudProviderSettingsAttributes{
63-
Gcp: &console.GcpSettingsAttributes{
64-
ApplicationCredentials: p.CloudSettings.GCP.Credentials.ValueString(),
65-
},
66-
}
67-
}
68-
69-
return nil
7089
}
7190

72-
func (p *Provider) CreateAttributes() console.ClusterProviderAttributes {
73-
return console.ClusterProviderAttributes{
74-
Name: p.Name.ValueString(),
75-
Namespace: p.Namespace.ValueStringPointer(),
76-
Cloud: p.Cloud.ValueStringPointer(),
77-
CloudSettings: p.CloudProviderSettingsAttributes(),
78-
}
91+
type ProviderCloudSettingsGCP struct {
92+
Credentials types.String `tfsdk:"credentials"`
7993
}
8094

81-
func (p *Provider) UpdateAttributes() console.ClusterProviderUpdateAttributes {
82-
return console.ClusterProviderUpdateAttributes{
83-
CloudSettings: p.CloudProviderSettingsAttributes(),
95+
func (p *ProviderCloudSettingsGCP) Attributes() *console.GcpSettingsAttributes {
96+
return &console.GcpSettingsAttributes{
97+
ApplicationCredentials: p.Credentials.ValueString(),
8498
}
8599
}
86-
87-
func (p *Provider) From(cp *console.ClusterProviderFragment) {
88-
p.Id = types.StringValue(cp.ID)
89-
p.Name = types.StringValue(cp.Name)
90-
p.Namespace = types.StringValue(cp.Namespace)
91-
p.Editable = types.BoolPointerValue(cp.Editable)
92-
p.Cloud = types.StringValue(cp.Cloud)
93-
}

internal/resource/cluster/cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ func (r *clusterResource) Read(ctx context.Context, req resource.ReadRequest, re
316316
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read cluster, got error: %s", err))
317317
return
318318
}
319+
if result == nil || result.Cluster == nil {
320+
resp.Diagnostics.AddError("Not Found", fmt.Sprintf("Unable to find cluster, it looks like it was deleted manually"))
321+
return
322+
}
319323

320324
data.From(result.Cluster, resp.Diagnostics)
321325
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

internal/resource/provider.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (r *providerResource) Create(ctx context.Context, req resource.CreateReques
182182
return
183183
}
184184

185-
result, err := r.client.CreateClusterProvider(ctx, data.CreateAttributes())
185+
result, err := r.client.CreateClusterProvider(ctx, data.Attributes())
186186
if err != nil {
187187
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create provider, got error: %s", err))
188188
return
@@ -204,6 +204,10 @@ func (r *providerResource) Read(ctx context.Context, req resource.ReadRequest, r
204204
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read provider, got error: %s", err))
205205
return
206206
}
207+
if result == nil || result.ClusterProvider == nil {
208+
resp.Diagnostics.AddError("Not Found", fmt.Sprintf("Unable to find provider, it looks like it was deleted manually"))
209+
return
210+
}
207211

208212
data.From(result.ClusterProvider)
209213
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

0 commit comments

Comments
 (0)