Skip to content

Commit

Permalink
Update state based on resources that have been remotely deleted (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
hesto2 authored Aug 5, 2024
1 parent 781f527 commit 19dc8ad
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 18 deletions.
42 changes: 33 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,39 @@
- [Terraform](https://www.terraform.io/downloads.html) 1.0.1
- [Go](https://golang.org/doc/install) 1.18 (to build the provider plugin)

## Using the provider

Add the jupiterone provider to your project's terraform:

```hcl
terraform {
required_providers {
jupiterone = {
source = "JupiterOne/jupiterone"
version = "x.x.x" # Replace with desired version
}
}
}
provider "jupiterone" {
# Configuration options
account_id = "xxxxx"
api_key = "xxxx"
region = "us"
}
```

## Example Usage

See the `examples` directory
See the [examples](./examples) directory

## Building The Provider

1. Install [Go](https://go.dev/doc/install) and `make`
1. Clone the repository
2. Enter the repository directory
3. Build the provider with `make build` or invoke `go install` directly.
1. Enter the repository directory
1. Build the provider with `make build` or invoke `go install` directly.
1. Install the provider locally as referenced [here](#using-development-environment-provider-locally)

## Adding Dependencies

Expand All @@ -27,10 +51,6 @@ go get github.com/author/dependency
go mod tidy
```

## Using the provider

If you're building the provider, follow the instructions to [install it as a plugin.](https://www.terraform.io/docs/plugins/basics.html#installing-a-plugin) After placing it into your plugins directory, run `terraform init` to initialize it.

## Developing the Provider

If this is your first time developing in go, or developing a terraform provider, it may be wise to do some of the [GO Terraform Plugin Framework tutorial](https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-provider). This is what is used to build this provider.
Expand Down Expand Up @@ -217,7 +237,11 @@ func (r *J1EntityResource) Read(ctx context.Context, req resource.ReadRequest, r
// See other resource.go files for examples
entity, err := client.GetJ1Entity(ctx, r.qlient, data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to get j1 entity", err.Error())
if strings.Contains(err.Error(), "not found") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to get entity", err.Error())
}
return
}

Expand Down Expand Up @@ -358,7 +382,7 @@ export TF_LOG=DEBUG
make testacc
```

### Using development environment provider locally
## Using development environment provider locally

In order to check changes you made locally to the provider, you can use the binary you just compiled by adding the following
to your `~/.terraformrc` file. This is valid for Terraform 0.14+. Please see
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A JupiterOne insights dashboard.
## Example Usage

```terraform
resource "jupiterone_dashboard" "compliance_dashboard" {
resource "jupiterone_dashboard" "compliance" {
name = "Compliance Dashboard"
type = "Account"
}
Expand Down
7 changes: 6 additions & 1 deletion jupiterone/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jupiterone
import (
"context"
"fmt"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -133,7 +134,11 @@ func (r *DashboardResource) Read(ctx context.Context, req resource.ReadRequest,

dashboard, err := client.GetDashboard(ctx, r.qlient, data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to get dashboard", err.Error())
if strings.Contains(err.Error(), "not found") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to get dashboard", err.Error())
}
return
}

Expand Down
9 changes: 8 additions & 1 deletion jupiterone/resource_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -220,7 +221,13 @@ func (r *ComplianceFrameworkResource) Read(ctx context.Context, req resource.Rea

var f client.GetComplianceFrameworkByIdComplianceFramework
if r, err := client.GetComplianceFrameworkById(ctx, r.qlient, data.Id.ValueString()); err != nil {
resp.Diagnostics.AddError("failed to find framework", err.Error())
if err != nil {
if strings.Contains(err.Error(), "Could not find") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to find framework", err.Error())
}
}
return
} else {
f = r.ComplianceFramework
Expand Down
9 changes: 8 additions & 1 deletion jupiterone/resource_frameworkitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jupiterone
import (
"context"
"fmt"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -182,7 +183,13 @@ func (r *ComplianceFrameworkItemResource) Read(ctx context.Context, req resource

var i client.GetComplianceFrameworkItemByIdComplianceFrameworkItem
if r, err := client.GetComplianceFrameworkItemById(ctx, r.qlient, data.Id.ValueString()); err != nil {
resp.Diagnostics.AddError("failed to find framework item", err.Error())
if err != nil {
if strings.Contains(err.Error(), "Could not find") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to find framework item", err.Error())
}
}
return
} else {
i = r.ComplianceFrameworkItem
Expand Down
9 changes: 8 additions & 1 deletion jupiterone/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -174,7 +175,13 @@ func (r *ComplianceGroupResource) Read(ctx context.Context, req resource.ReadReq
group, err := getGroup(ctx, r.qlient, data.FrameworkId.ValueString(), data.Id.ValueString())

if err != nil {
resp.Diagnostics.AddError("failed to find group", err.Error())
if err != nil {
if strings.Contains(err.Error(), "Could not find") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to find group", err.Error())
}
}
return
}

Expand Down
9 changes: 8 additions & 1 deletion jupiterone/resource_libraryitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jupiterone
import (
"context"
"fmt"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -174,7 +175,13 @@ func (r *ComplianceLibraryItemResource) Read(ctx context.Context, req resource.R

var i client.GetComplianceLibraryItemByIdComplianceLibraryItem
if r, err := client.GetComplianceLibraryItemById(ctx, r.qlient, data.Id.ValueString()); err != nil {
resp.Diagnostics.AddError("failed to find library item", err.Error())
if err != nil {
if strings.Contains(err.Error(), "Could not find") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to find library item", err.Error())
}
}
return
} else {
i = r.ComplianceLibraryItem
Expand Down
7 changes: 6 additions & 1 deletion jupiterone/resource_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand Down Expand Up @@ -444,7 +445,11 @@ func (r *QuestionRuleResource) Read(ctx context.Context, req resource.ReadReques

getResp, err := client.GetQuestionRuleInstance(ctx, r.qlient, oldData.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to get rule", err.Error())
if strings.Contains(err.Error(), "does not exist") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to get rule", err.Error())
}
return
}
rule := getResp.QuestionRuleInstance
Expand Down
7 changes: 6 additions & 1 deletion jupiterone/resource_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -171,7 +172,11 @@ func (r *UserGroupResource) Read(ctx context.Context, req resource.ReadRequest,

group, err := client.GetUserGroup(ctx, r.qlient, data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to get user group", err.Error())
if strings.Contains(err.Error(), "does not exist") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to get user group", err.Error())
}
return
}

Expand Down
7 changes: 6 additions & 1 deletion jupiterone/resource_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -150,7 +151,11 @@ func (r *WidgetResource) Read(ctx context.Context, req resource.ReadRequest, res
// Fetch the widget data from the API
response, err := client.GetWidget(ctx, r.qlient, data.DashboardId.ValueString(), "Account", data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to get widget", err.Error())
if strings.Contains(err.Error(), "not found") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("failed to get widget", err.Error())
}
return
}

Expand Down

0 comments on commit 19dc8ad

Please sign in to comment.