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

Add user and users data source #102

Merged
merged 23 commits into from
Jul 8, 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
9 changes: 9 additions & 0 deletions .github/workflows/testacc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ jobs:
ASTRO_API_HOST: https://api.astronomer-dev.io
SKIP_CLUSTER_RESOURCE_TESTS: ${{ env.SKIP_CLUSTER_RESOURCE_TESTS }}
HOSTED_TEAM_ID: clx44rvzr01nc01o06pze6qb7
HOSTED_USER_ID: clhpichn8002m01mqa4ocs7g6
HOSTED_DEPLOYMENT_ID: clyd4zf7g01uu01oxpve7tfvx
HOSTED_WORKSPACE_ID: clx42sxw501gl01o0gjenthnh
TESTARGS: "-failfast"
run: make testacc

Expand Down Expand Up @@ -133,6 +136,9 @@ jobs:
HYBRID_NODE_POOL_ID: clxm3xg9e05bk01ixrqk52cob
ASTRO_API_HOST: https://api.astronomer-stage.io
HOSTED_TEAM_ID: clx486hno068301il306nuhsm
HOSTED_USER_ID: cljftnljr00i001nl6bnngxdb
HOSTED_DEPLOYMENT_ID: cly6exz4a00zd01k18t5bo1vf
HOSTED_WORKSPACE_ID: clx480rvx068u01j9mp7t7fqh
TESTARGS: "-failfast"
run: make testacc

Expand Down Expand Up @@ -177,5 +183,8 @@ jobs:
HYBRID_NODE_POOL_ID: clxkqfzvm001c01nc1eosyxzg
ASTRO_API_HOST: https://api.astronomer-dev.io
HOSTED_TEAM_ID: clx44rvzr01nc01o06pze6qb7
HOSTED_USER_ID: clhpichn8002m01mqa4ocs7g6
HOSTED_DEPLOYMENT_ID: clyd4zf7g01uu01oxpve7tfvx
HOSTED_WORKSPACE_ID: clx42sxw501gl01o0gjenthnh
TESTARGS: "-failfast"
run: make testacc
55 changes: 55 additions & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astro_user Data Source - astro"
subcategory: ""
description: |-
User data source
---

# astro_user (Data Source)

User data source

## Example Usage

```terraform
data "astro_user" "example" {
id = "clhpichn8002m01mqa4ocs7g6"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `id` (String) User identifier

### Read-Only

- `avatar_url` (String) User avatar URL
- `created_at` (String) User creation timestamp
- `deployment_roles` (Attributes Set) The roles assigned to the deployments (see [below for nested schema](#nestedatt--deployment_roles))
- `full_name` (String) User full name
- `organization_role` (String) The role assigned to the organization
- `status` (String) User status
- `updated_at` (String) User last updated timestamp
- `username` (String) User username
- `workspace_roles` (Attributes Set) The roles assigned to the workspaces (see [below for nested schema](#nestedatt--workspace_roles))

<a id="nestedatt--deployment_roles"></a>
### Nested Schema for `deployment_roles`

Read-Only:

- `deployment_id` (String) The ID of the deployment the role is assigned to
- `role` (String) The role assigned to the deployment


<a id="nestedatt--workspace_roles"></a>
### Nested Schema for `workspace_roles`

Read-Only:

- `role` (String) The role assigned to the workspace
- `workspace_id` (String) The ID of the workspace the role is assigned to
73 changes: 73 additions & 0 deletions docs/data-sources/users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astro_users Data Source - astro"
subcategory: ""
description: |-
Users data source
---

# astro_users (Data Source)

Users data source

## Example Usage

```terraform
data "astro_users" "example_users" {}

data "astro_users" "example_users_filter_by_workspace_id" {
workspace_id = "clx42sxw501gl01o0gjenthnh"
}

