Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate GrantKit before running terraform plan or apply #44

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"mockAcceptance": true,
"providerGoModuleName": "abbey/v2",
"providerName": "abbey",
"providerVersion": "0.2.8",
"providerVersion": "0.2.9",
"githubRepoName": "terraform-provider-abbey"
}
},
Expand Down Expand Up @@ -78,7 +78,7 @@
},
"providerGoModuleName": "abbey/v2",
"providerName": "abbey",
"providerVersion": "0.2.8",
"providerVersion": "0.2.9",
"mockAcceptance": true,
"hideComputedDiff": false
},
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# abbey Terraform Provider 0.2.8
# abbey Terraform Provider 0.2.9

The public Abbey API. Used for integrating with Abbey and building interfaces to extend the Abbey platform. See https://docs.abbey.io for more information.
This repository contains a Terraform provider that allows you to manage resources through the abbey API.
Expand Down Expand Up @@ -40,11 +40,11 @@ go build -o terraform-provider-abbey
5. Move the provider to your plugins directory:

```bash
mkdir -p ~/.terraform.d/plugins/example.com/user/abbey/0.2.8/<distribution>
mv terraform-provider-abbey ~/.terraform.d/plugins/example.com/user/abbey/0.2.8/<distribution>
mkdir -p ~/.terraform.d/plugins/example.com/user/abbey/0.2.9/<distribution>
mv terraform-provider-abbey ~/.terraform.d/plugins/example.com/user/abbey/0.2.9/<distribution>
```

Note: The directory structure is important. The provider must be located at `~/.terraform.d/plugins/example.com/user/abbey/0.2.8/<distribution>/terraform-provider-abbey`
Note: The directory structure is important. The provider must be located at `~/.terraform.d/plugins/example.com/user/abbey/0.2.9/<distribution>/terraform-provider-abbey`
Also please change `example.com/user` and `<distribution>` to match your real values.
To get the <distribution> run `terraform version`, possible values: `linux_amd64`, `darwin_arm64`, `windows_amd64`, etc.

Expand Down Expand Up @@ -120,7 +120,7 @@ make acceptance-test
1. Tag your release:

```bash
git tag v0.2.8
git tag v0.2.9
git push --tags
```

