Skip to content

Commit

Permalink
Fix build examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonfournier committed Jan 24, 2024
1 parent fddcbca commit 8ec09b9
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 44 deletions.
5 changes: 3 additions & 2 deletions docs/resources/project_access.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ProjectAccess schema

```terraform
import {
project = "default"
to = unleash_project_access.default_project_access
id = "default"
to = unleash_project_access.default_project_access
}
resource "unleash_project" "sample_project" {
Expand Down Expand Up @@ -68,6 +68,7 @@ resource "unleash_project_access" "sample_project_access" {
}
resource "unleash_project_access" "default_project_access" {
project = "default"
roles = [
{
role = data.unleash_role.project_owner_role.id
Expand Down
15 changes: 0 additions & 15 deletions docs/resources/role.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ Role schema
## Example Usage

```terraform
import {
id = 5
to = unleash_role.project_member_role
}
resource "unleash_role" "project_member_role" {
permissions = [{
name = "CREATE_PROJECT"
}, {
name = "UPDATE_PROJECT"
}, {
name = "DELETE_PROJECT"
}]
}
resource "unleash_role" "custom_root_role" {
name = "A custom role"
type = "root-custom"
Expand Down
5 changes: 3 additions & 2 deletions examples/resources/unleash_project_access/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
project = "default"
to = unleash_project_access.default_project_access
id = "default"
to = unleash_project_access.default_project_access
}

resource "unleash_project" "sample_project" {
Expand Down Expand Up @@ -53,6 +53,7 @@ resource "unleash_project_access" "sample_project_access" {
}

resource "unleash_project_access" "default_project_access" {
project = "default"
roles = [
{
role = data.unleash_role.project_owner_role.id
Expand Down
15 changes: 0 additions & 15 deletions examples/resources/unleash_role/resource.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import {
id = 5
to = unleash_role.project_member_role
}

resource "unleash_role" "project_member_role" {
permissions = [{
name = "CREATE_PROJECT"
}, {
name = "UPDATE_PROJECT"
}, {
name = "DELETE_PROJECT"
}]
}

resource "unleash_role" "custom_root_role" {
name = "A custom role"
type = "root-custom"
Expand Down
10 changes: 0 additions & 10 deletions examples/staged/stage_4/default_project.tf

This file was deleted.

42 changes: 42 additions & 0 deletions examples/staged/stage_4/imported_resources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
id = "default"
to = unleash_project.default_project
}

resource "unleash_project" "default_project" {
id = "default"
name = "Taken over by Terraform"
description = "This was default project"
}

import {
id = 1
to = unleash_user.admin_user
}

resource "unleash_user" "admin_user" {
root_role = 1
username = "admin"
}

import {
id = "default"
to = unleash_project_access.default_project_access
}

data "unleash_role" "project_owner_role" {
name = "Owner"
}

resource "unleash_project_access" "default_project_access" {
project = "default"
roles = [
{
role = data.unleash_role.project_owner_role.id
users = [
unleash_user.admin_user.id
]
groups = []
},
]
}
7 changes: 7 additions & 0 deletions internal/provider/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,20 @@ func (r *projectResource) Read(ctx context.Context, req resource.ReadRequest, re

projects, api_response, err := r.client.ProjectsAPI.GetProjects(ctx).Execute()

tflog.Debug(ctx, "Searching for project", map[string]any{"id": state.Id.ValueString()})
var project unleash.ProjectSchema
for _, p := range projects.Projects {
if p.Id == state.Id.ValueString() {
project = p
}
}

// validate if project was found
if project.Id == "" {
resp.Diagnostics.AddError(fmt.Sprintf("Project with id %s not found", state.Id.ValueString()), "NotFound")
return
}

if !ValidateApiResponse(api_response, 200, &resp.Diagnostics, err) {
return
}
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/project_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func TestAccProjectResource(t *testing.T) {
resource.TestCheckResourceAttrSet("unleash_project.test_project2", "name"),
),
},
{
Config: `resource "unleash_project" "newly_imported" {}`,
ImportStateId: "TestId2",
ResourceName: "unleash_project.newly_imported",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
6 changes: 6 additions & 0 deletions internal/provider/role_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func TestAccRoleResource(t *testing.T) {
customCheckRolePermissionExists("unleash_role.project_role", "UPDATE_FEATURE_ENVIRONMENT", "development"),
),
},
{
Config: `resource "unleash_role" "project_role" {}`,
ResourceName: "unleash_role.project_role",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
7 changes: 7 additions & 0 deletions internal/provider/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -93,6 +94,8 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"send_email": schema.BoolAttribute{
Description: "Send a welcome email to the customer or not. Defaults to true",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
},
}
Expand Down Expand Up @@ -199,6 +202,10 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
} else {
state.Name = types.StringNull()
}
if state.SendEmail.IsNull() || state.SendEmail.IsUnknown() {
state.SendEmail = types.BoolValue(false)
}

state.RootRole = types.Int64Value(int64(*user.RootRole))
tflog.Debug(ctx, "Finished populating model", map[string]any{"success": true})

Expand Down
28 changes: 28 additions & 0 deletions internal/provider/user_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ func TestAccUserResource(t *testing.T) {
})
}

func TestAccUserResourceImport(t *testing.T) {
if os.Getenv("UNLEASH_ENTERPRISE") != "true" {
t.Skip("Skipping enterprise tests")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccSampleUserResource("Test User"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("unleash_user.the_newbie", "id"),
resource.TestCheckResourceAttr("unleash_user.the_newbie", "name", "Test User"),
resource.TestCheckResourceAttr("unleash_user.the_newbie", "email", "[email protected]"),
resource.TestCheckResourceAttr("unleash_user.the_newbie", "root_role", "2"),
// TODO test the remote object matches https://developer.hashicorp.com/terraform/plugin/testing/testing-patterns#basic-test-to-verify-attributes
),
},
{
Config: `resource "unleash_user" "the_newbie" {}`,
ResourceName: "unleash_user.the_newbie",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckUserResourceDestroy(s *terraform.State) error {
// TODO retrieve the client from Provider configuration rather than creating a new client
configuration := unleash.NewConfiguration()
Expand Down

0 comments on commit 8ec09b9

Please sign in to comment.