data "astro_users" "example_users_filter_by_deployment_id" {
deployment_id = "clx44jyu001m201m5dzsbexqr"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `deployment_id` (String)
- `workspace_id` (String)

### Read-Only

- `users` (Attributes Set) (see [below for nested schema](#nestedatt--users))

<a id="nestedatt--users"></a>
### Nested Schema for `users`

Required:

- `id` (String) User identifier

Read-Only:

- `avatar_url` (String) User avatar URL
- `created_at` (String) User creation timestamp
- `deployment_roles` (Attributes Set) The roles assigned to the deployments (see [below for nested schema](#nestedatt--users--deployment_roles))
- `full_name` (String) User full name
- `organization_role` (String) The role assigned to the organization
- `status` (String) User status
- `updated_at` (String) User last updated timestamp
- `username` (String) User username
- `workspace_roles` (Attributes Set) The roles assigned to the workspaces (see [below for nested schema](#nestedatt--users--workspace_roles))

<a id="nestedatt--users--deployment_roles"></a>
### Nested Schema for `users.deployment_roles`

Read-Only:

- `deployment_id` (String) The ID of the deployment the role is assigned to
- `role` (String) The role assigned to the deployment


<a id="nestedatt--users--workspace_roles"></a>
### Nested Schema for `users.workspace_roles`

Read-Only:

- `role` (String) The role assigned to the workspace
- `workspace_id` (String) The ID of the workspace the role is assigned to
35 changes: 17 additions & 18 deletions docs/resources/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,23 @@ import {
to = astro_deployment.imported_deployment
}
resource "astro_deployment" "imported_deployment" {
original_astro_runtime_version = "11.3.0"
name = "import me"
description = "an existing deployment"
type = "DEDICATED"
cluster_id = "clv17vgft000801kkydsws63x"
contact_emails = ["preview@astronomer.test"]
default_task_pod_cpu = "0.25"
default_task_pod_memory = "0.5Gi"
executor = "KUBERNETES"
is_cicd_enforced = true
is_dag_deploy_enabled = true
is_development_mode = false
is_high_availability = true
resource_quota_cpu = "10"
resource_quota_memory = "20Gi"
scheduler_size = "SMALL"
workspace_id = "clnp86ly5000401ndaga21g81"
environment_variables = []
name = "import me"
description = "an existing deployment"
type = "DEDICATED"
cluster_id = "clv17vgft000801kkydsws63x"
contact_emails = ["preview@astronomer.test"]
default_task_pod_cpu = "0.25"
default_task_pod_memory = "0.5Gi"
executor = "KUBERNETES"
is_cicd_enforced = true
is_dag_deploy_enabled = true
is_development_mode = false
is_high_availability = true
resource_quota_cpu = "10"
resource_quota_memory = "20Gi"
scheduler_size = "SMALL"
workspace_id = "clnp86ly5000401ndaga21g81"
environment_variables = []
}
```

Expand Down
3 changes: 3 additions & 0 deletions examples/data-sources/astro_user/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "astro_user" "example" {
id = "clhpichn8002m01mqa4ocs7g6"
}
9 changes: 9 additions & 0 deletions examples/data-sources/astro_users/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "astro_users" "example_users" {}

data "astro_users" "example_users_filter_by_workspace_id" {
workspace_id = "clx42sxw501gl01o0gjenthnh"
}

data "astro_users" "example_users_filter_by_deployment_id" {
deployment_id = "clx44jyu001m201m5dzsbexqr"
}
35 changes: 17 additions & 18 deletions examples/resources/astro_deployment/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,21 @@ import {
to = astro_deployment.imported_deployment
}
resource "astro_deployment" "imported_deployment" {
original_astro_runtime_version = "11.3.0"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed this as it was causing issues with importing deployments. when running terraform plan it stated that a new resource was to be created and another to be deleted. after removing this field, when running tf import it stated a resource was to be updated instead (which is the intended behaviour).

name = "import me"
description = "an existing deployment"
type = "DEDICATED"
cluster_id = "clv17vgft000801kkydsws63x"
contact_emails = ["preview@astronomer.test"]
default_task_pod_cpu = "0.25"
default_task_pod_memory = "0.5Gi"
executor = "KUBERNETES"
is_cicd_enforced = true
is_dag_deploy_enabled = true
is_development_mode = false
is_high_availability = true
resource_quota_cpu = "10"
resource_quota_memory = "20Gi"
scheduler_size = "SMALL"
workspace_id = "clnp86ly5000401ndaga21g81"
environment_variables = []
name = "import me"
description = "an existing deployment"
type = "DEDICATED"
cluster_id = "clv17vgft000801kkydsws63x"
contact_emails = ["preview@astronomer.test"]
default_task_pod_cpu = "0.25"
default_task_pod_memory = "0.5Gi"
executor = "KUBERNETES"
is_cicd_enforced = true
is_dag_deploy_enabled = true
is_development_mode = false
is_high_availability = true
resource_quota_cpu = "10"
resource_quota_memory = "20Gi"
scheduler_size = "SMALL"
workspace_id = "clnp86ly5000401ndaga21g81"
environment_variables = []
}
109 changes: 109 additions & 0 deletions internal/provider/datasources/data_source_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package datasources

import (
"context"
"fmt"

"github.com/astronomer/terraform-provider-astro/internal/clients"
"github.com/astronomer/terraform-provider-astro/internal/clients/iam"
"github.com/astronomer/terraform-provider-astro/internal/provider/models"
"github.com/astronomer/terraform-provider-astro/internal/provider/schemas"
"github.com/astronomer/terraform-provider-astro/internal/utils"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &userDataSource{}
var _ datasource.DataSourceWithConfigure = &userDataSource{}

func NewUserDataSources() datasource.DataSource {
return &userDataSource{}
}

// userDataSource defines the data source implementation.
type userDataSource struct {
IamClient iam.ClientWithResponsesInterface
OrganizationId string
}

func (d *userDataSource) Metadata(
ctx context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_user"
}

func (d *userDataSource) Schema(
ctx context.Context,
req datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "User data source",
Attributes: schemas.UserDataSourceSchemaAttributes(),
}
}

func (d *userDataSource) Configure(
ctx context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

apiClients, ok := req.ProviderData.(models.ApiClientsModel)
if !ok {
utils.DataSourceApiClientConfigureError(ctx, req, resp)
return
}

d.IamClient = apiClients.IamClient
d.OrganizationId = apiClients.OrganizationId
}

func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data models.User

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

user, err := d.IamClient.GetUserWithResponse(ctx, d.OrganizationId, data.Id.ValueString())
if err != nil {
tflog.Error(ctx, "Failed to get user", map[string]interface{}{"error": err})
resp.Diagnostics.AddError(
"Client Error",
fmt.Sprintf("Unable to read user, got error: %s", err),
)
return
}
_, diagnostic := clients.NormalizeAPIError(ctx, user.HTTPResponse, user.Body)
if diagnostic != nil {
resp.Diagnostics.Append(diagnostic)
return
}
if user.JSON200 == nil {
tflog.Error(ctx, "failed to get user", map[string]interface{}{"error": "nil response"})
resp.Diagnostics.AddError("Client Error", "Unable to read user, got nil response")
return
}

// Populate the model with the response data
diags := data.ReadFromResponse(ctx, user.JSON200)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading