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 Api Tokens Data Source #104

Merged
merged 34 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e4bd1d5
setup api token data source
ichung08 Jul 8, 2024
d951f3f
add to provider and fix descriptions
ichung08 Jul 8, 2024
90b2b4d
fix schema and add api tokens to provider
ichung08 Jul 8, 2024
ed8e5e8
add tests
ichung08 Jul 8, 2024
38d6a25
fix test
ichung08 Jul 8, 2024
7004e92
Update examples/data-sources/astro_api_tokens/data-source.tf
ichung08 Jul 9, 2024
8432791
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
2e4031e
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
0ff3bdb
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
9e634d1
address comments
ichung08 Jul 9, 2024
a606068
fix test
ichung08 Jul 9, 2024
00f7179
fix test
ichung08 Jul 9, 2024
1f5bd10
fix test
ichung08 Jul 9, 2024
584b2c7
fix test
ichung08 Jul 9, 2024
295cea4
fix test
ichung08 Jul 9, 2024
2cc0da3
fix test
ichung08 Jul 9, 2024
e01842a
fix test
ichung08 Jul 9, 2024
781c9c4
fix test
ichung08 Jul 9, 2024
c2c0c86
fix test
ichung08 Jul 9, 2024
c53a75c
fix test
ichung08 Jul 9, 2024
09c4824
Update internal/provider/schemas/api_token.go
ichung08 Jul 9, 2024
71b3ce8
Update internal/provider/schemas/api_token.go
ichung08 Jul 9, 2024
512ee36
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
3c221fc
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
b6e1f13
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
cb9cd29
Update internal/provider/datasources/data_source_api_tokens_test.go
ichung08 Jul 9, 2024
5d30bf6
Update internal/provider/datasources/data_source_api_tokens_test.go
ichung08 Jul 9, 2024
470d8d5
address comments
ichung08 Jul 9, 2024
c1c8049
Merge branch '103-add-api-tokens-data-source' of github.com:astronome…
ichung08 Jul 9, 2024
314065b
fix tests
ichung08 Jul 9, 2024
7f687ca
address comments
ichung08 Jul 9, 2024
86691c5
fix
ichung08 Jul 9, 2024
04b2057
fix
ichung08 Jul 9, 2024
a3a3a9b
fix deployment test
ichung08 Jul 9, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/testacc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
HOSTED_USER_ID: clhpichn8002m01mqa4ocs7g6
HOSTED_DEPLOYMENT_ID: clyd4zf7g01uu01oxpve7tfvx
HOSTED_WORKSPACE_ID: clx42sxw501gl01o0gjenthnh
HOSTED_API_TOKEN_ID: clxm4836f00ql01me3nigmcr6
TESTARGS: "-failfast"
run: make testacc

Expand Down Expand Up @@ -139,6 +140,7 @@ jobs:
HOSTED_USER_ID: cljftnljr00i001nl6bnngxdb
HOSTED_DEPLOYMENT_ID: cly6exz4a00zd01k18t5bo1vf
HOSTED_WORKSPACE_ID: clx480rvx068u01j9mp7t7fqh
HOSTED_API_TOKEN_ID: clxm46ged05b301neuucdqwox
TESTARGS: "-failfast"
run: make testacc

Expand Down Expand Up @@ -186,5 +188,6 @@ jobs:
HOSTED_USER_ID: clhpichn8002m01mqa4ocs7g6
HOSTED_DEPLOYMENT_ID: clyd4zf7g01uu01oxpve7tfvx
HOSTED_WORKSPACE_ID: clx42sxw501gl01o0gjenthnh
HOSTED_API_TOKEN_ID: clxm4836f00ql01me3nigmcr6
TESTARGS: "-failfast"
run: make testacc
77 changes: 77 additions & 0 deletions docs/data-sources/api_token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astro_api_token Data Source - astro"
subcategory: ""
description: |-
API Token data source
---

# astro_api_token (Data Source)

API Token data source

## Example Usage

```terraform
data "astro_api_token" "example" {
id = "clxm4836f00ql01me3nigmcr6"
}
```

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

### Required

- `id` (String) API Token identifier

### Read-Only

