Skip to content

Commit

Permalink
Convert static structs to pointers and force return results to be cop…
Browse files Browse the repository at this point in the history
…ies.
  • Loading branch information
wparad committed Feb 27, 2023
1 parent 918fa26 commit 68f0c85
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 70 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ test:
go test -count=1 -parallel=4 ./...

integration:
TF_ACC=1 AUTHRESS_KEY=1 go test -count=1 -parallel=4 -timeout 10m -v ./...
TF_ACC=1 AUTHRESS_KEY=KEY go test -count=1 -parallel=4 -timeout 10m -v ./...

integration_examples:
TF_LOG=debug terraform plan
51 changes: 1 addition & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,4 @@ terraform {


## Development

### Setup

Update your `~/.terraformrc` with the following

```hcl
provider_installation {
dev_overrides {
"localhost.com/authress/authress" = "~/go/bin"
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
```

Test the provider out locally to validate it works
```sh
go run main.go
```

Run the following command to build the provider

```shell
go build -o terraform-provider-authress
```

#### Update Dependencies
Run:

```shell
go mod tidy
```

## Test sample configuration

First, build and install the provider.

```shell
make install
```

Then, run the following command to initialize the workspace and apply the sample configuration.

```shell
terraform init && terraform apply
```
For developing this plugin see more information in [Development Docs](./development-examples/README.md).
44 changes: 44 additions & 0 deletions development-examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Try out the development example
### Setup

Update your `~/.terraformrc` with the following

```hcl
provider_installation {
dev_overrides {
"hashicorp.com/authress/authress" = "~/go/bin"
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
```

Test the provider out locally to validate it works
```sh
go run main.go
```

Run the following command to build the provider

```shell
go build -o terraform-provider-authress && cp terraform-provider-authress ~/go/bin
terraform init
```

## Test sample configuration

First, build and install the provider.

```shell
make install
```

Then, run the following command to initialize the workspace and apply the sample configuration.

```shell
terraform init && terraform apply
```
2 changes: 1 addition & 1 deletion examples/main.tf → development-examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
authress = {
version = "0.2"
source = "localhost.com/authress/authress"
source = "hashicorp.com/authress/authress"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"context"
"terraform-provider-authress/src"
"context"
authress "terraform-provider-authress/src"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
)

func main() {
providerserver.Serve(context.Background(), authress.New, providerserver.ServeOpts{
Address: "localhost.com/authress/authress",
Address: "hashicorp.com/authress/authress",
})
}
18 changes: 9 additions & 9 deletions src/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *RoleInterfaceProvider) Create(ctx context.Context, req resource.CreateR
}

// Create new role
authressSdkRole := *MapTerraformRoleToSdk(plannedAuthressRoleResource)
authressSdkRole := MapTerraformRoleToSdk(&plannedAuthressRoleResource)
returnedRole, err := r.client.CreateRole(authressSdkRole)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -146,7 +146,7 @@ func (r *RoleInterfaceProvider) Create(ctx context.Context, req resource.CreateR
}

// Map response body to schema and populate Computed attribute values
plannedAuthressRoleResource = *MapSdkRoleToTerraform(*returnedRole)
plannedAuthressRoleResource = MapSdkRoleToTerraform(returnedRole)
plannedAuthressRoleResource.LastUpdated = TerraformType.StringValue(time.Now().Format(time.RFC850))

// Set state to fully populated data
Expand Down Expand Up @@ -181,7 +181,7 @@ func (r *RoleInterfaceProvider) Read(ctx context.Context, req resource.ReadReque
}

// Set refreshed currentAuthressRoleResource
currentAuthressRoleResource = *MapSdkRoleToTerraform(*authressSdkRole)
currentAuthressRoleResource = MapSdkRoleToTerraform(authressSdkRole)
diags = resp.State.Set(ctx, &currentAuthressRoleResource)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -200,7 +200,7 @@ func (r *RoleInterfaceProvider) Update(ctx context.Context, req resource.UpdateR
}

// Generate API request body from plannedAuthressRoleResource
authressSdkRole := *MapTerraformRoleToSdk(plannedAuthressRoleResource)
authressSdkRole := MapTerraformRoleToSdk(&plannedAuthressRoleResource)

// Update existing role
returnedRole, err := r.client.UpdateRole(plannedAuthressRoleResource.RoleID.ValueString(), authressSdkRole)
Expand All @@ -215,7 +215,7 @@ func (r *RoleInterfaceProvider) Update(ctx context.Context, req resource.UpdateR
return
}

plannedAuthressRoleResource = *MapSdkRoleToTerraform(*returnedRole)
plannedAuthressRoleResource = MapSdkRoleToTerraform(returnedRole)
plannedAuthressRoleResource.LastUpdated = TerraformType.StringValue(time.Now().Format(time.RFC850))

diags = resp.State.Set(ctx, plannedAuthressRoleResource)
Expand Down Expand Up @@ -254,7 +254,7 @@ func (r *RoleInterfaceProvider) ImportState(ctx context.Context, req resource.Im
resource.ImportStatePassthroughID(ctx, path.Root("role_id"), req, resp)
}

func MapSdkRoleToTerraform(authressSdkRole AuthressSdk.Role) (*AuthressRoleResource) {
func MapSdkRoleToTerraform(authressSdkRole *AuthressSdk.Role) (AuthressRoleResource) {
terraformRole := AuthressRoleResource {
RoleID: TerraformType.StringValue(authressSdkRole.RoleID),
LegacyID: TerraformType.StringValue(authressSdkRole.RoleID),
Expand All @@ -271,10 +271,10 @@ func MapSdkRoleToTerraform(authressSdkRole AuthressSdk.Role) (*AuthressRoleResou
}
}

return &terraformRole
return terraformRole
}

func MapTerraformRoleToSdk(terraformRole AuthressRoleResource) (*AuthressSdk.Role) {
func MapTerraformRoleToSdk(terraformRole *AuthressRoleResource) (AuthressSdk.Role) {
authressSdkRole := AuthressSdk.Role {
RoleID: terraformRole.RoleID.ValueString(),
Name: terraformRole.Name.ValueString(),
Expand All @@ -291,5 +291,5 @@ func MapTerraformRoleToSdk(terraformRole AuthressRoleResource) (*AuthressSdk.Rol
authressSdkRole.Permissions = append(authressSdkRole.Permissions, authressSdkRolePermissions)
}

return &authressSdkRole
return authressSdkRole
}
10 changes: 5 additions & 5 deletions src/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestRoleResource(t *testing.T) {
// Create and Read testing
{
Config: providerConfig + `
resource "authress_role" "test-1" {
resource "authress_role" "test-100" {
role_id = "test-1"
name = "Terraform Test Role"
permissions = {
Expand All @@ -23,14 +23,14 @@ resource "authress_role" "test-1" {
}
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("authress_role.test-1", "role_id", "test-1"),
resource.TestCheckResourceAttr("authress_role.test-1", "permissions.one.allow", "true"),
resource.TestCheckResourceAttrSet("authress_role.test-1", "last_updated"),
resource.TestCheckResourceAttr("authress_role.test-100", "role_id", "test-1"),
resource.TestCheckResourceAttr("authress_role.test-100", "permissions.one.allow", "true"),
resource.TestCheckResourceAttrSet("authress_role.test-100", "last_updated"),
),
},
// ImportState testing
{
ResourceName: "authress_role.test-1",
ResourceName: "authress_role.test-100",
ImportState: true,
ImportStateVerify: true,
// The last_updated attribute does not exist in the Authress API, therefore there is no value for it during import.
Expand Down

0 comments on commit 68f0c85

Please sign in to comment.