Skip to content

Commit

Permalink
cluster improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Nov 24, 2023
1 parent bb3249e commit 2e6a6cc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
20 changes: 2 additions & 18 deletions internal/model/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@ type Cluster struct {
Bindings *ClusterBindings `tfsdk:"bindings"`
}

func (c *Cluster) CloudSettingsAttributes() *console.CloudSettingsAttributes {
if IsCloud(c.Cloud.ValueString(), CloudAWS) {
return &console.CloudSettingsAttributes{Aws: c.CloudSettings.AWS.Attributes()}
}

if IsCloud(c.Cloud.ValueString(), CloudAzure) {
return &console.CloudSettingsAttributes{Azure: c.CloudSettings.Azure.Attributes()}
}

if IsCloud(c.Cloud.ValueString(), CloudGCP) {
return &console.CloudSettingsAttributes{Gcp: c.CloudSettings.GCP.Attributes()}
}

return nil
}

type ClusterNodePool struct {
Name types.String `tfsdk:"name"`
MinSize types.Int64 `tfsdk:"min_size"`
Expand Down Expand Up @@ -73,14 +57,14 @@ func (c *Cluster) TagsAttribute(ctx context.Context, d diag.Diagnostics) (result
return
}

func (c *Cluster) CreateAttributes(ctx context.Context, d diag.Diagnostics) console.ClusterAttributes {
func (c *Cluster) Attributes(ctx context.Context, d diag.Diagnostics) console.ClusterAttributes {
return console.ClusterAttributes{
Name: c.Name.ValueString(),
Handle: c.Handle.ValueStringPointer(),
ProviderID: c.ProviderId.ValueStringPointer(),
Version: c.Version.ValueStringPointer(),
Protect: c.Protect.ValueBoolPointer(),
CloudSettings: c.CloudSettingsAttributes(),
CloudSettings: c.CloudSettings.Attributes(),
NodePools: c.NodePoolsAttribute(),
ReadBindings: c.Bindings.ReadAttributes(),
WriteBindings: c.Bindings.WriteAttributes(),
Expand Down
20 changes: 20 additions & 0 deletions internal/model/cluster_cloud_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ type ClusterCloudSettings struct {
BYOK *ClusterCloudSettingsBYOK `tfsdk:"byok"`
}

func (c *ClusterCloudSettings) Attributes() *console.CloudSettingsAttributes {
if c == nil {
return nil
}

if c.AWS != nil {
return &console.CloudSettingsAttributes{Aws: c.AWS.Attributes()}
}

if c.Azure != nil {
return &console.CloudSettingsAttributes{Azure: c.Azure.Attributes()}
}

if c.GCP != nil {
return &console.CloudSettingsAttributes{Gcp: c.GCP.Attributes()}
}

return nil
}

type ClusterCloudSettingsAWS struct {
Region types.String `tfsdk:"region"`
}
Expand Down
4 changes: 4 additions & 0 deletions internal/model/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type ProviderCloudSettings struct {
}

func (p *ProviderCloudSettings) Attributes() *console.CloudProviderSettingsAttributes {
if p == nil {
return nil
}

if p.GCP != nil {
return &console.CloudProviderSettingsAttributes{Aws: p.AWS.Attributes()}
}
Expand Down
50 changes: 26 additions & 24 deletions internal/resource/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Computed: true,
},
"version": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Description: "Desired Kubernetes version for this cluster. Leave empty for bring your own cluster.",
MarkdownDescription: "Desired Kubernetes version for this cluster. Leave empty for bring your own cluster.",
Optional: true,
Computed: true,
},
"provider_id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Description: "Provider used to create this cluster. Leave empty for bring your own cluster.",
MarkdownDescription: "Provider used to create this cluster. Leave empty for bring your own cluster.",
Optional: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
Expand All @@ -103,38 +103,40 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
PlanModifiers: []planmodifier.Object{objectplanmodifier.RequiresReplace()},
},
"node_pools": schema.ListNestedAttribute{
Description: "",
MarkdownDescription: "",
Description: "List of node pool specs managed by this cluster. Leave empty for bring your own cluster.",
MarkdownDescription: "List of node pool specs managed by this cluster. Leave empty for bring your own cluster.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Description: "Node pool name. Must be unique.",
MarkdownDescription: "Node pool name. Must be unique.",
Required: true,
},
"min_size": schema.Int64Attribute{
Description: "",
MarkdownDescription: "",
Description: "Minimum number of instances in this node pool.",
MarkdownDescription: "Minimum number of instances in this node pool.",
Required: true,
},
"max_size": schema.Int64Attribute{
Description: "",
MarkdownDescription: "",
Description: "Maximum number of instances in this node pool.",
MarkdownDescription: "Maximum number of instances in this node pool.",
Required: true,
},
"instance_type": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Description: "The type of node to use. Usually cloud-specific.",
MarkdownDescription: "The type of node to use. Usually cloud-specific.",
Required: true,
},
"labels": schema.MapAttribute{
ElementType: types.StringType,
Optional: true,
Description: "Kubernetes labels to apply to the nodes in this pool. Useful for node selectors.",
MarkdownDescription: "Kubernetes labels to apply to the nodes in this pool. Useful for node selectors.",
ElementType: types.StringType,
Optional: true,
},
"taints": schema.ListNestedAttribute{
Description: "",
MarkdownDescription: "",
Description: "Any taints you'd want to apply to a node, i.e. for preventing scheduling on spot instances.",
MarkdownDescription: "Any taints you'd want to apply to a node, i.e. for preventing scheduling on spot instances.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
Expand All @@ -159,13 +161,13 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Optional: true,
Attributes: map[string]schema.Attribute{
"aws": schema.SingleNestedAttribute{
Description: "",
MarkdownDescription: "",
Description: "AWS node pool customizations.",
MarkdownDescription: "AWS node pool customizations.",
Optional: true,
Attributes: map[string]schema.Attribute{
"launch_template_id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Description: "Custom launch template for your nodes. Useful for Golden AMI setups.",
MarkdownDescription: "Custom launch template for your nodes. Useful for Golden AMI setups.",
Optional: true,
},
},
Expand All @@ -177,7 +179,7 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
},
"protect": schema.BoolAttribute{
Description: "If set to `true` then this cluster cannot be deleted.",
Description: "If set to \"true\" then this cluster cannot be deleted.",
MarkdownDescription: "If set to `true` then this cluster cannot be deleted.",
Optional: true,
Computed: true,
Expand Down Expand Up @@ -275,7 +277,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
return
}

result, err := r.client.CreateCluster(ctx, data.CreateAttributes(ctx, resp.Diagnostics))
result, err := r.client.CreateCluster(ctx, data.Attributes(ctx, resp.Diagnostics))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create cluster, got error: %s", err))
return
Expand Down

0 comments on commit 2e6a6cc

Please sign in to comment.