generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add streaming tenant tokens datasource (#189)
- Loading branch information
1 parent
ec9e325
commit db9c1d1
Showing
8 changed files
with
249 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "astra_streaming_tenant_tokens Data Source - terraform-provider-astra" | ||
subcategory: "" | ||
description: |- | ||
astra_streaming_tenant_tokens provides a datasource that lists streaming tenant tokens. | ||
--- | ||
|
||
# astra_streaming_tenant_tokens (Data Source) | ||
|
||
`astra_streaming_tenant_tokens` provides a datasource that lists streaming tenant tokens. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "astra_streaming_tenant_tokens" "tokens" { | ||
tenant_name = "mytenant" | ||
cluster_name = "pulsar-gcp-useast4" | ||
} | ||
// Example referencing astra_streaming_tenant | ||
resource "astra_streaming_tenant" "tenant" { | ||
tenant_name = "mytenant" | ||
topic = "topic1" | ||
region = "us-east4" | ||
cloud_provider = "gcp" | ||
user_email = "user@example.com" | ||
} | ||
data "astra_streaming_tenant_tokens" "tokens" { | ||
tenant_name = astra_streaming_tenant.tenant.tenant_name | ||
cluster_name = astra_streaming_tenant.tenant.cluster_name | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `cluster_name` (String) Name of the Pulsar Cluster. Format: `pulsar-<cloud provider>-<cloud region>`. Example: `pulsar-gcp-useast1` | ||
- `tenant_name` (String) Name of the streaming tenant for which to fetch tokens. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `tokens` (List of Object) The list of tokens for the specified tenant. (see [below for nested schema](#nestedatt--tokens)) | ||
|
||
<a id="nestedatt--tokens"></a> | ||
### Nested Schema for `tokens` | ||
|
||
Read-Only: | ||
|
||
- `iat` (Number) | ||
- `iss` (String) | ||
- `sub` (String) | ||
- `token` (String) | ||
- `token_id` (String) | ||
|
||
|
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
19 changes: 19 additions & 0 deletions
19
examples/data-sources/astra_streaming_tenant_tokens/data-source.tf
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,19 @@ | ||
data "astra_streaming_tenant_tokens" "tokens" { | ||
tenant_name = "mytenant" | ||
cluster_name = "pulsar-gcp-useast4" | ||
} | ||
|
||
|
||
// Example referencing astra_streaming_tenant | ||
resource "astra_streaming_tenant" "tenant" { | ||
tenant_name = "mytenant" | ||
topic = "topic1" | ||
region = "us-east4" | ||
cloud_provider = "gcp" | ||
user_email = "user@example.com" | ||
} | ||
|
||
data "astra_streaming_tenant_tokens" "tokens" { | ||
tenant_name = astra_streaming_tenant.tenant.tenant_name | ||
cluster_name = astra_streaming_tenant.tenant.cluster_name | ||
} |
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
129 changes: 129 additions & 0 deletions
129
internal/provider/data_source_streaming_tenant_tokens.go
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,129 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
astrastreaming "github.com/datastax/astra-client-go/v2/astra-streaming" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceStreamingTenantTokens() *schema.Resource { | ||
return &schema.Resource { | ||
Description: "`astra_streaming_tenant_tokens` provides a datasource that lists streaming tenant tokens.", | ||
|
||
ReadContext: dataSourceStreamingTenantTokensRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
// Required | ||
"cluster_name": { | ||
Description: "Name of the Pulsar Cluster. Format: `pulsar-<cloud provider>-<cloud region>`. Example: `pulsar-gcp-useast1`", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"tenant_name": { | ||
Description: "Name of the streaming tenant for which to fetch tokens.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
// Computed | ||
"tokens": { | ||
Type: schema.TypeList, | ||
Description: "The list of tokens for the specified tenant.", | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"iat": { | ||
Description: "IAT of the token.", | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"iss": { | ||
Description: "ISS of the token.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sub": { | ||
Description: "Client subscriber of the token.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"token_id": { | ||
Description: "ID of the token.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"token": { | ||
Description: "Pulsar JWT token.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceStreamingTenantTokensRead (ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
streamingClient := meta.(astraClients).astraStreamingClient.(*astrastreaming.ClientWithResponses) | ||
|
||
tenantName := d.Get("tenant_name").(string) | ||
clusterName := d.Get("cluster_name").(string) | ||
|
||
params := astrastreaming.IdListTenantTokensParams{ | ||
XDataStaxPulsarCluster: clusterName, | ||
} | ||
tokenResponse, err := streamingClient.IdListTenantTokensWithResponse(ctx, tenantName, ¶ms) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if tokenResponse.StatusCode() != http.StatusOK { | ||
return diag.Errorf("Failed to get list of tenants. ResponseCode: %d, message = %s.", tokenResponse.StatusCode(), string(tokenResponse.Body)) | ||
} | ||
|
||
if err := setTenantTokensData(ctx, d, streamingClient, *tokenResponse.JSON200); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(tenantName) | ||
|
||
return nil | ||
} | ||
|
||
func setTenantTokensData(ctx context.Context, d *schema.ResourceData, streamingClient *astrastreaming.ClientWithResponses, tokenList []astrastreaming.TenantToken) error { | ||
tenantName := d.Get("tenant_name").(string) | ||
clusterName := d.Get("cluster_name").(string) | ||
params := astrastreaming.GetTokenByIDParams{ | ||
XDataStaxPulsarCluster: clusterName, | ||
} | ||
tokens := make([]map[string]interface{}, 0, len(tokenList)) | ||
for _, token := range tokenList { | ||
tokenMap := map[string]interface{} { | ||
"token_id" : token.TokenID, | ||
"iat" : token.Iat, | ||
"iss" : token.Iss, | ||
"sub" : token.Sub, | ||
} | ||
// now fetch the JWT token | ||
tokenResponse, err := streamingClient.GetTokenByIDWithResponse(ctx, tenantName, *token.TokenID, ¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
if tokenResponse.StatusCode() != http.StatusOK { | ||
return fmt.Errorf("Failed to fetch token by ID. Token ID: %s, Tenant Name: %s, Cluster Name: %s.", *token.TokenID, tenantName, clusterName) | ||
} | ||
tokenMap["token"] = string(tokenResponse.Body) | ||
tokens = append(tokens, tokenMap) | ||
} | ||
if err := d.Set("tokens", tokens); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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