Skip to content

Commit

Permalink
Merge pull request terraform-community-providers#3 from terraform-com…
Browse files Browse the repository at this point in the history
…munity-providers/provisioner

Fix autoscaling support
  • Loading branch information
pksunkara authored May 31, 2023
2 parents 499c206 + 892326c commit c93a3f8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
1 change: 1 addition & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Read-Only:

- `host` (String) Host of the endpoint.
- `id` (String) Identifier of the endpoint.
- `provisioner` (String) Provisioner of the endpoint.

## Import

Expand Down
3 changes: 3 additions & 0 deletions internal/provider/client_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Endpoint struct {
RegionId string `json:"region_id"`
AutoscalingLimitMinCu float64 `json:"autoscaling_limit_min_cu"`
AutoscalingLimitMaxCu float64 `json:"autoscaling_limit_max_cu"`
Provisioner string `json:"provisioner"`
CurrentState string `json:"current_state"`
}

Expand Down Expand Up @@ -66,6 +67,7 @@ type ProjectCreateInputProject struct {
Branch ProjectCreateInputProjectBranch `json:"branch"`
AutoscalingLimitMinCu float64 `json:"autoscaling_limit_min_cu"`
AutoscalingLimitMaxCu float64 `json:"autoscaling_limit_max_cu"`
Provisioner string `json:"provisioner"`
}

type ProjectCreateInput struct {
Expand Down Expand Up @@ -115,6 +117,7 @@ type EndpointOutput struct {
type EndpointUpdateInputEndpoint struct {
AutoscalingLimitMinCu float64 `json:"autoscaling_limit_min_cu"`
AutoscalingLimitMaxCu float64 `json:"autoscaling_limit_max_cu"`
Provisioner string `json:"provisioner"`
}

type EndpointUpdateInput struct {
Expand Down
61 changes: 41 additions & 20 deletions internal/provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ type ProjectResource struct {
}

type ProjectResourceBranchEndpointModel struct {
Id types.String `tfsdk:"id"`
Host types.String `tfsdk:"host"`
MinCu types.Float64 `tfsdk:"min_cu"`
MaxCu types.Float64 `tfsdk:"max_cu"`
Id types.String `tfsdk:"id"`
Host types.String `tfsdk:"host"`
MinCu types.Float64 `tfsdk:"min_cu"`
MaxCu types.Float64 `tfsdk:"max_cu"`
Provisioner types.String `tfsdk:"provisioner"`
}

var branchEndpointAttrTypes = map[string]attr.Type{
"id": types.StringType,
"host": types.StringType,
"min_cu": types.Float64Type,
"max_cu": types.Float64Type,
"id": types.StringType,
"host": types.StringType,
"min_cu": types.Float64Type,
"max_cu": types.Float64Type,
"provisioner": types.StringType,
}

type ProjectResourceBranchModel struct {
Expand Down Expand Up @@ -193,6 +195,10 @@ func (r *ProjectResource) Schema(ctx context.Context, req resource.SchemaRequest
float64validator.OneOf(0.25, 0.5, 1, 2, 3, 4, 5, 6, 7),
},
},
"provisioner": schema.StringAttribute{
MarkdownDescription: "Provisioner of the endpoint.",
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -260,6 +266,12 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest
input.Project.AutoscalingLimitMinCu = branchEndpointData.MinCu.ValueFloat64()
input.Project.AutoscalingLimitMaxCu = branchEndpointData.MaxCu.ValueFloat64()

if input.Project.AutoscalingLimitMinCu == input.Project.AutoscalingLimitMaxCu {
input.Project.Provisioner = "k8s-pod"
} else {
input.Project.Provisioner = "k8s-neonvm"
}

var project ProjectCreateOutput

err := call(r.client, http.MethodPost, "/projects", input, &project)
Expand Down Expand Up @@ -301,10 +313,11 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest
"endpoint": types.ObjectValueMust(
branchEndpointAttrTypes,
map[string]attr.Value{
"id": types.StringValue(project.Endpoints[0].Id),
"host": types.StringValue(project.Endpoints[0].Host),
"min_cu": types.Float64Value(project.Endpoints[0].AutoscalingLimitMinCu),
"max_cu": types.Float64Value(project.Endpoints[0].AutoscalingLimitMaxCu),
"id": types.StringValue(project.Endpoints[0].Id),
"host": types.StringValue(project.Endpoints[0].Host),
"min_cu": types.Float64Value(project.Endpoints[0].AutoscalingLimitMinCu),
"max_cu": types.Float64Value(project.Endpoints[0].AutoscalingLimitMaxCu),
"provisioner": types.StringValue(project.Endpoints[0].Provisioner),
},
),
},
Expand Down Expand Up @@ -361,10 +374,11 @@ func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, re
"endpoint": types.ObjectValueMust(
branchEndpointAttrTypes,
map[string]attr.Value{
"id": types.StringValue(endpoint.Id),
"host": types.StringValue(endpoint.Host),
"min_cu": types.Float64Value(endpoint.AutoscalingLimitMinCu),
"max_cu": types.Float64Value(endpoint.AutoscalingLimitMaxCu),
"id": types.StringValue(endpoint.Id),
"host": types.StringValue(endpoint.Host),
"min_cu": types.Float64Value(endpoint.AutoscalingLimitMinCu),
"max_cu": types.Float64Value(endpoint.AutoscalingLimitMaxCu),
"provisioner": types.StringValue(endpoint.Provisioner),
},
),
},
Expand Down Expand Up @@ -446,6 +460,12 @@ func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest
},
}