Expand Down
2 changes: 1 addition & 1 deletion examples/provider/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
abbey = {
source = "hashicorp.com/edu/abbey"
version = "0.2.8"
version = "0.2.9"
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions internal/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,60 @@ Or at a later stage:

#### ListGrantKits

List Grant Kits

#### CreateGrantKit

Create a Grant Kit

#### ValidateGrantKit

Validate a Grant Kit

#### GetGrantKitById

Retrieve a Grant Kit by ID

#### UpdateGrantKit

Update a Grant Kit

#### DeleteGrantKit

Delete a Grant Kit

### IdentitiesService

#### ListEnrichedIdentities

List all Identities with enriched metadata

#### CreateIdentity

Create an Identity

#### GetIdentity

Retrieve an Identity

#### UpdateIdentity

Update an Identity

#### DeleteIdentity

Delete an Identity

### DemoService

#### GetDemo

Get Demo

#### CreateDemo

Create Demo Access

#### DeleteDemo

Delete Demo Access
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type DefaultHeadersHandler[T any] struct {

func NewDefaultHeadersHandler[T any]() *DefaultHeadersHandler[T] {
defaultHeaders := map[string]string{
"User-Agent": "liblab/2.0.18 go/1.18",
"User-Agent": "liblab/2.0.20 go/1.18",
"Content-type": "application/json",
}

Expand Down
2 changes: 1 addition & 1 deletion internal/client/pkg/clientconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Config struct {
}

func NewConfig() Config {
baseUrl := "https://api.abbey.io/v1"
baseUrl := DEFAULT_ENVIRONMENT
newConfig := Config{
BaseUrl: &baseUrl,
}
Expand Down
5 changes: 5 additions & 0 deletions internal/client/pkg/clientconfig/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package clientconfig

const (
DEFAULT_ENVIRONMENT = "https://api.abbey.io/v1"
)
18 changes: 18 additions & 0 deletions internal/client/pkg/grantkits/grant_kits_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ func (api *GrantKitsService) CreateGrantKit(ctx context.Context, grantKitCreateP
return shared.NewClientResponse[GrantKit](resp), nil
}

// Validate a Grant Kit
func (api *GrantKitsService) ValidateGrantKit(ctx context.Context, validateGrantKitRequest ValidateGrantKitRequest) (*shared.ClientResponse[any], *shared.ClientError) {
config := *api.getConfig()

client := restClient.NewRestClient[any](config)

request := httptransport.NewRequest(ctx, "POST", "/grant-kit/validate", config)

request.Body = validateGrantKitRequest

resp, err := client.Call(request)
if err != nil {
return nil, shared.NewClientError[any](err)
}

return shared.NewClientResponse[any](resp), nil
}

// Returns the details of a Grant Kit.
func (api *GrantKitsService) GetGrantKitById(ctx context.Context, grantKitIdOrName string) (*shared.ClientResponse[GrantKit], *shared.ClientError) {
config := *api.getConfig()
Expand Down
41 changes: 41 additions & 0 deletions internal/client/pkg/grantkits/validate_grant_kit_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package grantkits

import (
"github.com/go-provider-sdk/internal/marshal"
"github.com/go-provider-sdk/internal/unmarshal"
)

type ValidateGrantKitRequest struct {
GrantKitCreateParams *GrantKitCreateParams
GrantKitUpdateParams *GrantKitUpdateParams
}

func (v *ValidateGrantKitRequest) SetGrantKitCreateParams(grantKitCreateParams GrantKitCreateParams) {
v.GrantKitCreateParams = &grantKitCreateParams
}

func (v *ValidateGrantKitRequest) GetGrantKitCreateParams() *GrantKitCreateParams {
if v == nil {
return nil
}
return v.GrantKitCreateParams
}

func (v *ValidateGrantKitRequest) SetGrantKitUpdateParams(grantKitUpdateParams GrantKitUpdateParams) {
v.GrantKitUpdateParams = &grantKitUpdateParams
}

func (v *ValidateGrantKitRequest) GetGrantKitUpdateParams() *GrantKitUpdateParams {
if v == nil {
return nil
}
return v.GrantKitUpdateParams
}

func (v ValidateGrantKitRequest) MarshalJSON() ([]byte, error) {
return marshal.FromComplexObject(v)
}

func (v *ValidateGrantKitRequest) UnmarshalJSON(data []byte) error {
return unmarshal.ToComplexObject[ValidateGrantKitRequest](data, v)
}
6 changes: 3 additions & 3 deletions internal/provider/grantkit/acceptance/resource_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func getGrantKitProviderConfig(serverUrl string) string {
}

func setupGrantKitMockServer() *httptest.Server {
db := make(map[string]*grantkit.GrantKit)
db := make(map[string]*grant_kit.GrantKit)
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodGet {
Id := path.Base(req.URL.Path)
Expand All @@ -50,7 +50,7 @@ func setupGrantKitMockServer() *httptest.Server {
if req.Method == http.MethodPost {
id := "id"

grant_kit := grantkit.GrantKit{}
grant_kit := grant_kit.GrantKit{}

bodyBytes, _ := io.ReadAll(req.Body)
json.Unmarshal(bodyBytes, &grant_kit)
Expand All @@ -71,7 +71,7 @@ func setupGrantKitMockServer() *httptest.Server {
return
}

grant_kit := grantkit.GrantKit{}
grant_kit := grant_kit.GrantKit{}

bodyBytes, _ := io.ReadAll(req.Body)
json.Unmarshal(bodyBytes, &grant_kit)
Expand Down
118 changes: 118 additions & 0 deletions internal/provider/grantkit/resource.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by liblab | https://liblab.com/
package grantkit

import (
Expand All @@ -11,6 +12,7 @@ import (
"abbey/v2/internal/shared/models/step"
"abbey/v2/internal/utils"
"context"
"encoding/json"
"fmt"
"github.com/go-provider-sdk/pkg/client"
"github.com/go-provider-sdk/pkg/grantkits"
Expand Down Expand Up @@ -1123,3 +1125,119 @@ func (r *GrantKitResource) ImportState(ctx context.Context, req resource.ImportS
// Retrieve import ID and save to id attribute
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *GrantKitResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
var stateModel = &GrantKitResourceModel{}
var dataModel = &GrantKitResourceModel{}
utils.PopulateModelData(ctx, &stateModel, resp.Diagnostics, req.State.Get)
utils.PopulateModelData(ctx, &dataModel, resp.Diagnostics, req.Plan.Get)

if resp.Diagnostics.HasError() {
return
}

// Prepare requestBody for validation depending on create vs. update
requestBody := grantkits.ValidateGrantKitRequest{}
if stateModel == nil { // No state; this is a create operation
requestBody.GrantKitCreateParams = &grantkits.GrantKitCreateParams{
Name: dataModel.Name.ValueStringPointer(),
Description: dataModel.Description.ValueStringPointer(),

Workflow: utils.NullableTfStateObject(dataModel.Workflow, func(from *grant_workflow.GrantWorkflow) grantkits.GrantWorkflow {
return grantkits.GrantWorkflow{
Steps: utils.MapList(from.Steps, func(from step.Step) grantkits.Step {
return grantkits.Step{

Reviewers: utils.NullableTfStateObject(from.Reviewers, func(from *reviewers.Reviewers) grantkits.Reviewers {
return grantkits.Reviewers{
OneOf: utils.FromListToPrimitiveSlice[string](ctx, from.OneOf, &resp.Diagnostics),
AllOf: utils.FromListToPrimitiveSlice[string](ctx, from.AllOf, &resp.Diagnostics),
}
}),
SkipIf: utils.MapList(from.SkipIf, func(from policy.Policy) grantkits.Policy {
return grantkits.Policy{
Bundle: from.Bundle.ValueStringPointer(),
Query: from.Query.ValueStringPointer(),
}
}),
}
}),
}
}),

Output: utils.NullableTfStateObject(dataModel.Output, func(from *output.Output) grantkits.Output {
return grantkits.Output{
Location: from.Location.ValueStringPointer(),
Append: from.Append.ValueStringPointer(),
Overwrite: from.Overwrite.ValueStringPointer(),
}
}),
Policies: utils.MapList(dataModel.Policies, func(from policy.Policy) grantkits.Policy {
return grantkits.Policy{
Bundle: from.Bundle.ValueStringPointer(),
Query: from.Query.ValueStringPointer(),
}
}),
}
} else {
requestBody.GrantKitUpdateParams = &grantkits.GrantKitUpdateParams{
Name: dataModel.Name.ValueStringPointer(),
Description: dataModel.Description.ValueStringPointer(),

Workflow: utils.NullableTfStateObject(dataModel.Workflow, func(from *grant_workflow.GrantWorkflow) grantkits.GrantWorkflow {
return grantkits.GrantWorkflow{
Steps: utils.MapList(from.Steps, func(from step.Step) grantkits.Step {
return grantkits.Step{

Reviewers: utils.NullableTfStateObject(from.Reviewers, func(from *reviewers.Reviewers) grantkits.Reviewers {
return grantkits.Reviewers{
OneOf: utils.FromListToPrimitiveSlice[string](ctx, from.OneOf, &resp.Diagnostics),
AllOf: utils.FromListToPrimitiveSlice[string](ctx, from.AllOf, &resp.Diagnostics),
}
}),
SkipIf: utils.MapList(from.SkipIf, func(from policy.Policy) grantkits.Policy {
return grantkits.Policy{
Bundle: from.Bundle.ValueStringPointer(),
Query: from.Query.ValueStringPointer(),
}
}),
}
}),
}
}),

Output: utils.NullableTfStateObject(dataModel.Output, func(from *output.Output) grantkits.Output {
return grantkits.Output{
Location: from.Location.ValueStringPointer(),
Append: from.Append.ValueStringPointer(),
Overwrite: from.Overwrite.ValueStringPointer(),
}
}),
Policies: utils.MapList(dataModel.Policies, func(from policy.Policy) grantkits.Policy {
return grantkits.Policy{
Bundle: from.Bundle.ValueStringPointer(),
Query: from.Query.ValueStringPointer(),
}
}),
}
}

// Make the call to validate the GrantKit
_, err := r.client.GrantKits.ValidateGrantKit(ctx, requestBody)
if err != nil {
var errMsg string

var errBody map[string]any
marshallErr := json.Unmarshal(err.Body, &errBody)
if marshallErr != nil {
errMsg = string(err.Body)
} else {
errMsg = errBody["message"].(string)
}

resp.Diagnostics.AddError(
"Error validating GrantKit",
errMsg,
)
}
}
Loading