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

Docs: define import example for resources #113

Merged
merged 5 commits into from
Jan 25, 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
18 changes: 18 additions & 0 deletions docs/resources/project_access.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ ProjectAccess schema
## Example Usage

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

resource "unleash_project" "sample_project" {
id = "sample"
name = "sample-project"
Expand Down Expand Up @@ -61,6 +66,19 @@ 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
users = [
unleash_user.test_user.id
]
groups = []
},
]
}
```

<!-- schema generated by tfplugindocs -->
Expand Down
12 changes: 12 additions & 0 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ User schema
## Example Usage

```terraform
import {
id = 1
to = unleash_user.admin
}

resource "unleash_user" "admin" {
email = "[email protected]"
name = "Chuck Norris"
root_role = 1
send_email = false
}

resource "unleash_user" "chuck" {
email = "[email protected]"
name = "Chuck Norris"
Expand Down
20 changes: 19 additions & 1 deletion examples/resources/unleash_project_access/resource.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
id = "default"
to = unleash_project_access.default_project_access
}

resource "unleash_project" "sample_project" {
id = "sample"
name = "sample-project"
Expand Down Expand Up @@ -45,4 +50,17 @@ resource "unleash_project_access" "sample_project_access" {
groups = []
},
]
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intention is to import the default project access and override the owner

resource "unleash_project_access" "default_project_access" {
project = "default"
roles = [
{
role = data.unleash_role.project_owner_role.id
users = [
unleash_user.test_user.id
]
groups = []
},
]
}
2 changes: 1 addition & 1 deletion examples/resources/unleash_role/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ resource "unleash_role" "project_role" {
name = "UPDATE_FEATURE_ENVIRONMENT"
environment = "development"
}]
}
}
14 changes: 13 additions & 1 deletion examples/resources/unleash_user/resource.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import {
id = 1
to = unleash_user.admin
}

resource "unleash_user" "admin" {
email = "[email protected]"
name = "Chuck Norris"
root_role = 1
send_email = false
}

resource "unleash_user" "chuck" {
email = "[email protected]"
name = "Chuck Norris"
Expand All @@ -11,4 +23,4 @@ resource "unleash_user" "with_password" {
root_role = 1
send_email = false
password = "youcanseeme"
}
}
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
Loading