if endpointInput.Endpoint.AutoscalingLimitMinCu == endpointInput.Endpoint.AutoscalingLimitMaxCu {
endpointInput.Endpoint.Provisioner = "k8s-pod"
} else {
endpointInput.Endpoint.Provisioner = "k8s-neonvm"
}

endpoint, err := endpointUpdate(r.client, data.Id.ValueString(), branchEndpointData.Id.ValueString(), endpointInput)

if err != nil {
Expand All @@ -469,10 +489,11 @@ func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest
"endpoint": types.ObjectValueMust(
branchEndpointAttrTypes,
map[string]attr.Value{
"id": types.StringValue(endpoint.Endpoint.Id),
"host": types.StringValue(endpoint.Endpoint.Host),
"min_cu": types.Float64Value(endpoint.Endpoint.AutoscalingLimitMinCu),
"max_cu": types.Float64Value(endpoint.Endpoint.AutoscalingLimitMaxCu),
"id": types.StringValue(endpoint.Endpoint.Id),
"host": types.StringValue(endpoint.Endpoint.Host),
"min_cu": types.Float64Value(endpoint.Endpoint.AutoscalingLimitMinCu),
"max_cu": types.Float64Value(endpoint.Endpoint.AutoscalingLimitMaxCu),
"provisioner": types.StringValue(endpoint.Endpoint.Provisioner),
},
),
},
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestAccProjectResourceDefault(t *testing.T) {
resource.TestMatchResourceAttr("neon_project.test", "branch.endpoint.host", hostRegex("us-east-2")),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.min_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.max_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.provisioner", "k8s-pod"),
),
},
// ImportState testing
Expand All @@ -55,6 +56,7 @@ func TestAccProjectResourceDefault(t *testing.T) {
resource.TestMatchResourceAttr("neon_project.test", "branch.endpoint.host", hostRegex("us-east-2")),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.min_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.max_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.provisioner", "k8s-pod"),
),
},
// Update just name
Expand All @@ -72,6 +74,7 @@ func TestAccProjectResourceDefault(t *testing.T) {
resource.TestMatchResourceAttr("neon_project.test", "branch.endpoint.host", hostRegex("us-east-2")),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.min_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.max_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.provisioner", "k8s-pod"),
),
},
// Update and Read testing
Expand All @@ -89,6 +92,7 @@ func TestAccProjectResourceDefault(t *testing.T) {
resource.TestMatchResourceAttr("neon_project.test", "branch.endpoint.host", hostRegex("us-east-2")),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.min_cu", "1"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.max_cu", "2"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.provisioner", "k8s-neonvm"),
),
},
// ImportState testing
Expand Down Expand Up @@ -122,6 +126,7 @@ func TestAccProjectResourceNonDefault(t *testing.T) {
resource.TestMatchResourceAttr("neon_project.test", "branch.endpoint.host", hostRegex("us-east-2")),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.min_cu", "1"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.max_cu", "2"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.provisioner", "k8s-neonvm"),
),
},
// ImportState testing
Expand All @@ -145,6 +150,7 @@ func TestAccProjectResourceNonDefault(t *testing.T) {
resource.TestMatchResourceAttr("neon_project.test", "branch.endpoint.host", hostRegex("us-east-2")),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.min_cu", "1"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.max_cu", "2"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.provisioner", "k8s-neonvm"),
),
},
// Update with null values
Expand All @@ -162,6 +168,7 @@ func TestAccProjectResourceNonDefault(t *testing.T) {
resource.TestMatchResourceAttr("neon_project.test", "branch.endpoint.host", hostRegex("us-east-2")),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.min_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.max_cu", "0.25"),
resource.TestCheckResourceAttr("neon_project.test", "branch.endpoint.provisioner", "k8s-pod"),
),
},
// ImportState testing
Expand Down

0 comments on commit c93a3f8

Please sign in to comment.