Skip to content

Commit

Permalink
Merge pull request #15 from embracesbs/features/fix-contributor-updat…
Browse files Browse the repository at this point in the history
…e-role-removal-bug

Features/fix contributor update role and tenant removal bug
  • Loading branch information
SvenHamers authored Oct 27, 2022
2 parents 2776579 + 1069ad5 commit b5a91e4
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 76 deletions.
18 changes: 3 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
"configurations": [
{
"name": "Acceptance Tests",
"type": "go",
"request": "launch",
"mode": "test",
// this assumes your workspace is the root of the repo
"program": "${fileDirname}",
"env": {
"TF_ACC": "1",
},
"args": [],
},
{
"name": "Debug - Attach External CLI",
"name": "Squidex - provider debug launch",
"type": "go",
"request": "launch",
"mode": "debug",
// this assumes your workspace is the root of the repo
"program": "${workspaceFolder}",
"program": "${workspaceFolder}/main.go",
"env": {},
"args": [
// pass the debug flag for reattaching
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NAMESPACE=terraform.embracecloud.nl
COMPANY=embracecloud
PROVIDER=squidex
BINARY=terraform-provider-squidex
VERSION=0.6.1
VERSION=0.7.1
OS=linux
ARCH=amd64

Expand All @@ -15,7 +15,7 @@ build:
go build -o ./bin/${BINARY}_v${VERSION}

build-win:
mkdir -p ./bin
if not exist "./bin" md "./bin"
go build -o ./bin/${BINARY}_v${VERSION}.exe

install: build
Expand All @@ -29,10 +29,10 @@ local-install: build
mv ./bin/${BINARY}_v${VERSION} ./examples/.terraform/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/${OS}_${ARCH}/

local-install-win: build-win
mkdir -p ./examples/terraform.d/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/${OS}_${ARCH}
mkdir -p ./examples/.terraform/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/${OS}_${ARCH}
cp ./bin/${BINARY}_v${VERSION}.exe ./examples/terraform.d/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/${OS}_${ARCH}/
mv ./bin/${BINARY}_v${VERSION}.exe ./examples/.terraform/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/${OS}_${ARCH}/
if not exist "./examples/terraform.d/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/windows_${ARCH}" md "./examples/terraform.d/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/windows_${ARCH}"
if not exist "./examples/.terraform/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/windows_${ARCH}" md "./examples/.terraform/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/windows_${ARCH}"
copy .\bin\${BINARY}_v${VERSION}.exe "./examples/terraform.d/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/windows_${ARCH}/"
move .\bin\${BINARY}_v${VERSION}.exe "./examples/.terraform/plugins/${NAMESPACE}/${COMPANY}/${PROVIDER}/${VERSION}/windows_${ARCH}/"

test:
go test -i $(TEST) || exit 1
Expand Down
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
version: "3.8"
services:
squidex_mongo:
image: mongo:4.4.1

image: mongo:latest
ports:
- 27015:27015
networks:
- internal
restart: unless-stopped

squidex_squidex:
image: "squidex/squidex:5.5.0"
image: "squidex/squidex:5.8.2"
ports:
- "8081:80"
environment:
Expand Down
85 changes: 62 additions & 23 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ terraform {
required_providers {
squidex = {
source = "terraform.embracecloud.nl/embracecloud/squidex"
version = "0.7.1"
}
}

Expand All @@ -21,10 +22,15 @@ resource "squidex_app" "test" {
description = "description1"
}

resource "squidex_app" "test2" {
name = "squidex-provider-test2"
description = "description1"
}

resource "squidex_client" "test" {
app_name = squidex_app.test.name
name = "squidex-provider-test"
role = squidex_role.test.name
role = squidex_role.embracecloud_admin.name
}

resource "squidex_languages" "test" {
Expand All @@ -39,43 +45,76 @@ resource "squidex_languages" "test" {
}
}

resource "squidex_role" "test" {
resource "squidex_role" "embracecloud_admin" {
app_name = squidex_app.test.name
name = "squidex-provider-test"
name = "embracecloud-admin"
permissions = [
"*",
"contents.*",
"schemas.read",
"roles",
"contributors",
"assets",
"languages",
"patterns",
"workflows",
"search",
"backups",
"comments",
"clients",
"history",
"ping",
"plans",
"rules",
"translate",
"usage"
]
properties = {
"ui.api.hide" = true
}
properties = {}
}

resource "squidex_contributor" "test" {
app_name = squidex_app.test.name
contributor_email = "michiel@qvision.nl"
role = squidex_role.test.name
resource "squidex_role" "app_admin" {
app_name = squidex_app.test.name
name = "app-admin"
permissions = [
"contents.*",
"schemas.read",
"roles.read",
"contributors",
"assets",
"languages.read",
"patterns.read",
"workflows.read",
"search",
"backups",
"comments",
"history",
"rules",
"translate",
"squidex.apps.*"
]
// properties = {"ui.api.hide" = true}
properties = {}
}

resource "squidex_contributor" "admin_owner" {
resource "squidex_contributor" "embracecloud_contributors" {
for_each = toset(["admin@embracecloud.nl", "test1@embracecloud.nl", "test2@embracecloud.nl"])
app_name = squidex_app.test.name
contributor_email = "admin@embracecloud.nl"
role = "Owner"
contributor_email = each.value
role = squidex_role.embracecloud_admin.name
}

resource "squidex_contributor" "test_michiel_owner" {
app_name = squidex_app.test.name
contributor_email = "michiel.vanklinken@embracecloud.nl"
role = "Owner"
}

resource "squidex_contributor" "test_pietje_puk" {
resource "squidex_contributor" "app_admin_contributors" {
for_each = toset(["admin@embracecloud.nl", "test1@embracecloud.nl", "test2@embracecloud.nl"])
app_name = squidex_app.test.name
contributor_email = "pietje.puk@embracecloud.nl"
role = squidex_role.test.name
contributor_email = each.value
role = squidex_role.app_admin.name
}

resource "squidex_contributor" "admin_owner" {
app_name = squidex_app.test.name
contributor_email = "admin@embracecloud.nl"
role = "Owner"
}

# TODO: discuss strategy, do we allow for destroy and create on schema resources?
resource "squidex_schema" "test" {
schema_field_delete_allow = true
Expand Down
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"context"
"flag"
"log"
"os"

Expand All @@ -11,21 +13,26 @@ import (
)

func main() {
var debugMode bool

log.SetOutput(os.Stderr)
log.Printf("[TRACE] Running version %s!", meta.SDKVersionString())

flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

opts := &plugin.ServeOpts{
ProviderFunc: squidex.Provider,
}

/// Uncomment to debug the plugin
// if true {
// err := plugin.Debug(context.Background(), "terraform.embracecloud.nl/embracecloud/squidex", opts)
// if err != nil {
// log.Fatal(err.Error())
// }
// return
// }
/// debug the plugin
if debugMode {
err := plugin.Debug(context.Background(), "terraform.embracecloud.nl/embracecloud/squidex", opts)
if err != nil {
log.Fatal(err.Error())
}
return
}

plugin.Serve(opts)
}
6 changes: 5 additions & 1 deletion squidex/internal/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (e APIError) Error() string {
return val
}

func HandleAPIError(response *http.Response, err interface{}) error {
func HandleAPIError(response *http.Response, err interface{}, deleteFunction bool) error {

if response.StatusCode == http.StatusOK ||
response.StatusCode == http.StatusCreated ||
Expand All @@ -35,6 +35,10 @@ func HandleAPIError(response *http.Response, err interface{}) error {
return nil
}

if deleteFunction && response.StatusCode == http.StatusNotFound {
return nil
}

if response.StatusCode == http.StatusUnauthorized || response.StatusCode == http.StatusForbidden {
// do something with status
return &APIError{
Expand Down
5 changes: 4 additions & 1 deletion squidex/resource_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package squidex
import (
"context"

"github.com/embracesbs/terraform-provider-squidex/squidex/internal/common"
"github.com/embracesbs/terraform-provider-squidex/squidex/internal/squidexclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -98,7 +99,9 @@ func resourceAppDelete(ctx context.Context, data *schema.ResourceData, meta inte

client := meta.(providerConfig).Client
var diags diag.Diagnostics
_, err := client.AppsApi.AppsDeleteApp(ctx, name)
response, err := client.AppsApi.AppsDeleteApp(ctx, name)

err = common.HandleAPIError(response, err, true)

if err != nil {
return diag.FromErr(err)
Expand Down
10 changes: 5 additions & 5 deletions squidex/resource_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func resourceClientRead(ctx context.Context, data *schema.ResourceData, meta int

result, response, err := client.AppsApi.AppClientsGetClients(ctx, appName)

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, false)

if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -101,7 +101,7 @@ func resourceClientCreate(ctx context.Context, data *schema.ResourceData, meta i
Id: name,
})

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, false)

if err != nil {
data.Set("invalidated_state", true)
Expand Down Expand Up @@ -143,7 +143,7 @@ func resourceClientUpdate(ctx context.Context, data *schema.ResourceData, meta i
Role: &role,
})

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, false)

if err != nil {
data.Set("invalidated_state", true)
Expand All @@ -160,11 +160,11 @@ func resourceClientDelete(ctx context.Context, data *schema.ResourceData, meta i
var diags diag.Diagnostics

appName := data.Get("app_name").(string)
id := data.Get("id").(string)
id := data.Id()

_, response, err := client.AppsApi.AppClientsDeleteClient(ctx, appName, id)

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, true)

if err != nil {
return diag.FromErr(err)
Expand Down
27 changes: 22 additions & 5 deletions squidex/resource_contributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func resourceContributorRead(ctx context.Context, data *schema.ResourceData, met

result, response, err := client.AppsApi.AppContributorsGetContributors(ctx, appName)

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, false)

if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -106,7 +106,7 @@ func resourceContributorCreate(ctx context.Context, data *schema.ResourceData, m
Invite: invite,
})

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, false)

if err != nil {
data.Set("invalidated_state", true)
Expand Down Expand Up @@ -145,19 +145,35 @@ func resourceContributorUpdate(ctx context.Context, data *schema.ResourceData, m
invite := false // no invites send for updating role

// there is no update method, just use the create to set it again, but no invite!
_, response, err := client.AppsApi.AppContributorsPostContributor(ctx, appName, squidexclient.AssignContributorDto{
result, response, err := client.AppsApi.AppContributorsPostContributor(ctx, appName, squidexclient.AssignContributorDto{
ContributorId: contributorID,
Role: &role,
Invite: invite,
})

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, false)

if err != nil {
data.Set("invalidated_state", true)
return diag.FromErr(err)
}

var resultItem *squidexclient.ContributorDto
for i := range result.Items {
if strings.EqualFold(result.Items[i].ContributorId, contributorID) {
resultItem = &result.Items[i]
break
}
}

if resultItem == nil {
data.Set("invalidated_state", true)
source, _ := json.Marshal(result)
return diag.Errorf("Not Found: Contributor with id %s\nApi Response: %s", contributorID, string(source))
}

data.SetId(resultItem.ContributorId)

// there are no new values from the server, so no drifting, resourceContributorRead(ctx, data, meta)

return diags
Expand All @@ -171,9 +187,10 @@ func resourceContributorDelete(ctx context.Context, data *schema.ResourceData, m

client := meta.(providerConfig).Client
var diags diag.Diagnostics

_, response, err := client.AppsApi.AppContributorsDeleteContributor(ctx, appName, contributorID)

err = common.HandleAPIError(response, err)
err = common.HandleAPIError(response, err, true)

if err != nil {
return diag.FromErr(err)
Expand Down
Loading

0 comments on commit b5a91e4

Please sign in to comment.