- `created_at` (String) API Token creation timestamp
- `created_by` (Attributes) API Token creator (see [below for nested schema](#nestedatt--created_by))
- `description` (String) API Token description
- `end_at` (String) time when the API token will expire in UTC
- `expiry_period_in_days` (Number) API Token expiry period in days
- `last_used_at` (String) API Token last used timestamp
- `name` (String) API Token name
- `roles` (Attributes Set) The roles assigned to the API Token (see [below for nested schema](#nestedatt--roles))
- `short_token` (String) API Token short token
- `start_at` (String) time when the API token will become valid in UTC
- `type` (String) API Token type
- `updated_at` (String) API Token last updated timestamp
- `updated_by` (Attributes) API Token updater (see [below for nested schema](#nestedatt--updated_by))

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

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)


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

Read-Only:

- `entity_id` (String) The ID of the entity to assign the role to
- `entity_type` (String) The type of entity to assign the role to
- `role` (String) The role to assign to the deployment


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

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)
100 changes: 100 additions & 0 deletions docs/data-sources/api_tokens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "astro_api_tokens Data Source - astro"
subcategory: ""
description: |-
API Tokens data source
---

# astro_api_tokens (Data Source)

API Tokens data source

## Example Usage

```terraform
data "astro_api_tokens" "example" {}

data "astro_api_tokens" "organization_only_example" {
include_only_organization_tokens = true
}

data "astro_api_tokens" "workspace_example" {
workspace_id = "clx42sxw501gl01o0gjenthnh"
}

data "astro_api_tokens" "deployment_example" {
deployment_id = "clx44jyu001m201m5dzsbexqr"
}
```

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

### Optional

- `deployment_id` (String)
- `include_only_organization_tokens` (Boolean)
- `workspace_id` (String)

### Read-Only

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

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

Required:

- `id` (String) API Token identifier

Read-Only:

- `created_at` (String) API Token creation timestamp
- `created_by` (Attributes) API Token creator (see [below for nested schema](#nestedatt--api_tokens--created_by))
- `description` (String) API Token description
- `end_at` (String) time when the API token will expire in UTC
- `expiry_period_in_days` (Number) API Token expiry period in days
- `last_used_at` (String) API Token last used timestamp
- `name` (String) API Token name
- `roles` (Attributes Set) The roles assigned to the API Token (see [below for nested schema](#nestedatt--api_tokens--roles))
- `short_token` (String) API Token short token
- `start_at` (String) time when the API token will become valid in UTC
- `type` (String) API Token type
- `updated_at` (String) API Token last updated timestamp
- `updated_by` (Attributes) API Token updater (see [below for nested schema](#nestedatt--api_tokens--updated_by))

<a id="nestedatt--api_tokens--created_by"></a>
### Nested Schema for `api_tokens.created_by`

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)


<a id="nestedatt--api_tokens--roles"></a>
### Nested Schema for `api_tokens.roles`

Read-Only:

- `entity_id` (String) The ID of the entity to assign the role to
- `entity_type` (String) The type of entity to assign the role to
- `role` (String) The role to assign to the deployment


<a id="nestedatt--api_tokens--updated_by"></a>
### Nested Schema for `api_tokens.updated_by`

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)
3 changes: 3 additions & 0 deletions examples/data-sources/astro_api_token/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "astro_api_token" "example" {
id = "clxm4836f00ql01me3nigmcr6"
}
13 changes: 13 additions & 0 deletions examples/data-sources/astro_api_tokens/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data "astro_api_tokens" "example" {}

data "astro_api_tokens" "organization_only_example" {
include_only_organization_tokens = true
}

data "astro_api_tokens" "workspace_example" {
workspace_id = "clx42sxw501gl01o0gjenthnh"
}

data "astro_api_tokens" "deployment_example" {
deployment_id = "clx44jyu001m201m5dzsbexqr"
}
109 changes: 109 additions & 0 deletions internal/provider/datasources/data_source_api_token.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 = &apiTokenDataSource{}
var _ datasource.DataSourceWithConfigure = &apiTokenDataSource{}

func NewApiTokenDataSource() datasource.DataSource {
return &apiTokenDataSource{}
}

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

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

func (d *apiTokenDataSource) 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: "API Token data source",
Attributes: schemas.ApiTokenDataSourceSchemaAttributes(),
}
}

func (d *apiTokenDataSource) 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 *apiTokenDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data models.ApiTokenDataSource

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

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

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

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
51 changes: 51 additions & 0 deletions internal/provider/datasources/data_source_api_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package datasources_test

import (
"fmt"
"os"
"testing"

astronomerprovider "github.com/astronomer/terraform-provider-astro/internal/provider"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAcc_DataSource_ApiToken(t *testing.T) {
apiTokenId := os.Getenv("HOSTED_API_TOKEN_ID")
tfVarName := "test_data_api_token"
resourceVar := fmt.Sprintf("data.astro_api_token.%v", tfVarName)

resource.Test(t, resource.TestCase{
PreCheck: func() {
astronomerprovider.TestAccPreCheck(t)
},
ProtoV6ProviderFactories: astronomerprovider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: astronomerprovider.ProviderConfig(t, true) + apiToken(apiTokenId, tfVarName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceVar, "id"),
resource.TestCheckResourceAttrSet(resourceVar, "name"),
resource.TestCheckResourceAttrSet(resourceVar, "short_token"),
resource.TestCheckResourceAttrSet(resourceVar, "type"),
resource.TestCheckResourceAttrSet(resourceVar, "start_at"),
resource.TestCheckResourceAttrSet(resourceVar, "created_at"),
resource.TestCheckResourceAttrSet(resourceVar, "updated_at"),
resource.TestCheckResourceAttrSet(resourceVar, "created_by.id"),
resource.TestCheckResourceAttrSet(resourceVar, "updated_by.id"),
resource.TestCheckResourceAttrSet(resourceVar, "last_used_at"),
resource.TestCheckResourceAttrWith(resourceVar, "roles.#", CheckAttributeLengthIsNotEmpty),
ichung08 marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttrSet(resourceVar, "roles.0.entity_id"),
resource.TestCheckResourceAttrSet(resourceVar, "roles.0.entity_type"),
resource.TestCheckResourceAttrSet(resourceVar, "roles.0.role"),
),
},
},
})
}

func apiToken(apiTokenId string, tfVarName string) string {
return fmt.Sprintf(`
data astro_api_token "%v" {
id = "%v"
}`, tfVarName, apiTokenId)
}
Loading