Skip to content

Commit

Permalink
fix: tf fails on provider panic (#2)
Browse files Browse the repository at this point in the history
* fix: use Diagnostics to handle errors

* docs: update

* style: go fmt

* docs: bump changelog

* style: changelog lists
  • Loading branch information
agrrh authored Oct 16, 2023
1 parent 5d765ba commit bc3e19a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 46 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

...

## 0.1.1 (October 16, 2023)

FIXES:

- Make provider not ruin whole Terraform run

## 0.1.0 (October 16, 2023)

FEATURES:

* **New Resource:** `ipam_pool`
* **New Resource:** `ipam_allocation`
- **New Resource:** `ipam_pool`
- **New Resource:** `ipam_allocation`
4 changes: 1 addition & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ description: |-

```terraform
provider "ipam" {
alias = "lan"
file = "lan.ipam.json"
file = "default.ipam.json"
}
```

Expand Down
22 changes: 5 additions & 17 deletions docs/resources/allocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,13 @@ An IPAM Allocation represents an sub-ranges from previously defined IP Pool
## Example Usage

```terraform
resource "ipam_allocation" "foo" {
provider = ipam.lan
pool_id = ipam_pool.lan.id
size = 16
resource "ipam_pool" "home" {
cidr = "10.0.0.0/8"
}
resource "ipam_allocation" "bar" {
provider = ipam.lan
pool_id = ipam_pool.lan.id
size = 24
}
resource "ipam_allocation" "baz" {
provider = ipam.lan
pool_id = ipam_pool.lan.id
size = 32
resource "ipam_allocation" "foo" {
pool_id = ipam_pool.home.id
size = 16
}
```

Expand Down
4 changes: 1 addition & 3 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
provider "ipam" {
alias = "lan"

file = "lan.ipam.json"
file = "default.ipam.json"
}
22 changes: 5 additions & 17 deletions examples/resources/ipam_allocation/resource.tf
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
resource "ipam_allocation" "foo" {
provider = ipam.lan

pool_id = ipam_pool.lan.id
size = 16
resource "ipam_pool" "home" {
cidr = "10.0.0.0/8"
}

resource "ipam_allocation" "bar" {
provider = ipam.lan

pool_id = ipam_pool.lan.id
size = 24
}

resource "ipam_allocation" "baz" {
provider = ipam.lan

pool_id = ipam_pool.lan.id
size = 32
resource "ipam_allocation" "foo" {
pool_id = ipam_pool.home.id
size = 16
}
4 changes: 2 additions & 2 deletions internal/provider/allocation_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (r *AllocationResource) Create(ctx context.Context, req resource.CreateRequ
// }
prefix, err := r.ipam.AcquireChildPrefix(ctx, data.PoolId.ValueString(), uint8(data.Size.ValueInt64()))
if err != nil {
panic(err)
resp.Diagnostics.AddError("API Error Creating Resource", fmt.Sprintf("... details ... %s", err))
}

// For the purposes of this example code, hardcoding a response value to
Expand Down Expand Up @@ -203,7 +203,7 @@ func (r *AllocationResource) Delete(ctx context.Context, req resource.DeleteRequ
}
err := r.ipam.ReleaseChildPrefix(ctx, child)
if err != nil {
panic(err)
resp.Diagnostics.AddError("API Error Deleting Resource", fmt.Sprintf("... details ... %s", err))
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/provider/pool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *PoolResource) Create(ctx context.Context, req resource.CreateRequest, r
// }
_, err := r.ipam.NewPrefix(ctx, data.CIDR.ValueString())
if err != nil {
panic(err)
resp.Diagnostics.AddError("API Error Creating Resource", fmt.Sprintf("... details ... %s", err))
}

// For the purposes of this example code, hardcoding a response value to
Expand Down Expand Up @@ -173,7 +173,7 @@ func (r *PoolResource) Delete(ctx context.Context, req resource.DeleteRequest, r
// provider client data and make a call using it.
_, err := r.ipam.DeletePrefix(ctx, data.CIDR.ValueString())
if err != nil {
panic(err)
resp.Diagnostics.AddError("API Error Deleting Resource", fmt.Sprintf("... details ... %s", err))
}
}

Expand Down

0 comments on commit bc3e19a

Please sign in to comment.