Skip to content

Commit

Permalink
Add support for neon_branch
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jul 15, 2023
1 parent eeb6d27 commit 68b036a
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 11 deletions.
45 changes: 45 additions & 0 deletions docs/resources/branch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "neon_branch Resource - terraform-provider-neon"
subcategory: ""
description: |-
Neon branch.
---

# neon_branch (Resource)

Neon branch.

## Example Usage

```terraform
resource "neon_branch" "example" {
name = "analytics"
branch_id = neon_project.example.branch.id
project_id = neon_project.example.id
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the branch.
- `project_id` (String) Project the branch belongs to.

### Optional

- `parent_id` (String) ID of the parent branch. Defaults to the primary branch.

### Read-Only

- `id` (String) ID of the branch.

## Import

Import is supported using the following syntax:

```shell
terraform import neon_branch.example silent-wood-306223:br-mute-rain-788791
```
1 change: 1 addition & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Optional:

- `max_cu` (Number) Maximum number of compute units for the endpoint. **Default** `0.25`.
- `min_cu` (Number) Minimum number of compute units for the endpoint. **Default** `0.25`.
- `suspend_timeout` (Number) Suspend timeout of the endpoint. **Default** `300`.

Read-Only:

Expand Down
1 change: 1 addition & 0 deletions examples/resources/neon_branch/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import neon_branch.example silent-wood-306223:br-mute-rain-788791
5 changes: 5 additions & 0 deletions examples/resources/neon_branch/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "neon_branch" "example" {
name = "analytics"
branch_id = neon_project.example.branch.id
project_id = neon_project.example.id
}
40 changes: 35 additions & 5 deletions internal/provider/client_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func branchList(client *http.Client, projectId string) (BranchListOutput, error)
}

func branchEndpoint(client *http.Client, projectId string, branchId string) (Endpoint, error) {
endpoints, err := endpointList(client, projectId)
endpoints, err := branchEndpointList(client, projectId, branchId)

var endpoint Endpoint

Expand All @@ -44,9 +44,13 @@ func branchEndpoint(client *http.Client, projectId string, branchId string) (End
}

endpointIdx := slices.IndexFunc(endpoints.Endpoints, func(endpoint Endpoint) bool {
return endpoint.BranchId == branchId
return endpoint.Type == "read_write"
})

if endpointIdx == -1 {
return endpoint, fmt.Errorf("no read_write endpoint found for branch %s", branchId)
}

return endpoints.Endpoints[endpointIdx], nil
}

Expand All @@ -66,6 +70,20 @@ func branchGet(client *http.Client, projectId string, branchId string) (BranchOu
return branch, nil
}

func branchCreate(client *http.Client, projectId string, input BranchCreateInput) (BranchOutput, error) {
var branch BranchOutput

err := projectWait(client, projectId)

if err != nil {
return branch, err
}

err = call(client, http.MethodPost, fmt.Sprintf("/projects/%s/branches", projectId), input, &branch)

return branch, err
}

func branchUpdate(client *http.Client, projectId string, branchId string, input BranchUpdateInput) (BranchOutput, error) {
var branch BranchOutput

Expand All @@ -74,10 +92,22 @@ func branchUpdate(client *http.Client, projectId string, branchId string, input
return branch, err
}

func endpointList(client *http.Client, projectId string) (EndpointListOutput, error) {
var endpoints EndpointListOutput
func branchDelete(client *http.Client, projectId string, branchId string) error {
err := projectWait(client, projectId)

if err != nil {
return err
}

_, err = delete(client, fmt.Sprintf("/projects/%s/branches/%s", projectId, branchId))

return err
}

func branchEndpointList(client *http.Client, projectId string, branchId string) (BranchEndpointListOutput, error) {
var endpoints BranchEndpointListOutput

err := get(client, fmt.Sprintf("/projects/%s/endpoints", projectId), &endpoints)
err := get(client, fmt.Sprintf("/projects/%s/branches/%s/endpoints", projectId, branchId), &endpoints)

return endpoints, err
}
Expand Down
23 changes: 17 additions & 6 deletions internal/provider/client_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ type Project struct {
}

type Branch struct {
Id string `json:"id"`
ProjectId string `json:"project_id"`
Name string `json:"name"`
Primary bool `json:"primary"`
CurrentState string `json:"current_state"`
Id string `json:"id"`
ProjectId string `json:"project_id"`
ParentId *string `json:"parent_id"`
Name string `json:"name"`
Primary bool `json:"primary"`
CurrentState string `json:"current_state"`
}

type Role struct {
Expand All @@ -40,6 +41,7 @@ type Endpoint struct {
AutoscalingLimitMaxCu float64 `json:"autoscaling_limit_max_cu"`
Provisioner string `json:"provisioner"`
SuspendTimeoutSeconds int64 `json:"suspend_timeout_seconds"`
Type string `json:"type"`
CurrentState string `json:"current_state"`
}

Expand Down Expand Up @@ -104,6 +106,15 @@ type BranchOutput struct {
Branch Branch `json:"branch"`
}

type BranchCreateInputBranch struct {
Name string `json:"name"`
ParentId string `json:"parent_id,omitempty"`
}

type BranchCreateInput struct {
Branch BranchCreateInputBranch `json:"branch"`
}

type BranchUpdateInputBranch struct {
Name string `json:"name"`
}
Expand All @@ -112,7 +123,7 @@ type BranchUpdateInput struct {
Branch BranchUpdateInputBranch `json:"branch"`
}

type EndpointListOutput struct {
type BranchEndpointListOutput struct {
Endpoints []Endpoint `json:"endpoints"`
}

Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (p *NeonProvider) Resources(ctx context.Context) []func() resource.Resource
NewProjectResource,
NewRoleResource,
NewDatabaseResource,
NewBranchResource,
}
}

Expand Down
Loading

0 comments on commit 68b036a

Please sign in to comment.