generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d737328
setup user and users data source
ichung08 9658c08
added examples
ichung08 bb231f4
fixed list users
ichung08 b56ca76
add tests
ichung08 e6061ee
fix test
ichung08 e1bfc6c
fix deployment test
ichung08 6a844f8
fix deployment test
ichung08 aa26b0b
address comments
ichung08 5a171de
fix test
ichung08 64c9661
added tests:
ichung08 f59b0cd
fix test
ichung08 8f6e62d
fix test
ichung08 e94064b
added check for workspace and deployment roles
ichung08 e27eb63
fix test
ichung08 7819870
remove check for roles
ichung08 3333941
fix test
ichung08 f64408e
fix test
ichung08 258f850
fix test
ichung08 997a041
fix test
ichung08 641f7ab
fix test
ichung08 a3779de
fix test
ichung08 d3b2d70
fix test
ichung08 1b547ef
address comments
ichung08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "astro_user" "example" { | ||
id = "clhpichn8002m01mqa4ocs7g6" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)...) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).